B � f�M�@s�dZddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z ddl m Z mZddlmZddlmZddlmZdd �Zd d �Zd d �Zdd�Zddd�Zddd�ZGdd�de �ZdS)adistutils._msvccompiler Contains MSVCCompiler, an implementation of the abstract CCompiler class for Microsoft Visual Studio 2015. The module is compatible with VS 2015 and later. You can find legacy support for older versions in distutils.msvc9compiler and distutils.msvccompiler. �N)�DistutilsExecError�DistutilsPlatformError� CompileError�LibError� LinkError)� CCompiler�gen_lib_options)�log)� get_platform)�countc Cs�ytjtjdtjtjBd�}Wntk r<t�d�dSXd}d}|��x�t�D]�}yt� ||�\}}}Wntk r�PYnX|rT|tj krTt j � |�rTytt|��}Wnttfk r�wTYnX|dkrT||krT||}}qTWWdQRX||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�psz_get_vc_env..zUnable to find vcvarsall.batzcmd /u /c "{}" {} && set)�stderrzutf-16le�replace)r&zError executing {}cSs$i|]\}}}|r|r||���qSr)r8)r9rr5r:rrr r;�scss|]}|�d�VqdS)�=N)� partition)r9�linerrr � �sz_get_vc_env..)r�getenvr)�itemsr7rr+r,�formatZSTDOUT�decoder/r �error�output�cmd� splitlines)r4r6r5�out�exc�envrrr � _get_vc_envns$     rMcCsN|st�d��tj�}x2|D]*}tj�tj�|�|�}tj�|�r|SqW|S)atReturn path to an MSVC executable program. Tries to find the program in several places: first, one of the MSVC program search paths from the registry; next, the directories in the PATH environment variable. If any of those work, return an absolute path that is known to exist. If none of them work, just return the original program name, 'exe'. r)rrB�split�pathseprr-�abspathr3)Zexe�paths�p�fnrrr � _find_exe�s   rTZx86Z x86_amd64)Zwin32z win-amd64c s�eZdZdZdZiZdgZdddgZdgZdgZ eeee Z d Z d Z d Z d Zd ZZdZd(dd�Zd)dd�Zd*dd�Zd+dd�Zd,dd�Zd-dd�Z�fdd�Zd d!�Zd"d#�Zd$d%�Zd.d&d'�Z�ZS)/� MSVCCompilerzwConcrete class that implements an interface to Microsoft Visual C++, as defined by the CCompiler abstract class.Zmsvcz.cz.ccz.cppz.cxxz.rcz.mcz.resz.objz.libz.dllz%s%sz.exercCs t�||||�d|_d|_dS)NF)r�__init__� plat_name� initialized)�self�verboseZdry_runZforcerrr rV�szMSVCCompiler.__init__NcCs|dkrt�}|tkr(td�tt����t|}t|�}|sDtd��|�dd�|_|j�t j �}t d|�|_ t d|�|_ t d|�|_t d|�|_t d |�|_t d |�|_x2|�d d��t j �D]}|r�|�|�t j��q�Wx2|�d d��t j �D]}|r�|�|�t j��q�Wd|_d dddddg|_d dddddg|_d ddg}d dddg}|d �|_|d!�|_|d"�|_|d#�|_|�|_|�|_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�libz/nologoz/Oxz/W3z/GLz/DNDEBUGz/MDz/Odz/MDdz/Ziz/D_DEBUGz/INCREMENTAL:NOz/LTCGz /DEBUG:FULL�/MANIFEST:EMBED,ID=1�/DLL�/MANIFEST:EMBED,ID=2�/MANIFESTUAC:NOFT)r])r])r^r_r`)r^r_r`)%r �PLAT_TO_VCVARSrrD�tuplerMr*�_pathsrNrrOrT�cc�linkerr\�rc�mcZmtZadd_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�_ldflagsrX)rYrWr4Zvc_envrQ�dir�ldflagsZ ldflags_debugrrr � initialize�sX                     zMSVCCompiler.initializer[csT�fdd��jD��fdd��j�jD����p4d����fdd�}tt||��S)Ncsi|] }�j|�qSr)� obj_extension)r9�ext)rYrr r;sz1MSVCCompiler.object_filenames..csi|] }�j|�qSr)� res_extension)r9rq)rYrr r;sr[cs�tj�|�\}}�r"tj�|�}n2tj�|�\}}|�tjjtjjf�rT|dd�}ytj��|�|�St k r�t d� |���YnXdS)N�zDon't know how to compile {}) rr�splitext�basename� splitdrive� startswithri�altsepr-� LookupErrorrrD)rR�baserqr5)�ext_map� output_dir� strip_dirrr � make_out_path#s z4MSVCCompiler.object_filenames..make_out_path)�src_extensions�_rc_extensions�_mc_extensions�list�map)rYZsource_filenamesr}r|r~r)r{r|rYr}r �object_filenamess zMSVCCompiler.object_filenamesc  Cs�|js|��|�||||||�} | \}} }} } |p6g} | �d�|rT| �|j�n | �|j�d}�x| D�]}y| |\}}Wntk r�wlYnX|r�tj � |�}||j kr�d|}�nD||j kr�d|}d}�n*||j k�rB|}d|}y|�|jg| ||g�Wqltk �r<}z t|��Wdd}~XYqlXqln�||jk�r�tj �|�}tj �|�}y\|�|jd|d||g�tj �tj �|��\}}tj �||d �}|�|jd||g�Wqltk �r�}z t|��Wdd}~XYqlXqlntd �||���|jg| | }|�r$|�d �|�|�|�d |�|�|�y|�|�Wqltk �r�}z t|��Wdd}~XYqlXqlW| S) Nz/cFz/Tcz/TpTz/foz-hz-rz.rcz"Don't know how to compile {} to {}z/EHscz/Fo)rXroZ_setup_compile�append�extendrkrj�KeyErrorrrrP� _c_extensions�_cpp_extensionsr��spawnrfrrr��dirnamergrtrur-rDrd)rYZsourcesr|ZmacrosZ include_dirsr� extra_preargs�extra_postargsZdependsZ compile_info�objectsZpp_optsZbuildZ compile_optsZ add_cpp_opts�obj�srcrqZ input_optZ output_opt�msgZh_dirZrc_dirrzr5Zrc_file�argsrrr �compile9sn               zMSVCCompiler.compilec Cs�|js|��|�||�\}}|j||d�}|�||�r�|d|g}|rJy,t�d|jd�|��|� |jg|�Wq�t k r�}z t |��Wdd}~XYq�Xn t�d|�dS)N)r|z/OUT:zExecuting "%s" %s� zskipping %s (up-to-date)) rXro�_fix_object_args�library_filename� _need_linkr rr\r-r�rr) rYr�Zoutput_libnamer|r� target_lang�output_filenameZlib_argsr�rrr �create_static_lib�s zMSVCCompiler.create_static_libc Cs�|js|��|�||�\}}|�|||�}|\}}}|rL|�dt|��t||||�}|dk rptj� ||�}|� ||��r�|j || f}dd�|p�gD�}||||d|g}tj� |d�} |dk �r tj� tj�|��\}}tj� | |�|��}|�d|�| �r| |dd�<| �r.|�| �tj� tj�|��}|�|�y,t�d|jd� |��|�|jg|�Wn,tk �r�}z t|��Wdd}~XYnXn t�d |�dS) Nz5I don't know what to do with 'runtime_library_dirs': cSsg|] }d|�qS)z/EXPORT:r)r9Zsymrrr � �sz%MSVCCompiler.link..z/OUT:rz/IMPLIB:zExecuting "%s" %sr�zskipping %s (up-to-date))rXror�Z _fix_lib_args�warn�strrrrr-r�rlr�rtrur�r�r�rPZmkpathr rrer�rr)rYZ target_descr�r�r|Z librariesZ library_dirsZruntime_library_dirsZexport_symbolsrr�r�Z build_tempr�Z fixed_argsZlib_optsrnZ export_optsZld_argsZdll_nameZdll_extZ implib_filer�rrr �link�sL       zMSVCCompiler.linkc s4t�d�}z|jtjd<t��|�S|tjd<XdS)Nr)rrBrcr)�superr�)rYrHZold_path)� __class__rr r��s    zMSVCCompiler.spawncCsd|S)Nz /LIBPATH:r)rYrmrrr �library_dir_option�szMSVCCompiler.library_dir_optioncCs td��dS)Nz:don't know how to set runtime library search path for MSVC)r)rYrmrrr �runtime_library_dir_optionsz'MSVCCompiler.runtime_library_dir_optioncCs |�|�S)N)r�)rYr\rrr �library_optionszMSVCCompiler.library_optioncCs`|r|d|g}n|g}xB|D]6}x0|D](}tj�||�|��}tj�|�r(|Sq(WqWdSdS)NZ_d)rrr-r�r3)rY�dirsr\rZ try_namesrm�nameZlibfilerrr �find_library_files    zMSVCCompiler.find_library_file)rrr)N)rr[)NNNrNNN)NrN) NNNNNrNNNN)r)�__name__� __module__� __qualname__�__doc__Z compiler_typeZ executablesr�r�r�r�rrrrpZstatic_lib_extensionZshared_lib_extensionZstatic_lib_formatZshared_lib_formatZ exe_extensionrVror�r�r�r�r�r�r�r�r�� __classcell__rr)r�r rU�sL   P  [  8 rU)N)r�rZshutil�statr+rZdistutils.errorsrrrrrZdistutils.ccompilerrrZ distutilsr Zdistutils.utilr � itertoolsr r!r2r7rMrTrarUrrrr �s$   "