3
ßL]ne  ã               @   sÜ   d Z ddlmZmZm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mZ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mZmZmZmZm Z m!Z!m"Z" ddl#m$Z$ G dd„ deƒZdS )zC Modified version of build_ext that handles fortran source files.

é    )ÚdivisionÚabsolute_importÚprint_functionN)Úglob)Únewer_group)Ú	build_ext)ÚDistutilsFileErrorÚDistutilsSetupErrorÚDistutilsError)Ú	copy_file)Úlog)Úfilepath_from_subprocess_output)Úcombine_pathsÚsystem_info)Úfilter_sourcesÚhas_f_sourcesÚhas_cxx_sourcesÚget_ext_source_filesÚget_numpy_include_dirsÚis_sequenceÚget_build_architectureÚmsvc_version)Úshow_fortran_compilersc               @   s„   e Zd ZdZejddg Zejddd	efg Zd
d„ Zdd„ Z	dd„ Z
d dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!r   z:build C/C++/F extensions (compile/link to build directory)ú
fcompiler=Nú!specify the Fortran compiler typeú	parallel=Újúnumber of parallel jobszhelp-fcompilerz list available Fortran compilersc             C   s   t j| ƒ d | _d | _d S )N)Úold_build_extÚinitialize_optionsÚ	fcompilerÚparallel)Úself© r#   úD/tmp/pip-install-q_7g0qgj/Numpy/numpy/distutils/command/build_ext.pyr   +   s    
zbuild_ext.initialize_optionsc             C   sž   | j r4yt| j ƒ| _ W n tk
r2   tdƒ‚Y nX t| jtƒrP| jjtjƒ| _| jpXg }| j	jd krng | j	_| j	j| _| jj
|ƒ tj| ƒ | jddƒ d S )Nz)--parallel/-j argument must be an integerÚbuildr!   )r!   r!   )r!   ÚintÚ
ValueErrorÚ
isinstanceÚinclude_dirsÚstrÚsplitÚosÚpathsepÚdistributionÚextendr   Úfinalize_optionsÚset_undefined_options)r"   Z	incl_dirsr#   r#   r$   r0   0   s    



zbuild_ext.finalize_optionsc                sü  ˆ j s
d S ˆ jdƒ ˆ jjƒ ržˆ jrzˆ jjjdƒrJtjdƒ ˆ jj	dƒ}qŽˆ jj	dƒ}d|_|j
ƒ  |jƒ  dˆ jjd< nˆ jdƒ ˆ jdƒ}ˆ jj|jƒ nd }ddlm} ddlm} ˆ j}||ˆ jˆ jˆ jd�ˆ _ˆ jjˆ jƒ ˆ jjˆ ƒ ˆ jjƒ  tjjˆ jd	ƒˆ _tjj ˆ jƒ�s*tj!ˆ jƒ i }|d k	�r¤xj|j"�pDg D ]Z\}}||k�r–|| |k�r–tjd
|t#|| ƒd d… t#|ƒd d… f ƒ |||< �qFW x2ˆ jj"�p²g D ] \}}||k�rÊ�q´|||< �q´W t$ƒ }�xâˆ j D �]Ö}	t$ƒ }
g }g }g }x˜|	j"D ]Ž}||k�rj|| }||jdg ƒ7 }||jdg ƒ7 }x*|jdg ƒD ]}||k�rL|j|ƒ �qLW x(|j|i ƒjdg ƒD ]}|
j%|ƒ �q€W �qW |�rÔ|	j"| }tj&d|	j'|	j"|f ƒ ||	_"|	j| |	_|�rútj&d|	j'|f ƒ |	j(| |	_(t)|	j*ƒ�r|
j%dƒ t+|	j*ƒ�r&|
j%dƒ |	j,�p:ˆ jj-|	j*ƒ}|�rL|
j%|ƒ d|
k�r\d}n$d|
k�rld}nd|
k�r|d}nd}|�r®||k�r®|	j,�r®tjd|	j'||f ƒ ||	_,|j.|
ƒ �qèW d|k}d|k}d|k}|�r.||ˆ jˆ jˆ jd�ˆ _/ˆ j/}|jˆ j|d� |jˆ ƒ |jƒ  |j0ƒ ˆ _/nd ˆ _/|�r¸ˆ j1}|ˆ j1ˆ jˆ jˆ jdˆ jd�ˆ _2ˆ j2}|�r~|j3}|jˆ jƒ |�r¢|j4ƒ �r¢|jˆ ƒ |jƒ  nˆ jd| ƒ d ˆ _2nd ˆ _2|�rBˆ j1}|ˆ j1ˆ jˆ jˆ jdˆ jd�ˆ _5ˆ j5}|�r|j3}|jˆ jƒ |�r,|j4ƒ �r,|jˆ ƒ |jƒ  nˆ jd| ƒ d ˆ _5nd ˆ _5ˆ j6ƒ  ‡ fdd„ˆ j D ƒ}x’|D ]Š}tjj|d	ƒ}ˆ j�s”tjjˆ j7|ƒ}x\tj8ˆ jƒD ]L}tjj |ƒ�s¾tj!|ƒ |j9ƒ j:dƒ�sÒ�q¢tjjˆ j|ƒ}t;||ƒ �q¢W �qjW d S ) NÚ	build_srcÚ
build_clibzMbuild_clib already run, it is too late to ensure in-place build of build_clibé   r   )Únew_compiler)Únew_fcompiler)ÚcompilerÚverboseÚdry_runÚforcez.libszKlibrary %r defined more than once, overwriting build_info
%s... 
with
%s...i,  Ú	librariesÚlibrary_dirsÚmacrosZsource_languagesz-updating extension %r libraries from %r to %rz-extending extension %r defined_macros with %rÚf77zc++Úf90Úcz.resetting extension %r language from %r to %r.)Zneed_cxxF)r7   r8   r9   r:   Z
requiref90Z
c_compilerz!f77_compiler=%s is not available.Tz!f90_compiler=%s is not available.c                s"   h | ]}ˆ j |jƒjd ƒd ’qS )Ú.r   )Úget_ext_fullnameÚnamer+   )Ú.0Úext)r"   r#   r$   ú	<setcomp>  s   z build_ext.run.<locals>.<setcomp>z.dll)<Ú
extensionsÚrun_commandr.   Úhas_c_librariesÚinplaceÚhave_runÚgetr   ÚwarnÚget_command_objÚensure_finalizedÚrunÚget_finalized_commandr<   Úappendr3   Údistutils.ccompilerr5   Znumpy.distutils.fcompilerr6   r7   r8   r9   r:   Z	customizeZcustomize_cmdZshow_customizationr,   ÚpathÚjoinÚ
build_tempÚextra_dll_dirÚisdirÚmakedirsr;   ÚreprÚsetÚaddÚinforC   Údefine_macrosr   Úsourcesr   ÚlanguageZdetect_languageÚupdateÚ_cxx_compilerÚcxx_compilerr    Ú_f77_compilerÚcompiler_typeÚget_versionÚ_f90_compilerZbuild_extensionsÚ	build_libÚlistdirÚlowerÚendswithr   )r"   r3   r5   r6   re   ZclibsÚlibnameZ
build_infoZall_languagesrE   Zext_languagesZc_libsZ
c_lib_dirsr=   ZbinfoÚmÚlZ
new_c_libsZext_languageZneed_f90_compilerZneed_f77_compilerZneed_cxx_compilerr7   Úctyper    Z	pkg_rootsZpkg_rootZshared_lib_dirÚfnZruntime_libr#   )r"   r$   rP   J   s4   






*


















zbuild_ext.runc             C   s   |S )Nr#   )r"   r_   rG   r#   r#   r$   Úswig_sources  s    zbuild_ext.swig_sourcesc       $      C   s|  |j }|d kst|ƒ r&td#|j ƒ‚t|ƒ}|s6d S | j|jƒ}| jr–|jdƒ}dj|dd$… ƒ}|d% }| j	dƒ}|j
|ƒ}tjj|| j|ƒƒ}	ntjj| j| j|ƒƒ}	||j }
| jpÆt|
|	dƒsÚtjd	|jƒ d S tjd
|jƒ |j� pòg }|jd d … }x|jD ]}|j|fƒ �q
W t|j ƒ\}}}}| jjdk�r\|�rP|jdƒ ||7 }g }|jdk�rp| j}n"|jdk�r„| j}n| j�p�| j}|d k	�rØt|dƒ�r´|j �p¶g ng |_ t|dƒ�rÒ|j!�pÔg ng |_!| j"}|�rü|d k�rüt#d|j ƒ‚|�s|�r |d k�r t#d|j ƒ‚|jd&k�rF|d k�rF| j$d|j ƒ |jdk�rl|d k�rl| j$d|j ƒ d|ji}| j%}|j&t'ƒ  }g }|�r¾tjdƒ | jj(|f|||| j|dœ|—Ž}|�ròtjdƒ ||j(|f|||| j|dœ|—Ž7 }g }g }|�r2tjdƒ |j)d d … }tjj| j%tjj*| j|ƒƒƒ}| j+|ƒ |j,d k�rTt-dƒ}||j.||ƒ7 }||j(|| j%||| j||jd�7 }|j,d k�r2xžt-dƒD ]’}||k�r®�qœtjj||ƒ}tjj/|ƒtjj/|ƒk�rÚ�qœtjj0|ƒ�ròtj1|ƒ y| j2||ƒ W n( t3k
�r*   tj$d||f ƒ Y nX �qœW |�rdtjdƒ ||j(|| j%||| j||jd�7 }|�r„|j4| jƒ �r„|}|} ng }|| } |j5�r¤| j6|j5ƒ |j7�p®g }| j8|ƒd d … }!|j9d d … }"| jj:}#| jjd'k�rö| j;||!|"ƒ n|jd(k�r|d k	�r|j:}#|jdk�r.|d k	�r.|j:}#|d k	�rN| j<| |!||"|ƒ\} }!|#| |	|!|"|j=|| j>|ƒ| j| j%|jd!�
 d S ))Nz*in 'ext_modules' option (extension '%s'), z&'sources' must be present and must be za list of source filenamesrA   r   r4   Úbuild_pyÚnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extensionÚmsvcz/Zm1000r?   r>   Úextra_f77_compile_argsÚextra_f90_compile_argsz5extension %r has C++ sourcesbut no C++ compiler foundz>extension %r has Fortran sources but no Fortran compiler foundzTextension %r has Fortran libraries but no Fortran linker found, using default linkerzc++zLextension %r has C++ libraries but no C++ linker found, using default linkerÚdependszcompiling C sources)Ú
output_dirr=   r)   ÚdebugÚextra_postargszcompiling C++ sourcesz#compiling Fortran 90 module sourcesz*.mod)rx   r=   r)   ry   rz   rw   zfailed to move %r to %rzcompiling Fortran sourcesÚintelwÚintelemw)r;   r<   Úruntime_library_dirsrz   Úexport_symbolsry   rV   Ztarget_langzPin 'ext_modules' option (extension '%s'), 'sources' must be present and must be zjin 'ext_modules' option (extension '%s'), 'sources' must be present and must be a list of source filenameséÿÿÿÿr   )r>   r?   )rt   r{   r|   )r>   r?   )?r_   r   r	   rC   ÚlistrB   rJ   r+   rU   rQ   Zget_package_dirr,   rT   Úget_ext_filenamerh   rw   r:   r   r   ry   r]   Úextra_compile_argsr^   Úundef_macrosrR   r   r7   re   r`   rg   rd   Úhasattrru   rv   rb   r
   rM   rV   r)   r   ÚcompileÚmodule_dirsÚdirnameÚmkpathZmodule_dir_switchr   Zmodule_optionsÚabspathÚisfileÚremoveÚ	move_filer   Zcan_ccompiler_linkÚextra_objectsr/   Úextra_link_argsZget_librariesr<   Zlink_shared_objectÚ_libs_with_msvc_and_fortranÚ_process_unlinkable_fobjectsr}   Zget_export_symbols)$r"   rE   r_   ÚfullnameZmodpathÚpackageÚbaserr   Úpackage_dirZext_filenamerw   Ú
extra_argsr=   ZundefÚ	c_sourcesZcxx_sourcesZ	f_sourcesZfmodule_sourcesr    rc   Úkwsrx   r)   Z	c_objectsrz   Z	f_objectsr†   Zmodule_build_dirZexisting_modulesÚfÚtÚunlinkable_fobjectsÚobjectsr;   r<   Zlinkerr#   r#   r$   Úbuild_extension   s4    

























zbuild_ext.build_extensionc             C   sP   | j dƒj}| j dƒj}| jjtjj|dƒg| jd�}| jj	|d|| j
d� d S )Nr2   r3   zgfortran_vs2003_hack.c)rx   Z_gfortran_workaround)rx   ry   )rQ   r2   r3   r7   r…   r,   rT   rU   rV   Zcreate_static_libry   )r"   r–   r2   r3   r›   r#   r#   r$   Ú_add_dummy_mingwex_symß  s    
z build_ext._add_dummy_mingwex_symc             C   sú   t |ƒ}t |ƒ}t |ƒ}x¨t |ƒD ]œ}x–|D ]Ž}tjj||d ƒ}tjj|ƒr,|j|ƒ t|dƒ�}	|j|	jƒ j	ƒ ƒ W d Q R X tjj||d ƒ}
t|
dƒ�}	|j|	jƒ j	ƒ ƒ W d Q R X q,W q"W |ròdd„ |D ƒ}|j
|| j| jd�}|j|ƒ ||fS )Nz	.fobjectsÚrz	.cobjectsc             S   s   g | ]}t jj|ƒ‘qS r#   )r,   rT   Úrelpath)rD   Úobjr#   r#   r$   ú
<listcomp>   s    z:build_ext._process_unlinkable_fobjects.<locals>.<listcomp>)rx   rW   )r€   r,   rT   rU   rŠ   r‹   Úopenr/   ÚreadÚ
splitlinesZwrap_unlinkable_objectsrV   rW   )r"   r›   r;   r    r<   rš   ÚlibÚlibdirZfake_libr˜   Zc_libZfobjectsÚwrappedr#   r#   r$   r�   è  s(    

$

z&build_ext._process_unlinkable_fobjectsc             C   sâ  |d krd S xÚ|D ]Ò}|j dƒr"qd}x4|p.g D ](}tjj|d| ƒ}tjj|ƒr0d}P q0W |rbqd}xd|D ]\}tjj|d| ƒ}tjj|ƒrltjj| j|d ƒ}t||ƒ | j|krÂ|j| jƒ d}P qlW |rÒqtj	d||f ƒ qW g }	x^|j
D ]T}
|
j dƒ�r>ytjd	d
|
gƒ}
W n ttjfk
�r4   Y n
X t|
ƒ}
|	j|
ƒ qôW |j|	ƒ x†|jD ]|}|j dƒ�s^|j|ƒ t|	d| d ƒ}|�r^tjj| j|d ƒ}tjj|ƒ�sÀt|d |ƒ | j|k�r^|j| jƒ �q^W d S )Nrt   Fz%s.libTzlib%s.az.libz+could not find library %r in directories %sz/usr/libZcygpathz-wr¥   z.ar   )Ú
startswithr,   rT   rU   rŠ   rV   r   rR   r   rM   r<   Ú
subprocessÚcheck_outputÚOSErrorÚCalledProcessErrorr   r/   r;   r   )r"   r    Zc_librariesZc_library_dirsrl   Z
fileexistsr¦   ZlibfileZlibfile2Z
f_lib_dirsÚdirr¥   ÚpÚdst_namer#   r#   r$   r�     s^    






z%build_ext._libs_with_msvc_and_fortranc             C   s2   | j | jƒ g }x| jD ]}|jt|ƒƒ qW |S )N)Úcheck_extensions_listrG   r/   r   )r"   Ú	filenamesrE   r#   r#   r$   Úget_source_filesE  s
    zbuild_ext.get_source_filesc             C   sT   | j | jƒ g }x>| jD ]4}|js$q| j|jƒ}|jtjj| j	| j
|ƒƒƒ qW |S )N)r°   rG   r_   rB   rC   rR   r,   rT   rU   rh   r�   )r"   ÚoutputsrE   r‘   r#   r#   r$   Úget_outputsL  s    zbuild_ext.get_outputs)r   Nr   )r   r   r   )N)Ú__name__Ú
__module__Ú__qualname__Údescriptionr   Úuser_optionsÚhelp_optionsr   r   r0   rP   rq   rœ   r�   r�   r�   r²   r´   r#   r#   r#   r$   r      s*     
 S
 @	 =r   )%Ú__doc__Ú
__future__r   r   r   r,   r©   r   Údistutils.dep_utilr   Zdistutils.command.build_extr   r   Údistutils.errorsr   r	   r
   Údistutils.file_utilr   Znumpy.distutilsr   Znumpy.distutils.exec_commandr   Znumpy.distutils.system_infor   r   Znumpy.distutils.misc_utilr   r   r   r   r   r   r   r   Z'numpy.distutils.command.config_compilerr   r#   r#   r#   r$   Ú<module>   s   (