a
    Lf׀                     @   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$   Z/var/www/ai-form-bot/venv/lib/python3.9/site-packages/numpy/distutils/command/build_ext.pyr   4   s    
zbuild_ext.initialize_optionsc              
   C   s   | j rFzt| j | _ W n. tyD } ztd|W Y d }~n
d }~0 0 t| jtrb| jtj| _| jpjg }| j	jd u rg | 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   Zconf_target_groups)r#   eZ	incl_dirsr$   r$   r%   r1   >   s,     



zbuild_ext.finalize_optionsc           #   	      s   j s
d S  d  j r jrz jjdrJtd  j	d}q 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rZ|j/pg D ]Z\}
}|
|	v rN|	|
 |krNtd|
t0|	|
 d d t0|d d f  ||	|
< q jj/pfg D ] \}
}|
|	v r~qh||	|
< qht1 } j D ]}t1 }g }g }g }|j/D ]}
|
|	v r|	|
 }||dg 7 }||dg 7 }|dg D ]}||vr|| q|	|
i dg D ]}|2| q&q|rv|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}d}d |v rd }d!}nd|v r,d}d!}|j9r:|rh|rh||krh|j9rhtd"|j4||f  ||_9|;| qd |v }d|v }d|v }|r|| j j jd _< j<}|j j|d# |  |  |=  _<nd  _<|rp j>}| j> j j jd jd$ _? j?}|r6|j@}| j |rZ|A rZ|  |  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 ]}tj |d}  jsJtj  jD| } tE j,D ]L}!tj-| srt.|  |!F Gd)sqVtj  j,|!}"tH|"|  qVq 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-Werrornumpy	distutilsincludeznpy_cpu_dispatch_config.hzccompiler_opt_cache_ext.pycompiler_opt)r9   dispatch_hpathr   r   
cache_pathc                 S   s    t d t | jdd d S )Nz2
########### EXT COMPILER OPTIMIZATION ###########T)full)r	   inforeport)Zcoptr$   r$   r%   rE      s    
zbuild_ext.run.<locals>.reportz.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f77c++cFf90Tz.resetting extension %r language from %r to %r.)Zneed_cxx)r9   r:   r;   r<   Z
requiref90Z
c_compilerz!f77_compiler=%s is not available.z!f90_compiler=%s is not available.c                    s"   h | ]}  |jd d qS ).r   )get_ext_fullnamenamer,   ).0extr"   r$   r%   	<setcomp>S  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_commandrG   appendr5   distutils.ccompilerr7   Znumpy.distutils.fcompilerr8   r9   r:   r;   r<   Z	customizeZcustomize_cmdr   compiler_soZshow_customizationr    r-   pathjoinr4   abspath
build_temphasattrr@   Zcache_flushr   r   r   atexitregisterextra_dll_dirisdirmakedirsrF   reprsetaddrD   rO   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#   r5   r7   r8   ru   rA   Zopt_cache_pathrE   rf   ZclibslibnameZ
build_infoZall_languagesrQ   Zext_languagesZc_libsZ
c_lib_dirsrH   ZbinfomlZ
new_c_libsZext_languageZhas_fortranZneed_f90_compilerZneed_f77_compilerZneed_cxx_compilerr9   ctyper   Z	pkg_rootsZpkg_rootZshared_lib_dirfnZruntime_libr$   r"   r%   r\   `   s|   







$


















zbuild_ext.runc                 C   s   |S r   r$   )r#   ro   rS   r$   r$   r%   swig_sourcesc  s    zbuild_ext.swig_sourcesc           1         sP  |j }|d u st|s$td|j t|}|s4d S | |j}| jr|d}d|dd }|d }| 	d}|
|}tj|| |}	ntj| j| |}	||j }
| j}| js| j std d}|st|
|	dstd	|j d S td
|j |jpg }t|dd p$g }t|dd p6g }|jd d  }|jD ]}||f qLt|j \}}}}| jjdkr|r|d ||7 }||7 }g }|j dkr| j!}n"|j dkr| j"}n| j!p| j"}|d ur t#|dr|j$pg ng |_$t#|dr|j%pg ng |_%| j&}|rD|d u rDt'd|j |sP|rh|d u rht'd|j |j dv r|d u r| (d|j  |j dkr|d u r| (d|j  d|ji}| j)}|j*t+  }g }g }g }g }| js| 	dj,}tjddd}tj||}|| |} |f|df||f|dffD ]8\}!}" |!D ]&|" fd d!d d  D 7 }"qHq:| j- }n
|d" g }#|rtd# |#| jj.|f|| || || j|| |d$|7 }#|rtd% |#| jj.|f|| || || j|| d&|7 }#|rRtd' |#| jj/|f||| || j|| | d(|7 }#|rtd) |#|j/|f||| || j|| | d(|7 }#g }$g }%|rtd* |j0d d  }&tj| j)tj1| |}'| 2|' |j3d u rt4d+}(|$|5|&|'7 }$|%|j/|| j)||| j|$|jd,7 }%|j3d u rt4d+D ]})|)|(v rLq:tj|'|)}*tj6|)tj6|*krxq:tj7|*rt8|* z| 9|)|' W n& t:y   t(d-|)|'f  Y n0 q:|rtd. |%|j/|| j)||| j|$|jd,7 }%|%r|;| js|%}+|#},ng }+|#|% },|j<r<|,=|j< |j>pFg }| ?|d d  }-|j@d d  }.| jjA}/| jjd/v r| B||-|. |jCr|jCD ]&}0t4|0d0 D ]})tD|)| jE qqg |_Cn|j dv r|d ur|jA}/|j dkr|d ur|jA}/|d ur"| F|,|-||.|+\},}-|/|,|	|-|.|jC|| G|| j| j)|j d1
 d S )2Nzjin 'ext_modules' option (extension '%s'), 'sources' must be present and must be a list of source filenamesrM   r   build_pyz*Detected changes on compiler optimizationsTnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extensionZextra_c_compile_argsZextra_cxx_compile_argsmsvcz/Zm1000rL   rI   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)rI   rL   zTextension %r has Fortran libraries but no Fortran linker found, using default linkerrJ   zLextension %r has C++ libraries but no C++ linker found, using default linkerdependsr4   r=   r>   r?   )z.dispatch.c)z.dispatch.cppz.dispatch.cxxc                    s&   g | ]}|  r|qS r$   )r{   popindex)rP   sZ_extZ_srcr$   r%   
<listcomp>  s   z-build_ext.build_extension.<locals>.<listcomp>)ZNPY_DISABLE_OPTIMIZATIONr6   z#compiling C++ dispatch-able sources)
output_dirsrc_dirrH   r*   debugextra_postargsZ	ccompilerz!compiling C dispatch-able sources)r   r   rH   r*   r   r   zcompiling C sources)r   rH   r*   r   r   zcompiling C++ sourcesz#compiling Fortran 90 module sourcesz*.mod)r   rH   r*   r   r   r   zfailed to move %r to %rzcompiling Fortran sources)r   ZintelwZintelemwz/*.dll)rF   rG   runtime_library_dirsr   export_symbolsr   rd   Ztarget_lang)Hro   r   r   rO   listrN   rV   r,   rb   r]   Zget_package_dirr-   ra   get_ext_filenamerx   r   r<   r    r@   Z	is_cachedr	   r   r   rD   extra_compile_argsgetattrrn   undef_macrosr^   r   r9   ru   rp   rw   rt   re   r   r   rr   r   rY   rd   r*   r   r4   Zcpu_baseline_flagsZtry_dispatchcompilemodule_dirsdirnamemkpathZmodule_dir_switchr   Zmodule_optionsrc   isfileremove	move_filer   Zcan_ccompiler_linkextra_objectsr0   extra_link_argsZget_librariesrG   Zlink_shared_object_libs_with_msvc_and_fortranr   r   rh   _process_unlinkable_fobjectsZget_export_symbols)1r#   rQ   ro   fullnamemodpathpackagebaser   package_dirZext_filenamer   Zforce_rebuild
extra_argsZextra_cflagsZextra_cxxflagsrH   Zundef	c_sourcesZcxx_sourcesZ	f_sourcesZfmodule_sourcesr   rs   kwsr   r*   Zcopt_c_sourcesZcopt_cxx_sourcesZcopt_baseline_flagsZcopt_macrosZbsrc_dirrA   Zcopt_build_srcZ_srcsZ_dstZ	c_objectsr   Z	f_objectsr   Zmodule_build_dirZexisting_modulesftunlinkable_fobjectsobjectsrF   rG   Zlinkerdr$   r   r%   build_extensiong  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 )Nr4   r5   zgfortran_vs2003_hack.c)r   Z_gfortran_workaround)r   r   )r]   r4   r5   r9   r   r-   ra   rb   rd   Zcreate_static_libr   )r#   r   r4   r5   r   r$   r$   r%   _add_dummy_mingwex_symv  s    
z build_ext._add_dummy_mingwex_symc              
   C   s  t |}t |}t |}|d d  D ]}|D ]}tj||d }tj|r,|| t|"}	||	 	  W d    n1 s0    Y  tj||d }
t|
"}	||	 	  W d    q,1 s0    Y  q,q$|rdd |D }|j
|| j| jd}|| ||fS )Nz	.fobjectsz	.cobjectsc                 S   s   g | ]}t j|qS r$   )r-   ra   rc   )rP   objr$   r$   r%   r         z:build_ext._process_unlinkable_fobjects.<locals>.<listcomp>)r   rh   )r   r-   ra   rb   r   r   openr0   read
splitlinesZwrap_unlinkable_objectsrd   rh   )r#   r   rF   r   rG   r   liblibdirZfake_libr   Zc_libZfobjectswrappedr$   r$   r%   r     s*    

0
4
z&build_ext._process_unlinkable_fobjectsc              
   C   s  |d u rd S |D ]}| dr qd}|p*g D ]*}tj|d| }tj|r,d} qXq,|r^qd}|D ]^}tj|d| }tj|rftj| j|d }t|| | j|vr|| j d} qqf|rqt	d||f  qg }	|j
D ]R}
|
 dr2ztd	d
|
g}
W n ttjfy(   Y n
0 t|
}
|	|
 q||	 |jD ]|}| dsN|| t|	d| d }|rNtj| j|d }tj|st|d | | j|vrN|| j qNd S )Nr   Fz%s.libTzlib%s.az.libz+could not find library %r in directories %sz/usr/libZcygpathz-wr   z.ar   )
startswithr-   ra   rb   r   rd   r   r^   r	   rY   rG   
subprocesscheck_outputOSErrorCalledProcessErrorr
   r0   rF   r   )r#   r   Zc_librariesZc_library_dirsr|   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   s.   |  | j g }| jD ]}|t| q|S r   )check_extensions_listrS   r0   r   )r#   	filenamesrQ   r$   r$   r%   get_source_files  s
    
zbuild_ext.get_source_filesc              	   C   sP   |  | j g }| jD ]4}|js"q| |j}|tj| j	| 
| q|S r   )r   rS   ro   rN   rO   r^   r-   ra   rb   rx   r   )r#   outputsrQ   r   r$   r$   r%   get_outputs  s    
zbuild_ext.get_outputs)N)__name__
__module____qualname__descriptionr   user_optionshelp_optionsr   boolean_optionsr   r1   r\   r   r   r   r   r   r   r   r$   r$   r$   r%   r      s,   
"  
  	#=r   )!__doc__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   Znumpy.distutils.misc_utilr   r   r   r   r   r   Z'numpy.distutils.command.config_compilerr   Znumpy.distutils.ccompiler_optr   r   r$   r$   r$   r%   <module>   s    