o
    6aP{                     @   s   d 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 ddlmZmZmZmZmZmZ ddlmZ ddlmZm Z  G dd deZdS )zC Modified version of build_ext that handles fortran source files.

    N)glob)newer_group)	build_ext)DistutilsFileErrorDistutilsSetupErrorDistutilsError)	copy_file)log)filepath_from_subprocess_output)combine_paths)filter_sourcesget_ext_source_filesget_numpy_include_dirshas_cxx_sourceshas_f_sourcesis_sequence)show_fortran_compilers)new_ccompiler_optCCompilerOptc                   @   s   e Zd ZdZejg d Zejdddefg Zejdd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)))z
fcompiler=Nz!specify the Fortran compiler type)z	parallel=jznumber of parallel jobs)
warn-errorNz'turn all warnings into errors (-Werror))zcpu-baseline=Nz4specify a list of enabled baseline CPU optimizations)zcpu-dispatch=Nz.specify a list of dispatched CPU optimizations)disable-optimizationNz1disable CPU optimized code(dispatch,simd,fast...))z
simd-test=NzMspecify a list of CPU optimizations to be tested against NumPy SIMD interfacezhelp-fcompilerNz list available Fortran compilersr   r   c                 C   s8   t |  d | _d | _d | _d | _d | _d | _d | _d S N)	old_build_extinitialize_options	fcompilerparallel
warn_errorcpu_baselinecpu_dispatchdisable_optimization	simd_testself r$   C/usr/lib/python3/dist-packages/numpy/distutils/command/build_ext.pyr   4   s   

zbuild_ext.initialize_optionsc              
   C   s   | j rzt| j | _ W n ty } ztd|d }~ww t| jtr+| jtj| _| jp/g }| j	jd u r:g | j	_| j	j| _| j
| t|  | ddddddd | jtjd	< d S )
Nz)--parallel/-j argument must be an integerbuild)r   r   )r   r   )r   r   )r   r   )r    r    )r!   r!   r!   )r   int
ValueError
isinstanceinclude_dirsstrsplitospathsepdistributionextendr   finalize_optionsset_undefined_optionsr!   r   conf_target_groups)r#   e	incl_dirsr$   r$   r%   r1   >   s0   



zbuild_ext.finalize_optionsc           "   	      s   j sd S  d  j rO jr= jjdr%td  j	d}n" j	d}d|_|
  |  d jjd< n
 d  d} j|j nd }ddlm} ddlm}  j}|| j j jd _ j j  j   jr jjd	  jjd	  j   jstj d
ddd}tj  dj!|}tj"tj  j#d}t$ dr j%&  t' j| j( j)|d _%dd }dd l*}|+| j% tj  j#d _,tj- j,st. j, i }	|d ur'|j/pg D ],\}
}|
|	v r"|	|
 |kr"td|
t0|	|
 d d t0|d d f  ||	|
< q jj/p-g D ]\}
}|
|	v r9q.||	|
< q.t1 } j D ]}t1 }g }g }g }|j/D ]C}
|
|	v r|	|
 }||dg 7 }||dg 7 }|dg D ]}||vr|| qt|	|
i dg D ]}|2| qqS|r|j/| }t3d|j4|j/|f  ||_/|j| |_|rt3d|j4|f  |j5| |_5t6|j7r|2d t8|j7r|2d |j9p j:|j7}|r|2| d|v rd}nd|v r d}n
d|v rd}nd}|r!||kr!|j9r!td |j4||f  ||_9|;| qEd|v }d|v }d|v }|r`|| j j jd _< j<}|j j|d! |  |  |=  _<nd  _<|r j>}| j> j j jd" jd# _? j?}|r|j@}| j |r|A r|  |  n d$|  d  _?nd  _?|r j>}| j> j j jd% jd# _B jB}|r|j@}| j |r|A r|  |  n d&|  d  _Bnd  _B C   fd'd( j D }|D ]C}tj |d} jstj  jD|}tE j,D ]&} tj-|s&t.| | F Gd)s0qtj  j,| }!tH|!| qqd 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-Werrornumpy	distutilsincludeznpy_cpu_dispatch_config.hzccompiler_opt_cache_ext.pycompiler_opt)r;   dispatch_hpathr   r   
cache_pathc                 S   s    t d t | jdd d S )Nz2
########### EXT COMPILER OPTIMIZATION ###########T)full)r	   inforeport)coptr$   r$   r%   rG      s   
zbuild_ext.run.<locals>.reportz.libszKlibrary %r defined more than once, overwriting build_info
%s... 
with
%s...i,  	librarieslibrary_dirsmacrossource_languagesz-updating extension %r libraries from %r to %rz-extending extension %r defined_macros with %rf77c++f90cz.resetting extension %r language from %r to %r.)need_cxxF)r;   r<   r=   r>   
requiref90
c_compilerz!f77_compiler=%s is not available.Tz!f90_compiler=%s is not available.c                    s"   h | ]}  |jd d qS ).r   )get_ext_fullnamenamer,   ).0extr"   r$   r%   	<setcomp>B  s    z build_ext.run.<locals>.<setcomp>z.dll)I
extensionsrun_commandr/   has_c_librariesinplacehave_rungetr	   warnget_command_objensure_finalizedrunget_finalized_commandrJ   appendr7   distutils.ccompilerr9   numpy.distutils.fcompilerr:   r;   r<   r=   r>   	customizecustomize_cmdr   compiler_soshow_customizationr    r-   pathjoinr6   abspath
build_temphasattrrB   cache_flushr   r   r   atexitregisterextra_dll_dirisdirmakedirsrI   reprsetaddrF   rV   define_macrosr   sourcesr   languagedetect_languageupdate_cxx_compilercxx_compilerr   _f77_compilercompiler_typeget_version_f90_compilerbuild_extensions	build_liblistdirlowerendswithr   )"r#   r7   r9   r:   r   rC   opt_cache_pathrG   rr   clibslibname
build_infoall_languagesrX   ext_languagesc_libs
c_lib_dirsrK   binfoml
new_c_libsext_languageneed_f90_compilerneed_f77_compilerneed_cxx_compilerr;   ctyper   	pkg_rootspkg_rootshared_lib_dirfnruntime_libr$   r"   r%   rc   `   s|  








$





















zbuild_ext.runc                 C   s   |S r   r$   )r#   r{   rZ   r$   r$   r%   swig_sourcesR  s   zbuild_ext.swig_sourcesc           .         s  |j }|d u st|std|j t|}|sd S | |j}| jrJ|d}d|dd }|d }| 	d}|
|}tj|| |}	ntj| j| |}	||j }
| j}| jsl| j sltd d}|s}t|
|	ds}td	|j d S td
|j |jpg }|jd d  }|jD ]}||f qt|j \}}}}| jjdkr|r|d ||7 }g }|jdkr| j }n|jdkr| j!}n| j p| j!}|d urt"|dr|j#pg ng |_#t"|dr|j$pg ng |_$| j%}|r|d u rt&d|j |s|r|d u rt&d|j |jdv r#|d u r#| 'd|j  |jdkr6|d u r6| 'd|j  d|ji}| j(}|j)t*  }g }g }g }g }| js| 	dj+}tjddd}tj||}|| | jrpd n|}|f|df||f|dffD ]\}}  |D ]|  fddd d  D 7 } qq| j, }n|d  g }!|rtd! |!| jj-|f|||| || j||d"|7 }!|rtd# |!| jj-|f|||| || j|d$|7 }!|rtd% |!| jj.|f||| || j|| d&|7 }!|r#td' |!|j.|f||| || j|| d&|7 }!g }"g }#|rtd( |j/d d  }$tj| j(tj0| |}%| 1|% |j2d u rTt3d)}&|"|4|$|%7 }"|#|j.|| j(||| j|"|jd*7 }#|j2d u rt3d)D ]H}'|'|&v rqwtj|%|'}(tj5|'tj5|(krqwtj6|(rt7|( z	| 8|'|% W qw t9y   t'd+|'|%f  Y qww |rtd, |#|j.|| j(||| j|"|jd*7 }#|#r|:| js|#})|!}*ng })|!|# }*|j;r|*<|j; |j=pg }| >|d d  }+|j?d d  },| jj@}-| jjd-v r!| A||+|, n|jdv r/|d ur/|j@}-|jdkr=|d ur=|j@}-|d urM| B|*|+||,|)\}*}+|-|*|	|+|,|jC|| D|| j| j(|jd.
 d S )/Nzjin 'ext_modules' option (extension '%s'), 'sources' must be present and must be a list of source filenamesrT   r   build_pyz*Detected changes on compiler optimizationsTnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extensionmsvcz/Zm1000rO   rM   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 found)rM   rO   zTextension %r has Fortran libraries but no Fortran linker found, using default linkerrN   zLextension %r has C++ libraries but no C++ linker found, using default linkerdependsr6   r?   r@   rA   )z.dispatch.c)z.dispatch.cppz.dispatch.cxxc                    s&   g | ]}|  r|qS r$   )r   popindex)rW   s_ext_srcr$   r%   
<listcomp>  s
    
z-build_ext.build_extension.<locals>.<listcomp>)NPY_DISABLE_OPTIMIZATIONr8   z#compiling C++ dispatch-able sources)
output_dirsrc_dirrK   r*   debugextra_postargs	ccompilerz!compiling C dispatch-able sources)r   r   rK   r*   r   r   zcompiling C sources)r   rK   r*   r   r   zcompiling C++ sourcesz#compiling Fortran 90 module sourcesz*.mod)r   rK   r*   r   r   r   zfailed to move %r to %rzcompiling Fortran sources)r   intelwintelemw)rI   rJ   runtime_library_dirsr   export_symbolsr   ro   target_lang)Er{   r   r   rV   listrU   r]   r,   rm   rd   get_package_dirr-   rl   get_ext_filenamer   r   r>   r    rB   	is_cachedr	   r   r   rF   extra_compile_argsrz   undef_macrosre   r   r;   r   r|   r   r   rp   r   r   r   r   r`   ro   r*   r   r6   cpu_baseline_flagstry_dispatchcompilemodule_dirsdirnamemkpathmodule_dir_switchr   module_optionsrn   isfileremove	move_filer   can_ccompiler_linkextra_objectsr0   extra_link_argsget_librariesrJ   link_shared_object_libs_with_msvc_and_fortran_process_unlinkable_fobjectsr   get_export_symbols).r#   rX   r{   fullnamemodpathpackagebaser   package_dirext_filenamer   force_rebuild
extra_argsrK   undef	c_sourcescxx_sources	f_sourcesfmodule_sourcesr   r   kwsr   r*   copt_c_sourcescopt_cxx_sourcescopt_baseline_flagscopt_macrosbsrc_dirrC   copt_build_src_srcs_dst	c_objectsr   	f_objectsr   module_build_direxisting_modulesftunlinkable_fobjectsobjectsrI   rJ   linkerr$   r   r%   build_extensionV  s  

















	











zbuild_ext.build_extensionc                 C   sP   |  dj}|  dj}| jjtj|dg| jd}| jj	|d|| j
d d S )Nr6   r7   zgfortran_vs2003_hack.c)r   _gfortran_workaround)r   r   )rd   r6   r7   r;   r   r-   rl   rm   ro   create_static_libr   )r#   r   r6   r7   r   r$   r$   r%   _add_dummy_mingwex_symJ  s   

z build_ext._add_dummy_mingwex_symc              
   C   s  t |}t |}t |}|d d  D ]`}|D ][}tj||d }tj|rq|| t|d}	||	 	  W d    n1 sEw   Y  tj||d }
t|
d}	||	 	  W d    n1 slw   Y  qq|rdd |D }|j
|| j| jd}|| ||fS )Nz	.fobjectsrz	.cobjectsc                 S   s   g | ]}t j|qS r$   )r-   rl   rn   )rW   objr$   r$   r%   r   n  s    z:build_ext._process_unlinkable_fobjects.<locals>.<listcomp>)r   rt   )r   r-   rl   rm   r   r   openr0   read
splitlineswrap_unlinkable_objectsro   rt   )r#   r   rI   r   rJ   r   liblibdirfake_libr   c_libfobjectswrappedr$   r$   r%   r   S  s2   

z&build_ext._process_unlinkable_fobjectsc              
   C   s  |d u rd S |D ]g}| drqd}|pg D ]}tj|d| }tj|r+d} nq|r/qd}|D ]/}tj|d| }tj|rbtj| j|d }t|| | j|vr^|| j d} nq3|rfqt	d||f  qg }	|j
D ]'}
|
 drz
td	d
|
g}
W n ttjfy   Y nw t|
}
|	|
 qu||	 |jD ]9}| ds|| t|	d| d }|rtj| j|d }tj|st|d | | j|vr|| j qd S )Nr   Fz%s.libTzlib%s.az.libz+could not find library %r in directories %sz/usr/libcygpathz-wr   z.ar   )
startswithr-   rl   rm   r   ro   r   re   r	   r`   rJ   
subprocesscheck_outputOSErrorCalledProcessErrorr
   r0   rI   r   )r#   r   c_librariesc_library_dirsr   
fileexistsr   libfilelibfile2
f_lib_dirsdirr   pdst_namer$   r$   r%   r   v  sj   


	






z%build_ext._libs_with_msvc_and_fortranc                 C   s.   |  | j g }| jD ]	}|t| q|S r   )check_extensions_listrZ   r0   r   )r#   	filenamesrX   r$   r$   r%   get_source_files  s
   
zbuild_ext.get_source_filesc              	   C   sP   |  | j g }| jD ]}|jsq| |j}|tj| j	| 
| q|S r   )r  rZ   r{   rU   rV   re   r-   rl   rm   r   r   )r#   outputsrX   r   r$   r$   r%   get_outputs  s   
zbuild_ext.get_outputsr   )__name__
__module____qualname__descriptionr   user_optionshelp_optionsr   boolean_optionsr   r1   rc   r   r   r   r   r   r  r  r$   r$   r$   r%   r      s*    
" 
s u	#=r   )!__doc__r-   r  r   distutils.dep_utilr   distutils.command.build_extr   r   distutils.errorsr   r   r   distutils.file_utilr   numpy.distutilsr	   numpy.distutils.exec_commandr
   numpy.distutils.system_infor   numpy.distutils.misc_utilr   r   r   r   r   r   'numpy.distutils.command.config_compilerr   numpy.distutils.ccompiler_optr   r   r$   r$   r$   r%   <module>   s     