3
­?d]k  ã               @   sÜ  d dl mZmZmZ d dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlT d dlmZ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 d dlm Z m!Z!m"Z"m#Z#m$Z$ yd dl%Z%W n e&k
�r
   d dl'Z%Y nX da(e%j)ƒ Z*e+ƒ Z,dd„ Z-dd„ Z.dd„ Z/e.e0de/ƒ d\dd„Z1e.e0de1ƒ d]dd„Z2e.e0de2ƒ d^dd„Z3e.e0de3ƒ f fdd„Z4e.e0d e4ƒ d!d"„ Z5d#d$„ Z6e.e0d%e6ƒ d_d&d'„Z7e.e0d(e7ƒ d`d*d+„Z8d,d gfd-d.„Z9e.e0d/e9ƒ d0d1„ Z:e.e0d2e:ƒ dae;d6< dbe;d9< dce;d<< dde;d?< dee;dB< dfe;dF< e j<dm7  _<ej=dIk�rxdne;dM< e"ƒ �rxej>dNƒ dpej< e_<e?Z@dqdOdP„Z?e?e_?eAZBdQdR„ ZAeAe_Ax8drD ]0ZCejDjEdZeC d[ ƒZFeFdk	�r¤eGeFdReAƒ �q¤W dS )sé    )ÚdivisionÚabsolute_importÚprint_functionN)Úcopy)Ú	ccompiler)Ú*)ÚDistutilsExecErrorÚDistutilsModuleErrorÚDistutilsPlatformErrorÚCompileError)Úcustomize_compiler)ÚLooseVersion)Úlog)Úget_exception)Úfilepath_from_subprocess_outputÚforward_bytes_to_stdout)Ú	cyg2win32Úis_sequenceÚmingw32Úget_num_build_jobsÚ_commandline_dep_stringc             C   sÌ   | d }t jj|ƒsdS t|dƒ�}|jƒ }W dQ R X t|||ƒ}|d }||krVdS dj|dd… ƒ}	dd„ tj|	dd	�D ƒ}
y2t j	| ƒj
}x |
D ]}t j	|ƒj
|kr”dS q”W W n tk
rÆ   dS X d
S )z®
    Check if an objects needs to be rebuild based on its dependencies

    Parameters
    ----------
    obj : str
        object file

    Returns
    -------
    bool
    z.dTÚrNé   Ú c             S   s$   g | ]}|d kr|j dƒ r|‘qS )Ú
ú:)Úendswith)Ú.0Úx© r   ú:/tmp/pip-build-op5p2y5_/numpy/numpy/distutils/ccompiler.pyú
<listcomp>E   s    z _needs_build.<locals>.<listcomp>)ÚposixFéÿÿÿÿr#   )ÚosÚpathÚexistsÚopenÚ	readlinesr   ÚjoinÚshlexÚsplitÚstatÚst_mtimeÚOSError)ÚobjÚcc_argsÚextra_postargsÚpp_optsZdep_fileÚfÚlinesZcmdlineZlast_cmdlineÚcontentsÚdepsZt_objr   r   r    Ú_needs_build%   s&    
r7   c                s:   t jd dk rtjˆ d | ƒ}n‡ fdd„}t| ||ƒ d S )Nr   é   c                s   ˆ | f|ž|ŽS )Nr   )ÚselfÚargsÚkw)Úfuncr   r    Ú<lambda>\   s    z replace_method.<locals>.<lambda>)ÚsysÚversion_infoÚtypesÚ
MethodTypeÚsetattr)ÚklassÚmethod_namer<   Úmr   )r<   r    Úreplace_methodW   s    rF   c             C   s   dS )zÐ
    Does nothing here, but is called by the get_version method and can be
    overridden by subclasses. In particular it is redefined in the `FCompiler`
    class where more documentation can be found.

    Nr   )r9   r   r   r    ÚCCompiler_find_executablese   s    rG   Úfind_executablesc             C   sÔ   |dkr"|}t |ƒr"djt|ƒƒ}tj|ƒ ytj|ƒ W nJ tjk
rj } z|j}|j	}W Y dd}~X n  t
k
r„   d}d}Y nX dS t |ƒr djt|ƒƒ}t|ƒ tjd|ƒrºd}nd}td|||f ƒ‚dS )	a¯  
    Execute a command in a sub-process.

    Parameters
    ----------
    cmd : str
        The command to execute.
    display : str or sequence of str, optional
        The text to add to the log file kept by `numpy.distutils`.
        If not given, `display` is equal to `cmd`.

    Returns
    -------
    None

    Raises
    ------
    DistutilsExecError
        If the command failed, i.e. the exit status was not 0.

    Nú ó    é   s   Too many open filesz2
Try rerunning setup command until build succeeds.r   z)Command "%s" failed with exit status %d%s)r   r)   Úlistr   ÚinfoÚ
subprocessÚcheck_outputÚCalledProcessErrorÚoutputÚ
returncoder.   r   ÚreÚsearchr   )r9   ÚcmdÚdisplayÚexcÚoÚsÚmsgr   r   r    ÚCCompiler_spawns   s,    

r[   Úspawnr   c             C   sî   |dkrd}g }xØ|D ]Ð}t jjt jj|ƒƒ\}}t jj|ƒd }|t jj|ƒd… }|jdƒrž|jdƒd }|d|… }	t jjt jj	|	ƒƒ}	|	||d…  }|| j
kr¸td||f ƒ‚|rÈt jj|ƒ}t jj||| j ƒ}
|j|
ƒ qW |S )aÞ  
    Return the name of the object files for the given source files.

    Parameters
    ----------
    source_filenames : list of str
        The list of paths to source files. Paths can be either relative or
        absolute, this is handled transparently.
    strip_dir : bool, optional
        Whether to strip the directory from the returned paths. If True,
        the file name prepended by `output_dir` is returned. Default is False.
    output_dir : str, optional
        If given, this path is prepended to the returned paths to the
        object files.

    Returns
    -------
    obj_names : list of str
        The list of paths to the object files corresponding to the source
        files in `source_filenames`.

    Nr   r   z..é   z"unknown file type '%s' (from '%s'))r$   r%   ÚsplitextÚnormpathÚ
splitdriveÚisabsÚ
startswithÚrfindÚbasenameÚabspathZsrc_extensionsÚUnknownFileErrorr)   Zobj_extensionÚappend)r9   Zsource_filenamesZ	strip_dirÚ
output_dirZ	obj_namesÚsrc_nameÚbaseÚextÚiÚdZobj_namer   r   r    ÚCCompiler_object_filenames°   s&    


rn   Zobject_filenamesc	                sr  t ƒ }	t� tdkrtj|	ƒaW dQ R X |s0g S tjd dk rTddlm}
m	}m
} nddlm}
m	}m
} tˆ|
ƒrÀg }x<dD ]4}tˆd	| ƒ}|dkr˜q||jd
|dj|ƒf ƒ q|W dj|ƒ}nˆj}ddj|ƒf }tj|ƒ ˆj|||||ˆƒ\}}‰‰}ˆjˆ||ƒ‰ ddjˆ ƒ }ˆ�r2|ddjˆƒ 7 }tj|ƒ ‡ ‡‡‡fdd„}tˆ|
ƒ�rt|jƒ ƒ}g g  }}x||D ]t}||k�rv|| \}}ˆjdk�r¬t|ƒ}t|ƒ}||ƒ�rÖ||ƒ �rÖ|j|||ffƒ n|j|||ffƒ �qvW |}x |D ]}||ƒ �qøW n|jƒ }t|ƒdk�rV|	dk�rVddl}|jj|	ƒ}|j||ƒ |jƒ  nx|D ]}||ƒ �q\W |S )aÁ  
    Compile one or more source files.

    Please refer to the Python distutils API reference for more details.

    Parameters
    ----------
    sources : list of str
        A list of filenames
    output_dir : str, optional
        Path to the output directory.
    macros : list of tuples
        A list of macro definitions.
    include_dirs : list of str, optional
        The directories to add to the default include file search path for
        this compilation only.
    debug : bool, optional
        Whether or not to output debug symbols in or alongside the object
        file(s).
    extra_preargs, extra_postargs : ?
        Extra pre- and post-arguments.
    depends : list of str, optional
        A list of file names that all targets depend on.

    Returns
    -------
    objects : list of str
        A list of object file names, one per source file `sources`.

    Raises
    ------
    CompileError
        If compilation fails.

    Nr   r8   r   )Ú	FCompilerÚ	is_f_fileÚhas_f90_headerÚf77Úf90ÚfixZ	compiler_zFortran %s compiler: %srI   r   zC compiler: %s
zcompile options: '%s'z
extra options: '%s'c                sœ   | \}\}}t |ˆ ˆˆƒsd S x2t� |tkr:tj|ƒ P W d Q R X tjdƒ q W z(t� ˆj|||ˆ ˆˆƒ W d Q R X W d t� tj|ƒ W d Q R X X d S )Ngš™™™™™¹?)	r7   Ú_global_lockÚ_processing_filesÚaddÚtimeÚsleepÚ_job_semaphoreÚ_compileÚremove)r:   r/   Úsrcrk   )r0   r1   r2   r9   r   r    Úsingle_compile/  s    
"z)CCompiler_compile.<locals>.single_compileZabsoft)rr   rs   rt   ) r   ru   rz   Ú	threadingÚ	Semaphorer>   r?   Z	fcompilerro   rp   rq   Znumpy.distutils.fcompilerÚ
isinstanceÚgetattrrg   r)   Úcompiler_sor   rM   Z_setup_compileZ_get_cc_argsrL   ÚkeysÚcompiler_typer   ÚitemsÚlenZmultiprocessing.poolÚpoolZ
ThreadPoolÚmapÚclose)r9   Úsourcesrh   ÚmacrosÚinclude_dirsÚdebugÚextra_preargsr1   ÚdependsÚjobsro   rp   rq   rV   ZfcZfcompZccompZobjectsÚbuildr~   Zobjects_to_buildZf77_objectsZother_objectsr/   r}   rk   Zbuild_itemsrX   Úmultiprocessingrˆ   r   )r0   r1   r2   r9   r    ÚCCompiler_compileß   sh    +









r”   Úcompilec                sæ   t jd| jjˆ jjf ƒ ‡ ‡fdd„}|dƒr<| jˆ jƒ |dƒrdxˆ jD ]\}}| j||ƒ qLW |dƒr†xˆ jD ]}| j	|ƒ qtW |dƒr | j
| jˆ j ƒ |dƒrº| j| jˆ j ƒ |d	ƒrÎ| jˆ jƒ |d
ƒrâ| jˆ jƒ dS )aã  
    Customize compiler using distutils command.

    Parameters
    ----------
    cmd : class instance
        An instance inheriting from `distutils.cmd.Command`.
    ignore : sequence of str, optional
        List of `CCompiler` commands (without ``'set_'``) that should not be
        altered. Strings that are checked for are:
        ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs',
        'rpath', 'link_objects')``.

    Returns
    -------
    None

    zcustomize %s using %sc                s   t ˆ | d ƒd k	o| ˆkS )N)r‚   )Úattr)rU   Úignorer   r    Úallow‡  s    z&CCompiler_customize_cmd.<locals>.allowr�   ÚdefineÚundefÚ	librariesÚlibrary_dirsÚrpathÚlink_objectsN)r   rM   Ú	__class__Ú__name__Zset_include_dirsr�   r™   Zdefine_macrorš   Zundefine_macroZset_librariesr›   Zset_library_dirsrœ   Zset_runtime_library_dirsr�   Zset_link_objectsrž   )r9   rU   r—   r˜   ÚnameÚvalueÚmacror   )rU   r—   r    ÚCCompiler_customize_cmdr  s&    r¤   Zcustomize_cmdc          
      s¨   g }d}t | jjƒ ƒ}xdD ]}||kr|j|ƒ qW x@|D ]8}t| |ƒr<t| |ƒ}t|t|ƒƒ}|j|t|ƒfƒ q<W dt|d ƒ d ‰ ‡ fdd„|D ƒ}dj	|ƒS )Nr   Úversionr›   rœ   Úobject_switchÚcompile_switchr�   r™   rš   r�   rž   z%-r   zs = %sc                s   g | ]}ˆ | ‘qS r   r   )r   Úprop)Úfmtr   r    r!   ¬  s    z'_compiler_to_string.<locals>.<listcomp>r   )
r¥   r›   rœ   r¦   r§   r�   r™   rš   r�   rž   )
rL   Zexecutablesr„   rg   Úhasattrr‚   Úmaxr‡   Úreprr)   )ÚcompilerÚpropsZmxr„   ÚkeyÚvr4   r   )r©   r    Ú_compiler_to_string�  s      


r±   c             C   s`   y| j ƒ  W n tk
r    Y nX tjjdk r\tdd ƒ t| jƒ tt| ƒƒ tdd ƒ dS )z×
    Print the compiler customizations to stdout.

    Parameters
    ----------
    None

    Returns
    -------
    None

    Notes
    -----
    Printing is only done if the distutils log threshold is < 2.

    r]   r   éP   N)Úget_versionÚ	Exceptionr   Ú_global_logÚ	thresholdÚprintrŸ   r±   )r9   r   r   r    ÚCCompiler_show_customization¯  s    
r¸   Zshow_customizationc       
   "   C   sÖ  t jd| jj ƒ t| ƒ |ræy| jjdƒ W n ttfk
rF   Y nX t	| dƒr®d| j
d kr®| jsæ| j
d jdƒr€d\}}nd\}}| j
d j||ƒg| j
d	d
…  | _n8t	| dƒrÊt jd| j
f ƒ t	| dƒsæt jd| jj ƒ t	| dƒ�r*d| j
d k�s"d| j
d k�s"d| j
d k�r*d| _n¨tjdk�rÒdd
l}dd
l}|jƒ }zvyTtjj|dƒ}t|dƒ�}	|	jdƒ W d
Q R X | j|g|dd|d gd� d| _W n tk
�rÀ   d| _Y nX W d
|j|ƒ X d
S )am  
    Do any platform-specific customization of a compiler instance.

    This method calls `distutils.sysconfig.customize_compiler` for
    platform-specific customization, as well as optionally remove a flag
    to suppress spurious warnings in case C++ code is being compiled.

    Parameters
    ----------
    dist : object
        This parameter is not used for anything.
    need_cxx : bool, optional
        Whether or not C++ has to be compiled. If so (True), the
        ``"-Wstrict-prototypes"`` option is removed to prevent spurious
        warnings. Default is False.

    Returns
    -------
    None

    Notes
    -----
    All the default options used by distutils can be extracted with::

      from distutils import sysconfig
      sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS',
                                'CCSHARED', 'LDSHARED', 'SO')

    zcustomize %sz-Wstrict-prototypesr­   Úccr   Úgccúg++úc++r   Nz#### %s #######Úcompiler_cxxzMissing compiler_cxx fix for ZclangTr"   zfile.cÚwzint a;
z-MMDz-MFz.d)rh   r�   F)rº   r»   )r¹   r¼   )r   rM   rŸ   r    r   rƒ   r|   ÚAttributeErrorÚ
ValueErrorrª   r­   r½   rb   ÚreplaceÚwarnZ_auto_dependsr$   r¡   ÚtempfileÚshutilÚmkdtempr%   r)   r'   Úwriter•   r   Úrmtree)
r9   ÚdistZneed_cxxÚaÚbrÃ   rÄ   ZtmpdirÚfnr3   r   r   r    ÚCCompiler_customizeÔ  sJ    




rÌ   Z	customizeú[-.\d]+c                s   ‡ ‡‡fdd„}|S )aU  
    Simple matching of version numbers, for use in CCompiler and FCompiler.

    Parameters
    ----------
    pat : str, optional
        A regular expression matching version numbers.
        Default is ``r'[-.\d]+'``.
    ignore : str, optional
        A regular expression matching patterns to skip.
        Default is ``''``, in which case nothing is skipped.
    start : str, optional
        A regular expression matching the start of where to start looking
        for version numbers.
        Default is ``''``, in which case searching is started at the
        beginning of the version string given to `matcher`.

    Returns
    -------
    matcher : callable
        A function that is appropriate to use as the ``.version_match``
        attribute of a `CCompiler` class. `matcher` takes a single parameter,
        a version string.

    c                s~   |j ddƒ}d}ˆr0tjˆ|ƒ}|s(d S |jƒ }xBtjˆ||d … ƒ}|sNd S ˆ rntjˆ |jdƒƒrn|jƒ }q2P q2W |jdƒS )Nr   rI   r   )rÁ   rS   ÚmatchÚendrT   Úgroup)r9   Úversion_stringÚposrE   )r—   ÚpatÚstartr   r    ÚmatcherA  s     z%simple_version_match.<locals>.matcherr   )rÓ   r—   rÔ   rÕ   r   )r—   rÓ   rÔ   r    Úsimple_version_match'  s    rÖ   Fc       	   &      s:  | rt | dƒr| jS | jƒ  y
| j}W n tk
r<   dS X | sN|d  rRdS y
| j}W n@ tk
rœ   y
| j‰ W n tk
rŠ   dS X ‡ fdd„}Y nX ytj|tj	d�}W nL tj
k
râ } z|j}|j}W Y dd}~X n* tk
� rþ   d}d}Y nX t|ƒ}d}d}||k�r0||ƒ}|�r0t|ƒ}|| _|S )	a…  
    Return compiler version, or None if compiler is not available.

    Parameters
    ----------
    force : bool, optional
        If True, force a new determination of the version, even if the
        compiler already has a version attribute. Default is False.
    ok_status : list of int, optional
        The list of status values returned by the version look-up process
        for which a version string is returned. If the status value is not
        in `ok_status`, None is returned. Default is ``[0]``.

    Returns
    -------
    version : str or None
        Version string, in the format of `distutils.version.LooseVersion`.

    r¥   Nr   c                s"   t jˆ | ƒ}|sd S |jdƒ}|S )Nr¥   )rS   rÎ   rÐ   )rÑ   rE   r¥   )rÓ   r   r    rÕ   z  s
    
z&CCompiler_get_version.<locals>.matcher)ÚstderrrK   rJ   )rª   r¥   rH   Úversion_cmdr¿   Zversion_matchZversion_patternrN   rO   ÚSTDOUTrP   rQ   rR   r.   r   r   )	r9   ÚforceZ	ok_statusrØ   rÕ   rQ   rW   Ústatusr¥   r   )rÓ   r    ÚCCompiler_get_versionV  sD    




rÜ   r³   c             C   s’   | j d
kr| S t| ƒ}|jd g|jdd…  |_tjjdƒrrd|jd krr|jd |jd g|jd	d…  |_n|jd g|jdd…  |_|S )z²
    Return the C++ compiler.

    Parameters
    ----------
    None

    Returns
    -------
    cxx : class instance
        The C++ compiler, as a `CCompiler` instance.

    ÚmsvcÚintelwÚintelemwr   r   NÚaixZ	ld_so_aixr]   )rÝ   rÞ   rß   )r…   r   r½   rƒ   r>   Úplatformrb   Ú	linker_so)r9   Úcxxr   r   r    ÚCCompiler_cxx_compiler›  s    
rä   Zcxx_compilerÚintelccompilerÚIntelCCompilerú(Intel C Compiler for 32-bit applicationsÚintelÚIntelItaniumCCompilerú7Intel C Itanium Compiler for Itanium-based applicationsÚinteleÚIntelEM64TCCompilerú(Intel C Compiler for 64-bit applicationsÚintelemÚIntelCCompilerWú3Intel C Compiler for 32-bit applications on WindowsrÞ   ÚIntelEM64TCCompilerWú3Intel C Compiler for 64-bit applications on Windowsrß   ÚpathccompilerÚPathScaleCCompilerú2PathScale Compiler for SiCortex-based applicationsÚpathccúlinux.*ÚntÚwin32Úmingw32ccompilerÚMingw32CCompilerú>Mingw32 port of GNU C Compiler for Win32(for MSC built Python)r   z+Setting mingw32 as default compiler for nt.c          "   C   sR  | d krt j} y"|d kr t| ƒ}t| \}}}W n8 tk
rh   d|  }|d k	r\|d|  }t|ƒ‚Y nX d| }yt|ƒ W nr tk
rð   tt	ƒ ƒ}t
jdt|ƒƒ |dd … }yt|ƒ W n* tk
rê   tt	ƒ ƒ}td| ƒ‚Y nX Y nX ytj| }	t|	ƒ| }
W n& tk
�r2   td
||f ƒ‚Y nX |
d ||ƒ}t
jd	|
 ƒ |S )Nz5don't know how to compile C/C++ code on platform '%s'z with '%s' compilerznumpy.distutils.z,%s in numpy.distutils; trying from distutilsé   z4can't compile C/C++ code: unable to load module '%s'z4can't compile C/C++ code: unable to find class '%s' zin module '%s'znew_compiler returns %szBcan't compile C/C++ code: unable to find class '%s' in module '%s')r$   r¡   Zget_default_compilerÚcompiler_classÚKeyErrorr
   Ú
__import__ÚImportErrorÚstrr   r   rM   r	   r>   ÚmodulesÚvarsrŽ   )Úplatr­   ÚverboseÚdry_runrÚ   Úmodule_nameÚ
class_nameÚlong_descriptionrZ   ÚmodulerC   r   r   r    Únew_compilerØ  sD    



r  c             C   sF   t | |||ƒ}g }x.|D ]&}t|ƒr4|jt|ƒƒ q|j|ƒ qW |S )N)Ú_distutils_gen_lib_optionsr   ÚextendrL   rg   )r­   rœ   Úruntime_library_dirsr›   r   Zlib_optsrl   r   r   r    Úgen_lib_options  s    
r  Úmsvc9rÝ   Ú_msvcÚbcppÚcygwincÚemxcÚunixcz
distutils.r­   )N)r   r   )NNNr   NNN)r   )rÍ   r   r   )rå   ræ   rç   )rå   ré   rê   )rå   rì   rí   )rå   rï   rð   )rå   rñ   rò   )ró   rô   rõ   ©r÷   rè   ©r÷   rë   ©r÷   rî   ©r÷   rö   ©rø   rÞ   ©rø   rß   )r  r  r  r  r  r  )rú   rû   rü   ©rø   r   )r  )NNr   r   r   )r  rÝ   r  r  r  r  r  )HÚ
__future__r   r   r   r$   rS   r>   r@   r*   rx   rN   r   Ú	distutilsr   Údistutils.ccompilerZdistutils.errorsr   r	   r
   r   Údistutils.sysconfigr   Zdistutils.versionr   Znumpy.distutilsr   Znumpy.distutils.compatr   Znumpy.distutils.exec_commandr   r   Znumpy.distutils.misc_utilr   r   r   r   r   r   r  Údummy_threadingrz   ÚLockru   Úsetrv   r7   rF   rG   Z	CCompilerr[   rn   r”   r¤   r±   r¸   rÌ   rÖ   rÜ   rä   rþ   Z_default_compilersrá   rM   r  Z_distutils_new_compilerr  r  Z_ccr  ÚgetÚ_mrB   r   r   r   r    Ú<module>   s°   2

;
-  
 )#
Q
/C           

    
%

