o
    NZh!H                     @  s  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 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red 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/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   K/var/www/auris/lib/python3.10/site-packages/setuptools/command/build_ext.py_customize_compiler_for_shlib*   s   

r!   FZsharedr   TntRTLD_NOWc                  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_suffixN   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)selfZ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joinZget_package_dirospathbasename	build_lib)
r+   build_pyr,   fullnamefilenamemodpathpackageZpackage_dirinplace_fileregular_filer   r   r    _get_inplace_equivalente   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 )Nr:   )levelT)compile)get_finalized_command
extensionsrA   r6   r7   existsoptional	copy_fileverbose_needs_stub_get_equivalent_stub_write_stub_file)r+   r:   r,   r?   r@   inplace_stubr   r   r    r*   o   s   

z#build_ext.copy_extensions_to_sourceoutput_filestrc                 C  s2   t j|}|jd\}}}t j|| dS Nr/   .py)r6   r7   dirnamer2   
rpartitionr5   )r+   r,   rO   Zdir__r2   r   r   r    rL      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 )Nr:   install_lib )optimization)r'   rE   optimizerF   rA   rK   rL   _compiled_file_name)
r+   r:   optr,   r?   r@   rN   Zregular_stubZinplace_cacheZoutput_cacher   r   r    _get_output_mapping   s    



zbuild_ext._get_output_mappingr;   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 )NZSETUPTOOLS_EXT_SUFFIXr/   
EXT_SUFFIXzwConfiguration variable EXT_SUFFIX not found for this platform and environment variable SETUPTOOLS_EXT_SUFFIX is missingzdl-)r6   getenvr7   r5   r4   r(   r3   r   
isinstancerP   OSErrorext_mapr%   Zpy_limited_apilenr   splitextshlib_compilerlibrary_filenamelibtype	use_stubs_links_to_dynamic)	r+   r;   Zso_extr<   Z
ext_suffixr,   Zabi3_suffixfndr   r   r    r3      s,   






zbuild_ext.get_ext_filenamec                 C  s&   t |  d | _g | _i | _d| _d S )NF)r(   initialize_optionsre   shlibsrb   editable_moder+   r   r   r    rl      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   )r`   r   .0r,   r   r   r    
<listcomp>   s    z.build_ext.finalize_options.<locals>.<listcomp>r/   r0   FT)r(   finalize_optionsrF   Zcheck_extensions_listrm   setup_shlib_compilerr1   r2   
_full_namerb   r4   links_to_dynamicrh   r`   r   ri   rK   r3   
_file_namer6   r7   rS   r5   r9   library_dirsappendcurdirruntime_library_dirsrn   r'   )r+   r,   r;   Zltdnsr<   libdirr   r   r    rs      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   re   r!   Zinclude_dirsZset_include_dirsZdefineZdefine_macroZundefZundefine_macro	librariesZset_librariesrx   Zset_library_dirsZrpathZset_runtime_library_dirsZlink_objectsZset_link_objectslink_shared_object__get__)r+   r   r2   valuemacror   r   r    rt      s*   







zbuild_ext.setup_shlib_compilerc                 C  s   t |tr|jS t| |S N)r`   r   export_symbolsr(   get_export_symbolsr+   r,   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 )Nr:   )Z_convert_pyx_sources_to_langr   r`   r   re   r(   build_extensionrK   rE   r9   
write_stub)r+   r,   Z	_compilerr9   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   )ru   )rq   libr   r   r    rr     s    z.build_ext.links_to_dynamic.<locals>.<listcomp>r/   Nr0   rX   c                 3  s    | ]	}|  v V  qd S r   r   )rq   ZlibnameZlibnamespkgr   r    	<genexpr>      z-build_ext.links_to_dynamic.<locals>.<genexpr>)dictfromkeysrm   r5   ru   r4   anyr   r   r   r   r    rv     s    zbuild_ext.links_to_dynamic	list[str]c                 C  s   g t | |  S r   )r(   get_source_files_get_internal_dependsro   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)rq   r,   depr   r   r    r     s    z2build_ext._get_internal_depends.<locals>.<genexpr>	orig_pathrP   reasonr-   rB   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   rP   r   rP   r-   rB   )r   r&   Zsrc_rootr6   rz   resolverF   is_absolutepartsra   relative_to
ValueErroras_posix)r+   Zproject_rootr   r   r   r7   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_outputsro   r   r   r    r   >  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>F  s    z.build_ext.get_output_mapping.<locals>.<lambda>)key)r]   r   r   )r+   mappingr   r   r    r   C  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 )r/   N)rK   r6   r7   r5   r9   ru   r4   rp   ro   r   r    r   J  s    
z0build_ext.__get_stubs_outputs.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S r   r   )rq   baseZfnextr   r   r    r   Q  r   )rF   	itertoolsproduct!_build_ext__get_output_extensionsr   )r+   Zns_ext_basespairsr   ro   r    Z__get_stubs_outputsH  s
   
zbuild_ext.__get_stubs_outputsc                 c  s(    dV  dV  |  djrdV  d S d S )NrR   z.pycr:   z.pyo)rE   rZ   ro   r   r   r    Z__get_output_extensionsS  s   
z!build_ext.__get_output_extensionsc                 C  s2   t jj|g|jdR  d }| ||| d S rQ   )r6   r7   r5   ru   r4   rM   )r+   
output_dirr,   rD   	stub_filer   r   r    r   Y  s    zbuild_ext.write_stubr   c                 C  s   t d|j| |rtj|rt|d | jsLt|ddd$}t	
dtj|jd ddt  }|| W d    n1 sGw   Y  |rU| | d S d S )	Nz writing stub loader for %s to %sz already exists! Please delete.wzutf-8)encodinga7  
                    def __bootstrap__():
                       global __bootstrap__, __file__, __loader__
                       import sys, os, importlib.resources as irs, importlib.util
                    #rtld   import dl
                       with irs.files(__name__).joinpath(
                         a  ) as __file__:
                          del __bootstrap__
                          if '__loader__' in globals():
                              del __loader__
                    #rtld      old_flags = sys.getdlopenflags()
                          old_dir = os.getcwd()
                          try:
                            os.chdir(os.path.dirname(__file__))
                    #rtld        sys.setdlopenflags(dl.RTLD_NOW)
                            spec = importlib.util.spec_from_file_location(
                                       __name__, __file__)
                            mod = importlib.util.module_from_spec(spec)
                            spec.loader.exec_module(mod)
                          finally:
                    #rtld        sys.setdlopenflags(old_flags)
                            os.chdir(old_dir)
                    __bootstrap__()
                    z#rtld)r   r   ru   r6   r7   rG   r
   r~   opentextwrapdedentr8   rw   lstripreplace	have_rtldwrite_compile_and_remove_stub)r+   r   r,   rD   fcontentr   r   r    rM   ]  s    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)rZ   r   r~   rW   )	Zdistutils.utilr   r~   rE   rZ   r6   r7   rG   unlink)r+   r   r   rZ   r   r   r    r     s   z"build_ext._compile_and_remove_stubN)r,   r   r-   r.   )r-   rB   )r,   r   rO   rP   r-   rP   )r-   rV   )r;   rP   r-   rP   )r-   r   )r-   r   )r-   r   )F)r   rP   r,   r   )r   rP   )__name__
__module____qualname____annotations__rn   r'   r)   rA   r*   rL   r]   r3   rl   rs   rt   r   r   rv   r   r   r   r   r   r   r   rM   r   r   r   r   r    r   X   s2   
 








	

&
&r   debugboolr-   rB   c                 C  s(   |  | j|||||||||	|
|| d S r   )linkZSHARED_LIBRARY)r+   objectsoutput_libnamer   r   rx   r{   r   r   extra_preargsextra_postargs
build_temptarget_langr   r   r    r     s   r   Zstaticc                 C  s^   |d u sJ t j|\}}t j|\}}| ddr$|dd  }| ||||| d S )Nr   r      )r6   r7   r4   rd   rf   
startswithZcreate_static_lib)r+   r   r   r   r   rx   r{   r   r   r   r   r   r   r<   r8   Z_extr   r   r    r     s   )
NNNNNFNNNN)r   r   r-   rB   )1
__future__r   r   r6   r   r   collections.abcr   Zimportlib.machineryr   importlib.utilr   r[   pathlibr   typingr   Zsetuptools.distr	   Zsetuptools.errorsr
   Zsetuptools.extensionr   r   	distutilsr   Zdistutils.ccompilerr   distutils.sysconfigr   r   Zdistutils.command.build_extr   r(   ZCython.Distutils.build_ext
__import__ImportErrorr   r   r!   r   rh   rg   r   r2   dlhasattrr%   r   r   r   r   r    <module>   s    


  =!