o
    5h!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 -dynamiclibZCCSHAREDz.dylibZSO)sysplatform_CONFIG_VARScopyr   clearupdate)compilertmp r   <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 )Nr8   )levelT)compile)get_finalized_command
extensionsr?   r4   r5   existsZoptionalZ	copy_fileverbose_needs_stub_get_equivalent_stub_write_stub_file)r)   r8   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)r4   r5   dirnamer0   
rpartitionr3   )r)   r*   rK   Zdir__r0   r   r   r   rH      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 )Nr8   install_lib )optimization)r%   rC   optimizerD   r?   rG   rH   _compiled_file_name)
r)   r8   optr*   r=   r>   rJ   Zregular_stubZinplace_cacheZoutput_cacher   r   r   _get_output_mapping   s    



zbuild_ext._get_output_mappingr9   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-   Z
EXT_SUFFIXzwConfiguration variable EXT_SUFFIX not found for this platform and environment variable SETUPTOOLS_EXT_SUFFIX is missingzdl-)r4   getenvr5   r3   r2   r&   r1   r   
isinstancerL   OSErrorext_mapr#   Zpy_limited_apilenr   splitextshlib_compilerlibrary_filenamelibtype	use_stubs_links_to_dynamic)	r)   r9   Zso_extr:   Z
ext_suffixr*   Zabi3_suffixfndr   r   r   r1      s,   






zbuild_ext.get_ext_filenamec                 C  s&   t |  d | _g | _i | _d| _d S )NF)r&   initialize_optionsr`   shlibsr]   editable_moder)   r   r   r   rg      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-   r.   FT)r&   finalize_optionsrD   Zcheck_extensions_listrh   setup_shlib_compilerr/   r0   
_full_namer]   r2   links_to_dynamicrc   r[   r   rd   rG   r1   
_file_namer4   r5   rO   r3   r7   library_dirsappendcurdirruntime_library_dirsri   r%   )r)   r*   r9   Zltdnsr:   libdirr   r   r   rn      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   ry   rz   r`   r   Zinclude_dirsZset_include_dirsZdefineZdefine_macroZundefZundefine_macro	librariesZset_librariesrs   Zset_library_dirsZrpathZset_runtime_library_dirsZlink_objectsZset_link_objectslink_shared_object__get__)r)   r   r0   valueZmacror   r   r   ro      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 )Nr8   )Z_convert_pyx_sources_to_langr   r[   r   r`   r&   build_extensionrG   rC   r7   
write_stub)r)   r*   Z	_compilerr7   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   )rp   )rl   libr   r   r   rm     s    z.build_ext.links_to_dynamic.<locals>.<listcomp>r-   Nr.   rT   c                 3  s    | ]	}|  v V  qd S r   r   )rl   ZlibnameZlibnamesZpkgr   r   	<genexpr>      z-build_ext.links_to_dynamic.<locals>.<genexpr>)dictfromkeysrh   r3   rp   r2   anyr{   r   r   r   r   rq     s    zbuild_ext.links_to_dynamic	list[str]c                 C  s   g t | |  S r   )r&   get_source_files_get_internal_dependsrj   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)rl   r*   depr   r   r   r     s    z2build_ext._get_internal_depends.<locals>.<genexpr>	orig_pathrL   reasonr+   r@   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   rL   r   rL   r+   r@   )r   r$   Zsrc_rootr4   ru   resolverD   is_absolutepartsr\   relative_to
ValueErroras_posix)r)   Zproject_rootr   r   r   r5   Z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_outputsrj   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)rY   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)rG   r4   r5   r3   r7   rp   r2   rk   rj   r   r   r   J  s    
z0build_ext.__get_stubs_outputs.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S r   r   )rl   baseZfnextr   r   r   r   Q  r   )rD   	itertoolsproduct!_build_ext__get_output_extensionsr   )r)   Zns_ext_basespairsr   rj   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 )NrN   z.pycr8   z.pyo)rC   rV   rj   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 rM   )r4   r5   r3   rp   r2   rI   )r)   
output_dirr*   rB   	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   rp   r4   r5   rE   r
   ry   opentextwrapdedentr6   rr   lstripreplace	have_rtldwrite_compile_and_remove_stub)r)   r   r*   rB   fZcontentr   r   r   rI   ]  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)rV   rz   ry   rS   )	Zdistutils.utilr   ry   rC   rV   r4   r5   rE   unlink)r)   r   r   rV   r   r   r   r     s   z"build_ext._compile_and_remove_stubN)r*   r   r+   r,   )r+   r@   )r*   r   rK   rL   r+   rL   )r+   rR   )r9   rL   r+   rL   )r+   r   )r+   r   )r+   r   )F)r   rL   r*   r   )r   rL   )__name__
__module____qualname____annotations__ri   r%   r'   r?   r(   rH   rY   r1   rg   rn   ro   r   r   rq   r   r   r   r   r   r   r   rI   r   r   r   r   r   r   X   s2   
 








	

&
&r   debugboolr+   r@   c                 C  s(   |  | j|||||||||	|
|| d S r   )linkZSHARED_LIBRARY)r)   objectsoutput_libnamer   r{   rs   rv   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      )r4   r5   r2   r_   ra   
startswithZcreate_static_lib)r)   r   r   r   r{   rs   rv   r   r   r   r   r   r   r:   r6   Z_extr   r   r   r|     s   )
NNNNNFNNNN)r   r   r+   r@   )1Z
__future__r   r   r4   r   r   Zcollections.abcr   importlib.machineryr   importlib.utilr   rW   pathlibr   typingr   Zsetuptools.distr	   Zsetuptools.errorsr
   Zsetuptools.extensionr   r   	distutilsr   Zdistutils.ccompilerr   Zdistutils.sysconfigr   r   Zdistutils.command.build_extr   r&   ZCython.Distutils.build_ext
__import__ImportErrorr   r   r   r   rc   rb   r   r0   Zdlhasattrr#   r|   r   r   r   r   <module>   s    


  =!