B
    È S]Õv  ã               @   s¶   d Z ddlZddlZddlZddlZddlmZ ddlT ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ e d¡Zdd„ ZG dd„ deƒZdS )z¶distutils.command.build_ext

Implements the Distutils 'build_ext' command, for building extension
modules (currently limited to C extensions, should accommodate C++
extensions ASAP).é    N)ÚCommand)Ú*)Úcustomize_compilerÚget_python_version)Úget_config_h_filename)Únewer_group)Ú	Extension)Úget_platform)Úlog)Ú	USER_BASEz3^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$c              C   s   ddl m}  | ƒ  d S )Nr   )Úshow_compilers)Údistutils.ccompilerr   )r   © r   ú,lib/python3.7/distutils/command/build_ext.pyr      s    r   c               @   s  e Zd ZdZdej Zdddddeƒ  fdd	d
de fdddddde fddddddddddgZddddd gZ	d!d"d#e
fgZd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zejd6d7„ ƒZd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ ZdBdC„ ZdDdE„ ZdFdG„ Zd"S )HÚ	build_extz8build C/C++ extensions (compile/link to build directory)z (separated by '%s'))z
build-lib=Úbz(directory for compiled extension modules)zbuild-temp=Útz1directory for temporary files (build by-products)z
plat-name=Úpz>platform name to cross-compile for, if supported (default: %s))ÚinplaceÚiziignore build-lib and put compiled extensions into the source directory alongside your pure Python moduleszinclude-dirs=ÚIz.list of directories to search for header files)zdefine=ÚDzC preprocessor macros to define)zundef=ÚUz!C preprocessor macros to undefine)z
libraries=Úlz!external C libraries to link withzlibrary-dirs=ÚLz.directories to search for external C libraries)zrpath=ÚRz7directories to search for shared C libraries at runtime)zlink-objects=ÚOz2extra explicit link objects to include in the link)ÚdebugÚgz'compile/link with debugging information)ÚforceÚfz2forcibly build everything (ignore file timestamps))z	compiler=Úczspecify the compiler type)z	parallel=Újznumber of parallel build jobs)zswig-cppNz)make SWIG create C++ files (default is C))z
swig-opts=Nz!list of SWIG command line options)zswig=Nzpath to the SWIG executable)ÚuserNz#add user include, library and rpathr   r   r   zswig-cppr#   zhelp-compilerNzlist available compilersc             C   s‚   d | _ d | _d | _d | _d| _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _d | _d | _d | _d | _d | _d | _d S )Nr   )Ú
extensionsÚ	build_libÚ	plat_nameÚ
build_tempr   ÚpackageÚinclude_dirsÚdefineÚundefÚ	librariesÚlibrary_dirsÚrpathÚlink_objectsr   r   ÚcompilerÚswigÚswig_cppÚ	swig_optsr#   Úparallel)Úselfr   r   r   Úinitialize_optionsj   s*    zbuild_ext.initialize_optionsc       
   
   C   sü  ddl m} |  ddddddd	d
¡ | jd kr8| jj| _| jj| _| ¡ }|jdd}| j	d krn| jj	pjg | _	t
| j	tƒrŠ| j	 tj¡| _	tjtjkr®| j	 tj tjd¡¡ | j	 | tjj¡¡ ||krâ| j	 | tjj¡¡ |  d¡ |  d¡ | jd krg | _| jd krg | _nt
| jtƒr:| j tj¡| _| jd krNg | _nt
| jtƒrl| j tj¡| _tjdkrh| j tj tjd¡¡ tjtjkr¶| j tj tjd¡¡ | jrÒtj | jd¡| _ntj | jd¡| _| j	 tj tƒ ¡¡ t tdd ƒ}|r| j |¡ | j!dkr*d}n| j!dd … }tj tjd¡}|r\tj ||¡}| j |¡ tj"d d… dkrÆtj# $tj tjd¡¡rº| j tj tjddt%ƒ  d¡¡ n| j d¡ | &d¡rú|j'sî| j | &d ¡¡ n| j d¡ | j(r| j( d!¡}d"d#„ |D ƒ| _(| j)r4| j) d!¡| _)| j*d krHg | _*n| j* d$¡| _*| j+rºtj t,d¡}tj t,d¡}	tj -|¡r”| j	 |¡ tj -|	¡rº| j |	¡ | j |	¡ t
| j.tƒrøyt/| j.ƒ| _.W n t0k
rö   t1d%ƒ‚Y nX d S )&Nr   )Ú	sysconfigZbuild)r%   r%   )r'   r'   )r0   r0   )r   r   )r   r   )r4   r4   )r&   r&   é   )Zplat_specificZincluder,   r/   ÚntZlibsZDebugZReleaseÚ_homeÚwin32é   ZPCbuildé   ÚcygwinÚbinÚlibZpythonZconfigÚ.ÚPy_ENABLE_SHAREDZLIBDIRú,c             S   s   g | ]}|d f‘qS )Ú1r   )Ú.0Zsymbolr   r   r   ú
<listcomp>ù   s    z.build_ext.finalize_options.<locals>.<listcomp>ú zparallel should be an integer)2Ú	distutilsr7   Zset_undefined_optionsr(   ÚdistributionZext_packageZext_modulesr$   Zget_python_incr)   Ú
isinstanceÚstrÚsplitÚosÚpathsepÚsysÚexec_prefixÚbase_exec_prefixÚappendÚpathÚjoinÚextendZensure_string_listr,   r-   r.   ÚnameÚprefixr   r'   Údirnamer   Úgetattrr&   ÚplatformÚ
executableÚ
startswithr   Úget_config_varZpython_buildr*   r+   r3   r#   r   Úisdirr4   ÚintÚ
ValueErrorZDistutilsOptionError)
r5   r7   Z
py_includeZplat_py_includeZ	_sys_homeÚsuffixZnew_libZdefinesZuser_includeZuser_libr   r   r   Úfinalize_options‚   sª    






zbuild_ext.finalize_optionsc             C   sr  ddl m} | jsd S | j ¡ rL|  d¡}| j | ¡ p:g ¡ | j	 
|j¡ || j| j| j| jd| _t| jƒ tjdkr”| jtƒ kr”| j | j¡ | jd k	r¬| j | j¡ | jd k	rØx | jD ]\}}| j ||¡ q¾W | jd k	rþx| jD ]}| j |¡ qêW | jd k	r| j | j¡ | j	d k	r2| j | j	¡ | jd k	rL| j | j¡ | j d k	rf| j !| j ¡ |  "¡  d S )Nr   )Únew_compilerÚ
build_clib)r0   ÚverboseÚdry_runr   r9   )#r   rc   r$   rI   Zhas_c_librariesÚget_finalized_commandr,   rU   Zget_library_namesr-   rR   rd   r0   re   rf   r   r   rM   rV   r&   r	   Z
initializer)   Zset_include_dirsr*   Zdefine_macror+   Zundefine_macroZset_librariesZset_library_dirsr.   Zset_runtime_library_dirsr/   Zset_link_objectsÚbuild_extensions)r5   rc   rd   rV   ÚvalueÚmacror   r   r   Úrun  s>    





zbuild_ext.runc       
      C   s„  t |tƒstdƒ‚xjt|ƒD ]\\}}t |tƒr4qt |tƒrJt|ƒdkrRtdƒ‚|\}}t d|¡ t |t	ƒrzt
 |¡s‚tdƒ‚t |tƒs”tdƒ‚t||d ƒ}x*dD ]"}| |¡}|d	k	r¨t|||ƒ q¨W | d
¡|_d|krìt d¡ | d¡}|rtg |_g |_xj|D ]b}	t |	tƒr,t|	ƒdks4tdƒ‚t|	ƒdkrT|j |	d ¡ nt|	ƒdkr|j |	¡ qW |||< qW d	S )a¬  Ensure that the list of extensions (presumably provided as a
        command option 'extensions') is valid, i.e. it is a list of
        Extension objects.  We also support the old-style list of 2-tuples,
        where the tuples are (ext_name, build_info), which are converted to
        Extension instances here.

        Raise DistutilsSetupError if the structure is invalid anywhere;
        just returns otherwise.
        z:'ext_modules' option must be a list of Extension instancesé   zMeach element of 'ext_modules' option must be an Extension instance or 2-tuplezvold-style (ext_name, build_info) tuple found in ext_modules for extension '%s' -- please convert to Extension instancezRfirst element of each tuple in 'ext_modules' must be the extension name (a string)zOsecond element of each tuple in 'ext_modules' must be a dictionary (build info)Úsources)r)   r-   r,   Úextra_objectsÚextra_compile_argsÚextra_link_argsNr.   Zdef_filez9'def_file' element of build info dict no longer supportedÚmacros)r8   rl   z9'macros' element of build info dict must be 1- or 2-tupler8   r   )rJ   ÚlistÚDistutilsSetupErrorÚ	enumerater   ÚtupleÚlenr
   ÚwarnrK   Úextension_name_reÚmatchÚdictÚgetÚsetattrÚruntime_library_dirsÚdefine_macrosÚundef_macrosrR   )
r5   r$   r   ÚextÚext_nameZ
build_infoÚkeyÚvalrq   rj   r   r   r   Úcheck_extensions_listV  sP    










zbuild_ext.check_extensions_listc             C   s0   |   | j¡ g }x| jD ]}| |j¡ qW |S )N)r„   r$   rU   rm   )r5   Ú	filenamesr€   r   r   r   Úget_source_files¤  s
    zbuild_ext.get_source_filesc             C   s6   |   | j¡ g }x | jD ]}| |  |j¡¡ qW |S )N)r„   r$   rR   Úget_ext_fullpathrV   )r5   Zoutputsr€   r   r   r   Úget_outputs­  s
    zbuild_ext.get_outputsc             C   s(   |   | j¡ | jr|  ¡  n|  ¡  d S )N)r„   r$   r4   Ú_build_extensions_parallelÚ_build_extensions_serial)r5   r   r   r   rh   »  s    
zbuild_ext.build_extensionsc                s¾   ˆj }ˆj dkrt ¡ }yddlm} W n tk
r@   d }Y nX |d krVˆ ¡  d S ||dT‰ ‡ ‡fdd„ˆjD ƒ}x6tˆj|ƒD ]&\}}ˆ 	|¡ | 
¡  W d Q R X q†W W d Q R X d S )NTr   )ÚThreadPoolExecutor)Zmax_workersc                s   g | ]}ˆ   ˆj|¡‘qS r   )ZsubmitÚbuild_extension)rE   r€   )Úexecutorr5   r   r   rF   Ñ  s   z8build_ext._build_extensions_parallel.<locals>.<listcomp>)r4   rM   Ú	cpu_countZconcurrent.futuresr‹   ÚImportErrorrŠ   r$   ÚzipÚ_filter_build_errorsÚresult)r5   Zworkersr‹   Zfuturesr€   Zfutr   )r   r5   r   r‰   Ã  s     


z$build_ext._build_extensions_parallelc          
   C   s4   x.| j D ]$}|  |¡ |  |¡ W d Q R X qW d S )N)r$   r‘   rŒ   )r5   r€   r   r   r   rŠ   ×  s    z"build_ext._build_extensions_serialc          
   c   sT   y
d V  W nD t ttfk
rN } z |js*‚ |  d|j|f ¡ W d d }~X Y nX d S )Nz"building extension "%s" failed: %s)ZCCompilerErrorZDistutilsErrorZCompileErrorZoptionalrw   rV   )r5   r€   Úer   r   r   r‘   Ü  s    
zbuild_ext._filter_build_errorsc       
      C   sT  |j }|d kst|ttfƒs*td|j ƒ‚t|ƒ}|  |j¡}||j }| jslt	||dƒslt
 d|j¡ d S t
 d|j¡ |  ||¡}|jpŽg }|jd d … }x|jD ]}| |f¡ q¦W | jj|| j||j| j||jd}|d d … | _|jrü| |j¡ |jpg }|jp| j |¡}	| jj|||  |¡|j|j||   |¡| j| j|	d
 d S )Nzjin 'ext_modules' option (extension '%s'), 'sources' must be present and must be a list of source filenamesZnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extension)Z
output_dirrq   r)   r   Úextra_postargsÚdepends)r,   r-   r}   r”   Úexport_symbolsr   r'   Ztarget_lang)!rm   rJ   rr   ru   rs   rV   r‡   r•   r   r   r
   r   ÚinfoÚswig_sourcesro   r~   r   rR   r0   Úcompiler'   r)   Z_built_objectsrn   rU   rp   ÚlanguageZdetect_languageZlink_shared_objectÚget_librariesr-   r}   Úget_export_symbols)
r5   r€   rm   Úext_pathr•   Z
extra_argsrq   r+   Zobjectsrš   r   r   r   rŒ   æ  sN    



zbuild_ext.build_extensionc             C   s0  g }g }i }| j rt d¡ | j s6d| jks6d|jkr<d}nd}xX|D ]P}tj |¡\}}	|	dkrŒ| |d | ¡ | |¡ |d ||< qF| |¡ qFW |s¢|S | jp®|  	¡ }
|
dg}| 
| j¡ | j rÔ| d¡ | jsôx|jD ]}| |¡ qâW x6|D ].}|| }t d	||¡ |  |d
||g ¡ qúW |S )züWalk the list of source files in 'sources', looking for SWIG
        interface (.i) files.  Run SWIG on all that are found, and
        return a modified 'sources' list with SWIG source files replaced
        by the generated C (or C++) files.
        z/--swig-cpp is deprecated - use --swig-opts=-c++z-c++z.cppz.cz.iÚ_wrapéÿÿÿÿz-pythonzswigging %s to %sz-o)r2   r
   rw   r3   rM   rS   ÚsplitextrR   r1   Ú	find_swigrU   r—   Zspawn)r5   rm   Ú	extensionZnew_sourcesr˜   Zswig_targetsZ
target_extÚsourceÚbaser€   r1   Zswig_cmdÚoÚtargetr   r   r   r˜   1  s>    





zbuild_ext.swig_sourcesc             C   s`   t jdkrdS t jdkrNxBdD ]&}t j d| d¡}t j |¡r|S qW dS ntdt j ƒ‚dS )	z›Return the name of the SWIG executable.  On Unix, this is
        just "swig" -- it should be in the PATH.  Tries a bit harder on
        Windows.
        Úposixr1   r9   )z1.3z1.2z1.1z	c:\swig%szswig.exez>I don't know how to find (much less run) SWIG on platform '%s'N)rM   rV   rS   rT   ÚisfileZDistutilsPlatformError)r5   ZversÚfnr   r   r   r¡   g  s    


zbuild_ext.find_swigc             C   sŽ   |   |¡}| d¡}|  |d ¡}| jsRtjj|dd… |g Ž }tj | j|¡S d |dd… ¡}|  d¡}tj 	| 
|¡¡}tj ||¡S )z¡Returns the path of the filename for a given extension.

        The file is located in `build_lib` or directly in the package
        (inplace option).
        rA   rŸ   Nr   Úbuild_py)Úget_ext_fullnamerL   Úget_ext_filenamer   rM   rS   rT   r%   rg   ÚabspathZget_package_dir)r5   r   ÚfullnameZmodpathÚfilenamer(   rª   Zpackage_dirr   r   r   r‡     s    


zbuild_ext.get_ext_fullpathc             C   s    | j dkr|S | j d | S dS )zSReturns the fullname of a given extension name.

        Adds the `package.` prefixNrA   )r(   )r5   r   r   r   r   r«   š  s    
zbuild_ext.get_ext_fullnamec             C   s.   ddl m} | d¡}|dƒ}tjj|Ž | S )z¦Convert the name of an extension (eg. "foo.bar") into the name
        of the file from which it will be loaded (eg. "foo/bar.so", or
        "foo\bar.pyd").
        r   )r]   rA   Z
EXT_SUFFIX)Údistutils.sysconfigr]   rL   rM   rS   rT   )r5   r   r]   r   Z
ext_suffixr   r   r   r¬   £  s    
zbuild_ext.get_ext_filenamec             C   s0   d|j  d¡d  }||jkr*|j |¡ |jS )a  Return the list of symbols that a shared extension has to
        export.  This either uses 'ext.export_symbols' or, if it's not
        provided, "PyInit_" + module_name.  Only relevant on Windows, where
        the .pyd file (DLL) must export the module "PyInit_" function.
        ZPyInit_rA   rŸ   )rV   rL   r–   rR   )r5   r€   Zinitfunc_namer   r   r   rœ   ­  s    
zbuild_ext.get_export_symbolsc             C   sÜ   t jdkrdddlm} t| j|ƒs\d}| jr4|d }|t jd? t jd? d@ f }|j|g S |jS ntt jd	krt|jS t jd
d… dkrŒ|jS ddl	m
} | d¡rÒd t jd? t jd? d@ | d¡¡}|j|g S |jS d
S )zÓReturn the list of libraries to link against when building a
        shared extension.  On most platforms, this is just 'ext.libraries';
        on Windows, we add the Python library (eg. python20.dll).
        r;   r   )ÚMSVCCompilerz
python%d%dZ_dé   é   éÿ   ÚdarwinNé   Zaix)r7   rB   zpython{}.{}{}ZABIFLAGS)rO   rZ   Zdistutils._msvccompilerr±   rJ   r0   r   Ú
hexversionr,   rH   r7   r]   Úformat)r5   r€   r±   ÚtemplateZ	pythonlibr7   r   r   r   r›   ¸  s*    



zbuild_ext.get_libraries) Ú__name__Ú
__module__Ú__qualname__ZdescriptionrM   rN   Zsep_byr	   Zuser_optionsZboolean_optionsr   Zhelp_optionsr6   rb   rk   r„   r†   rˆ   rh   r‰   rŠ   Ú
contextlibÚcontextmanagerr‘   rŒ   r˜   r¡   r‡   r«   r¬   rœ   r›   r   r   r   r   r   !   s^   

 @N	
K6	
r   )Ú__doc__r½   rM   ÚrerO   Zdistutils.corer   Zdistutils.errorsr°   r   r   r   Zdistutils.dep_utilr   Zdistutils.extensionr   Zdistutils.utilr	   rH   r
   Zsiter   r™   rx   r   r   r   r   r   r   Ú<module>   s"   