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Zd dlZd dlZd dlZd dlmZmZ d dlmZ dd	lmZmZ dd
lmZ G dd deZG dd de
ZdS )    )print_functiondivisionabsolute_import)dir_utillog)	build_ext)	ExtensionN)sigutilstyping)global_compiler_lock   )ModuleCompilerExportEntry)	Toolchainc               @   s  e Zd ZdZddgZi Zi Zd,ddZedd Z	ed	d
 Z
e
jdd
 Z
edd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zdd Zedd Zdd Zdd Zd d! Zd"d# Zd$d% Zed&d' Zed(d) Zd*d+ ZdS )-CCz_
    An ahead-of-time compiler to create extension modules that don't
    depend on Numba.
    zmodulemixin.cz../_math_c99.cNc             C   s   d|krt d|| _d| | _i | _td}|d krH|j}|d }n$t|dr`|j}|j	}ntj
| j}|dd| _|| _t | _d| _tj| j| _| j|| _d	| _d| _d S )
N.z;basename should be a simple module name, not qualified nameZ
pycc_init_r   __name____file__ FT)
ValueError	_basename_init_function_exported_functionssys	_getframe	f_globalshasattr__dict__r   modulesget_source_pathZ_source_moduler   
_toolchain_verboseospathdirname_output_dirZget_ext_filename_output_file_use_nrt_target_cpu)selfZextension_nameZsource_modulefZdct r,   ,lib/python3.7/site-packages/numba/pycc/cc.py__init__*   s*    



zCC.__init__c             C   s   | j S )z=
        The name of the extension module to create.
        )r   )r*   r,   r,   r-   nameG   s    zCC.namec             C   s   | j S )zJ
        The specific output file (a DLL) that will be generated.
        )r'   )r*   r,   r,   r-   output_fileN   s    zCC.output_filec             C   s
   || _ d S )N)r'   )r*   valuer,   r,   r-   r0   U   s    c             C   s   | j S )z?
        The directory the output file will be put in.
        )r&   )r*   r,   r,   r-   
output_dirY   s    zCC.output_dirc             C   s
   || _ d S )N)r&   )r*   r1   r,   r,   r-   r2   `   s    c             C   s   | j S )N)r(   )r*   r,   r,   r-   use_nrtd   s    z
CC.use_nrtc             C   s
   || _ d S )N)r(   )r*   r1   r,   r,   r-   r3   h   s    c             C   s   | j S )z;
        The target CPU model for code generation.
        )r)   )r*   r,   r,   r-   
target_cpul   s    zCC.target_cpuc             C   s
   || _ d S )N)r)   )r*   r1   r,   r,   r-   r4   s   s    c             C   s   | j S )zI
        Whether to display detailed information when compiling.
        )r"   )r*   r,   r,   r-   verbosew   s    z
CC.verbosec             C   s
   || _ d S )N)r"   )r*   r1   r,   r,   r-   r5   ~   s    c                sH   t \}}tj|f|  jkr4td   fdd}|S )zH
        Mark a function for exporting in the extension module.
        zduplicated export symbol %sc                s   t  | }|j < | S )N)r   r   )funcentry)exported_namer*   sigr,   r-   	decorator   s    
zCC.export.<locals>.decorator)r	   Znormalize_signaturer
   Z	signaturer   KeyError)r*   r8   r9   Zfn_argsZfn_rettyr:   r,   )r8   r*   r9   r-   export   s    
z	CC.exportc             C   s   t | j dd dS )Nc             S   s   | j S )N)Zsymbol)r7   r,   r,   r-   <lambda>   s    z$CC._export_entries.<locals>.<lambda>)key)sortedr   values)r*   r,   r,   r-   _export_entries   s    
zCC._export_entriesc                s<   t jt | jd d  }| jr*|d  fdd|D S )Nz../runtime/nrt.cc                s   g | ]}t j |qS r,   )r#   r$   join).0r+   )herer,   r-   
<listcomp>   s    z)CC._get_mixin_sources.<locals>.<listcomp>)r#   r$   r%   r   _mixin_sourcesr(   append)r*   Zmixin_sourcesr,   )rD   r-   _get_mixin_sources   s
    
zCC._get_mixin_sourcesc             C   s   d| j fdt| jfgS )NZPYCC_MODULE_NAMEZPYCC_USE_NRT)r   intr(   )r*   r,   r,   r-   _get_mixin_defines   s    zCC._get_mixin_definesc             C   s(   | j tjg }|s$| j tjg }|S )N)_extra_cflagsr   r   platformr#   r/   )r*   extra_cflagsr,   r,   r-   _get_extra_cflags   s    zCC._get_extra_cflagsc             C   s(   | j tjg }|s$| j tjg }|S )N)_extra_ldflagsr   r   rL   r#   r/   )r*   extra_ldflagsr,   r,   r-   _get_extra_ldflags   s    zCC._get_extra_ldflagsc             C   s<   |   }|  }| j }|  }| jj|||||d}|S )N)include_dirsmacrosrM   )rH   rJ   r!   get_python_include_dirsrN   Zcompile_objects)r*   	build_dirsourcesrS   rR   rM   objectsr,   r,   r-   _compile_mixins   s    

zCC._compile_mixinsc             C   sj   t | j| j| j| jd}| j|_tj	|tj
| jd d }td| j| |j|dd |g|jfS )N)Zcpu_namer   z.oz%generating LLVM code for '%s' into %sT)Zwrap)r   rA   r   r(   r)   r   Zexternal_init_functionr#   r$   rB   splitextr'   r   infoZwrite_native_objectdll_exports)r*   rU   compilerZtemp_objr,   r,   r-   _compile_object_files   s    

zCC._compile_object_filesc             C   s   | j | j_ tjd| j d}| |\}}|| |7 }|  }tj	
| j| j}| j }| j }| jj||||||d t| dS )z/
        Compile the extension module.
        zpycc-build-%s-)prefix)Zexport_symbolsrP   N)r5   r!   tempfileZmkdtempr   r]   rX   rQ   r#   r$   rB   r&   r'   get_python_librariesget_python_library_dirsZlink_sharedshutilZrmtree)r*   rU   rW   r[   rP   Z
output_dll	librarieslibrary_dirsr,   r,   r-   compile   s    



z
CC.compilec       
      K   s   | dg |   }| dg | jg }| dg |   }| dg |   }| dg | j  }| dg | j  }| dg | j  }t	f | j
|  |||||||d	|}	|	  | |	_|	S )	z`
        Create a distutils extension object that can be used in your
        setup.py.
        rS   dependsextra_compile_argsextra_link_argsrR   rc   rd   )	r/   rV   rf   Zdefine_macrosrR   rc   rd   rg   rh   )poprJ   r    rN   rQ   r!   rT   r`   ra   _CCExtensionr   rH   monkey_patch_distutils_cc)
r*   kwargsrS   rf   rg   rh   rR   rc   rd   extr,   r,   r-   distutils_extension   s2    






zCC.distutils_extension)N)r   
__module____qualname____doc__rF   rK   rO   r.   propertyr/   r0   setterr2   r3   r4   r5   r<   rA   rH   rJ   rN   rQ   rX   r   r]   re   ro   r,   r,   r,   r-   r      s4   
r   c               @   s,   e Zd ZdZdZdZdd Zedd ZdS )rj   zj
    A Numba-specific Extension subclass to LLVM-compile pure Python code
    to an extension module.
    NFc             C   s,   | j }t|j ||j\}}|| _d S )N)rl   r   ZmkpathZ
build_tempr]   Zextra_objects)r*   r   ZccrW   _r,   r,   r-   _prepare_object_files  s    z"_CCExtension._prepare_object_filesc                s4   | j r
dS tj G  fddd }|t_d| _ dS )z}
        Monkey-patch distutils with our own build_ext class knowing
        about pycc-compiled extensions modules.
        Nc                   s   e Zd Z fddZdS )z:_CCExtension.monkey_patch_distutils.<locals>._CC_build_extc                s$   t |tr||   | | d S )N)
isinstancerj   rv   build_extension)r*   rn   )_orig_build_extr,   r-   rx   #  s    

zJ_CCExtension.monkey_patch_distutils.<locals>._CC_build_ext.build_extensionN)r   rp   rq   rx   r,   )ry   r,   r-   _CC_build_ext!  s   rz   T)_distutils_monkey_patchedr   )clsrz   r,   )ry   r-   rk     s    z#_CCExtension.monkey_patch_distutils)	r   rp   rq   rr   rl   r{   rv   classmethodrk   r,   r,   r,   r-   rj     s
   rj   )Z
__future__r   r   r   Z	distutilsr   r   Zdistutils.commandr   Zdistutils.extensionr   r#   rb   r   r_   Znumbar	   r
   Znumba.compiler_lockr   r\   r   r   rL   r   objectr   rj   r,   r,   r,   r-   <module>   s    v