a
    
d                     @   s  d dl 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
Z
d dlmZ d dlmZ d dlmZ e jjjZe jjjZe jjjZe jjZddd	Zd
d Zeed ZeedZedd Zejdddd ZG dd de Z!G dd de Z"dd Z#dd Z$dS )    N)	build_ext)Distribution)mkdtemp)contextmanager)Path)z.dllz.pyd).sor   )windefaultc                 C   s   t tjd d t d |  S )N   r	   )_configsgetsysplatformarg r   \/var/www/html/stable-diffusion-webui/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 q0 t| n0z|  t| W n   Y n0 t| 0 d S )Nztemp%swt)r   openospathjoincloseremovermdir)suffixtmpdirntfr   r   r   _gentmpfile#   s     r    )maxsizec               	   C   s   t  } t|  dD ]}zdt|H}d}|| |  |  | j|jgt|jj	d W d   n1 sn0    Y  W q t
y   Y  dS 0 qdS )zv
    Returns True if the "external compiler" bound in numpy.distutil is present
    and working, False otherwise.
    )z.cz.cxxzint main(void) { return 0; })
output_dirNFT)new_compilercustomize_compilerr    writeflushr   compilenamer   anchor	Exception)compilerr   r   Zsimple_cr   r   r   external_compiler_works5   s    

<r,   c                   @   s   e Zd Zg ZdS )_DummyExtensionN)__name__
__module____qualname__	librariesr   r   r   r   r-   L   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   s   t  s|   d| _t | _t| j tt | _| j	  | jj
| _| jj| _dt gi}tjdkrrg |d< n
dg|d< || _d S )NFinclude_dirswin32r1   m)r,   _raise_external_compiler_error_verboser#   	_compilerr$   r   r   
_build_extfinalize_optionslibrary_dirs_py_lib_dirsr3   _py_include_dirsnpZget_includer   r   
_math_info)selfZnp_compile_argsr   r   r   __init__R   s    






zToolchain.__init__c                 C   s   | j S N)r7   r@   r   r   r   verbosed   s    zToolchain.verbosec                 C   s    || _ t|rtjntj d S rB   )r7   logset_thresholdINFOWARN)r@   valuer   r   r   rD   h   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 %slinuxl        zgcc_linux-32zgxx_linux-32zgcc_linux-64zgxx_linux-64z%s %s darwinzclang_osx-64zclangxx_osx-64r4   zCannot find suitable msvc.zUnknown platform %s)r   r   
startswithr!   r   RuntimeError)r@   ZbasemsgZ	conda_msgZpltZ	compilersmsgZwinmsgr   r   r   r6   o   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"   r3   dependsmacrosextra_preargs)r8   r'   )r@   sourcesr"   r3   rP   rQ   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_symbolsrR   N)r   r   splitr8   link	CCompilerZSHARED_OBJECT)	r@   outputrT   r1   r;   rV   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.
        r4   msvcrtr1   )r9   Zget_librariesr-   r   r   r?   )r@   Zlibsr   r   r   get_python_libraries   s    

zToolchain.get_python_librariesc                 C   s
   t | jS )zL
        Get the library directories necessary to link with Python.
        )listr<   rC   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.
        r3   )r^   r=   r?   rC   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.
        )r9   get_ext_filename)r@   Zext_namer   r   r   ra      s    zToolchain.get_ext_filename)r   r   r   N)r   r   r   N)r.   r/   r0   rA   propertyrD   setterr6   rU   r[   r]   r_   r`   ra   r   r   r   r   r2   P   s"   

  
   
r2   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   
_quote_arg   s    re   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   _is_sequence   s    rj   )%
setuptoolsZsetuptools.command.build_extr   Zsetuptools.distr   numpyr>   	functoolsr   
subprocessr   tempfiler   
contextlibr   pathlibr   	distutilsZ	ccompilerrY   r#   	sysconfigr$   rE   r   r   partialZfind_shared_endingZfind_pyext_endingr    	lru_cacher,   objectr-   r2   re   rj   r   r   r   r   <module>   s8   





x