From cbffe8a2f9c715096a33e37131f2e9d6cfcda489 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 22 May 2024 15:25:05 +0200 Subject: [PATCH] wrapper: Load libraries globally instead of relative to `$PWD` There's no real reason to force `./` in default paths when the library is more and more likely to be installed on the system via `PATH` (on Windows it's in the Vulkan SDK, on Linux `directx-shader-compiler` is a common package). Furthermore, stop appending this path to the optional `lib_path` argument, if that `lib_path` doesn't **exist** as a file. In the case that a user specifies a (relative) path that doesn't exist, this throws an obscure error that `libdxcompiler.so/./libdxcompiler.so` doesn't exist because we appended a precooked filename a second time. --- src/wrapper.rs | 48 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/src/wrapper.rs b/src/wrapper.rs index 7ff8fee..ee0d53a 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -8,10 +8,10 @@ use crate::ffi::*; use crate::os::{HRESULT, LPCWSTR, LPWSTR, WCHAR}; use crate::utils::{from_wide, to_wide, HassleError, Result}; use com::{class, interfaces::IUnknown, production::Class, production::ClassAllocation, Interface}; -use libloading::{Library, Symbol}; +use libloading::{library_filename, Library, Symbol}; use std::cell::RefCell; use std::ops::Deref; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::pin::Pin; pub struct DxcBlob { @@ -491,34 +491,12 @@ pub struct Dxc { dxc_lib: Library, } -#[cfg(target_os = "windows")] -fn dxcompiler_lib_name() -> &'static Path { - Path::new("dxcompiler.dll") -} - -#[cfg(any(target_os = "linux", target_os = "android"))] -fn dxcompiler_lib_name() -> &'static Path { - Path::new("./libdxcompiler.so") -} - -#[cfg(target_os = "macos")] -fn dxcompiler_lib_name() -> &'static Path { - Path::new("./libdxcompiler.dylib") -} - impl Dxc { - /// `dxc_path` can point to a library directly or the directory containing the library, - /// in which case the appended filename depends on the platform. + /// `lib_path` is an optional path to the library. Otherwise + /// [`libloading::library_filename("dxcompiler")`] is used. pub fn new(lib_path: Option) -> Result { - let lib_path = if let Some(lib_path) = lib_path { - if lib_path.is_file() { - lib_path - } else { - lib_path.join(dxcompiler_lib_name()) - } - } else { - dxcompiler_lib_name().to_owned() - }; + let lib_path = lib_path.unwrap_or_else(|| PathBuf::from(library_filename("dxcompiler"))); + let dxc_lib = unsafe { Library::new(&lib_path) }.map_err(|e| HassleError::LoadLibraryError { filename: lib_path, @@ -678,19 +656,11 @@ impl Dxil { )) } - /// `dxil_path` can point to a library directly or the directory containing the library, - /// in which case `dxil.dll` is appended. + /// `lib_path` is an optional path to the library. Otherwise + /// [`libloading::library_filename("dxil")`] is used. #[cfg(windows)] pub fn new(lib_path: Option) -> Result { - let lib_path = if let Some(lib_path) = lib_path { - if lib_path.is_file() { - lib_path - } else { - lib_path.join("dxil.dll") - } - } else { - PathBuf::from("dxil.dll") - }; + let lib_path = lib_path.unwrap_or_else(|| PathBuf::from(library_filename("dxil"))); let dxil_lib = unsafe { Library::new(&lib_path) }.map_err(|e| HassleError::LoadLibraryError {