o
    0i;                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlZd d	lmZmZ e	e	eZed
d Z	ddddZdS )    )annotationsN)interrupt_main)contextmanager)glob)path)Timer)
ModuleType)get_torch_version_tupleoptional_importc                 c  s    d }z=zt | t}d|_|  d V  W n ty0 } z|d ur'| r'|t||d }~ww W |d ur?z|  W d S w d S |d urMz|  W w w w )NT)r   r   daemonstartKeyboardInterruptis_aliveTimeoutErrorcancel)timemessagetimere r   Z/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/_extensions/loader.pytimeout   s,   




r   F,  module_namestrdefinesdict | Noneverbose_buildboolbuild_timeoutintreturnr   c                   sN  t t| }t |std|  dt  dt  d}|ddd t dd D 7 } durCd| gdd	  	 D  } t
t |d
ddd}tj ri|t
t |d
ddd7 }|dtjj 7 } smg n fdd	 D }t|d" tddd\}}	| |dd }
||
||||d}W d   |S 1 sw   Y  |S )a  
    Handles the loading of c++ extension modules.

    Args:
        module_name: Name of the module to load.
            Must match the name of the relevant source directory in the `_extensions` directory.
        defines: Dictionary containing names and values of compilation defines.
        verbose_build: Set to true to enable build logging.
        build_timeout: Time in seconds before the build will throw an exception to prevent hanging.
    zNo extension module named _ c                 s  s    | ]}| V  qd S )Nr   .0vr   r   r   	<genexpr>E   s    zload_module.<locals>.<genexpr>N   c                 S  s   g | ]}| qS r   r   r$   r   r   r   
<listcomp>H   s    zload_module.<locals>.<listcomp>z**z*.cppT)	recursivez*.cuc                   s    g | ]}d | d |  qS )z-D =r   )r%   keyr   r   r   r)   Q   s     zTBuild appears to be blocked. Is there a stopped process building the same extension?ztorch.utils.cpp_extensionload)name.)r/   sourcesZextra_cflagsZextra_cuda_cflagsverbose)r   joindir_pathexists
ValueErrorplatformsystempython_versionr	   valuesr   torchcudais_availableversionr   r
   replace)r   r   r   r   
module_dirZplatform_strsourceZdefine_argsr.   r"   r/   moduler   r-   r   load_module1   s,   
"


rC   )NFr   )
r   r   r   r   r   r   r   r    r!   r   )
__future__r   r7   _threadr   
contextlibr   r   osr   	threadingr   typesr   r;   monai.utils.moduler	   r
   dirnamerealpath__file__r4   r   rC   r   r   r   r   <module>   s   
