B
    Õ¹`ã2  ã            
   @   st  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mZ d dlmZ d dlmZ d d	lmZ y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r$yd dl!Z!e"e!dƒ ZZW n ek
r"   Y nX dd„ Z#dd„ Z$G dd„ deƒZesVe j dkrbddd„Z%ndZd dd„Z%dS )!é    N)ÚEXTENSION_SUFFIXES)Ú	build_ext)Ú	copy_file)Únew_compiler)Úcustomize_compilerÚget_config_var)ÚDistutilsError)Úlog)Ú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 d t ¡  t |¡ X n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   úD/tmp/pip-install-4tin_oll/setuptools/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_dl>   s    r    c              C   s*   x$t D ]} d| kr| S | dkr| S qW dS )z;Return the file extension for an abi3-compliant Extension()z.abi3z.pydN)r   )Úsuffixr   r   r   Úget_abi3_suffixB   s
    
r"   c               @   sv   e Z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„ Z
dd„ Zdd„ Zdd„ Zdd„ Zddd„ZdS )r   c             C   s.   | j d }| _ t | ¡ || _ |r*|  ¡  dS )z;Build extensions in build directory, then copy if --inplacer   N)ZinplaceÚ
_build_extÚrunÚcopy_extensions_to_source)ÚselfZold_inplacer   r   r   r$   L   s
    
zbuild_ext.runc       
      C   s®   |   d¡}xž| jD ]”}|  |j¡}|  |¡}| d¡}d |d d… ¡}| |¡}tj	 |tj	 
|¡¡}tj	 | j|¡}	t|	|| j| jd |jr|  |pžtj|d¡ qW d S )NÚbuild_pyÚ.éÿÿÿÿ)ÚverboseÚdry_runT)Úget_finalized_commandÚ
extensionsÚget_ext_fullnameÚnameÚget_ext_filenameÚsplitÚjoinZget_package_dirÚosÚpathÚbasenameÚ	build_libr   r*   r+   Ú_needs_stubÚ
write_stubÚcurdir)
r&   r'   ÚextÚfullnameÚfilenameZmodpathÚpackageÚpackage_dirZdest_filenameZsrc_filenamer   r   r   r%   T   s    




z#build_ext.copy_extensions_to_sourcec             C   s°   t  | |¡}|| jkr¬| j| }t|dƒo.tƒ }|rXtdƒ}|d t|ƒ … }|tƒ  }t|tƒr€t	j
 |¡\}}| j |t¡S tr¬|jr¬t	j
 |¡\}}t	j
 |d| ¡S |S )NZpy_limited_apiÚ
EXT_SUFFIXzdl-)r#   r0   Úext_mapÚgetattrr"   r   ÚlenÚ
isinstancer
   r3   r4   ÚsplitextÚshlib_compilerÚlibrary_filenameÚlibtypeÚ	use_stubsÚ_links_to_dynamicr1   r2   )r&   r;   r<   r:   Zuse_abi3Zso_extÚfnÚdr   r   r   r0   j   s    




zbuild_ext.get_ext_filenamec             C   s    t  | ¡ d | _g | _i | _d S )N)r#   Úinitialize_optionsrE   Úshlibsr@   )r&   r   r   r   rL   {   s    
zbuild_ext.initialize_optionsc             C   s4  t  | ¡ | jpg | _|  | j¡ dd„ | jD ƒ| _| jrB|  ¡  x| jD ]}|  |j¡|_qJW xÎ| 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 |¡ |rhtrhtj|jkrh|j tj¡ qhW d S )Nc             S   s   g | ]}t |tƒr|‘qS r   )rC   r
   )Ú.0r:   r   r   r   ú
<listcomp>…   s    z.build_ext.finalize_options.<locals>.<listcomp>r(   r)   F)r#   Úfinalize_optionsr-   Zcheck_extensions_listrM   Úsetup_shlib_compilerr.   r/   Ú
_full_namer@   r1   Úlinks_to_dynamicrH   rC   r
   rI   r7   r0   Ú
_file_namer3   r4   Údirnamer2   r6   Úlibrary_dirsÚappendr9   Úruntime_library_dirs)r&   r:   r;   ZltdÚnsr<   Zlibdirr   r   r   rP      s,    

zbuild_ext.finalize_optionsc             C   sî   t | j| j| jd }| _t|ƒ | jd k	r8| | j¡ | jd k	rbx| jD ]\}}| 	||¡ qJW | j
d k	r†x| j
D ]}| |¡ qtW | 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   r+   Úforce)r   r   r+   rZ   rE   r   Úinclude_dirsZset_include_dirsZdefineZdefine_macroZundefZundefine_macroÚ	librariesZset_librariesrV   Zset_library_dirsZrpathZset_runtime_library_dirsZlink_objectsZset_link_objectsÚlink_shared_objectÚ__get__)r&   r   r/   ÚvalueÚmacror   r   r   rQ   ž   s(    






zbuild_ext.setup_shlib_compilerc             C   s   t |tƒr|jS t | |¡S )N)rC   r
   Úexport_symbolsr#   Úget_export_symbols)r&   r:   r   r   r   rb   ¹   s    
zbuild_ext.get_export_symbolsc             C   s\   |  ¡  | j}z@t|tƒr"| j| _t | |¡ |jrL|  d¡j	}|  
||¡ W d || _X d S )Nr'   )Z_convert_pyx_sources_to_langr   rC   r
   rE   r#   Úbuild_extensionr7   r,   r6   r8   )r&   r:   Z	_compilerÚcmdr   r   r   rc   ¾   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   )rR   )rN   Úlibr   r   r   rO   Ð   s    z.build_ext.links_to_dynamic.<locals>.<listcomp>r(   Nr)   r   c             3   s   | ]}ˆ| ˆ kV  qd S )Nr   )rN   Zlibname)ÚlibnamesÚpkgr   r   ú	<genexpr>Ò   s    z-build_ext.links_to_dynamic.<locals>.<genexpr>)ÚdictÚfromkeysrM   r2   rR   r1   Úanyr\   )r&   r:   r   )rf   rg   r   rS   Ë   s     zbuild_ext.links_to_dynamicc             C   s   t  | ¡|  ¡  S )N)r#   Úget_outputsÚ_build_ext__get_stubs_outputs)r&   r   r   r   rl   Ô   s    zbuild_ext.get_outputsc                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)r7   r3   r4   r2   r6   rR   r1   )rN   r:   )r&   r   r   rh   Ú   s   z0build_ext.__get_stubs_outputs.<locals>.<genexpr>c             s   s   | ]\}}|| V  qd S )Nr   )rN   ÚbaseZfnextr   r   r   rh   à   s    )r-   Ú	itertoolsÚproductÚ!_build_ext__get_output_extensionsÚlist)r&   Zns_ext_basesÚpairsr   )r&   r   Z__get_stubs_outputs×   s    

zbuild_ext.__get_stubs_outputsc             c   s"   dV  dV  |   d¡jrdV  d S )Nz.pyz.pycr'   z.pyo)r,   Úoptimize)r&   r   r   r   Z__get_output_extensionsâ   s    z!build_ext.__get_output_extensionsFc             C   s2  t  d|j|¡ tjj|f|j d¡žŽ d }|rJtj |¡rJt|d ƒ‚| j	s¾t
|dƒ}| 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¡¡ | ¡  |r.ddlm} ||gdd| j	d  |  d!¡j}|dkr||g|d| j	d  tj |¡r.| j	s.t |¡ d S )"Nz writing stub loader for %s to %sr(   z.pyz already exists! Please delete.ÚwÚ
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   )Úbyte_compileT)rt   rZ   r+   Úinstall_lib)r	   ÚinforR   r3   r4   r2   r1   Úexistsr   r+   ÚopenÚwriter    r5   rT   ÚcloseÚdistutils.utilrw   r,   rt   Úunlink)r&   Ú
output_dirr:   ÚcompileZ	stub_fileÚfrw   rt   r   r   r   r8   è   sX    



zbuild_ext.write_stubN)F)Ú__name__Ú
__module__Ú__qualname__r$   r%   r0   rL   rP   rQ   rb   rc   rS   rl   rm   rq   r8   r   r   r   r   r   K   s   	r   c             C   s(   |   | j|||||||||	|
||¡ d S )N)ÚlinkZSHARED_LIBRARY)r&   ÚobjectsÚoutput_libnamer€   r\   rV   rX   ra   ÚdebugÚextra_preargsÚextra_postargsÚ
build_tempÚtarget_langr   r   r   r]     s
    r]   Zstaticc             C   s^   |d kst ‚tj |¡\}}tj |¡\}}|  d¡ d¡rH|dd … }|  |||||¡ d S )NÚxre   é   )ÚAssertionErrorr3   r4   r1   rD   rF   Ú
startswithZcreate_static_lib)r&   r‡   rˆ   r€   r\   rV   rX   ra   r‰   rŠ   r‹   rŒ   r   r<   r5   r:   r   r   r   r]   -  s    )
NNNNNr   NNNN)
NNNNNr   NNNN)&r3   r   ro   Úimportlib.machineryr   Zdistutils.command.build_extr   Z_du_build_extÚdistutils.file_utilr   Údistutils.ccompilerr   Údistutils.sysconfigr   r   Údistutils.errorsr   Ú	distutilsr	   Zsetuptools.extensionr
   ZCython.Distutils.build_extr#   Ú
__import__ÚImportErrorr   r   r   r   rH   rG   r   r/   ÚdlÚhasattrr    r"   r]   r   r   r   r   Ú<module>   sV   

	 Q   	   