o
    DXh[                     @   s~  	 d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	m
Z
 d dlmZ dZzd dlZdZeZejZejZejZejZW n2 eyo   zd dlZd dlZdZeZejZejZejZejZW n eyl   ed Y nw Y nw er|ejejejejfZ dd	 Z!d
d Z"dd Z#G dd dZ$dd Z%dd Z&dd Z'G dd de	Z(e% dkre)d e(Z*d dl+m(Z( d dl+m$Z$ dS dS )    N)DistutilsExecErrorDistutilsPlatformErrorCompileErrorLibError	LinkError)	CCompilergen_lib_options)logFTzWarning: Can't read registry to find the necessary compiler setting
Make sure that Python modules winreg, win32api or win32con are installed.c                 C   sh   	 zt | |}W n
 ty   Y d S w g }d}	 zt||}W n
 ty)   Y |S w || |d7 }qNr   T   )RegOpenKeyExRegError
RegEnumKeyappend)basekeyhandleLik r   @/opt/python-3.10.19/usr/lib/python3.10/distutils/msvccompiler.py	read_keys7   s$   
r   c                 C   s|   	 zt | |}W n
 ty   Y d S w i }d}	 z
t||\}}}W n
 ty,   Y |S w | }t||t|< |d7 }qr
   )r   r   RegEnumValuelowerconvert_mbcs)r   r   r   dr   namevaluetyper   r   r   read_valuesH   s&   r    c                 C   s<   t | dd }|d urz|d} W | S  ty   Y | S w | S )Ndecodembcs)getattrUnicodeError)sZdecr   r   r   r   ]   s   
r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MacroExpanderc                 C   s   i | _ | | d S N)macrosload_macros)selfversionr   r   r   __init__g   s   zMacroExpander.__init__c                 C   s4   t D ]}t||}|r|| | jd| <  d S qd S )Nz$(%s))HKEYSr    r(   )r*   Zmacropathr   r   r   r   r   r   	set_macrok   s   
zMacroExpander.set_macroc           
   
   C   s   d| }|  d|d d |  d|d d d}|  d|d	 z|d
kr,|  d|d n|  d|d W n tyD } ztdd }~ww d}tD ](}zt||}W n	 ty[   Y qIw t|d}t|d||f }	|	d | jd< qId S )Nz%Software\Microsoft\VisualStudio\%0.1fZVCInstallDirz	\Setup\VCZ
productdirZVSInstallDirz	\Setup\VSz Software\Microsoft\.NETFrameworkZFrameworkDirZinstallrootg      @ZFrameworkSDKDirzsdkinstallrootv1.1Zsdkinstallroota  Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.z.Software\Microsoft\NET Framework Setup\Productr   z%s\%sr+   z$(FrameworkVersion))	r/   KeyErrorr   r-   r   r   r   r    r(   )
r*   r+   Zvsbasenetexcpr   hr   r   r   r   r   r)   r   s6   
zMacroExpander.load_macrosc                 C   s$   | j  D ]
\}}|||}q|S r'   )r(   itemsreplace)r*   r%   r   vr   r   r   sub   s   zMacroExpander.subN)__name__
__module____qualname__r,   r/   r)   r8   r   r   r   r   r&   f   s
    r&   c                  C   s   	 d} t j| }|dkrdS |t|  }t j|d  dd\}}t|d d d }|dkr4|d7 }t|dd	 d
 }|dkrDd}|dkrL|| S d S )NzMSC v.    r            g      $@r   )sysr+   findlensplitint)prefixr   r%   restZmajorVersionZminorVersionr   r   r   get_build_version   s    rJ   c                  C   sB   	 d} t j| }|dkrdS t jd|}t j|t|  | S )Nz bit (r<   Intel))rC   r+   rD   rE   )rH   r   jr   r   r   get_build_architecture   s   rN   c                 C   s2   	 g }| D ]}t j|}||vr|| q|S r'   )osr.   normpathr   )pathsZreduced_pathsr3   Znpr   r   r   normalize_and_reduce_paths   s   
rR   c                   @   s   e Zd Z	 dZi ZdgZg dZdgZdgZee e e Z	dZ
dZdZd	Zd
 ZZdZd*ddZdd Z		d+ddZ		d,ddZ			d-ddZ										d.ddZdd Zdd Zdd  Zd/d!d"Zd#d$ Zd0d&d'Zd(d) ZdS )1MSVCCompilerZmsvcz.c)z.ccz.cppz.cxx.rcz.mcz.resz.objz.libz.dllz%s%sz.exer   c                 C   sv   t | ||| t | _t | _| jdkr.| jdkr$d| _t| j| _nd| _d| j | _	nd| jd  | _	d| _
d S )	NrK      zSoftware\Microsoft\VisualStudiozSoftware\Microsoft\DevstudiozVisual Studio version %szMicrosoft SDK compiler %sr=   F)r   r,   rJ   _MSVCCompiler__versionrN   _MSVCCompiler__arch_MSVCCompiler__rootr&   _MSVCCompiler__macros_MSVCCompiler__productinitialized)r*   verbosedry_runforcer   r   r   r,      s   


zMSVCCompiler.__init__c                 C   s  g | _ dtjv r"dtjv r"| dr"d| _d| _d| _d| _d| _n<| 	d| _ t
| j d	kr6td
| j | d| _| d| _| d| _| d| _| d| _| d | d ztjd dD ]}| j | qgW n	 tyz   Y nw t| j | _ d| j tjd< d | _| jdkrg d| _g d| _n
g d| _g d| _g d| _| jdkrg d| _ng d| _dg| _d| _d S )NZDISTUTILS_USE_SDKZMSSdkzcl.exezlink.exezlib.exezrc.exezmc.exer.   r   zxPython was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed.libZinclude;rK   )/nologo/Ox/MD/W3/GX/DNDEBUG)ra   /Od/MDdrd   re   /Z7/D_DEBUG)ra   rb   rc   rd   /GS-rf   )ra   rg   rh   rd   rk   ri   rj   )/DLLra   z/INCREMENTAL:NOrU   )rl   ra   /INCREMENTAL:no/DEBUG)rl   ra   rm   z	/pdb:Nonern   ra   T)_MSVCCompiler__pathsrO   environfind_execclinkerr_   rcmcget_msvc_pathsrE   r   rZ   set_path_env_varrF   r   r0   rR   joinZpreprocess_optionsrW   compile_optionscompile_options_debugldflags_sharedrV   ldflags_shared_debugZldflags_staticr[   )r*   r3   r   r   r   
initialize   sP   









zMSVCCompiler.initialize c                 C   s   |d u rd}g }|D ]b}t j|\}}t j|d }|t j|d  }|| jvr1td| |r9t j|}|| jv rL|	t j
||| j  q
|| jv r_|	t j
||| j  q
|	t j
||| j  q
|S )Nr~   r   zDon't know how to compile %s)rO   r.   splitext
splitdriveisabssrc_extensionsr   basename_rc_extensionsr   rx   res_extension_mc_extensionsobj_extension)r*   Zsource_filenamesZ	strip_dir
output_dirZ	obj_namesZsrc_namer   extr   r   r   object_filenames8  s,   


zMSVCCompiler.object_filenamesNc	                 C   s8  | j s|   | ||||||}	|	\}}
}}}|pg }|d |r*|| j n|| j |
D ]}z|| \}}W n	 tyE   Y q2w |rNtj	
|}|| jv rXd| }n|| jv rbd| }n|| jv r|}d| }z| | jg| |g |g  W n ty } zt|d }~ww q2|| jv rtj	|}tj	|}z6| | jgd|d|g |g  tj	tj	|\}}tj	||d }| | jgd| g |g  W n ty } zt|d }~ww q2td||f d	| }z| | jg| | ||g |  W q2 ty } zt|d }~ww |
S )
Nz/cz/Tcz/Tpz/foz-hz-rrT   z"Don't know how to compile %s to %sz/Fo)r[   r}   Z_setup_compiler   extendrz   ry   r0   rO   r.   abspath_c_extensions_cpp_extensionsr   spawnrt   r   r   r   dirnameru   r   r   rx   rr   )r*   Zsourcesr   r(   Zinclude_dirsdebugextra_preargsextra_postargsZdependsZcompile_infoobjectsZpp_optsZbuildZcompile_optsobjsrcr   Z	input_optZ
output_optmsgZh_dirZrc_dirr   _Zrc_filer   r   r   compileW  s   













zMSVCCompiler.compilec           	   
   C   s   | j s|   | ||\}}| j||d}| ||rC|d| g }|r&	 z| | jg|  W d S  tyB } zt|d }~ww t	
d| d S )N)r   /OUT:skipping %s (up-to-date))r[   r}   _fix_object_argslibrary_filename
_need_linkr   r_   r   r   r	   r   )	r*   r   Zoutput_libnamer   r   target_langoutput_filenameZlib_argsr   r   r   r   create_static_lib  s"   zMSVCCompiler.create_static_libc              
   C   s  | j s|   | ||\}}| |||}|\}}}|r&| dt|  t| |||}|d ur8tj	||}| 
||r|tjkrU|	rM| jdd  }n| jdd  }n	|	r[| j}n| j}g }|pcg D ]	}|d|  qd|| | | d| g }|d urtjtj|\}}tj	tj|d | |}|d|  |
r|
|d d< |r|| | tj| z| | jg|  W d S  ty } zt|d }~ww td| d S )Nz5I don't know what to do with 'runtime_library_dirs': r   z/EXPORT:r   r   z/IMPLIB:r   )r[   r}   r   Z_fix_lib_argswarnstrr   rO   r.   rx   r   r   Z
EXECUTABLEr|   r{   r   r   r   r   r   r   Zmkpathr   rs   r   r   r	   r   )r*   Ztarget_descr   r   r   Z	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsr   r   r   Z
build_tempr   Z
fixed_argsZlib_optsZldflagsZexport_optsZsymZld_argsZdll_nameZdll_extZimplib_filer   r   r   r   link  sl   




zMSVCCompiler.linkc                 C   s   d| S )Nz	/LIBPATH:r   r*   dirr   r   r   library_dir_option  s   zMSVCCompiler.library_dir_optionc                 C   s   t d)Nz<don't know how to set runtime library search path for MSVC++)r   r   r   r   r   runtime_library_dir_option  s   z'MSVCCompiler.runtime_library_dir_optionc                 C   s
   |  |S r'   )r   )r*   r_   r   r   r   library_option  s   
zMSVCCompiler.library_optionc                 C   s\   |r	|d |g}n|g}|D ]}|D ]}t j|| |}t j|r*|    S qqd S )NZ_d)rO   r.   rx   r   exists)r*   dirsr_   r   Z	try_namesr   r   Zlibfiler   r   r   find_library_file#  s   zMSVCCompiler.find_library_filec                 C   s|   	 | j D ]}tjtj||}tj|r|  S qtjd dD ]}tjtj||}tj|r;|  S q$|S )NPathr`   )ro   rO   r.   rx   r   isfilerp   rF   )r*   Zexer3   fnr   r   r   rq   5  s   
zMSVCCompiler.find_exex86c                 C   s   	 t sg S |d }| jdkrd| j| jf }nd| j|f }tD ]$}t||}|rD| jdkr;| j|| d  S || d  S q | jdkratD ]}t|d| j d ur`| d  g S qLg S )	Nz dirsrU   z6%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directoriesz?%s\6.0\Build System\Components\Platforms\Win32 (%s)\Directoriesr`   r=   z%s\6.0zIt seems you have Visual Studio 6 installed, but the expected registry settings are not present.
You must at least run the Visual Studio GUI once so that these entries are created.)	_can_read_regrV   rX   r-   r    rY   r8   rF   r   )r*   r.   platformr   r   r   r   r   r   rv   K  s4   





zMSVCCompiler.get_msvc_pathsc                 C   s<   	 |dkr|  d}n|  |}|rd|tj|< d S d S )Nr_   Zlibraryr`   )rv   rx   rO   rp   )r*   r   r3   r   r   r   rw   o  s   
zMSVCCompiler.set_path_env_var)r   r   r   )r   r~   )NNNr   NNN)Nr   N)
NNNNNr   NNNN)r   )r   )r9   r:   r;   Zcompiler_typeZexecutablesr   r   r   r   r   r   r   Zstatic_lib_extensionZshared_lib_extensionZstatic_lib_formatZshared_lib_formatZexe_extensionr,   r}   r   r   r   r   r   r   r   r   rq   rv   rw   r   r   r   r   rS      sb    
B
 
X

S

$rS   g       @z3Importing new compiler from distutils.msvc9compiler)rS   )r&   ),rC   rO   Zdistutils.errorsr   r   r   r   r   Zdistutils.ccompilerr   r   Z	distutilsr	   r   winregZhkey_modZ	OpenKeyExr   ZEnumKeyr   Z	EnumValuer   errorr   ImportErrorZwin32apiZwin32coninfoZ
HKEY_USERSHKEY_CURRENT_USERHKEY_LOCAL_MACHINEZHKEY_CLASSES_ROOTr-   r   r    r   r&   rJ   rN   rR   rS   r   ZOldMSVCCompilerZdistutils.msvc9compilerr   r   r   r   <module>   sj    



	-   
9
