a �DOg'N�@s�ddlZddlZddlZddlmZmZmZmZmZddl m Z m Z ddl m Z ddlmZddlmZdd�Zd d �Zd d d dd�Zdd�Zdd�Zddd�Zd dddd�ZGdd�de �ZdS)�N)�DistutilsExecError�DistutilsPlatformError� CompileError�LibError� LinkError)� CCompiler�gen_lib_options)�log)� get_platform)�countc CsztjtjdtjtjBd�}Wnty<t�d�YdS0d}d}|��t�D]�}zt� ||�\}}}Wnty�Yq�Yn0|rR|tj krRt j � |�rRztt|��}Wnttfy�YqRYn0|dkrR||krR||}}qRWd�n1�s�0Y||fS)Nz'Software\Microsoft\VisualStudio\SxS\VC7)�accesszVisual C++ is not registered�NNr�)�winregZ OpenKeyEx�HKEY_LOCAL_MACHINEZKEY_READZKEY_WOW64_32KEY�OSErrorr �debugr Z EnumValueZREG_SZ�os�path�isdir�int�float� ValueError� TypeError)�key� best_version�best_dir�i�vZvc_dirZvt�version�r �//usr/lib64/python3.9/distutils/_msvccompiler.py� _find_vc2015s2 �       ,r"c Cs�tj�d�ptj�d�}|s dSz8tjtj�|ddd�ddd d d d d dg ddd���}Wntjt t fytYdS0tj�|ddd�}tj� |�r�d|fSdS)NzProgramFiles(x86)Z ProgramFilesr zMicrosoft Visual StudioZ Installerz vswhere.exez-latestz -prereleasez -requiresz1Microsoft.VisualStudio.Component.VC.Tools.x86.x64z -propertyZinstallationPathz -products�*�mbcs�strict)�encoding�errorsZVCZ AuxiliaryZBuild�) r�environ�get� subprocess� check_outputr�join�strip�CalledProcessErrorr�UnicodeDecodeErrorr)�rootrr r r!� _find_vc20178s( �� r2�x86Zx64ZarmZarm64)r3� x86_amd64�x86_arm� x86_arm64cCs\t�\}}|st�\}}|s*t�d�dStj�|d�}tj�|�sTt�d|�dS|dfS)Nz$No suitable Visual C++ version foundr z vcvarsall.batz%s cannot be found)r2r"r rrrr-�isfile)� plat_spec�_rr� vcvarsallr r r!�_find_vcvarsall_s     r;c Cs�t�d�rdd�tj��D�St|�\}}|s6td��z&tjd�||�tj d�j ddd �}WnBtj y�}z(t � |j�td �|j���WYd}~n d}~00d d�d d �|��D�D�}|S)NZDISTUTILS_USE_SDKcSsi|]\}}|��|�qSr ��lower)�.0r�valuer r r!� ss�z_get_vc_env..zUnable to find vcvarsall.batzcmd /u /c "{}" {} && set)�stderrzutf-16le�replace)r'zError executing {}cSs$i|]\}}}|r|r|��|�qSr r<)r>rr9r?r r r!r@�s�css|]}|�d�VqdS)�=N)� partition)r>�liner r r!� ��z_get_vc_env..)r�getenvr)�itemsr;rr+r,�format�STDOUT�decoder/r �error�output�cmd� splitlines)r8r:r9�out�exc�envr r r!� _get_vc_envqs. �  ��  ��rTcCsN|st�d��tj�}|D].}tj�tj�|�|�}tj�|�r|Sq|S�Nr)rrH�split�pathseprr-�abspathr7)Zexe�paths�p�fnr r r!� _find_exe�s   r\r4r5r6)�win32z win-amd64z win-arm32z win-arm64c s�eZdZdZiZdgZgd�ZdgZdgZeeeeZ dZ dZ dZ d Z d ZZd Zd%d d�Zd&dd�Zd'dd�Zd(dd�Zd)dd�Zd*dd�Z�fdd�Zdd�Zdd �Zd!d"�Zd+d#d$�Z�ZS),� MSVCCompilerZmsvcz.c)z.ccz.cppz.cxx�.rcz.mcz.resz.objz.libz.dllz%s%sz.exercCs t�||||�d|_d|_dS)NF)r�__init__� plat_name� initialized)�self�verbose�dry_run�forcer r r!r`�szMSVCCompiler.__init__NcCs |durt�}|tvr(td�tt����t|}t|�}|sDtd��|�dd�|_|j�t j �}t d|�|_ t d|�|_ t d|�|_t d|�|_t d |�|_t d |�|_|�d d��t j �D]}|r�|�|�t j��q�|�d d��t j �D]}|r�|�|�t j��q�d|_gd �|_gd�|_gd�}gd�}g|�d�|_g|�d�|_g|�d�d�d�|_g|�d�d�d�|_g|�|_g|�|_tj df|jtj df|jtj df|jtj!df|jtj!df|jtj!df|jtj"df|jtj"df|jtj"df|ji |_#d|_$dS)Nz--plat-name must be one of {}z7Unable to find a compatible Visual Studio installation.r�zcl.exezlink.exezlib.exezrc.exezmc.exezmt.exeZinclude�lib)�/nologoz/Ox�/W3z/GLz/DNDEBUGz/MD)riz/Odz/MDdz/Zirjz/D_DEBUG)ri�/INCREMENTAL:NO�/LTCG)rirkrlz /DEBUG:FULLz/MANIFEST:EMBED,ID=1z/DLLz/MANIFEST:EMBED,ID=2z/MANIFESTUAC:NOFT)%r �PLAT_TO_VCVARSrrJ�tuplerTr*�_pathsrVrrWr\�cc�linkerrh�rc�mc�mtZadd_include_dir�rstrip�sepZadd_library_dirZpreprocess_options�compile_options�compile_options_debugZ ldflags_exeZldflags_exe_debugZldflags_sharedZldflags_shared_debugZldflags_staticZldflags_static_debugrZ EXECUTABLEZ SHARED_OBJECTZSHARED_LIBRARY�_ldflagsrb)rcrar8Zvc_envrY�dir�ldflagsZ ldflags_debugr r r!� initialize�s\�                   � zMSVCCompiler.initializergcsXi�fdd��jD���fdd��j�jD����p8d����fdd�}tt||��S)Ncsi|] }|�j�qSr )� obj_extension�r>�ext�rcr r!r@"rGz1MSVCCompiler.object_filenames..csi|] }|�j�qSr )� res_extensionr~r�r r!r@#rGrgcs�tj�|�\}}�r"tj�|�}n2tj�|�\}}|�tjjtjjf�rT|dd�}ztj��|�|�WSt y�t d� |���Yn0dS)N�zDon't know how to compile {}) rr�splitext�basename� splitdrive� startswithrv�altsepr-� LookupErrorrrJ)rZ�baserr9)�ext_map� output_dir� strip_dirr r!� make_out_path(s  z4MSVCCompiler.object_filenames..make_out_path)�src_extensions�_rc_extensions�_mc_extensions�list�map)rcZsource_filenamesr�r�r�r )r�r�rcr�r!�object_filenamess��zMSVCCompiler.object_filenamesc  Cs�|js|��|�||||||�} | \}} }} } |p6g} | �d�|rT| �|j�n | �|j�d}| D�]}z| |\}}Wnty�YqhYn0|r�tj � |�}||j vr�d|}�nH||j vr�d|}d}�n.||j v�r@|}d|}z|�|jg| ||g�Wqht�y:}zt|��WYd}~qhd}~00qhn�||jv�r�tj �|�}tj �|�}z\|�|jd|d||g�tj �tj �|��\}}tj �||d �}|�|jd||g�Wqht�y�}zt|��WYd}~qhd}~00qhntd �||���|jg| | }|�r$|�d �|�|�|�d |�|�|�z|�|�Wqht�y�}zt|��WYd}~qhd}~00qh| S) Nz/cFz/Tcz/TpTz/foz-hz-rr_z"Don't know how to compile {} to {}z/EHscz/Fo)rbr|Z_setup_compile�append�extendrxrw�KeyErrorrrrX� _c_extensions�_cpp_extensionsr��spawnrrrrr��dirnamersr�r�r-rJrp)rcZsourcesr�ZmacrosZ include_dirsr� extra_preargs�extra_postargsZdependsZ compile_info�objectsZpp_optsZbuildZ compile_optsZ add_cpp_opts�obj�srcrZ input_optZ output_opt�msgZh_dirZrc_dirr�r9Zrc_file�argsr r r!�compile>sr �            �    zMSVCCompiler.compilec Cs�|js|��|�||�\}}|j||d�}|�||�r�|d|g}|rJz,t�d|jd�|��|� |jg|�Wq�t y�}zt |��WYd}~q�d}~00n t�d|�dS)N)r��/OUT:�Executing "%s" %s� �skipping %s (up-to-date)) rbr|�_fix_object_args�library_filename� _need_linkr rrhr-r�rr) rcr�Zoutput_libnamer�r� target_lang�output_filenameZlib_argsr�r r r!�create_static_lib�s�  zMSVCCompiler.create_static_libc Cs�|js|��|�||�\}}|�|||�}|\}}}|rL|�dt|��t||||�}|durptj� ||�}|� ||��r�|j || f}dd�|p�gD�}||||d|g}tj� |d�} |du�r tj� tj�|��\}}tj� | |�|��}|�d|�| �r| |dd�<| �r.|�| �tj� tj�|��}|�|�z,t�d|jd� |��|�|jg|�Wn.t�y�}zt|��WYd}~n d}~00n t�d |�dS) Nz5I don't know what to do with 'runtime_library_dirs': cSsg|] }d|�qS)z/EXPORT:r )r>Zsymr r r!� �rGz%MSVCCompiler.link..r�rz/IMPLIB:r�r�r�)rbr|r�Z _fix_lib_args�warn�strrrrr-r�ryr�r�r�r�r�r�rXZmkpathr rrqr�rr)rcZ target_descr�r�r�Z librariesZ library_dirsZruntime_library_dirsZexport_symbolsrr�r�Z build_tempr�Z fixed_argsZlib_optsr{Z export_optsZld_argsZdll_nameZdll_extZ implib_filer�r r r!�link�s^� �� ��  ��    zMSVCCompiler.linkc s@t�d�}z$|jtjd<t��|�W|tjd<S|tjd<0dSrU)rrHror)�superr�)rcrOZold_path�� __class__r r!r��s    �zMSVCCompiler.spawncCsd|S)Nz /LIBPATH:r �rcrzr r r!�library_dir_optionszMSVCCompiler.library_dir_optioncCs td��dS)Nz:don't know how to set runtime library search path for MSVC)rr�r r r!�runtime_library_dir_options�z'MSVCCompiler.runtime_library_dir_optioncCs |�|�S)N)r�)rcrhr r r!�library_option szMSVCCompiler.library_optioncCs\|r|d|g}n|g}|D]:}|D]0}tj�||�|��}tj�|�r$|Sq$qdS)NZ_d)rrr-r�r7)rc�dirsrhrZ try_namesrz�nameZlibfiler r r!�find_library_file s zMSVCCompiler.find_library_file)rrr)N)rrg)NNNrNNN)NrN) NNNNNrNNNN)r)�__name__� __module__� __qualname__Z compiler_typeZ executablesr�r�r�r�r�r�r}Zstatic_lib_extensionZshared_lib_extensionZstatic_lib_formatZshared_lib_formatZ exe_extensionr`r|r�r�r�r�r�r�r�r�r�� __classcell__r r r�r!r^�sZ��  P� "� ]� � E r^)N)rr+rZdistutils.errorsrrrrrZdistutils.ccompilerrrZ distutilsr Zdistutils.utilr � itertoolsr r"r2ZPLAT_SPEC_TO_RUNTIMEr;rTr\rmr^r r r r!�s.   !� �