U
    ±§f`E  ã                
   @  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mZ d d
lmZ d dlmZ d dlmZmZ zd dlmZ edƒ W n e k
rÔ   eZY nX edƒ d dlm!Z" dd„ Z#dZ$dZ%dZ&ej'dkrdZ%n>ej(dkrNzd dl)Z)e*e)dƒ Z%Z$W n e k
rL   Y nX dd„ Z+dd„ Z,G dd„ deƒZe%s€ej(dkrŒd"dd„Z-nd Z&d#d!d„Z-dS )$é    )ÚannotationsN©ÚEXTENSION_SUFFIXES)Úcache_from_source)ÚIterator)ÚPath)Ú	build_ext)Únew_compiler)Úcustomize_compilerÚget_config_var)Úlog)Ú	BaseError)Ú	ExtensionÚLibraryzCython.Compiler.MainÚLDSHARED)Ú_config_varsc              	   C  sZ   t jdkrNt ¡ }z$dtd< dtd< dtd< t| ƒ W 5 t ¡  t |¡ X nt| ƒ d S )NÚdarwinz0gcc -Wl,-x -dynamiclib -undefined dynamic_lookupr   z -dynamiclibZCCSHAREDz.dylibZSO)ÚsysÚplatformÚ_CONFIG_VARSÚcopyÚclearÚupdater
   )ÚcompilerZtmp© r   ú;lib/python3.8/site-packages/setuptools/command/build_ext.pyÚ_customize_compiler_for_shlib$   s    
ÿr   FZsharedr   TÚntÚRTLD_NOWc                 C  s   t r| S dS )NÚ )Ú	have_rtld)Úsr   r   r   Úif_dlH   s    r"   c                  C  s.   t D ]$} d| k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_suffixL   s    
r$   c                   @  s  e Zd ZU dZded< dZded< dd„ Zddd	œd
d„Zdd„ Zddddœdd„Z	ddœdd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$dœd%d&„Zd'dœd(d)„Zd$dœd*d+„Zd,dœd-d.„Zd/d0„ Zd1d2„ Zd<d3d4„Zd=ddd5œd6d7„Zdd8œd9d:„Zd;S )>r   FÚboolÚeditable_modeÚinplacec                 C  s.   | j d }| _ t | ¡ || _ |r*|  ¡  dS )z;Build extensions in build directory, then copy if --inplacer   N)r'   Ú
_build_extÚrunÚcopy_extensions_to_source)ÚselfZold_inplacer   r   r   r)   Z   s
    
zbuild_ext.runr   ztuple[str, str])ÚextÚreturnc           
      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_equivalentb   s    


z!build_ext._get_inplace_equivalentc                 C  sp   |   d¡}| jD ]Z}|  ||¡\}}tj |¡s6|jsH| j||| jd |j	r|  
||¡}| j||dd qd S )Nr9   )ÚlevelT)Úcompile)Úget_finalized_commandÚ
extensionsr@   r5   r6   ÚexistsZoptionalZ	copy_fileÚverboseÚ_needs_stubÚ_get_equivalent_stubÚ_write_stub_file)r+   r9   r,   r>   r?   Úinplace_stubr   r   r   r*   l   s    

z#build_ext.copy_extensions_to_sourceÚstr)r,   Úoutput_filer-   c                 C  s2   t j |¡}|j d¡\}}}t j ||¡› dS ©Nr.   ú.py)r5   r6   Údirnamer1   Ú
rpartitionr4   )r+   r,   rL   Zdir_Ú_r1   r   r   r   rH   }   s    zbuild_ext._get_equivalent_stubzIterator[tuple[str, str]])r-   c           
      c  sŽ   | j s
d S |  d¡}|  d¡jp"d}| jD ]^}|  ||¡\}}||fV  |jr*|  ||¡}|  ||¡}t||d}t||d}	|	|fV  q*d S )Nr9   Úinstall_libr   )Úoptimization)r'   rC   ÚoptimizerD   r@   rG   rH   Ú_compiled_file_name)
r+   r9   Zoptr,   r>   r?   rJ   Zregular_stubZinplace_cacheZoutput_cacher   r   r   Ú_get_output_mapping‚   s    


zbuild_ext._get_output_mappingc                 C  sÖ   t  d¡}|r&t jj| d¡Ž | }nt | |¡}tdƒ}|| jkrÒ| j| }|j	oXt
ƒ }|r~|d t|ƒ … }t
ƒ }|| }t|tƒr¦t j |¡\}}| j |t¡S trÒ|jrÒt j |¡\}}t j |d| ¡S |S )NZSETUPTOOLS_EXT_SUFFIXr.   Z
EXT_SUFFIXzdl-)r5   Úgetenvr6   r4   r3   r(   r2   r   Úext_mapZpy_limited_apir$   ÚlenÚ
isinstancer   ÚsplitextÚshlib_compilerÚlibrary_filenameÚlibtypeÚ	use_stubsÚ_links_to_dynamic)r+   r:   Zso_extr;   r,   Zuse_abi3ÚfnÚdr   r   r   r2   ›   s&    




zbuild_ext.get_ext_filenamec                 C  s&   t  | ¡ d | _g | _i | _d| _d S )NF)r(   Úinitialize_optionsr\   ÚshlibsrX   r&   ©r+   r   r   r   rc   ²   s
    
zbuild_ext.initialize_optionsc                 C  s:  t  | ¡ | jpg | _|  | j¡ dd„ | jD ƒ| _| jrB|  ¡  | jD ]}|  |j¡|_qH| jD ]Ä}|j}|| j	|< || j	| 
d¡d < | jrš|  |¡pœd}|o°to°t|tƒ }||_||_|  |¡ }|_tj tj | j|¡¡}|r||jkr|j |¡ |rbtrbtj|jkrb|j tj¡ qb| jr6d| _d S )Nc                 S  s   g | ]}t |tƒr|‘qS r   )rZ   r   ©Ú.0r,   r   r   r   Ú
<listcomp>½   s     
 z.build_ext.finalize_options.<locals>.<listcomp>r.   r/   FT)r(   Úfinalize_optionsrD   Zcheck_extensions_listrd   Úsetup_shlib_compilerr0   r1   Ú
_full_namerX   r3   Úlinks_to_dynamicr_   rZ   r   r`   rG   r2   Ú
_file_namer5   r6   rO   r4   r8   Úlibrary_dirsÚappendÚcurdirÚruntime_library_dirsr&   r'   )r+   r,   r:   ZltdÚnsr;   Zlibdirr   r   r   ri   ¹   s0    



zbuild_ext.finalize_optionsc                 C  sæ   t | j| j| jd }| _t|ƒ | jd k	r8| | j¡ | jd k	r^| jD ]\}}| 	||¡ qH| j
d k	r~| j
D ]}| |¡ qn| jd k	r”| | j¡ | jd k	rª| | j¡ | jd k	rÀ| | j¡ | jd k	rÖ| | j¡ t |¡|_d S )N)r   Údry_runÚforce)r	   r   rs   rt   r\   r   Zinclude_dirsZset_include_dirsZdefineZdefine_macroZundefZundefine_macroÚ	librariesZset_librariesrn   Zset_library_dirsZrpathZset_runtime_library_dirsZlink_objectsZset_link_objectsÚlink_shared_objectÚ__get__)r+   r   r1   ÚvalueZmacror   r   r   rj   Ø   s.      ÿ







zbuild_ext.setup_shlib_compilerc                 C  s   t |tƒr|jS t | |¡S ©N)rZ   r   Úexport_symbolsr(   Úget_export_symbols©r+   r,   r   r   r   r{   ó   s    
zbuild_ext.get_export_symbolsc                 C  s\   |  ¡  | j}z@t|tƒr"| j| _t | |¡ |jrL|  d¡j	}|  
||¡ W 5 || _X d S )Nr9   )Z_convert_pyx_sources_to_langr   rZ   r   r\   r(   Úbuild_extensionrG   rC   r8   Ú
write_stub)r+   r,   Z	_compilerr8   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   )rk   )rg   Úlibr   r   r   rh   
  s     z.build_ext.links_to_dynamic.<locals>.<listcomp>r.   Nr/   r   c                 3  s   | ]}ˆ| ˆ kV  qd S ry   r   )rg   Zlibname©ZlibnamesÚpkgr   r   Ú	<genexpr>  s     z-build_ext.links_to_dynamic.<locals>.<genexpr>)ÚdictÚfromkeysrd   r4   rk   r3   Úanyru   r|   r   r€   r   rl     s     zbuild_ext.links_to_dynamicz	list[str]c                 C  s   t  | ¡|  ¡ •S ry   )r(   Úget_source_filesÚ_get_internal_dependsre   r   r   r   r†     s    zbuild_ext.get_source_fileszIterator[str]c              	   c  sì   t | jjptjƒ ¡ }dd„ | jD ƒ}ddddœdd„}|D ]ª}t |ƒ}| ¡ r\||dƒ q<d	|jkrr||d
ƒ q<z|| jdd}W n" t	k
r¨   ||dƒ Y q<Y nX z| 
|¡ W n" tk
rÚ   ||dƒ Y q<Y nX | ¡ V  q<dS )zAYield ``ext.depends`` that are contained by the project directoryc                 s  s   | ]}|j D ]
}|V  qqd S ry   )Údepends)rg   r,   Údepr   r   r   r‚     s       z2build_ext._get_internal_depends.<locals>.<genexpr>rK   ÚNone)Ú	orig_pathÚreasonr-   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   ZdistributionZsrc_rootr5   rp   ZresolverD   Zis_absoluteÚpartsÚOSErrorÚrelative_toÚ
ValueErrorZas_posix)r+   Zproject_rootrˆ   rŽ   r‰   r6   Zresolvedr   r   r   r‡     s,    






zbuild_ext._get_internal_dependsc                 C  s,   | j rt|  ¡  ¡ ƒS tt | ¡|  ¡  ƒS ry   )r'   ÚlistÚget_output_mappingÚkeysÚsortedr(   Úget_outputsÚ_build_ext__get_stubs_outputsre   r   r   r   r˜   7  s    zbuild_ext.get_outputsz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>?  ó    z.build_ext.get_output_mapping.<locals>.<lambda>)Úkey)rV   rƒ   r—   )r+   Úmappingr   r   r   r•   <  s    zbuild_ext.get_output_mappingc                   s6   ‡ fdd„ˆ j D ƒ}t |ˆ  ¡ ¡}tdd„ |D ƒƒS )Nc                 3  s0   | ](}|j rtjjˆ jf|j d ¡žŽ V  qdS )r.   N)rG   r5   r6   r4   r8   rk   r3   rf   re   r   r   r‚   C  s   þz0build_ext.__get_stubs_outputs.<locals>.<genexpr>c                 s  s   | ]\}}|| V  qd S ry   r   )rg   ÚbaseZfnextr   r   r   r‚   J  s     )rD   Ú	itertoolsÚproductÚ!_build_ext__get_output_extensionsr”   )r+   Zns_ext_basesZpairsr   re   r   Z__get_stubs_outputsA  s
    
þzbuild_ext.__get_stubs_outputsc                 c  s"   dV  dV  |   d¡jrdV  d S )NrN   z.pycr9   z.pyo)rC   rT   re   r   r   r   Z__get_output_extensionsL  s    z!build_ext.__get_output_extensionsc                 C  s0   t jj|f|j d¡žŽ d }|  |||¡ d S rM   )r5   r6   r4   rk   r3   rI   )r+   Ú
output_dirr,   rB   Ú	stub_filer   r   r   r~   R  s    zbuild_ext.write_stub)r¤   r,   c                 C  s¾   t  d|j|¡ |r,tj |¡r,t|d ƒ‚| js¬t|dddf}d 	ddd	t
d
ƒ dtj |j¡ dddt
dƒdddt
dƒdddddt
dƒdddg¡}| |¡ W 5 Q R X |rº|  |¡ 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, dlz:   __file__ = pkg_resources.resource_filename(__name__,%r)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   rk   r5   r6   rE   r   rs   Úopenr4   r"   r7   rm   ÚwriteÚ_compile_and_remove_stub)r+   r¤   r,   rB   ÚfZcontentr   r   r   rI   V  sB    
ÿêzbuild_ext._write_stub_file)r¤   c                 C  sh   ddl m} ||gdd| jd |  d¡j}|dkrH||g|d| jd tj |¡rd| jsdt |¡ d S )Nr   )Úbyte_compileT)rT   rt   rs   rR   )	Zdistutils.utilr¬   rs   rC   rT   r5   r6   rE   Úunlink)r+   r¤   r¬   rT   r   r   r   rª   x  s    üz"build_ext._compile_and_remove_stubN)F)F)Ú__name__Ú
__module__Ú__qualname__r&   Ú__annotations__r'   r)   r@   r*   rH   rV   r2   rc   ri   rj   r{   r}   rl   r†   r‡   r˜   r•   r™   r¢   r~   rI   rª   r   r   r   r   r   V   s.   

	&
"r   c                 C  s(   |   | j|||||||||	|
||¡ d S ry   )ÚlinkZSHARED_LIBRARY)r+   ÚobjectsÚoutput_libnamer£   ru   rn   rq   rz   ÚdebugÚextra_preargsÚextra_postargsÚ
build_tempÚtarget_langr   r   r   rv   ‹  s    órv   Zstaticc                 C  s^   |d kst ‚tj |¡\}}tj |¡\}}|  d¡ d¡rH|dd … }|  |||||¡ d S )Nrš   r   é   )ÚAssertionErrorr5   r6   r3   r[   r]   Ú
startswithZcreate_static_lib)r+   r³   r´   r£   ru   rn   rq   rz   rµ   r¶   r·   r¸   r¹   r;   r7   r,   r   r   r   rv   ®  s    )
NNNNNFNNNN)
NNNNNFNNNN).Z
__future__r   r5   r   r    Úimportlib.machineryr   Úimportlib.utilr   rU   Útypingr   Úpathlibr   Zdistutils.command.build_extr   Z_du_build_extZdistutils.ccompilerr	   Zdistutils.sysconfigr
   r   Ú	distutilsr   Zsetuptools.errorsr   Zsetuptools.extensionr   r   ZCython.Distutils.build_extr(   Ú
__import__ÚImportErrorr   r   r   r    r_   r^   r   r1   ZdlÚhasattrr"   r$   rv   r   r   r   r   Ú<module>   sz   

  4          ó!          ó