a
    –§»ah$  ã                   @   s  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 )é    )Ú	CCompilerÚnew_compiler)Ú	build_ext)Úcustomize_compiler)ÚlogN)ÚNamedTemporaryFileÚmkdtempÚ
gettempdir)Úcontextmanager)z.dllz.pyd)ú.sor   )ÚwinÚdefaultc                 C   s   t  tjd d… t d ¡|  S )Né   r   )Ú_configsÚgetÚsysÚplatform©Úarg© r   úM/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/pycc/platform.pyÚget_configs   s    r   é   c                 c   sŒ   zVt ƒ }ttj |d|  ¡dƒ}|V  W z| ¡  t |¡ W n   Y qˆ0 t |¡ n0z| ¡  t |¡ W n   Y n0 t |¡ 0 d S )Nztemp%sÚwt)r   ÚopenÚosÚpathÚjoinÚcloseÚremoveÚrmdir)ÚsuffixZtmpdirÚntfr   r   r   Ú_gentmpfile   s     úr#   c               	   C   s’   t ƒ } t| ƒ dD ]z}z^t|ƒ�B}d}| |¡ | ¡  | ¡  | j|jgtƒ d� W d   ƒ n1 sh0    Y  W q t	yŠ   Y  dS 0 qdS )N)z.cz.cxxzint main(void) { return 0; })Ú
output_dirFT)
r   r   r#   ÚwriteÚflushr   ÚcompileÚnamer	   Ú	Exception)Úcompilerr!   r"   Zsimple_cr   r   r   Ú_check_external_compiler.   s    

6r+   c                   @   s   e Zd Zg ZdS )Ú_DummyExtensionN)Ú__name__Ú
__module__Ú__qualname__Ú	librariesr   r   r   r   r,   F   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_errorÚdistutils.distr2   Ú_verboser   Ú	_compilerr   r   Ú
_build_extÚfinalize_optionsÚlibrary_dirsÚ_py_lib_dirsÚinclude_dirsÚ_py_include_dirsÚnp_miscÚget_infoÚ
_math_info)Úselfr2   r   r   r   Ú__init__L   s    



zToolchain.__init__c                 C   s   | j S ©N)r6   ©rA   r   r   r   Úverbose[   s    zToolchain.verbosec                 C   s    || _ t |rtjntj¡ d S rC   )r6   r   Úset_thresholdÚINFOÚWARN)rA   Úvaluer   r   r   rE   _   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¡r’d}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 %sÚ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)rA   ZbasemsgZ	conda_msgZpltZ	compilersÚmsgZwinmsgr   r   r   r4   f   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$   r<   ÚdependsÚmacrosÚextra_preargs)r7   r'   )rA   Úsourcesr$   r<   rR   rS   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_symbolsrT   N)r   r   Úsplitr7   Úlinkr   ZSHARED_OBJECT)	rA   ÚoutputrV   r0   r:   rX   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.
        rM   Úmsvcrtr0   )r8   Zget_librariesr,   r   r   r@   )rA   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.
        r:   )Úlistr;   r@   rD   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.
        r<   )r_   r=   r@   rD   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.
        )r8   Úget_ext_filename)rA   Zext_namer   r   r   rb   ¸   s    zToolchain.get_ext_filename)r   r   r   N)r   r   r   N)r-   r.   r/   rB   ÚpropertyrE   Úsetterr4   rW   r\   r^   r`   ra   rb   r   r   r   r   r1   J   s"   

  þ
   þ
r1   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    rh   c           	      K   s,  |du rt jdk}|du 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r”t| ƒr”d d	d
„ | D ƒ¡} |pšd}ztj| ||tjtjdd�}W n t	yÎ   Y dS 0 | 
¡ \}}|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ÚposixÚSHELLz/bin/shz-crK   FÚntc                 s   s   | ]}t |ƒV  qd S rC   )Ú
_quote_arg)Ú.0r   r   r   r   Ú	<genexpr>ä   ó    z _exec_command.<locals>.<genexpr>T)ÚshellÚenvÚstdoutÚstderrÚuniversal_newlines)Ú é   Ú
éÿÿÿÿ)r   r(   Úenvironr   Ú_is_sequencer   Ú
subprocessÚPopenÚPIPEÚOSErrorÚcommunicateÚ
returncodeÚprint)	ÚcommandZ	use_shellZuse_teerq   Ú
executableÚshÚprocÚtextÚerrr   r   r   rf   Ì   s@    



ý
rf   c                 C   s   d| vrd| v rd|  S | S )zB
    Quote the argument for safe use in a shell command line.
    ú"rK   z"%s"r   r   r   r   r   rl   ÿ   s    rl   c                 C   s:   t | ttfƒrdS zt| ƒ W dS  ty4   Y dS 0 d S )NFT)Ú
isinstanceÚstrÚbytesÚlenr)   r   r   r   r   rz   
  s    rz   )NN)%Údistutils.ccompilerr   r   Zdistutils.command.build_extr   Údistutils.sysconfigr   re   r   Znumpy.distutils.misc_utilZ	misc_utilr>   Ú	functoolsr   r{   r   Útempfiler   r   r	   Ú
contextlibr
   r   r   ÚpartialZfind_shared_endingZfind_pyext_endingr#   r+   r3   Úobjectr,   r1   rh   rf   rl   rz   r   r   r   r   Ú<module>   s4   ý
{
3