B
     \$                 @   s$  d dl mZmZmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlm  mZ d dlZd dlZd dlZd dlZd dlmZmZmZ d dlmZ d	d
dZdd Zeed ZeedZedd Zdd Z e  Z!G dd de"Z#G dd de"Z$dd Z%dddZ&dd Z'dd Z(dS )     )print_functiondivisionabsolute_import)	CCompilernew_compiler)	build_ext)customize_compiler)logN)NamedTemporaryFilemkdtemp
gettempdir)contextmanager)z.dllz.pyd)z.soz.so)windefaultc             C   s   t tjd d t d |  S )N   r   )_configsgetsysplatform)arg r   2lib/python3.7/site-packages/numba/pycc/platform.pyget_configs   s    r      c             c   s^   z(t  }ttj|d|  d}|V  W d y|  t| W n   Y nX t| X d S )Nztemp%sZwt)r   openospathjoincloseremovermdir)suffixZtmpdirntfr   r   r   _gentmpfile   s    
r#   c           
   C   s   t  } t|  xldD ]d}yJt|8}d}|| |  |  | j|jgt d W d Q R X W q t	k
rv   dS X qW dS )N)z.cz.cxxzint main(void) { return 0; })
output_dirFT)
r   r   r#   writeflushr   compilenamer   	Exception)Zcompilerr!   r"   Zsimple_cr   r   r   _check_external_compiler0   s    


"
r*   c               @   s   e Zd Zg ZdS )_DummyExtensionN)__name__
__module____qualname__	librariesr   r   r   r   r+   H   s   r+   c               @   sj   e Zd Zdd Zedd Zejdd Zdd Zdd
dZdddZ	dd Z
dd Zdd Zdd Zd	S )	Toolchainc             C   sj   t s|   ddlm} d| _t | _t| j t| | _	| j	
  | j	j| _| j	j| _td| _d S )Nr   )DistributionFZnpymath)_external_compiler_ok_raise_external_compiler_errorZdistutils.distr1   _verboser   	_compilerr   r   
_build_extZfinalize_optionslibrary_dirs_py_lib_dirsinclude_dirs_py_include_dirsnp_miscZget_info
_math_info)selfr1   r   r   r   __init__N   s    



zToolchain.__init__c             C   s   | j S )N)r4   )r=   r   r   r   verbose]   s    zToolchain.verbosec             C   s    || _ t|rtjntj d S )N)r4   r	   Zset_thresholdINFOZWARN)r=   valuer   r   r   r?   a   s    c             C   s   d}d}t j}|drLt jdkr,ddg}nddg}d	||d
| f }nN|drvddg}d	||d
| f }n$|drd}d	||f }nd| }t|d S )NzYAttempted to compile AOT function without the compiler used by `numpy.distutils` present.z(If using conda try:

#> conda install %sZlinuxl        zgcc_linux-32zgxx_linux-32zgcc_linux-64zgxx_linux-64z%s %s darwinzclang_osx-64zclangxx_osx-64win32zCannot find suitable msvc.zUnknown platform %s)r   r   
startswithmaxsizer   RuntimeError)r=   ZbasemsgZ	conda_msgZpltZ	compilersmsgZwinmsgr   r   r   r3   h   s     




z(Toolchain._raise_external_compiler_errorr   Nc             C   s    | j j|||||pg |d}|S )a  
        Compile the given source files into a separate object file each,
        all beneath the *output_dir*.  A list of paths to object files
        is returned.

        *macros* has the same format as in distutils: a list of 1- or 2-tuples.
        If a 1-tuple (name,), the given name is considered undefined by
        the C preprocessor.
        If a 2-tuple (name, value), the given name is expanded into the
        given value by the C preprocessor.
        )r$   r9   dependsmacrosextra_preargs)r5   r'   )r=   Zsourcesr$   r9   rI   rJ   Zextra_cflagsobjectsr   r   r   compile_objects}   s    zToolchain.compile_objectsc       	   
   C   s2   t j|\}}| jjtj|||||||d dS )zu
        Create a shared library *output* linking the given *objects*
        and *libraries* (all strings).
        )export_symbolsrK   N)r   r   splitr5   linkr   ZSHARED_OBJECT)	r=   outputrL   r/   r7   rN   Zextra_ldflagsr$   Zoutput_filenamer   r   r   link_shared   s    zToolchain.link_sharedc             C   s0   | j t }tjdkr"|dg }|| jd  S )zJ
        Get the library arguments necessary to link with Python.
        rD   Zmsvcrtr/   )r6   Zget_librariesr+   r   r   r<   )r=   Zlibsr   r   r   get_python_libraries   s    

zToolchain.get_python_librariesc             C   s   t | j| jd  S )zL
        Get the library directories necessary to link with Python.
        r7   )listr8   r<   )r=   r   r   r   get_python_library_dirs   s    z!Toolchain.get_python_library_dirsc             C   s   t | j| jd  S )zo
        Get the include directories necessary to compile against the Python
        and Numpy C APIs.
        r9   )rT   r:   r<   )r=   r   r   r   get_python_include_dirs   s    z!Toolchain.get_python_include_dirsc             C   s   | j |S )zR
        Given a C extension's module name, return its intended filename.
        )r6   get_ext_filename)r=   Zext_namer   r   r   rW      s    zToolchain.get_ext_filename)r   r   r   N)r   r   r   N)r,   r-   r.   r>   propertyr?   setterr3   rM   rR   rS   rU   rV   rW   r   r   r   r   r0   L   s    
  
r0   c              C   s"   dd l m  m}  | j}t| _d S )Nr   )Znumpy.distutils.exec_command	distutilsZexec_command_exec_command)modZorig_exec_commandr   r   r   _patch_exec_command   s    r]   c       	      K   s,  |dkrt jdk}|dkr$t jdk}d}t jdkrn|rnt jdd}t| r^|dd| g} n
|d| g} d}n&t jdkrt| rdd	d
 | D } |pd}ytj| ||tjtjdd}W n t	k
r   dS X |
 \}}|jr|r|d7 }||7 }|dd dkr|dd }|r"t| |j|fS )zg
    Internal workhorse for exec_command().
    Code from https://github.com/numpy/numpy/pull/7862
    NposixZSHELLz/bin/shz-crB   Fntc             s   s   | ]}t |V  qd S )N)
_quote_arg).0r   r   r   r   	<genexpr>   s    z _exec_command.<locals>.<genexpr>T)shellenvstdoutstderrZuniversal_newlines)    
)r   r(   environr   _is_sequencer   
subprocessPopenPIPEEnvironmentErrorZcommunicate
returncodeprint)	ZcommandZ	use_shellZuse_teerd   
executableZshproctexterrr   r   r   r[      s>    



r[   c             C   s   d| krd| krd|  S | S )zB
    Quote the argument for safe use in a shell command line.
    "rB   z"%s"r   )r   r   r   r   r`     s    r`   c             C   s8   t | ttfrdS yt|  dS  tk
r2   dS X d S )NFT)
isinstancestrbyteslenr)   )r   r   r   r   rl     s    rl   )NN))Z
__future__r   r   r   Zdistutils.ccompilerr   r   Zdistutils.command.build_extr   Zdistutils.sysconfigr   rZ   r	   Znumpy.distutils.misc_utilZ	misc_utilr;   	functoolsr   rm   r   Ztempfiler
   r   r   
contextlibr   r   r   partialZfind_shared_endingZfind_pyext_endingr#   r*   r2   objectr+   r0   r]   r[   r`   rl   r   r   r   r   <module>   s2   {
3