Skip to main content

dynlink_cuda/
load.rs

1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::path::{Path, PathBuf};
5
6use crate::error::CudaError;
7
8// The dynamic loading aspects here were inspired by clang-sys.
9
10pub struct SharedLibrary {
11    pub(crate) library: libloading::Library,
12    path: PathBuf,
13}
14
15impl SharedLibrary {
16    fn new(library: libloading::Library, path: PathBuf) -> Self {
17        Self { library, path }
18    }
19
20    pub fn path(&self) -> &Path {
21        &self.path
22    }
23}
24
25pub fn load_manually() -> Result<SharedLibrary, CudaError> {
26    #[cfg(target_os = "windows")]
27    let path = PathBuf::from("nvcuda.dll");
28    #[cfg(not(target_os = "windows"))]
29    let path = PathBuf::from("libcuda.so");
30    let library = unsafe { libloading::Library::new(&path) }.map_err(|source| {
31        CudaError::DynLibLoadError {
32            lib: path.display().to_string(),
33            source,
34        }
35    })?;
36    let library = SharedLibrary::new(library, path);
37
38    Ok(library)
39}
40
41pub fn load() -> Result<SharedLibrary, CudaError> {
42    let library = load_manually()?;
43    Ok(library)
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::load::*;
49    #[ignore = "requires CUDA shared library to be present at runtime"]
50    #[test]
51    fn test_load_unload() {
52        load().expect("load");
53    }
54}