B
    †Ž0dŽ:  ã               @   sÌ   d dl Z d dlZd dlZd dlmZ d dlZddlmZ ddddd	d
ddg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$dd„Zd d„ Zd!d"„ ZdS )%é    N)Úget_platformé   )Úcombine_dictÚneeds_g77_abi_wrapperÚget_g77_abi_wrappersÚgfortran_legacy_flag_hookÚblas_ilp64_pre_build_hookÚget_f2py_int64_optionsÚgeneric_pre_build_hookÚwrite_file_contentÚilp64_pre_build_hookc              C   sZ   dgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdœ} | S )zX
    Dictionary of compiler flags for switching to 8-byte default integer
    size.
    z-i8z/integer_size:64z-fdefault-integer-8z-qintsize=8z--long)ÚabsoftZcompaqZcompaqvZg95Úgnu95ZibmÚintelÚinteleÚintelemÚintelvÚintelevÚintelvemÚlaheyÚmipsÚnagÚnagforÚpathf95ZpgÚflangÚsun© )Úflagsr   r   úM/var/www/html/venv/lib/python3.7/site-packages/scipy/_build_utils/_fortran.pyÚget_fcompiler_ilp64_flags   s*    
r   c             C   s„   dd|  g}dd|  g}ddd|  gdddd|  g||||||ddd|  gd	d|  g||d
dd|  gddd|  gdd|  gdœ}|S )z›
    Dictionary of compiler flags for cpp-style preprocessing, with
    an #include search path, and safety options necessary for macro
    expansion.
    z-fppz-Iz-W132z-cppz-ffree-line-length-nonez-ffixed-line-length-nonez-Cppz--widez-col120z-ftppz-macro-expandz-Mpreprocessz-Mextend)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )ÚpathZ
intel_optsZnag_optsr   r   r   r   Ú!get_fcompiler_macro_include_flags4   s&    

r!   c             C   s6   t  d¡}|  dd¡}x|D ]}| |¡rdS qW dS )NZmklÚ	librariesÚ TF)ÚreÚcompileÚgetÚsearch)ÚinfoZr_mklr"   Zlibraryr   r   r   Úuses_mklV   s    


r)   c             C   s8   yt tjd ƒdk}W n tk
r2   t| ƒ}Y nX |S )z-Returns True if g77 ABI wrapper must be used.ZSCIPY_USE_G77_ABI_WRAPPERr   )ÚintÚosÚenvironÚKeyErrorr)   )r(   Zneeds_wrapperr   r   r   r   `   s
    c             C   s`   g }t j t j t¡¡}t| ƒrF|t j |dd¡t j |dd¡g7 }n|t j |dd¡g7 }|S )zY
    Returns file names of source files containing Fortran ABI wrapper
    routines.
    Úsrczwrap_g77_abi_f.fzwrap_g77_abi_c.czwrap_dummy_g77_abi.f)r+   r    ÚabspathÚdirnameÚ__file__r   Újoin)r(   Zwrapper_sourcesr    r   r   r   r   i   s    c             C   s¤   ddl m} ddlm} t|tƒrH| j| dg ¡f| j| dg ¡ff}n| j|j	f| j
|jff}x>|D ]6\}}|dkrxqf|jdkrf|j|d	ƒkrf|||d
ƒ qfW dS )zQ
    Pre-build hook to add dd gfortran legacy flag -fallow-argument-mismatch
    r   )Útry_add_flagr   )ÚLooseVersionÚextra_f77_compile_argsÚextra_f90_compile_argsNr   Ú10z-fallow-argument-mismatch)Zcompiler_helperr3   Zdistutils.versionr4   Ú
isinstanceÚdictÚ_f_compilerÚ
setdefaultÚ_f77_compilerr5   Ú_f90_compilerr6   Úcompiler_typeÚversion)ÚcmdÚextr3   r4   Ú	compilersÚcompilerÚargsr   r   r   r   }   s    

c              C   s.   dj tƒ ftjd d… žŽ } tj dd|  ¡S )Nz	.{}-{}.{}é   Úbuildr.   )Úformatr   ÚsysÚversion_infor+   r    r2   )Úplat_specifierr   r   r   Ú_get_build_src_dir•   s    rK   c              C   sŠ   t  d¡t  t j¡krd} n@t  d¡t  t j¡kr8d} n$t  d¡t  t j¡krTd} ntdƒ‚tj tƒ d¡}d	| | f }t||ƒ d
|gS )NÚir*   ÚlÚlongÚqZ	long_longz)No 64-bit integer type available in f2py!zint64.f2cmapz/{'integer': {'': '%s'}, 'logical': {'': '%s'}}
z--f2cmap)	ÚnpZdtypeZint64ÚRuntimeErrorr+   r    r2   rK   r   )Z
int64_nameZ	f2cmap_fnÚtextr   r   r   r	   š   s    

c             C   s   t ƒ }t| ||dS )zj
    Pre-build hook for adding Fortran compiler flags that change
    default integer size to 64-bit.
    )Úfcompiler_flags)r   r
   )r@   rA   rS   r   r   r   r   ­   s    c                s   ‡ fdd„S )ay  
    Pre-build hook for adding ILP64 BLAS compilation flags, and
    mangling Fortran source files to rename BLAS/LAPACK symbols when
    there are symbol suffixes.

    Examples
    --------
    ::

        from scipy._build_utils import blas_ilp64_pre_build_hook
        ext = config.add_extension(...)
        ext._pre_build_hook = blas_ilp64_pre_build_hook(blas_info)

    c                s   t | |ˆ ƒS )N)Ú_blas_ilp64_pre_build_hook)r@   rA   )Ú	blas_infor   r   Ú<lambda>Å   ó    z+blas_ilp64_pre_build_hook.<locals>.<lambda>r   )rU   r   )rU   r   r   ¶   s    c          	      sâ  t | dg ¡ƒ}| dd¡}| dd¡}|rP| d¡sDtd |¡ƒ‚|d d… }|sZ|rÆtj tƒ d¡}t	t
ƒ t|ƒƒ}t|t ƒr | d	g ¡ |d	  |¡ n|j |¡ d
‰ d}tj |ˆ ¡}	tj ||¡}
d}x²tƒ D ]¨}|d ||||¡7 }|d | ¡ |||¡7 }xvdD ]n}|d |…  ¡ ||d …  ¡  }|d ||||¡7 }|d |…  ¡ ||d …  ¡  }|d ||||¡7 }qW qÜW t|	|ƒ tjdd|tjd}t|
|d | ƒ ‡ fdd„}n
t
ƒ }d }t| |||ddS )NÚdefine_macrosZBLAS_SYMBOL_PREFIXr#   ZBLAS_SYMBOL_SUFFIXÚ_z0BLAS/LAPACK has incompatible symbol suffix: {!r}éÿÿÿÿzblas64-includeÚinclude_dirszblas64-prefix-defines.inczblas64-prefix-defines.hz#define {} {}{}_{}
)r   rE   z^#define (.*) (.*)$z#define \1_ \2_)r   Ú
c                s   d  ˆ ¡}||7 }|S )Nz#include "{}"
)rG   )ÚfilenameZold_textrR   )Úinclude_name_fr   r   Úpatch_sourceü   s    
z0_blas_ilp64_pre_build_hook.<locals>.patch_sourceZ_blas64)rS   Úpatch_source_funcÚsource_fnpart)r9   r&   ÚendswithrQ   rG   r+   r    r2   rK   r   r   r!   r8   r;   Úappendr[   Úget_blas_lapack_symbolsÚupperÚlowerr   r$   ÚsubÚMr
   )r@   rA   rU   ÚmacrosÚprefixÚsuffixZinclude_dirrS   Zinclude_name_cZinclude_fn_fZinclude_fn_crR   ÚsymbolÚjÚsZctextr_   r   )r^   r   rT   È   sN    




  
rT   c                s^  t |tƒ}|r"|}~| j}| j}n| j}| j}|r\| dg ¡}	| dg ¡}
||	f||
fg}n||jf||jfg}xv|D ]n\}‰ |dkrˆqvy||j }W n4 t	k
rÊ } zt
d |j¡ƒ|‚W dd}~X Y nX ˆ  ‡ fdd„|D ƒ¡ qvW |dk	rZ|r0| dg ¡ |d ¡ t|d ||ƒ}||d dd…< n*|j |j¡ t|j||ƒ}||jdd…< dS )	aû  
    Pre-build hook for adding compiler flags and patching sources.

    Parameters
    ----------
    cmd : distutils.core.Command
        Hook input. Current distutils command (build_clib or build_ext).
    ext : dict or numpy.distutils.extension.Extension
        Hook input. Configuration information for library (dict, build_clib)
        or extension (numpy.distutils.extension.Extension, build_ext).
    fcompiler_flags : dict
        Dictionary of ``{'compiler_name': ['-flag1', ...]}`` containing
        compiler flags to set.
    patch_source_func : callable, optional
        Function patching sources, see `_generic_patch_sources` below.
    source_fnpart : str, optional
        String to append to the modified file basename before extension.

    r5   r6   Nz5Compiler {!r} is not supported in this configuration.c             3   s   | ]}|ˆ kr|V  qd S )Nr   )Ú.0Úflag)rD   r   r   ú	<genexpr>A  s    z)generic_pre_build_hook.<locals>.<genexpr>ÚdependsÚsources)r8   r9   r:   r<   r=   r;   r5   r6   r>   r-   rQ   rG   ÚextendÚ_generic_patch_sourcesrr   rs   )r@   rA   rS   r`   ra   Zis_clibZ
build_infoZf77Úf90Zf77_argsZf90_argsrB   rC   r   ÚeZnew_sourcesr   )rD   r   r
   
  sF    


c          
   C   s  g }|dkrt  ¡ }t j |¡}t j |tƒ ¡}xä| D ]Ü}t j t j |¡¡\}}|dkrf| |¡ q6t	|dƒ}	|||	 
¡ ƒ}
W dQ R X t j t j |¡¡}xF||gD ](}t j ||g¡|kr¨t j ||¡}P q¨W t|›d|›ƒ‚t j |||| | ¡}t||
ƒ | |¡ q6W |S )a   
    Patch Fortran sources, creating new source files.

    Parameters
    ----------
    filenames : list
        List of Fortran source files to patch.
        Files not ending in ``.f`` or ``.f90`` are left unaltered.
    patch_source_func : callable(filename, old_contents) -> new_contents
        Function to apply to file contents, returning new file contents
        as a string.
    source_fnpart : str
        String to append to the modified file basename before extension.
    root_dir : str, optional
        Source root directory. Default: cwd

    Returns
    -------
    new_filenames : list
        List of names of the newly created patched sources.

    N)z.fz.f90Úrz not under )r+   Úgetcwdr    r/   r2   rK   ÚsplitextÚbasenamerc   ÚopenÚreadr0   Ú
commonpathÚrelpathÚ
ValueErrorr   )Ú	filenamesr`   ra   Úroot_dirZnew_filenamesÚsrc_dirr.   ÚbaserA   ÚfsrcrR   Zsrc_pathZbasedirÚrel_pathÚdstr   r   r   ru   Q  s,    


ru   c          	   C   s|   t j | ¡r6t| dƒ}| ¡ }W dQ R X ||kr6dS t j | ¡}t j |¡sXt  |¡ t| dƒ}| |¡ W dQ R X dS )zM
    Write content to file, but only if it differs from the current one.
    rx   NÚw)	r+   r    Úisfiler|   r}   r0   ÚisdirÚmakedirsÚwrite)r]   ÚcontentÚfZold_contentr0   r   r   r   r   Œ  s    
c        
   
   C   s  t tdd ƒ} | d k	r| S tj tj t¡tjd¡}g }xZdD ]R}ttj ||¡dƒ4}x,|D ]$}t	 
d|¡}|r\| | d¡¡ q\W W d Q R X q<W tj |d¡}t|dƒ}| ¡ }W d Q R X dd	i}	t||	ƒ | |	d
 ¡ | |	d ¡ ttt|ƒƒƒt_tjS )NÚcachedZlinalg)zcython_blas_signatures.txtzcython_lapack_signatures.txtrx   z^\s*[a-z]+\s+([a-z0-9]+)\(r   z_cython_signature_generator.pyÚ__name__z<module>Zblas_exclusionsZlapack_exclusions)Úgetattrrd   r+   r    r2   r0   r1   Úpardirr|   r$   Úmatchrc   Úgroupr}   Úexecrt   ÚtupleÚsortedÚsetr   )
r   ÚsrcdirÚsymbolsÚfnrŽ   ÚlineÚmZsig_fnÚcodeÚnsr   r   r   rd   Ÿ  s(    

"
rd   )NN)N)r$   r+   rH   Údistutils.utilr   ÚnumpyrP   Zsystem_infor   Ú__all__r   r!   r)   r   r   r   rK   r	   r   r   rT   r
   ru   r   rd   r   r   r   r   Ú<module>   s2   #"
		B 
F
;