a �DOg�v�@sJddlZddlZddlZddlZddlmZmZmZmZm Z ddl m Z m Z ddl mZddlmZddlZejZejZejZejZejejejejfZejdko�ej dkZ!e!r�dZ"d Z#d Z$n d Z"d Z#d Z$ddd�Z%Gdd�d�Z&Gdd�d�Z'dd�Z(dd�Z)dd�Z*dd�Z+d#dd�Z,e(�Z-e-dk�r6ed e-��Gd!d"�d"e �Z.dS)$�N)�DistutilsExecError�DistutilsPlatformError� CompileError�LibError� LinkError)� CCompiler�gen_lib_options)�log)� get_platform�win32lz1Software\Wow6432Node\Microsoft\VisualStudio\%0.1fz5Software\Wow6432Node\Microsoft\Microsoft SDKs\Windowsz,Software\Wow6432Node\Microsoft\.NETFrameworkz%Software\Microsoft\VisualStudio\%0.1fz)Software\Microsoft\Microsoft SDKs\Windowsz Software\Microsoft\.NETFramework�x86Zamd64�r z win-amd64c@sLeZdZdd�Zee�Zdd�Zee�Zdd�Zee�Zdd�Zee�Zd S) �RegcCs:tD](}|�||�}|r||vr||Sqt|��dS�N)�HKEYS� read_values�KeyError)�cls�path�key�base�d�r�//usr/lib64/python3.9/distutils/msvc9compiler.py� get_value?s   z Reg.get_valuecCsjzt||�}Wnty"YdS0g}d}zt||�}WntyPYqfYn0|�|�|d7}q,|S�Nr�)� RegOpenKeyEx�RegError� RegEnumKey�append)rrr�handle�L�i�krrr� read_keysGs     z Reg.read_keysc Cs�zt||�}Wnty"YdS0i}d}zt||�\}}}WntyVYq~Yn0|��}|�|�||�|�<|d7}q,|Sr)rr� RegEnumValue�lower� convert_mbcs) rrrr!rr#�name�value�typerrrrYs    zReg.read_valuescCs8t|dd�}|dur4z |d�}Wnty2Yn0|S)N�decode�mbcs)�getattr� UnicodeError)�sZdecrrrr(os   zReg.convert_mbcsN) �__name__� __module__� __qualname__r� classmethodr%rr(� staticmethodrrrrr;src@s,eZdZdd�Zdd�Zdd�Zdd�Zd S) � MacroExpandercCsi|_t||_|�|�dSr)�macros�VS_BASE�vsbase� load_macros)�self�versionrrr�__init__{s zMacroExpander.__init__cCst�||�|jd|<dS)Nz$(%s))rrr7)r;Zmacrorrrrr� set_macro�szMacroExpander.set_macroc Cs|�d|jdd�|�d|jdd�|�dtd�z$|dkrP|�d td �ntd ��Wntyttd ��Yn0|d kr�|�d |jd�|�dtd�n`d}tD]V}zt||�}Wnty�Yq�Yn0t |d�}t � |d||f�}|d|j d<q�dS)NZ VCInstallDirz \Setup\VC� productdirZ VSInstallDirz \Setup\VSZ FrameworkDirZ installroot� @ZFrameworkSDKDirzsdkinstallrootv2.0aPython was built with Visual Studio 2008; extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2008 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing "-c mingw32" to setup.py.g"@ZFrameworkVersionz clr versionZ WindowsSdkDirZcurrentinstallfolderz.Software\Microsoft\NET Framework Setup\Productrz%s\%sr<z$(FrameworkVersion)) r>r9�NET_BASErr� WINSDK_BASErrrrrrr7)r;r<�pr�hrrrrrr:�s2�  �    zMacroExpander.load_macroscCs$|j��D]\}}|�||�}q |Sr)r7�items�replace)r;r0r$�vrrr�sub�szMacroExpander.subN)r1r2r3r=r>r:rHrrrrr6ysr6cCs�d}tj�|�}|dkrdS|t|�}tj|d��dd�\}}t|dd��d}|dkrf|d7}t|dd ��d }|dkr�d }|dkr�||SdS) NzMSC v.������� r������ ��g$@r)�sysr<�find�len�split�int)�prefixr#r0�restZ majorVersionZ minorVersionrrr�get_build_version�s  rWcCs0g}|D]"}tj�|�}||vr|�|�q|Sr)�osr�normpathr )�pathsZ reduced_pathsrCZnprrr�normalize_and_reduce_paths�s   r[cCs<|�tj�}g}|D]}||vr|�|�qtj�|�}|Sr)rSrX�pathsepr �join)�variableZoldListZnewListr#Z newVariablerrr�removeDuplicates�s   r_cCst|}zt�d|d�}Wn ty<t�d�d}Yn0|rNtj�|�s�d|}tj � |d�}|r�tj�|�r�tj� |tj tj d�}tj� |�}tj�|�s�t�d|�dSnt�d|�|s�t�d�dStj� |d �}tj�|�r�|St�d �dS) Nz %s\Setup\VCr?z%Unable to find productdir in registryzVS%0.f0COMNTOOLSZVCz%s is not a valid directoryz Env var %s is not set or invalidzNo productdir foundz vcvarsall.bat�Unable to find vcvarsall.bat)r8rrrr �debugrXr�isdir�environ�getr]�pardir�abspath�isfile)r<r9r?ZtoolskeyZtoolsdir� vcvarsallrrr�find_vcvarsall�s4 �        ric CsHt|�}hd�}i}|dur$td��t�d||�tjd||ftjtjd�}z�|��\}}|��dkrvt|� d���|� d�}|� d�D]d}t � |�}d |vr�q�|� �}|� d d �\} } | ��} | |vr�| �tj�r�| dd �} t| �|| <q�W|j��|j��n|j��|j��0t|�t|�k�rDttt|������|S) N>�libZlibpath�includerr`z'Calling 'vcvarsall.bat %s' (version=%s)z "%s" %s & set)�stdout�stderrrr-� �=rrI)rirr ra� subprocess�Popen�PIPE� communicate�waitr,rSrr(�stripr'�endswithrXr\r_rl�closermrR� ValueError�str�list�keys) r<ZarchrhZ interesting�result�popenrlrm�linerr*rrr�query_vcvarsall�sB�        �  rr@z(VC %0.1f is not supported by this modulec @s�eZdZdZiZdgZgd�ZdgZdgZeeeeZ dZ dZ dZ d Z d ZZd Zd+d d�Zd,dd�Zd-dd�Zd.dd�Zd/dd�Zd0dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd1d'd(�Zd)d*�ZdS)2� MSVCCompilerZmsvcz.c)z.ccz.cppz.cxx�.rcz.mcz.resz.objz.libz.dllz%s%sz.exercCs8t�||||�t|_d|_g|_d|_d|_d|_dS)NzSoftware\Microsoft\VisualStudioF) rr=�VERSION�_MSVCCompiler__versionZ_MSVCCompiler__root�_MSVCCompiler__paths� plat_name�_MSVCCompiler__arch� initialized)r;�verbose�dry_run�forcerrrr=HszMSVCCompiler.__init__NcCs�|durt�}d}||vr(td|f��dtjvrfdtjvrf|�d�rfd|_d|_d|_d|_d |_ n�|t�ksx|d kr�t |}nt t�d t |}t t |�}|d � tj�|_|d tjd <|dtjd<t|j�dkr�td|j��|�d�|_|�d�|_|�d�|_|�d�|_|�d �|_ z(tjd � d�D]}|j�|��q:Wnt�ydYn0t|j�|_d�|j�tjd <d|_|jdk�r�gd�|_gd�|_ngd�|_gd�|_gd�|_|jdk�r�gd�|_dg|_d|_dS)Nr z--plat-name must be one of %sZDISTUTILS_USE_SDKZMSSdkzcl.exezlink.exezlib.exezrc.exezmc.exer �_rrjrkrzxPython was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed.�;r )�/nologo�/Ox�/MD�/W3�/DNDEBUG)r��/Od�/MDdr��/Z7�/D_DEBUG)r�r�r�r��/GS-r�)r�r�r�r�r�r�r�)�/DLLr�z/INCREMENTAL:NO�)r�r�z/INCREMENTAL:noz/DEBUGr�T)r rrXrc�find_exe�cc�linkerrj�rc�mc�PLAT_TO_VCVARSrr�rSr\r�rRZ_MSVCCompiler__productr rr[r]Zpreprocess_optionsr��compile_options�compile_options_debug�ldflags_sharedr��ldflags_shared_debugZldflags_staticr�)r;r�Zok_platsZ plat_specZvc_envrCrrr� initializeSsb�  � �              zMSVCCompiler.initialize�cCs�|dur d}g}|D]�}tj�|�\}}tj�|�d}|tj�|�d�}||jvrbtd|��|rrtj�|�}||jvr�|� tj� |||j ��q||j vr�|� tj� |||j ��q|� tj� |||j ��q|S)Nr�rzDon't know how to compile %s)rXr�splitext� splitdrive�isabs�src_extensionsr�basename�_rc_extensionsr r]� res_extension�_mc_extensions� obj_extension)r;Zsource_filenamesZ strip_dir� output_dirZ obj_namesZsrc_namer�extrrr�object_filenames�s,      �  � �zMSVCCompiler.object_filenamesc  Cst|js|��|�||||||�} | \}} }} } |p6g} | �d�|rT| �|j�n | �|j�| D�]}z| |\}}Wnty�YqdYn0|r�tj � |�}||j vr�d|}�nX||j vr�d|}�nB||j v�r<|}d|}z"|�|jg| |g|g�Wqdt�y6}zt|��WYd}~qdd}~00qdn�||jv�rtj �|�}tj �|�}zl|�|jgd|d|g|g�tj �tj �|��\}}tj �||d�}|�|jgd|g|g�Wqdt�y�}zt|��WYd}~qdd}~00qdntd||f��d |}z&|�|jg| | ||g|�Wqdt�yl}zt|��WYd}~qdd}~00qd| S) Nz/cz/Tcz/Tpz/foz-hz-rr�z"Don't know how to compile %s to %sz/Fo)r�r�Z_setup_compiler �extendr�r�rrXrrf� _c_extensions�_cpp_extensionsr��spawnr�rrr��dirnamer�r�r�r]r�)r;Zsourcesr�r7Z include_dirsra� extra_preargs�extra_postargsZdependsZ compile_info�objectsZpp_optsZbuildZ compile_opts�obj�srcr�Z input_optZ output_opt�msgZh_dirZrc_dirrr�Zrc_filerrr�compile�s� �           ��     �� �� ���  zMSVCCompiler.compilec Cs�|js|��|�||�\}}|j||d�}|�||�r�|d|g}|rJz|�|jg|�Wq�ty�}zt|��WYd}~q�d}~00n t � d|�dS)N)r��/OUT:�skipping %s (up-to-date)) r�r��_fix_object_args�library_filename� _need_linkr�rjrrr ra) r;r�Zoutput_libnamer�ra� target_lang�output_filenameZlib_argsr�rrr�create_static_libs�  zMSVCCompiler.create_static_libc CsX|js|��|�||�\}}|�|||�}|\}}}|rL|�dt|��t||||�}|durptj� ||�}|� ||��rH|t j kr�| r�|j dd�}q�|jdd�}n| r�|j }n|j}g}|p�gD]}|�d|�q�||||d|g}tj�|d�} |du�rLtj�tj�|��\}}tj� | |�|��}|�d|�|�|| |�| �rl| |dd�<| �r||�| �|�tj�|��z|�|jg|�Wn.t�y�}zt|��WYd}~n d}~00|�||�}|du�rT|\}}d||f}z|�dd d ||g�Wn.t�yD}zt|��WYd}~n d}~00n t�d |�dS) Nz5I don't know what to do with 'runtime_library_dirs': rz/EXPORT:r�rz/IMPLIB:z-outputresource:%s;%szmt.exez-nologoz -manifestr�)r�r�r�Z _fix_lib_args�warnryrrXrr]r�r� EXECUTABLEr�r�r r�r�r�r��manifest_setup_ldargsr�Zmkpathr�r�rr�manifest_get_embed_infor ra)r;� target_descr�r�r�Z librariesZ library_dirsZruntime_library_dirsZexport_symbolsrar�r�� build_tempr�Z fixed_argsZlib_optsZldflagsZ export_optsZsym�ld_argsZdll_nameZdll_extZ implib_filer�ZmfinfoZ mffilename�mfidZout_argrrr�link5s�� ��   ��  ��      �  zMSVCCompiler.linkcCs,tj�|tj�|�d�}|�d|�dS)Nz .manifest�/MANIFESTFILE:)rXrr]r�r )r;r�r�r�� temp_manifestrrrr��s �z"MSVCCompiler.manifest_setup_ldargscCs^|D]"}|�d�r|�dd�d}q,qdS|tjkr|)r�z*\s*zI|)�w) �open�readrw�rer��DOTALLrH�search�write�OSError)r;Z manifest_fileZ manifest_fZ manifest_buf�patternrrrr��s6  ��  � z!MSVCCompiler._remove_visual_c_refcCsd|S)Nz /LIBPATH:r�r;�dirrrr�library_dir_option�szMSVCCompiler.library_dir_optioncCs td��dS)NzsN  ��>. # )