B � f/w�@sRdZddlZddlZddlZddlZddlmZmZmZm Z m Z ddl m Z m Z mZddlmZddlmZddlZejZejZejZejZejejejejfZ ej!dko�ej"dkZ#e#r�d Z$d Z%d Z&n d Z$d Z%dZ&ddd�Z'Gdd�d�Z(Gdd�d�Z)dd�Z*dd�Z+dd�Z,dd�Z-d$dd�Z.e*�Z/e/d k�r>ed!e/��Gd"d#�d#e �Z0dS)%a distutils.msvc9compiler Contains MSVCCompiler, an implementation of the abstract CCompiler class for the Microsoft Visual Studio 2008. The module is compatible with VS 2005 and VS 2008. You can find legacy support for older versions of VS in distutils.msvccompiler. �N)�DistutilsExecError�DistutilsPlatformError� CompileError�LibError� LinkError)� CCompiler�gen_preprocess_options�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@sPeZdZdZdd�Zee�Zdd�Zee�Zdd�Zee�Zdd �Ze e�Zd S) �Regz2Helper class to read values from the registry cCs:x,tD]$}|�||�}|r||kr||SqWt|��dS)N)�HKEYS� read_values�KeyError)�cls�path�key�base�d�r�t�d�d}YnX|rPtj�|�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) z�Find the vcvarsall.bat file At first it tries to find the productdir of VS 2008 in the registry. If that fails it falls back to the VS90COMNTOOLS env var. z %s\Setup\VCr>z%Unable to find productdir in registryNzVS%0.f0COMNTOOLSZVCz%s is not a valid directoryz Env var %s is not set or invalidzNo productdir foundz vcvarsall.batzUnable to find vcvarsall.bat)r7rrrr �debugrVr�isdir�environ�getr[�pardir�abspath�isfile)r;r8r>ZtoolskeyZtoolsdir� vcvarsallrrr�find_vcvarsall�s2         rec Cs<t|�}ddddh}i}|dkr(td��t�d||�tjd||ftjtjd �}z�|��\}}|��d krzt|� d ���|� d �}xr|� d �D]d}t � |�}d |kr�q�|� �}|� d d�\} } | ��} | |kr�| �tj�r�| dd�} t| �|| <q�WWd|j��|j��Xt|�t|�k�r8ttt|������|S)zDLaunch vcvarsall.bat and read the settings from its environment �include�libZlibpathrNzUnable to find vcvarsall.batz'Calling 'vcvarsall.bat %s' (version=%s)z "%s" %s & set)�stdout�stderrrr+� �=rrG)rerr r]� subprocess�Popen�PIPEZ communicate�waitr*rQrr&�stripr%�endswithrVrZr\rh�closerirP� ValueError�str�list�keys) r;ZarchrdZ interesting�result�popenrhri�linerr(rrr�query_vcvarsall�s<          rzg @z(VC %0.1f is not supported by this modulec @s�eZdZdZdZiZdgZdddgZdgZdgZ eeee Z d Z d Z d Z d Zd ZZdZd.dd�Zd/dd�Zd0dd�Zd1dd�Zd2dd�Zd3dd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd4d*d+�Zd,d-�ZdS)5� 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.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:�verboseZdry_runZforcerrrr<IszMSVCCompiler.__init__NcCs|dkrt�}d}||kr(td|f��dtjkrfdtjkrf|�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 �|_ y,x&tjd � d�D]}|j�|��q||j k�r>|}d|}y"|�|jg| |g|g�Wqhtk �r8}z t|��Wdd}~XYqhXqhn�||jk�rtj �|�}tj �|�}yl|�|jgd|d|g|g�tj �tj �|��\}}tj �||d�}|�|jgd|g|g�Wqhtk �r�}z t|��Wdd}~XYqhXqhntd||f��d |}y&|�|jg| | ||g|�Wqhtk �rj}z t|��Wdd}~XYqhXqhW| S) Nz/cz/Tcz/Tpz/foz-hz-rz.rcz"Don't know how to compile %s to %sz/Fo)r�r�Z_setup_compiler�extendr�r�rrVrrb� _c_extensions�_cpp_extensionsr��spawnr�rrr��dirnamer�r�r�r[r�)r:Zsourcesr�r6Z include_dirsr]� extra_preargs�extra_postargsZdependsZ compile_info�objectsZpp_optsZbuildZ compile_opts�obj�srcr�Z input_optZ output_opt�msgZh_dirZrc_dirrr�Zrc_filerrr�compile�sj              zMSVCCompiler.compilec Cs�|js|��|�||�\}}|j||d�}|�||�r�|d|g}|rJy|�|jg|�Wq�tk r�}z t|��Wdd}~XYq�Xn t � d|�dS)N)r�z/OUT:zskipping %s (up-to-date)) r�r��_fix_object_args�library_filename� _need_linkr�rgrrr r]) r:r�Zoutput_libnamer�r]� target_lang�output_filenameZlib_argsr�rrr�create_static_libs zMSVCCompiler.create_static_libc CsX|js|��|�||�\}}|�|||�}|\}}}|rL|�dt|��t||||�}|dk rptj� ||�}|� ||��rH|t j kr�| r�|j dd�}q�|jdd�}n| r�|j }n|j}g}x|p�gD]}|�d|�q�W||||d|g}tj�|d�} |dk �rPtj�tj�|��\}}tj� | |�|��}|�d|�|�|| |�| �rp| |dd�<| �r�|�| �|�tj�|��y|�|jg|�Wn,tk �r�}z t|��Wdd}~XYnX|�||�}|dk �rT|\}}d||f}y|�dd d ||g�Wn,tk �rD}z t|��Wdd}~XYnXn t�d |�dS) Nz5I don't know what to do with 'runtime_library_dirs': rz/EXPORT:z/OUT:rz/IMPLIB:z-outputresource:%s;%szmt.exez-nologoz -manifestzskipping %s (up-to-date))r�r�r�Z _fix_lib_args�warnrtr rVrr[r�r� EXECUTABLEr�r�rr�r�r�r��manifest_setup_ldargsr�Zmkpathr�r�rr�manifest_get_embed_infor r])r:� target_descr�r�r�Z librariesZ library_dirsZruntime_library_dirsZexport_symbolsr]r�r�� build_tempr�Z fixed_argsZlib_optsZldflagsZ export_optsZsym�ld_argsZdll_nameZdll_extZ implib_filer�ZmfinfoZ mffilename�mfidZout_argrrr�link6sl           zMSVCCompiler.linkcCs,tj�|tj�|�d�}|�d|�dS)Nz .manifestz/MANIFESTFILE:)rVrr[r�r)r:r�r�r�� temp_manifestrrrr��sz"MSVCCompiler.manifest_setup_ldargscCs`x,|D] }|�d�r|�dd�d}PqWdS|tjkr>d}nd}|�|�}|dkrXdS||fS)Nz/MANIFESTFILE:�:rrL)� startswithrQrr��_remove_visual_c_ref)r:r�r��argr�r�rrrr��s    z$MSVCCompiler.manifest_get_embed_infocCs�y�t|�}z |��}Wd|��Xt�dtj�}t�|d|�}d}t�|d|�}t�dtj�}t�||�dkrrdSt|d�}z|�|�|S|��XWnt k r�YnXdS)NzU|)r�z*\s*zI|)�w) �open�readrr�rer��DOTALLrF�search�write�OSError)r:Z manifest_fileZ manifest_fZ manifest_buf�patternrrrr��s.     z!MSVCCompiler._remove_visual_c_refcCsd|S)Nz /LIBPATH:r)r:�dirrrr�library_dir_option�szMSVCCompiler.library_dir_optioncCs td��dS)Nztjd�d�D]*}tj�tj�|�|�}tj�|�rH|SqHW|S)a�Return 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'. ZPathr�)r~rVrr[rbrcr_rQ)r:ZexerA�fnrrrr�s   zMSVCCompiler.find_exe)rrr)N)rr�)NNNrNNN)NrN) NNNNNrNNNN)r) r/r0r1r2Z 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�r�r�r�rrrrr{+sR  W  V  R + r{)r )1r2rVrlrNr�Zdistutils.errorsrrrrrZdistutils.ccompilerrrr Z distutilsr Zdistutils.utilr �winregZ OpenKeyExrZEnumKeyrZ EnumValuer$�errorrZ HKEY_USERS�HKEY_CURRENT_USER�HKEY_LOCAL_MACHINEZHKEY_CLASSES_ROOTr�platform�maxsizeZ NATIVE_WIN64r7r@r?r�rr5rUrYr\rerzr|r{rrrr�sL  >. # )