o
    5‘ði¤  ã                   @  sú   d Z ddlm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	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZmZmZmZmZ ddlmZ ddlm Z m!Z!m"Z" ddl#m$Z$m%Z%m&Z& e '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).é    )ÚannotationsN)ÚCallable)Úlog)Ú	USER_BASE)ÚClassVaré   )Únewer_group)Únew_compilerÚshow_compilers)ÚCommand)ÚCCompilerErrorÚCompileErrorÚDistutilsErrorÚDistutilsOptionErrorÚDistutilsPlatformErrorÚDistutilsSetupError)Ú	Extension)Úcustomize_compilerÚget_config_h_filenameÚget_python_version)Úget_platformÚis_freethreadedÚis_mingwz3^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$c                   @  sR  e Zd ZU dZdej› d�Zdddddeƒ › d	�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e
d < d!d"d#efgZd$e
d%< d&d'„ Zed(d)„ ƒZdUd,d-„ZdUd.d/„ZdUd0d1„Zd2d3„ Zd4d5„ ZdUd6d7„Zd8d9„ Zd:d;„ Zejd<d=„ ƒZdUd>d?„Zd@dA„ ZdBdC„ ZdVdFdG„ZdVdHdI„ZdVdJdK„Z dWdOdP„Z!dQdR„ Z"dWdSdT„Z#d"S )XÚ	build_extz8build C/C++ extensions (compile/link to build directory)z (separated by 'z'))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: ú])Ú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ClassVar[list[str]]Úboolean_optionszhelp-compilerNzlist available compilerszAClassVar[list[tuple[str, str | None, str, Callable[[], object]]]]Úhelp_optionsc                 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 )NF)Ú
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©Úself© rD   úf/root/parts/websockify/install/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.pyÚinitialize_optionsp   s*   
zbuild_ext.initialize_optionsc                 c  sb   � |   d¡sdS | jrdV  dS tjdkr)|   d¡}|   d¡}tj ||¡V  dS |   d¡V  dS )zµ
        Resolve Python's library directory for building extensions
        that rely on a shared Python library.

        See python/cpython#44264 and python/cpython#48686
        ÚPy_ENABLE_SHAREDNÚ.ÚzosÚbaseÚ
platlibdirÚLIBDIR)Úget_config_varÚpython_buildÚsysÚplatformÚosÚpathÚjoin)Ú	sysconfigÚinstalled_dirÚlib_dirrD   rD   rE   Ú_python_lib_dirˆ   s   €



zbuild_ext._python_lib_dirÚreturnÚNonec           	   
   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)tƒ �s)| 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 ƒ ¡¡ | j t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�rO|j#�sI| j tj tjddt$ƒ  d¡¡ n| j d¡ | j |  %|¡¡ | j&�rj| j& d¡}dd„ |D ƒ| _&| j'�ru| j' d¡| _'| j(d u �rg | _(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  t.�yÔ   t/d!ƒ‚w d S )"Nr   )rT   Úbuild)r2   r2   )r4   r4   )r=   r=   )r'   r'   )r)   r)   )rA   rA   )r3   r3   T)Úplat_specificÚincluder9   r<   ÚntÚlibsÚDebugÚReleaseÚwin32é   ÚPCbuildé   ÚcygwinÚlibÚpythonÚconfigrH   ú,c                 S  s   g | ]}|d f‘qS )Ú1rD   )Ú.0ÚsymbolrD   rD   rE   Ú
<listcomp>  s    z.build_ext.finalize_options.<locals>.<listcomp>ú zparallel should be an integer)0Ú	distutilsrT   Úset_undefined_optionsr5   ÚdistributionÚext_packageÚext_modulesr1   Úget_python_incr6   Ú
isinstanceÚstrÚsplitrQ   ÚpathseprO   Úexec_prefixÚbase_exec_prefixÚappendrR   rS   ÚextendÚensure_string_listr9   r:   r;   Únamer   Úprefixr'   r4   Údirnamer   r3   rP   rN   r   rW   r7   r8   r@   r.   r   ÚisdirrA   ÚintÚ
ValueErrorr   )	rC   rT   Ú
py_includeÚplat_py_includeÚsuffixÚnew_libÚdefinesÚuser_includeÚuser_librD   rD   rE   Úfinalize_options¡   sª   ø








ÿÿÿýzbuild_ext.finalize_optionsc                 C  sp  | j sd S | j ¡ r |  d¡}| j | ¡ pg ¡ | j |j	¡ t
| j| j| jd�| _t| jƒ tjdkrB| jtƒ krB| j | j¡ tjdkrQtƒ rQ| j dd¡ | jd ur]| j | j¡ | jd urq| jD ]\}}| j ||¡ qe| jd ur‚| jD ]}| j |¡ qy| 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 )NÚ
build_clib)r=   Úverboser)   r]   ÚPy_GIL_DISABLEDrj   )"r1   rq   Úhas_c_librariesÚget_finalized_commandr9   r|   Úget_library_namesr:   r{   rŒ   r	   r=   r�   r)   r   rQ   r~   r3   r   Ú
initializer   Údefine_macror6   Úset_include_dirsr7   r8   Úundefine_macroÚset_librariesÚset_library_dirsr;   Úset_runtime_library_dirsr<   Úset_link_objectsÚbuild_extensions)rC   rŒ   r~   ÚvalueÚmacrorD   rD   rE   Úrun,  sB   

ý








zbuild_ext.runc           
      C  sh  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ƒsGtdƒ‚t||d ƒ}dD ]}| |¡}|d	urat|||ƒ qP| d
¡|_d|v rqt d¡ | d¡}|r­g |_g |_|D ],}	t |	tƒr�t|	ƒdv s‘tdƒ‚t|	ƒdkr |j |	d ¡ q€t|	ƒdkr¬|j |	¡ q€|||< q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 instancesr   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)r6   r:   r9   Úextra_objectsÚextra_compile_argsÚextra_link_argsNr;   Údef_filez9'def_file' element of build info dict no longer supportedÚmacros)é   r   z9'macros' element of build info dict must be 1- or 2-tupler¤   r   )ru   Úlistr   Ú	enumerater   ÚtupleÚlenr   Úwarningrv   Úextension_name_reÚmatchÚdictÚgetÚsetattrÚruntime_library_dirsÚdefine_macrosÚundef_macrosr{   )
rC   r1   r   ÚextÚext_nameÚ
build_infoÚkeyÚvalr£   rœ   rD   rD   rE   Úcheck_extensions_listq  sb   

ÿ
ÿüÿ
ÿ
€

ÿ€
¹zbuild_ext.check_extensions_listc                 C  s,   |   | j¡ g }| jD ]}| |j¡ q|S ©N)r·   r1   r|   rž   )rC   Ú	filenamesr²   rD   rD   rE   Úget_source_filesÉ  s
   
zbuild_ext.get_source_filesc                   s    ˆ   ˆ j¡ ‡ fdd„ˆ jD ƒS )Nc                   s   g | ]}ˆ   |j¡‘qS rD   )Úget_ext_fullpathr~   ©rk   r²   rB   rD   rE   rm   Û  s    z)build_ext.get_outputs.<locals>.<listcomp>)r·   r1   rB   rD   rB   rE   Úget_outputsÒ  s   zbuild_ext.get_outputsc                 C  s*   |   | j¡ | jr|  ¡  d S |  ¡  d S r¸   )r·   r1   rA   Ú_build_extensions_parallelÚ_build_extensions_serialrB   rD   rD   rE   rš   Ý  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 rD   )ÚsubmitÚbuild_extensionr¼   ©ÚexecutorrC   rD   rE   rm   ó  s    ÿz8build_ext._build_extensions_parallel.<locals>.<listcomp>)rA   rQ   Ú	cpu_countÚconcurrent.futuresrÀ   ÚImportErrorr¿   r1   ÚzipÚ_filter_build_errorsÚresult)rC   ÚworkersrÀ   Úfuturesr²   ÚfutrD   rÄ   rE   r¾   å  s,   
ÿÿ
ÿ€ÿ"üz$build_ext._build_extensions_parallelc              	   C  sD   | j D ]}|  |¡� |  |¡ W d   ƒ n1 sw   Y  qd S r¸   )r1   rÊ   rÃ   )rC   r²   rD   rD   rE   r¿   ú  s   
ÿ€ÿz"build_ext._build_extensions_serialc              
   c  s\   � zd V  W d S  t ttfy- } z|js‚ |  d|j› d|› �¡ W Y d }~d S d }~ww )Nzbuilding extension "z
" failed: )r   r   r   ÚoptionalÚwarnr~   )rC   r²   ÚerD   rD   rE   rÊ   ÿ  s   €&€ýzbuild_ext._filter_build_errorsc           
      C  sP  |j }|d u st|ttfƒstd|j› d�ƒ‚t|ƒ}|  |j¡}||j }| j	s8t
||dƒs8t d|j¡ d S t d|j¡ |  ||¡}|jpIg }|jd d … }|jD ]}| |f¡ qT| 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 )Nz$in 'ext_modules' option (extension 'zD'), 'sources' must be present and must be a list of source filenamesÚnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extension)Ú
output_dirr£   r6   r'   Úextra_postargsÚdepends)r9   r:   r¯   rÔ   Úexport_symbolsr'   r4   Útarget_lang)"rž   ru   r¥   r§   r   r~   Úsortedr»   rÕ   r)   r   r   r'   ÚinfoÚswig_sourcesr    r°   r±   r{   r=   Úcompiler4   r6   Ú_built_objectsrŸ   r|   r¡   ÚlanguageÚdetect_languageÚlink_shared_objectÚget_librariesr:   r¯   Úget_export_symbols)
rC   r²   rž   Úext_pathrÕ   Ú
extra_argsr£   r8   ÚobjectsrÝ   rD   rD   rE   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rE| |d | ¡ | |¡ |d ||< q"| |¡ q"|sO|S | jpU|  	¡ }
|
dg}| 
| j¡ | j rh| d¡ | jsq| 
|j¡ |D ]}|| }t d	||¡ |  |d
||g ¡ qs|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)r?   r   r©   r@   rQ   rR   Úsplitextr{   r>   Ú	find_swigr|   rÙ   Úspawn)rC   rž   Ú	extensionÚnew_sourcesrÚ   Úswig_targetsÚ
target_extÚsourcerJ   r²   r>   Úswig_cmdÚtargetrD   rD   rE   rÚ   Y  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› d�ƒ‚)	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.
        Úposixr>   r]   )z1.3z1.2z1.1zc:\swigzswig.exez;I don't know how to find (much less run) SWIG on platform 'ú')rQ   r~   rR   rS   Úisfiler   )rC   ÚversÚfnrD   rD   rE   rè   ‘  s   

ÿÿzbuild_ext.find_swigr³   rv   c                 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 )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).
        rH   ræ   Nr   Úbuild_py)Úget_ext_fullnamerw   Úget_ext_filenamer   rQ   rR   rS   r2   r�   ÚabspathÚget_package_dir)rC   r³   ÚfullnameÚmodpathÚfilenamer5   rö   Úpackage_dirrD   rD   rE   r»   ©  s   


zbuild_ext.get_ext_fullpathc                 C  s   | j du r|S | j d | S )zSReturns the fullname of a given extension name.

        Adds the `package.` prefixNrH   )r5   )rC   r³   rD   rD   rE   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   ©rM   rH   Ú
EXT_SUFFIX)rT   rM   rw   rQ   rR   rS   )rC   r³   rM   râ   Ú
ext_suffixrD   rD   rE   rø   Í  s   
zbuild_ext.get_ext_filenamer²   r   ú	list[str]c                 C  st   |   |¡}z| d¡ W n ty#   d| d¡ dd¡ d¡ }Y nw d| }d| }||jvr7|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.
        ÚasciiÚU_Úpunycodeó   -ó   _Ú_ÚPyInit)Ú_get_module_name_for_symbolÚencodeÚUnicodeEncodeErrorÚreplaceÚdecoderÖ   r{   )rC   r²   r~   r†   Úinitfunc_namerD   rD   rE   rá   Ø  s   
 ÿ
zbuild_ext.get_export_symbolsc                 C  s4   |j  d¡}|d dkrt|ƒdkr|d S |d S )NrH   ræ   Ú__init__r   éþÿÿÿ)r~   rw   r¨   )rC   r²   ÚpartsrD   rD   rE   r
  í  s   z%build_ext._get_module_name_for_symbolc                 C  sô   t jdkr4tƒ s4ddlm} t| j|ƒs1d}| jr|d }|t jd? t jd? d@ f }|j	|g S |j	S dd	l
m} d
}|dƒritt dƒrHd}n!t jdksPtƒ rSd}ndtjv ri|dƒdkrad}n|dƒdkrid}|rw|dƒ}|j	d| g S |j	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).
        ra   r   )ÚMSVCCompilerz
python%d%dÚ_dé   é   éÿ   rÿ   FrG   ÚgetandroidapilevelTre   Ú_PYTHON_HOST_PLATFORMÚANDROID_API_LEVELr   ÚMACHDEPÚ	LDVERSIONrg   )rO   rP   r   Ú_msvccompilerr  ru   r=   r'   Ú
hexversionr9   rT   rM   ÚhasattrrQ   Úenviron)rC   r²   r  ÚtemplateÚ	pythonlibrM   Úlink_libpythonÚ	ldversionrD   rD   rE   rà   ö  s8   
þì

zbuild_ext.get_libraries)rX   rY   )r³   rv   rX   rv   )r²   r   rX   r  )$Ú__name__Ú
__module__Ú__qualname__ÚdescriptionrQ   rx   Úsep_byr   Úuser_optionsr/   Ú__annotations__r
   r0   rF   ÚstaticmethodrW   r‹   r�   r·   rº   r½   rš   r¾   r¿   Ú
contextlibÚcontextmanagerrÊ   rÃ   rÚ   rè   r»   r÷   rø   rá   r
  rà   rD   rD   rD   rE   r   &   sx   
 ÿýýýÛ(
	ÿ

 

EX	


Q8


	
	r   )*Ú__doc__Ú
__future__r   r-  rQ   ÚrerO   Úcollections.abcr   Údistutils._logr   Úsiter   Útypingr   Ú	_modifiedr   Ú	ccompilerr	   r
   Úcorer   Úerrorsr   r   r   r   r   r   rê   r   rT   r   r   r   Úutilr   r   r   rÛ   rª   r   rD   rD   rD   rE   Ú<module>   s&     
