o
    í@Ëaå  ã                   @   sž   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
Z
ddlZddlZzddlmZ W n ey;   dd„ ZY nw eƒ ZG d	d
„ d
eƒZeƒ eje< dS )z:OpenMP wrapper using a libgomp dynamically loaded library.é    )Úfind_library)Úcheck_outputÚCalledProcessErrorÚDEVNULL)Úmsvc_runtime_majorÚget_shared_lib_extensionN)Úcompilerc                   C   s   t j dd¡S )NZCXXzc++)ÚosÚenvironÚget© r   r   ú./usr/lib/python3/dist-packages/omp/__init__.pyr      s   r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚOpenMPz¦
    Internal representation of the OpenMP module.

    Custom class is used to dynamically add omp runtime function
    to this library when function is called.
    c                 C   s(   t ƒ }|d u r|  ¡  d S |  |¡ d S )N)r   Úinit_not_msvcÚ	init_msvc)ÚselfÚverr   r   r   Ú__init__   s   zOpenMP.__init__c                 C   s.   t d| ƒ}|stdƒ‚t |¡| _d| _d S )Nzvcomp%d.dllz(I can't find a shared library for vcomp.é   )r   ÚImportErrorÚctypesÚCDLLÚlibompÚversion)r   r   Z
vcomp_pathr   r   r   r   %   s
   
zOpenMP.init_msvcc                 C   s   g d¢S )z&Return list of OpenMP libraries to try)ZompZgompZiomp5r   )r   r   r   r   Úget_libomp_names.   s   zOpenMP.get_libomp_namesc              
   C   s`  g }t jdkrddg}ndg}g }|D ]}tj |d¡}|r'| | tj¡¡ q|  ¡ }t	durb|D ]/}t	d 
|tƒ ¡g}zt|td}tj | ¡  ¡ ¡}	|	rU| |	¡ W q2 ttfya   Y q2w |D ]B}t|ƒ}
|
s‡|D ]}	tj |	d	 
|tƒ ¡¡}tj |¡r†|}
 nqn|
r¦zt |
¡| _W n tyŸ   td
 
|
¡ƒ‚w d| _ dS qdtd 
|d ¡ƒ‚)z? Find OpenMP library and try to load if using ctype interface. ÚdarwinZDYLD_LIBRARY_PATHZDYLD_FALLBACK_LIBRARY_PATHZLD_LIBRARY_PATHÚ Nz-print-file-name=lib{}{})Ústderrzlib{}{}z[found openMP library '{}' but couldn't load it. This may happen if you are cross-compiling.é-   zkI can't find a shared library for libomp, you may need to install it or adjust the {} environment variable.r   )ÚsysÚplatformr	   r
   r   ÚextendÚsplitÚpathsepr   ÚcxxÚformatr   r   r   ÚpathÚdirnameÚdecodeÚstripÚappendÚOSErrorr   r   ÚjoinÚisfiler   r   r   r   r   )r   Zenv_varsÚpathsZenv_varZ	env_pathsZlibomp_namesZlibomp_nameÚcmdÚoutputr&   Zlibomp_pathZcandidate_pathr   r   r   r   2   st   

€þÿÿ
€ÿÿþþÿÿù	
ÿzOpenMP.init_not_msvcc                 C   s   |dkr| j S t| jd| ƒS )z£
        Get correct function name from libgomp ready to be use.

        __getattr__ is call only `name != libomp` as libomp is a real
        attribute.
        ZVERSIONZomp_)r   Úgetattrr   )r   Únamer   r   r   Ú__getattr__v   s   zOpenMP.__getattr__N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r3   r   r   r   r   r      s    	Dr   )r7   Zctypes.utilr   Ú
subprocessr   r   r   Znumpy.distutils.misc_utilr   r   r   r	   r   Zpythran.configr   r   r$   Úobjectr   Úmodulesr4   r   r   r   r   Ú<module>   s    ÿm