o
    8ήcT                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ejdkr$dZ	dZ
nejdkr.d	Z	d
Z
ndZ	d
Z
dd Zdd ZdddZdd Zdd Zdd ZdddZdS )a2  Cudatoolkit libraries lookup utilities.

Cudatoolkit libraries can be available via either:

- the `cudatoolkit` conda package,
- a user supplied location from CUDA_HOME,
- a system wide location,
- package-specific locations (e.g. the Debian NVIDIA packages),
- or can be discovered by the system loader.
    N)find_lib)get_cuda_pathswin32z%s.dllz%s.libdarwinzlib%s.dylibzlib%s.azlib%s.soc                  C   s   t  } | d j}|S )N	libdevice)r   info)dpaths r
   >/tmp/pip-target-vg8gfxp4/lib/python/numba/cuda/cudadrv/libs.pyget_libdevice   s   
r   c                  C   s8   t t d} |  W  d    S 1 sw   Y  d S )Nrb)openr   read)bcfiler
   r
   r   open_libdevice$   s   $r   Fc                 C   sX   | dkrt  d jptd S t  d j}t| |||d}|r tnt}|r(t|S ||  S )a  
    Find the path of a CUDA library based on a search of known locations. If
    the search fails, return a generic filename for the library (e.g.
    'libnvvm.so' for 'nvvm') so that we may attempt to load it using the system
    loader's search mechanism.
    nvvmcudalib_dir)platformstatic)r   r   _dllnamepatternr   _staticnamepatternmax)libr   r   libdir
candidatesnamepatternr
   r
   r   get_cudalib)   s   r   c                 C   s   t | }t|S )N)r   ctypesCDLLr   pathr
   r
   r   open_cudalib:   s   
r"   c                 C   s*   t | dd}tj|st| dd S )NTr   z
 not found)r   osr!   isfileFileNotFoundErrorr    r
   r
   r   check_static_lib?   s   r'   c                 C   s4   | dkr
t  d jS | dkrt  d jS t  d jS )Nr   r   r   )r   by)r   r
   r
   r   _get_source_variableE   s
   r)   Tc                 C   s  d}d  }|D ]V}t|| }td|t| |r!td| n	tdtj| | dtj	fv r^ztddd	 t
| td
 W q ty] } ztd||f  d}W Y d}~qd}~ww qd}t|| dd}td|t| |rztd| n	tdtj| zt| W n ty } ztd||f  d}W Y d}~nd}~ww td}td|  tddd	 t }|rtd
 | S td d}| S )z:Test library lookup.  Path info is printed to stdout.
    Fznvvm cudartzFinding {} from {}z	located atz	named Nz	trying to open libraryz...)endz	okz	ERROR: failed to open %s:
%sT	cudadevrtr#   z	ERROR: failed to find %s:
%sr   zFinding libdevice from z	ERROR: can't open libdevice)splitr   printformatr)   r$   r!   basenamesysr   r"   OSErrorr'   r&   r   )	_platformprint_pathsfailedlibsr   r!   ewherer
   r
   r   testN   sT   

r8   )NF)NT)__doc__r$   r0   r   numba.misc.findlibr   numba.cuda.cuda_pathsr   r   r   r   r   r   r   r"   r'   r)   r8   r
   r
   r
   r   <module>   s*    


	