o
     JAf“{  ã                   @   s´   	 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 )é    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   ú2/usr/lib/python3.10/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g d¢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d,d-„ Zd.d/„ Zd0d1„ Zejd2d3„ ƒZd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ ZdBdC„ ZdS )DÚ	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)ú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 rpath)r   r   r    r$   r%   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 u r| jj| _| jj| _| ¡ }|jdd}| j	d u r7| jj	p5g | _	t
| j	tƒrE| j	 tj¡| _	tjtjkrW| j	 tj tjd¡¡ | j	 | tjj¡¡ ||krq| j	 | tjj¡¡ |  d¡ |  d¡ | jd u rƒg | _| jd u rŒg | _nt
| jtƒrš| j tj¡| _| jd u r£g | _nt
| jtƒr±| j tj¡| _tjdkr-| 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¡¡rV| j tj tjddt%ƒ  d¡¡ n| j d¡ | &d¡rv|j'sp| j | &d ¡¡ n| j d¡ | j(rˆ| j( d!¡}d"d#„ |D ƒ| _(| j)r“| j) d!¡| _)| j*d u rg | _*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óz	t/| j.ƒ| _.W d S  t0yò   t1d%ƒ‚w d S )&Nr   )Ú	sysconfigZbuild)r'   r'   )r)   r)   )r2   r2   )r   r   )r    r    )r6   r6   )r(   r(   é   )Zplat_specificZincluder.   r1   ÚntZlibsZDebugZReleaseÚ_homeÚwin32é   ZPCbuildé   ÚcygwinÚbinÚlibÚpythonZconfigÚ.ÚPy_ENABLE_SHAREDZLIBDIRú,c                 S   s   g | ]}|d f‘qS )Ú1r   )Ú.0Úsymbolr   r   r   Ú
<listcomp>ù   s    z.build_ext.finalize_options.<locals>.<listcomp>ú zparallel should be an integer)2Ú	distutilsr:   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/   r0   ÚnameÚprefixr   r)   Údirnamer   Úgetattrr(   ÚplatformÚ
executableÚ
startswithr   Úget_config_varZpython_buildr,   r-   r5   r%   r   Úisdirr6   ÚintÚ
ValueErrorZDistutilsOptionError)
r8   r:   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   sb  ddl m} | jsd S | j ¡ r&|  d¡}| j | ¡ pg ¡ | j	 
|j¡ || j| j| j| jd| _t| jƒ tjdkrJ| jtƒ krJ| j | j¡ | jd urV| j | j¡ | jd urj| jD ]\}}| j ||¡ q^| jd ur{| jD ]}| j |¡ qr| jd ur‡| j | j¡ | j	d ur“| j | j	¡ | jd urŸ| j | j¡ | j d ur«| j !| j ¡ |  "¡  d S )Nr   )Únew_compilerÚ
build_clib)r2   ÚverboseÚdry_runr    r<   )#r   rh   r&   rN   Zhas_c_librariesÚget_finalized_commandr.   rZ   Zget_library_namesr/   rW   ri   r2   rj   rk   r    r   rR   r[   r(   r	   Z
initializer+   Zset_include_dirsr,   Zdefine_macror-   Zundefine_macroZset_librariesZset_library_dirsr0   Zset_runtime_library_dirsr1   Zset_link_objectsÚbuild_extensions)r8   rh   ri   r[   ÚvalueÚmacror   r   r   Úrun  s@   

ý








zbuild_ext.runc           
      C   sj  	 t |tƒs
tdƒ‚t|ƒD ]¤\}}t |tƒrqt |tƒr#t|ƒdkr'tdƒ‚|\}}t d|¡ t |t	ƒr;t
 |¡s?tdƒ‚t |tƒsHtdƒ‚t||d ƒ}dD ]}| |¡}|d urbt|||ƒ qQ| d	¡|_d
|v rrt d¡ | d¡}|r®g |_g |_|D ],}	t |	tƒrŽt|	ƒdv s’tdƒ‚t|	ƒdkr¡|j |	d ¡ qt|	ƒdkr­|j |	¡ q|||< qd S )Nz:'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_argsr0   Zdef_filez9'def_file' element of build info dict no longer supportedÚmacros)r;   rq   z9'macros' element of build info dict must be 1- or 2-tupler;   r   )rO   ÚlistÚDistutilsSetupErrorÚ	enumerater   ÚtupleÚlenr
   ÚwarnrP   Úextension_name_reÚmatchÚdictÚgetÚsetattrÚruntime_library_dirsÚdefine_macrosÚundef_macrosrW   )
r8   r&   r   ÚextÚext_nameZ
build_infoÚkeyÚvalrv   ro   r   r   r   Úcheck_extensions_listV  sf   
	ÿ
ÿþ
ÿÿ
ÿ
€

ÿ€
Âzbuild_ext.check_extensions_listc                 C   s,   |   | j¡ g }| jD ]}| |j¡ q|S ©N)r‰   r&   rZ   rr   )r8   Ú	filenamesr…   r   r   r   Úget_source_files¤  s
   
zbuild_ext.get_source_filesc                 C   s2   |   | j¡ g }| jD ]}| |  |j¡¡ q|S rŠ   )r‰   r&   rW   Úget_ext_fullpathr[   )r8   Zoutputsr…   r   r   r   Úget_outputs­  s
   
zbuild_ext.get_outputsc                 C   s*   |   | j¡ | jr|  ¡  d S |  ¡  d S rŠ   )r‰   r&   r6   Ú_build_extensions_parallelÚ_build_extensions_serialr7   r   r   r   rm   »  s   zbuild_ext.build_extensionsc              
      sâ   ˆj }ˆj du rt ¡ }zddlm} W n ty   d }Y nw |d u r*ˆ ¡  d S ||d8‰ ‡ ‡fdd„ˆjD ƒ}tˆj|ƒD ]\}}ˆ 	|¡ | 
¡  W d   ƒ n1 sYw   Y  qAW d   ƒ d S 1 sjw   Y  d S )NTr   )ÚThreadPoolExecutor)Úmax_workersc                    s   g | ]	}ˆ   ˆj|¡‘qS r   )ÚsubmitÚbuild_extension)rI   r…   ©Úexecutorr8   r   r   rK   Ñ  s    ÿz8build_ext._build_extensions_parallel.<locals>.<listcomp>)r6   rR   Ú	cpu_countÚconcurrent.futuresr‘   ÚImportErrorr   r&   ÚzipÚ_filter_build_errorsÚresult)r8   Úworkersr‘   Úfuturesr…   Úfutr   r•   r   r   Ã  s,   
ÿÿ
ÿ€ÿ"ýz$build_ext._build_extensions_parallelc              	   C   sD   | j D ]}|  |¡ |  |¡ W d   ƒ n1 sw   Y  qd S rŠ   )r&   r›   r”   )r8   r…   r   r   r   r   ×  s   
ÿ€ÿz"build_ext._build_extensions_serialc              
   c   sX    zd V  W d S  t ttfy+ } z|js‚ |  d|j|f ¡ W Y d }~d S d }~ww )Nz"building extension "%s" failed: %s)ZCCompilerErrorZDistutilsErrorZCompileErrorZoptionalr|   r[   )r8   r…   Úer   r   r   r›   Ü  s   €ÿ€ýzbuild_ext._filter_build_errorsc           
      C   sL  |j }|d u st|ttfƒstd|j ƒ‚t|ƒ}|  |j¡}||j }| j	s6t
||dƒs6t d|j¡ d S t d|j¡ |  ||¡}|jpGg }|jd d … }|jD ]}| |f¡ qR| 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_dirrv   r+   r   Úextra_postargsÚdepends)r.   r/   r‚   r¡   Úexport_symbolsr   r)   Ztarget_lang)"rr   rO   rw   rz   rx   r[   Úsortedr   r¢   r    r   r
   r   ÚinfoÚswig_sourcesrt   rƒ   r„   rW   r2   Úcompiler)   r+   Z_built_objectsrs   rZ   ru   ÚlanguageZdetect_languageZlink_shared_objectÚget_librariesr/   r‚   Úget_export_symbols)
r8   r…   rr   Úext_pathr¢   Ú
extra_argsrv   r-   Zobjectsr¨   r   r   r   r”   æ  sV   þÿ


ú


÷zbuild_ext.build_extensionc                 C   s&  	 g }g }i }| j rt d¡ | j sd| jv sd|jv rd}nd}|D ](}tj |¡\}}	|	dkrF| |d | ¡ | |¡ |d ||< q#| |¡ q#|sP|S | jpV|  	¡ }
|
dg}| 
| j¡ | j ri| d¡ | jsw|jD ]}| |¡ qo|D ]}|| }t d	||¡ |  |d
||g ¡ qy|S )Nz/--swig-cpp is deprecated - use --swig-opts=-c++z-c++z.cppz.cz.iÚ_wrapéÿÿÿÿz-pythonzswigging %s to %sz-o)r4   r
   r|   r5   rR   rX   ÚsplitextrW   r3   Ú	find_swigrZ   r¥   Úspawn)r8   rr   Ú	extensionZnew_sourcesr¦   Zswig_targetsZ
target_extÚsourceÚbaser…   r3   Zswig_cmdÚoÚtargetr   r   r   r¦   2  s@   




zbuild_ext.swig_sourcesc                 C   s\   	 t jdkrdS t jdkr'dD ]}t j d| d¡}t j |¡r$|  S qdS tdt j ƒ‚)NÚposixr3   r<   )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')rR   r[   rX   rY   ÚisfileZDistutilsPlatformError)r8   ZversÚfnr   r   r   r°   h  s   

ÿÿÿzbuild_ext.find_swigc                 C   s   	 |   |¡}| d¡}|  |d ¡}| js*tjj|d d… |g Ž }tj | j|¡S d |dd… ¡}|  d¡}tj 	| 
|¡¡}tj ||¡S )NrE   r®   r   Úbuild_py)Úget_ext_fullnamerQ   Úget_ext_filenamer   rR   rX   rY   r'   rl   ÚabspathZget_package_dir)r8   r†   ÚfullnameÚmodpathÚfilenamer*   rº   Zpackage_dirr   r   r   r   €  s   


zbuild_ext.get_ext_fullpathc                 C   s   	 | j d u r|S | j d | S )NrE   )r*   )r8   r†   r   r   r   r»   ›  s   
zbuild_ext.get_ext_fullnamec                 C   s0   	 ddl m} | d¡}|dƒ}tjj|Ž | S )Nr   ©rb   rE   Z
EXT_SUFFIX)Údistutils.sysconfigrb   rQ   rR   rX   rY   )r8   r†   rb   r«   Z
ext_suffixr   r   r   r¼   ¤  s
   
zbuild_ext.get_ext_filenamec                 C   sx   	 d|j  d¡d  }z| d¡ W n ty)   d| d¡ dd¡ d¡ }Y nw d	| }||jvr9|j |¡ |jS )
NÚ_rE   r®   Úasciir   Zpunycodeó   -ó   _ZPyInit)r[   rQ   ÚencodeÚUnicodeEncodeErrorÚreplaceÚdecoder£   rW   )r8   r…   rf   Zinitfunc_namer   r   r   rª   ®  s    ÿ
zbuild_ext.get_export_symbolsc                 C   sê   	 t jdkr2ddlm} t| j|ƒs/d}| jr|d }|t jd? t jd? d@ f }|j|g S |jS dd	l	m
} d
}|dƒrdtt dƒrFd}nt jdkrNd}ndtjv rd|dƒdkr\d}n|dƒdkrdd}|rr|dƒ}|jd| g S |jS )Nr>   r   )ÚMSVCCompilerz
python%d%dZ_dé   é   éÿ   rÁ   FrF   ÚgetandroidapilevelTrA   Z_PYTHON_HOST_PLATFORMZANDROID_API_LEVELZMACHDEPZ	LDVERSIONrD   )rT   r_   Zdistutils._msvccompilerrË   rO   r2   r   Ú
hexversionr.   rÂ   rb   ÚhasattrrR   Úenviron)r8   r…   rË   ÚtemplateZ	pythonlibrb   Zlink_libpythonZ	ldversionr   r   r   r©   Á  s8   
	ÿí


zbuild_ext.get_libraries) Ú__name__Ú
__module__Ú__qualname__ÚdescriptionrR   rS   Zsep_byr	   Zuser_optionsZboolean_optionsr   Zhelp_optionsr9   rg   rp   r‰   rŒ   rŽ   rm   r   r   Ú
contextlibÚcontextmanagerr›   r”   r¦   r°   r   r»   r¼   rª   r©   r   r   r   r   r   !   sp    
ÿÿÿÿÙ+ÿÿ @N	
	L6	
r   )rØ   rR   ÚrerT   Zdistutils.corer   Zdistutils.errorsrÂ   r   r   r   Zdistutils.dep_utilr   Zdistutils.extensionr   Zdistutils.utilr	   rM   r
   Zsiter   r§   r}   r   r   r   r   r   r   Ú<module>   s&    ÿ