o
    !gG                     @  s  d dl m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 d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZmZ erad dlmZ  nzd dl!mZ  e"d W n e#y|   d dlmZ  Y nw ed d dlm$Z% dd Z&dZ'dZ(dZ)ej*dkrdZ(nej+dkrzd dl,Z,e-e,d Z(Z'W n	 e#y   Y nw dd Z.dd Z/G dd de Ze(sej+dkr										d(d)d$d%Z0dS d&Z)										d(d)d'd%Z0dS )*    )annotationsN)IteratorEXTENSION_SUFFIXES)cache_from_source)Path)TYPE_CHECKING)Distribution)	BaseError)	ExtensionLibrary)log)new_compiler)customize_compilerget_config_var)	build_extzCython.Compiler.MainLDSHARED)_config_varsc              	   C  sl   t jdkr0t }zdtd< dtd< dtd< t|  W t  t| d S t  t| w t|  d S )Ndarwinz0gcc -Wl,-x -dynamiclib -undefined dynamic_lookupr   z -dynamiclibCCSHAREDz.dylibSO)sysplatform_CONFIG_VARScopyr   clearupdate)compilertmp r   [/root/parts/websockify/install/lib/python3.10/site-packages/setuptools/command/build_ext.py_customize_compiler_for_shlib)   s   

r!   Fsharedr   TntRTLD_NOWc                 C  s   t r| S dS )N )	have_rtld)sr   r   r    if_dlM   s   r(   c                  C  s.   t D ]} d| v r|   S | dkr|   S qdS )z;Return the file extension for an abi3-compliant Extension()z.abi3z.pydNr   )suffixr   r   r    get_abi3_suffixQ   s   r*   c                   @  s   e Zd ZU ded< dZdZdd Zd<d
dZd=ddZd>ddZ	d?ddZ
d@ddZdd Zd=ddZdd Zdd  Zd=d!d"Zd#d$ ZdAd&d'ZdBd)d*ZdAd+d,ZdCd.d/Zd0d1 Zd2d3 ZdDd=d4d5ZdDdEd7d8ZdFd9d:Zd;S )Gr   r	   distributionFc                 C  s2   | j d}| _ t|  || _ |r|   dS dS )z;Build extensions in build directory, then copy if --inplaceFN)inplace
_build_extruncopy_extensions_to_source)selfold_inplacer   r   r    r.   `   s   
zbuild_ext.runextr   returntuple[str, str]c           
      C  sj   |  |j}| |}|d}d|d d }||}tj|tj|}tj| j	|}	||	fS )N.)
get_ext_fullnamenameget_ext_filenamesplitjoinget_package_dirospathbasename	build_lib)
r0   build_pyr2   fullnamefilenamemodpathpackagepackage_dirinplace_fileregular_filer   r   r    _get_inplace_equivalenth   s   


z!build_ext._get_inplace_equivalentNonec                 C  sp   |  d}| jD ]-}| ||\}}tj|s|js$| j||| jd |j	r5| 
||}| j||dd qd S )NrA   )levelT)compile)get_finalized_command
extensionsrI   r=   r>   existsoptional	copy_fileverbose_needs_stub_get_equivalent_stub_write_stub_file)r0   rA   r2   rG   rH   inplace_stubr   r   r    r/   r   s   

z#build_ext.copy_extensions_to_sourceoutput_filestrc                 C  s2   t j|}|jd\}}}t j|| dS Nr5   .py)r=   r>   dirnamer8   
rpartitionr;   )r0   r2   rW   dir__r8   r   r   r    rT      s   zbuild_ext._get_equivalent_stubIterator[tuple[str, str]]c           
      c  s    | j sd S | d}| djpd}| jD ]/}| ||\}}||fV  |jrE| ||}| ||}t||d}t||d}	|	|fV  qd S )NrA   install_libr%   )optimization)r,   rM   optimizerN   rI   rS   rT   _compiled_file_name)
r0   rA   optr2   rG   rH   rV   regular_stubinplace_cacheoutput_cacher   r   r    _get_output_mapping   s    



zbuild_ext._get_output_mappingrB   c           	      C  s   t d}|rt jj|d | }nt| |}td}t|t	s&t
d|}|| jv ro| j| }t }|jrE|rE|d t|  | }t|trYt j|\}}| j|tS tro|jrot j|\}}t j|d| S |S )NSETUPTOOLS_EXT_SUFFIXr5   
EXT_SUFFIXzwConfiguration variable EXT_SUFFIX not found for this platform and environment variable SETUPTOOLS_EXT_SUFFIX is missingzdl-)r=   getenvr>   r;   r:   r-   r9   r   
isinstancerX   OSErrorext_mapr*   py_limited_apilenr   splitextshlib_compilerlibrary_filenamelibtype	use_stubs_links_to_dynamic)	r0   rB   so_extrC   
ext_suffixr2   abi3_suffixfndr   r   r    r9      s,   






zbuild_ext.get_ext_filenamec                 C  s&   t |  d | _g | _i | _d| _d S )NF)r-   initialize_optionsrr   shlibsrn   editable_moder0   r   r   r    r|      s
   

zbuild_ext.initialize_optionsc                 C  s8  t |  | jp	g | _| | j dd | jD | _| jr!|   | jD ]	}| |j|_q$| jD ]`}|j}|| j	|< || j	|
dd < | jrM| |pNd}|oXtoXt|t }||_||_| | }|_tjtj| j|}|r||jvr|j| |rtrtj|jvr|jtj q1| jrd| _d S d S )Nc                 S  s   g | ]	}t |tr|qS r   )rl   r   .0r2   r   r   r    
<listcomp>   s    z.build_ext.finalize_options.<locals>.<listcomp>r5   r6   FT)r-   finalize_optionsrN   check_extensions_listr}   setup_shlib_compilerr7   r8   
_full_namern   r:   links_to_dynamicru   rl   r   rv   rS   r9   
_file_namer=   r>   r[   r;   r@   library_dirsappendcurdirruntime_library_dirsr~   r,   )r0   r2   rB   ltdnsrC   libdirr   r   r    r      s4   




zbuild_ext.finalize_optionsc                 C  s   t | j| j| jd }| _t| | jd ur|| j | jd ur/| jD ]
\}}|	|| q$| j
d ur?| j
D ]}|| q7| jd urJ|| j | jd urU|| j | jd ur`|| j | jd urk|| j t||_d S )N)r   dry_runforce)r   r   r   r   rr   r!   include_dirsset_include_dirsdefinedefine_macroundefundefine_macro	librariesset_librariesr   set_library_dirsrpathset_runtime_library_dirslink_objectsset_link_objectslink_shared_object__get__)r0   r   r8   valuemacror   r   r    r      s*   







zbuild_ext.setup_shlib_compilerc                 C  s   t |tr|jS t| |S N)rl   r   export_symbolsr-   get_export_symbolsr0   r2   r   r   r    r      s   
zbuild_ext.get_export_symbolsc                 C  sl   |   | j}z*t|tr| j| _t| | |jr,| dj	}| 
|| W || _d S W || _d S || _w )NrA   )_convert_pyx_sources_to_langr   rl   r   rr   r-   build_extensionrS   rM   r@   
write_stub)r0   r2   	_compilerr@   r   r   r    r     s   

zbuild_ext.build_extensionc                   sP   t dd | jD  d|jddd dg t fdd|jD S )	z?Return true if 'ext' links to a dynamic lib in the same packagec                 S  s   g | ]}|j qS r   )r   )r   libr   r   r    r     s    z.build_ext.links_to_dynamic.<locals>.<listcomp>r5   Nr6   r%   c                 3  s    | ]	}|  v V  qd S r   r   )r   libnamelibnamespkgr   r    	<genexpr>      z-build_ext.links_to_dynamic.<locals>.<genexpr>)dictfromkeysr}   r;   r   r:   anyr   r   r   r   r    r     s    zbuild_ext.links_to_dynamic	list[str]c                 C  s   g t | |  S r   )r-   get_source_files_get_internal_dependsr   r   r   r    r     s   zbuild_ext.get_source_filesIterator[str]c              	   c  s    t | jjptj }dd | jD }ddd	}|D ]O}t |}| r+||d
 qd|jv r6||d qz
|| jdd}W n t	yN   ||d Y qw z|
| W n tyd   ||d Y qw | V  qdS )zAYield ``ext.depends`` that are contained by the project directoryc                 s  s     | ]}|j D ]}|V  qqd S r   )depends)r   r2   depr   r   r    r     s    z2build_ext._get_internal_depends.<locals>.<genexpr>	orig_pathrX   reasonr3   rJ   c                 S  s   t d| | d S )NzJdependency %s won't be automatically included in the manifest: the path %s)r   info)r   r   r   r   r    skip   s
   z-build_ext._get_internal_depends.<locals>.skipzmust be relativez..zcan't have `..` segmentsT)strictzdoesn't existzmust be inside the project rootN)r   rX   r   rX   r3   rJ   )r   r+   src_rootr=   r   resolverN   is_absolutepartsrm   relative_to
ValueErroras_posix)r0   project_rootr   r   r   r>   resolvedr   r   r    r     s4   





zbuild_ext._get_internal_dependsc                 C  s,   | j rt|   S tt| |   S r   )r,   listget_output_mappingkeyssortedr-   get_outputs_build_ext__get_stubs_outputsr   r   r   r    r   A  s   zbuild_ext.get_outputsdict[str, str]c                 C  s   |   }tt|dd dS )z1See :class:`setuptools.commands.build.SubCommand`c                 S  s   | d S )Nr   r   )xr   r   r    <lambda>I  s    z.build_ext.get_output_mapping.<locals>.<lambda>)key)rh   r   r   )r0   mappingr   r   r    r   F  s   zbuild_ext.get_output_mappingc                   s6    fdd j D }t|  }tdd |D S )Nc                 3  s4    | ]}|j rtjj jg|jd R  V  qdS )r5   N)rS   r=   r>   r;   r@   r   r:   r   r   r   r    r   M  s    
z0build_ext.__get_stubs_outputs.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S r   r   )r   basefnextr   r   r    r   T  r   )rN   	itertoolsproduct!_build_ext__get_output_extensionsr   )r0   ns_ext_basespairsr   r   r    __get_stubs_outputsK  s
   
zbuild_ext.__get_stubs_outputsc                 c  s(    dV  dV  |  djrdV  d S d S )NrZ   z.pycrA   z.pyo)rM   rb   r   r   r   r    __get_output_extensionsV  s   
z!build_ext.__get_output_extensionsc                 C  s2   t jj|g|jdR  d }| ||| d S rY   )r=   r>   r;   r   r:   rU   )r0   
output_dirr2   rL   	stub_filer   r   r    r   \  s    zbuild_ext.write_stubr   c                 C  s   t d|j| |rtj|rt|d | jsbt|ddd:}d	ddd	t
d
 dtj|jddddt
ddddt
ddddddt
ddddg}|| W d    n1 s]w   Y  |rk| | d S d S )Nz writing stub loader for %s to %sz already exists! Please delete.wzutf-8)encoding
zdef __bootstrap__():z-   global __bootstrap__, __file__, __loader__z0   import sys, os, pkg_resources, importlib.utilz, dlz7   __file__ = pkg_resources.resource_filename(__name__,)z   del __bootstrap__z    if '__loader__' in globals():z       del __loader__z#   old_flags = sys.getdlopenflags()z   old_dir = os.getcwd()z   try:z(     os.chdir(os.path.dirname(__file__))z$     sys.setdlopenflags(dl.RTLD_NOW)z3     spec = importlib.util.spec_from_file_location(z#                __name__, __file__)z0     mod = importlib.util.module_from_spec(spec)z!     spec.loader.exec_module(mod)z   finally:z"     sys.setdlopenflags(old_flags)z     os.chdir(old_dir)z__bootstrap__()r%   )r   r   r   r=   r>   rO   r
   r   openr;   r(   r?   r   write_compile_and_remove_stub)r0   r   r2   rL   fcontentr   r   r    rU   `  sF   
zbuild_ext._write_stub_filec                 C  sp   ddl m} ||gdd| jd | dj}|dkr$||g|d| jd tj|r4| js6t| d S d S d S )Nr   )byte_compileT)rb   r   r   r`   )	distutils.utilr   r   rM   rb   r=   r>   rO   unlink)r0   r   r   rb   r   r   r    r     s   z"build_ext._compile_and_remove_stubN)r2   r   r3   r4   )r3   rJ   )r2   r   rW   rX   r3   rX   )r3   r_   )rB   rX   r3   rX   )r3   r   )r3   r   )r3   r   )F)r   rX   r2   r   )r   rX   )__name__
__module____qualname____annotations__r~   r,   r.   rI   r/   rT   rh   r9   r|   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r   r   r    r   [   s2   
 








	

&
"r   debugboolr3   rJ   c                 C  s(   |  | j|||||||||	|
|| d S r   )linkSHARED_LIBRARY)r0   objectsoutput_libnamer   r   r   r   r   r   extra_preargsextra_postargs
build_temptarget_langr   r   r    r     s   r   staticc                 C  s^   |d u sJ t j|\}}t j|\}}| ddr$|dd  }| ||||| d S )Nr   r      )r=   r>   r:   rq   rs   
startswithcreate_static_lib)r0   r   r   r   r   r   r   r   r   r   r   r   r   rC   r?   _extr   r   r    r     s   )
NNNNNFNNNN)r   r   r3   rJ   )1
__future__r   r   r=   r   collections.abcr   importlib.machineryr   importlib.utilr   rc   pathlibr   typingr   setuptools.distr	   setuptools.errorsr
   setuptools.extensionr   r   	distutilsr   distutils.ccompilerr   distutils.sysconfigr   r   distutils.command.build_extr   r-   Cython.Distutils.build_ext
__import__ImportErrorr   r   r!   r&   ru   rt   r   r8   dlhasattrr(   r*   r   r   r   r   r    <module>   s    


  9!