3
eåU^Bg  ã               @   sÜ   d Z ddlmZmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZmZmZmZm Z m!Z!m"Z" ddl#m$Z$ G dd„ deƒZdS )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 Zd ZdZejd d!d"g Zejd
ddefg Zejdg Zdd„ Z	dd„ Z
dd„ Zd#dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )$r   z:build C/C++/F extensions (compile/link to build directory)ú
fcompiler=Nú!specify the Fortran compiler typeú	parallel=Újúnumber of parallel jobsú
warn-errorú'turn all warnings into errors (-Werror)zhelp-fcompilerz list available Fortran compilersc             C   s    t j| ƒ d | _d | _d | _d S )N)Úold_build_extÚinitialize_optionsÚ	fcompilerÚparallelÚ
warn_error)Úself© r&   úB/tmp/pip-build-fou5mlat/numpy/numpy/distutils/command/build_ext.pyr!   /   s    
zbuild_ext.initialize_optionsc             C   s    | j r4yt| j ƒ| _ W n tk
r2   tdƒ‚Y nX t| jtƒrP| jjtjƒ| _| jpXg }| j	jd krng | j	_| j	j| _| jj
|ƒ tj| ƒ | jdddƒ d S )Nz)--parallel/-j argument must be an integerÚbuildr#   r$   )r#   r#   )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'   r3   5   s     



zbuild_ext.finalize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jˆ jdˆ jd�ˆ _7ˆ j7}|�r,|j5}|jˆ jƒ |�rP|j6ƒ �rP|jˆ ƒ |jƒ  nˆ jd| ƒ d ˆ _7nd ˆ _7ˆ j8ƒ  ‡ fdd„ˆ j D ƒ}x’|D ]Š}tjj|d
ƒ}ˆ j�s¸tjjˆ j9|ƒ}x\tj:ˆ j!ƒD ]L}tjj"|ƒ�sâtj#|ƒ |j;ƒ j<d ƒ�sö�qÆtjjˆ j!|ƒ}t=||ƒ �qÆW �qŽW 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-Werrorz.libszKlibrary %r defined more than once, overwriting build_info
%s... 
with
%s...i,  Ú	librariesÚlibrary_dirsÚmacrosZsource_languagesz-updating extension %r libraries from %r to %rz-extending extension %r defined_macros with %rÚf77zc++Úf90Úcz.resetting extension %r language from %r to %r.)Zneed_cxxF)r:   r;   r<   r=   Z
requiref90Ú
c_compilerz!f77_compiler=%s is not available.Tz!f90_compiler=%s is not available.c                s"   h | ]}ˆ j |jƒjd ƒd ’qS )Ú.r   )Úget_ext_fullnameÚnamer.   )Ú.0Úext)r%   r&   r'   ú	<setcomp>  s   z build_ext.run.<locals>.<setcomp>z.dll)>Ú
extensionsÚrun_commandr1   Úhas_c_librariesÚinplaceÚhave_runÚgetr   ÚwarnÚget_command_objÚensure_finalizedÚrunÚget_finalized_commandr?   Úappendr6   Údistutils.ccompilerr8   Znumpy.distutils.fcompilerr9   r:   r;   r<   r=   Z	customizeZcustomize_cmdr$   Úcompiler_soZshow_customizationr/   ÚpathÚjoinÚ
build_tempÚextra_dll_dirÚisdirÚmakedirsr>   ÚreprÚsetÚaddÚinforG   Údefine_macrosr   Úsourcesr   ÚlanguageZdetect_languageÚupdateÚ_cxx_compilerÚcxx_compilerr"   Ú_f77_compilerÚcompiler_typeÚget_versionÚ_f90_compilerZbuild_extensionsÚ	build_libÚlistdirÚlowerÚendswithr   )r%   r6   r8   r9   rj   ZclibsÚlibnameZ
build_infoZall_languagesrI   Zext_languagesZc_libsZ
c_lib_dirsr@   ZbinfoÚmÚlZ
new_c_libsZext_languageZneed_f90_compilerZneed_f77_compilerZneed_cxx_compilerr:   Úctyper"   Z	pkg_rootsZpkg_rootZshared_lib_dirÚfnZruntime_libr&   )r%   r'   rT   R   s:   






*


















zbuild_ext.runc             C   s   |S )Nr&   )r%   rd   rK   r&   r&   r'   Úswig_sources)  s    zbuild_ext.swig_sourcesc       $      C   s|  |j }|d kst|ƒ r&td#|j ƒ‚t|ƒ}|s6d S | j|jƒ}| jr–|jdƒ}dj|dd$… ƒ}|d% }| j	dƒ}|j
|ƒ}tjj|| j|ƒƒ}	ntjj| j| j|ƒƒ}	||j }
| jpÆt|
|	dƒsÚtjd	|jƒ d S tjd
|jƒ |j� pòg }|jd d … }x|jD ]}|j|fƒ �q
W t|j ƒ\}}}}| jjdk�r\|�rP|jdƒ ||7 }g }|jdk�rp| j}n"|jdk�r„| j}n| j�p�| j}|d k	�rØt|dƒ�r´|j �p¶g ng |_ t|dƒ�rÒ|j!�pÔg ng |_!| j"}|�rü|d k�rüt#d|j ƒ‚|�s|�r |d k�r t#d|j ƒ‚|jd&k�rF|d k�rF| j$d|j ƒ |jdk�rl|d k�rl| j$d|j ƒ d|ji}| j%}|j&t'ƒ  }g }|�r¾tjdƒ | jj(|f|||| j|dœ|—Ž}|�ròtjdƒ ||j(|f|||| j|dœ|—Ž7 }g }g }|�r2tjdƒ |j)d d … }tjj| j%tjj*| j|ƒƒƒ}| j+|ƒ |j,d k�rTt-dƒ}||j.||ƒ7 }||j(|| j%||| j||jd�7 }|j,d k�r2xžt-dƒD ]’}||k�r®�qœtjj||ƒ}tjj/|ƒtjj/|ƒk�rÚ�qœtjj0|ƒ�ròtj1|ƒ y| j2||ƒ W n( t3k
�r*   tj$d||f ƒ Y nX �qœW |�rdtjdƒ ||j(|| j%||| j||jd�7 }|�r„|j4| jƒ �r„|}|} ng }|| } |j5�r¤| j6|j5ƒ |j7�p®g }| j8|ƒd d … }!|j9d d … }"| jj:}#| jjd'k�rö| j;||!|"ƒ n|jd(k�r|d k	�r|j:}#|jdk�r.|d k	�r.|j:}#|d k	�rN| j<| |!||"|ƒ\} }!|#| |	|!|"|j=|| j>|ƒ| j| j%|jd!�
 d S ))Nz*in 'ext_modules' option (extension '%s'), z&'sources' must be present and must be za list of source filenamesrE   r   r7   Úbuild_pyÚnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extensionÚmsvcz/Zm1000rB   rA   Ú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 linkerzc++zLextension %r has C++ libraries but no C++ linker found, using default linkerÚdependszcompiling C sources)Ú
output_dirr@   r,   ÚdebugÚextra_postargszcompiling C++ sourcesz#compiling Fortran 90 module sourcesz*.mod)r}   r@   r,   r~   r   r|   zfailed to move %r to %rzcompiling Fortran sourcesÚintelwÚintelemw)r>   r?   Úruntime_library_dirsr   Úexport_symbolsr~   r[   Ztarget_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„   )rA   rB   )ry   r€   r�   )rA   rB   )?rd   r   r	   rG   ÚlistrF   rN   r.   rZ   rU   Zget_package_dirr/   rY   Úget_ext_filenamerm   r|   r=   r   r   r~   rb   Úextra_compile_argsrc   Úundef_macrosrV   r   r:   rj   re   rl   ri   Úhasattrrz   r{   rg   r
   rQ   r[   r,   r   ÚcompileÚmodule_dirsÚdirnameÚmkpathZmodule_dir_switchr   Zmodule_optionsÚabspathÚisfileÚremoveÚ	move_filer   Zcan_ccompiler_linkÚextra_objectsr2   Úextra_link_argsZget_librariesr?   Zlink_shared_objectÚ_libs_with_msvc_and_fortranÚ_process_unlinkable_fobjectsr‚   Zget_export_symbols)$r%   rI   rd   ÚfullnameZmodpathÚpackageÚbaserw   Úpackage_dirZext_filenamer|   Ú
extra_argsr@   ZundefÚ	c_sourcesZcxx_sourcesZ	f_sourcesZfmodule_sourcesr"   rh   Úkwsr}   r,   Z	c_objectsr   Z	f_objectsr‹   Zmodule_build_dirZexisting_modulesÚfÚtÚunlinkable_fobjectsÚobjectsr>   r?   Zlinkerr&   r&   r'   Úbuild_extension-  s4    

























zbuild_ext.build_extensionc             C   sP   | j dƒj}| j dƒj}| jjtjj|dƒg| jd�}| jj	|d|| j
d� d S )Nr5   r6   zgfortran_vs2003_hack.c)r}   Z_gfortran_workaround)r}   r~   )rU   r5   r6   r:   rŠ   r/   rY   rZ   r[   Zcreate_static_libr~   )r%   r›   r5   r6   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 ]Ž}tjj||d ƒ}tjj|ƒr,|j|ƒ t|dƒ�}	|j|	jƒ j	ƒ ƒ W d Q R X tjj||d ƒ}
t|
dƒ�}	|j|	jƒ j	ƒ ƒ W d Q R X q,W q"W |ròdd„ |D ƒ}|j
|| j| jd�}|j|ƒ ||fS )Nz	.fobjectsÚrz	.cobjectsc             S   s   g | ]}t jj|ƒ‘qS r&   )r/   rY   Úrelpath)rH   Úobjr&   r&   r'   ú
<listcomp>  s    z:build_ext._process_unlinkable_fobjects.<locals>.<listcomp>)r}   r\   )r…   r/   rY   rZ   r�   r�   Úopenr2   ÚreadÚ
splitlinesZwrap_unlinkable_objectsr[   r\   )r%   r    r>   r"   r?   rŸ   ÚlibÚlibdirZfake_libr�   Zc_libZfobjectsÚwrappedr&   r&   r'   r•   õ  s(    

$

z&build_ext._process_unlinkable_fobjectsc             C   sâ  |d krd S xÚ|D ]Ò}|j dƒr"qd}x4|p.g D ](}tjj|d| ƒ}tjj|ƒr0d}P q0W |rbqd}xd|D ]\}tjj|d| ƒ}tjj|ƒrltjj| j|d ƒ}t||ƒ | j|krÂ|j| jƒ d}P qlW |rÒqtj	d||f ƒ qW g }	x^|j
D ]T}
|
j dƒ�r>ytjd	d
|
gƒ}
W n ttjfk
�r4   Y n
X t|
ƒ}
|	j|
ƒ qôW |j|	ƒ x†|jD ]|}|j dƒ�s^|j|ƒ t|	d| d ƒ}|�r^tjj| j|d ƒ}tjj|ƒ�sÀt|d |ƒ | j|k�r^|j| jƒ �q^W d S )Nry   Fz%s.libTzlib%s.az.libz+could not find library %r in directories %sz/usr/libZcygpathz-wrª   z.ar   )Ú
startswithr/   rY   rZ   r�   r[   r   rV   r   rQ   r?   Ú
subprocessÚcheck_outputÚOSErrorÚCalledProcessErrorr   r2   r>   r   )r%   r"   Zc_librariesZc_library_dirsrq   Z
fileexistsr«   ZlibfileZlibfile2Z
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| jD ]}|jt|ƒƒ qW |S )N)Úcheck_extensions_listrK   r2   r   )r%   Ú	filenamesrI   r&   r&   r'   Úget_source_filesR  s
    zbuild_ext.get_source_filesc             C   sT   | j | jƒ g }x>| jD ]4}|js$q| j|jƒ}|jtjj| j	| j
|ƒƒƒ qW |S )N)rµ   rK   rd   rF   rG   rV   r/   rY   rZ   rm   r†   )r%   ÚoutputsrI   r–   r&   r&   r'   Úget_outputsY  s    zbuild_ext.get_outputs)r   Nr   )r   r   r   )r   Nr   )N)Ú__name__Ú
__module__Ú__qualname__Údescriptionr    Úuser_optionsÚhelp_optionsr   Úboolean_optionsr!   r3   rT   rv   r¡   r¢   r•   r”   r·   r¹   r&   r&   r&   r'   r      s0      
 X
 @	 =r   )%Ú__doc__Ú
__future__r   r   r   r/   r®   r   Údistutils.dep_utilr   Zdistutils.command.build_extr   r    Zdistutils.errorsr   r	   r
   Údistutils.file_utilr   Znumpy.distutilsr   Znumpy.distutils.exec_commandr   Znumpy.distutils.system_infor   r   Znumpy.distutils.misc_utilr   r   r   r   r   r   r   r   Z'numpy.distutils.command.config_compilerr   r&   r&   r&   r'   Ú<module>   s   (