o
    'Ti^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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rid 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 Z0G dd de"Z!e*sej-dkr										d&d'd"d#Z1dS d$Z+										d&d'd%d#Z1d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   P/home/ubuntu/.local/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 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_suffixO   s   r&   c                   @  s   e Zd ZU ded< dZdZd<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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	   distributionFreturnNonec                 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   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)
r.   build_pyr0   fullnamefilenamemodpathpackagepackage_dirinplace_fileregular_filer   r   r    _get_inplace_equivalentf   s   


z!build_ext._get_inplace_equivalentc                 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
extensionsrF   r:   r;   existsoptional	copy_fileverbose_needs_stub_get_equivalent_stub_write_stub_file)r.   r>   r0   rD   rE   inplace_stubr   r   r    r-   p   s   

z#build_ext.copy_extensions_to_sourceoutput_filestrc                 C  s2   t j|}|jd\}}}t j|| dS Nr2   .py)r:   r;   dirnamer5   
rpartitionr8   )r.   r0   rS   dir__r5   r   r   r    rP      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*   rI   optimizerJ   rF   rO   rP   _compiled_file_name)
r.   r>   optr0   rD   rE   rR   regular_stubinplace_cache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 )NSETUPTOOLS_EXT_SUFFIXr2   
EXT_SUFFIXzwConfiguration variable EXT_SUFFIX not found for this platform and environment variable SETUPTOOLS_EXT_SUFFIX is missingzdl-)r:   getenvr;   r8   r7   r+   r6   r   
isinstancerT   OSErrorext_mapr&   py_limited_apilenr   splitextshlib_compilerlibrary_filenamelibtype	use_stubs_links_to_dynamic)	r.   r?   so_extr@   
ext_suffixr0   abi3_suffixfndr   r   r    r6      s,   






zbuild_ext.get_ext_filenamec                 C  s&   t |  d | _g | _i | _d| _d S )NF)r+   initialize_optionsro   shlibsrk   editable_moder.   r   r   r    ry      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   )ri   r   .0r0   r   r   r    
<listcomp>   s    z.build_ext.finalize_options.<locals>.<listcomp>r2   r3   FT)r+   finalize_optionsrJ   check_extensions_listrz   setup_shlib_compilerr4   r5   
_full_namerk   r7   links_to_dynamicrr   ri   r   rs   rO   r6   
_file_namer:   r;   rW   r8   r=   library_dirsappendcurdirruntime_library_dirsr{   r*   )r.   r0   r?   ltdnsr@   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   ro   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__)r.   r   r5   valuemacror   r   r    r      s*   







zbuild_ext.setup_shlib_compilerc                 C  s   t |tr|jS t| |S N)ri   r   export_symbolsr+   get_export_symbolsr.   r0   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>   )_convert_pyx_sources_to_langr   ri   r   ro   r+   build_extensionrO   rI   r=   
write_stub)r.   r0   	_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>r2   Nr3   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fromkeysrz   r8   r   r7   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~   r0   depr   r   r    r     s    z2build_ext._get_internal_depends.<locals>.<genexpr>	orig_pathrT   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   rT   r   rT   r(   r)   )r   r'   src_rootr:   r   resolverJ   is_absolutepartsrj   relative_to
ValueErroras_posix)r.   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   ?  s   zbuild_ext.get_outputsdict[str, str]c                 C  s   |   }tt|tddS )z1See :class:`setuptools.commands.build.SubCommand`r   )key)re   r   r   operator
itemgetter)r.   mappingr   r   r    r   D  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 )r2   N)rO   r:   r;   r8   r=   r   r7   r}   r|   r   r    r   K  s    
z0build_ext.__get_stubs_outputs.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S r   r   )r~   basefnextr   r   r    r   R  r   )rJ   	itertoolsproduct!_build_ext__get_output_extensionsr   )r.   ns_ext_basespairsr   r|   r    __get_stubs_outputsI  s
   
zbuild_ext.__get_stubs_outputsc                 c  s(    dV  dV  |  djrdV  d S d S )NrV   z.pycr>   z.pyo)rI   r_   r|   r   r   r    __get_output_extensionsT  s   
z!build_ext.__get_output_extensionsc                 C  s2   t jj|g|jdR  d }| ||| d S rU   )r:   r;   r8   r   r7   rQ   )r.   
output_dirr0   rH   	stub_filer   r   r    r   Z  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   r   r:   r;   rK   r
   r   opentextwrapdedentr<   r   lstripreplace	have_rtldwrite_compile_and_remove_stub)r.   r   r0   rH   fcontentr   r   r    rQ   ^  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)r_   r   r   r\   )	distutils.utilr   r   rI   r_   r:   r;   rK   unlink)r.   r   r   r_   r   r   r    r     s   z"build_ext._compile_and_remove_stubN)r(   r)   )r0   r   r(   r1   )r0   r   rS   rT   r(   rT   )r(   r[   )r?   rT   r(   rT   )r(   r   )r(   r   )r(   r   )F)r   rT   r0   r   )r   rT   )__name__
__module____qualname____annotations__r{   r*   r,   rF   r-   rP   re   r6   ry   r   r   r   r   r   r   r   r   r   r   r   r   rQ   r   r   r   r   r    r   Y   s2   
 










	

&
'r   debugboolr(   r)   c                 C  s(   |  | j|||||||||	|
|| d S r   )linkSHARED_LIBRARY)r.   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 )Nxr      )r:   r;   r7   rn   rp   
startswithcreate_static_lib)r.   r   r   r   r   r   r   r   r   r   r   r   r   r@   r<   _extr   r   r    r     s   )
NNNNNFNNNN)r   r   r(   r)   )2
__future__r   r   r   r:   r   r   collections.abcr   importlib.machineryr   importlib.utilr   r`   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   rr   rq   r   r5   dlhasattrr&   r   r   r   r   r    <module>   s    


  >!