U
    |Ph;                     @  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dddddZdS )    )annotationsN)interrupt_main)contextmanager)glob)path)Timer)
ModuleType)get_torch_version_tupleoptional_importc                 c  s   d }zhz"t| t}d|_|  d V  W n@ tk
rh } z"|d k	rN| rN|t||W 5 d }~X Y nX W 5 |d k	rz|   W 5 X X d S )NT)cancelr   r   daemonstartKeyboardInterruptis_aliveTimeoutError)timemessagetimere r   M/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/_extensions/loader.pytimeout   s    

 r   F,  strzdict | Noneboolintr   )module_namedefinesverbose_buildbuild_timeoutreturnc              	     s8  t t| }t |s$td|  dt  dt  d}|ddd t dd D 7 } dk	rd| gdd	  	 D  } t
t |d
ddd}tj r|t
t |d
ddd7 }|dtjj 7 } sg n fdd	 D }t|d8 tddd\}}	| |dd }
||
||||d}W 5 Q R X |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.    
"
    rB   )NFr   )
__future__r   r6   _threadr   
contextlibr   r   osr   	threadingr   typesr   r:   monai.utils.moduler	   r
   dirnamerealpath__file__r3   r   rB   r   r   r   r   <module>   s    
     