a
    kh                     @  s   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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 dddd	d	d	dd
ddZejddddZddddddZddddddddddZdS )    )annotationsN)
ModuleType   )get_cache_manager   )knobsstrz	list[str])namesrcsrcdirlibrary_dirsinclude_dirs	librariesreturnc                 C  s6  t jj }r|| |||||S td}tj|dj| |d}tj	
d}	|	d u rtd}
td}|d urt|n|
}	|	d u rtdttdrt }nt }|d	krd
}tj|dd }t jj}|||g| }|	|ddddd|g}|dd |D 7 }|dd |D 7 }|dd |D 7 }tj|tjd |S )N
EXT_SUFFIXz{name}{suffix})r	   suffixCCclanggcczeFailed to find C compiler. Please specify via CC environment variable or set triton.knobs.build.impl.get_default_schemeZposix_localposix_prefix)schemeincludez-O3z-sharedz-fPICz
-Wno-psabiz-oc                 S  s   g | ]}d | qS )z-l ).0libr   r   B/var/www/auris/lib/python3.9/site-packages/triton/runtime/build.py
<listcomp>0       z_build.<locals>.<listcomp>c                 S  s   g | ]}d | qS )z-Lr   r   dirr   r   r   r   1   r   c                 S  s   g | ]}|d urd| qS )Nz-Ir   r   r   r   r   r   2   r   )stdout)r   buildimpl	sysconfigget_config_varospathjoinformatenvirongetshutilwhichRuntimeErrorhasattrr   _get_default_scheme	get_pathsZbackend_dirs
subprocess
check_callDEVNULL)r	   r
   r   r   r   r   r#   r   soccr   r   r   Zpy_include_dirZcustom_backend_dirscc_cmdr   r   r   _build   s6    




r8   )r   c                  C  s,   ddl m} m}m} d|  | g| S )Nr   machinesystemarchitecture,)platformr:   r;   r<   r(   r9   r   r   r   platform_key7   s    r?   r   )r	   r'   r   c                 C  sH   t j| |}|r|js,td|  d| t j|}|j| |S )NzFailed to load newly compiled z from )	importlibutilspec_from_file_locationloaderr.   module_from_specexec_module)r	   r'   specmodr   r   r   _load_module_from_path=   s    
rH   zlist[str] | None)r
   r	   r   r   r   r   c              	   C  sf  t | t  d }t|}td}|| | }|d urzt	||W S  t
tfy   tt}	|	d| d Y n0 t }
tj|
|d }t|d}||  W d    n1 s0    Y  t|||
|pg |pg |pg }t|d,}|j| | | dd	}W d    n1 s20    Y  W d    n1 sR0    Y  t	||S )
Nzutf-8r   z$Triton cache error: compiled module z.so could not be loadedz.cwrbT)binary)hashlibsha256r?   encode	hexdigestr   r$   r%   Zget_filerH   r.   ImportErrorlogging	getLogger__name__warningtempfileTemporaryDirectoryr&   r'   r(   openwriter8   putread)r
   r	   r   r   r   keycacher   
cache_pathlogtmpdirZsrc_pathfr5   r   r   r   compile_module_from_srcF   s$    


(\ra   )NNN)
__future__r   	functoolsrL   importlib.utilr@   rQ   r&   r,   r2   r$   rU   typesr   r\   r    r   r8   	lru_cacher?   rH   ra   r   r   r   r   <module>   s&   $	  