Skip to content

Commit

Permalink
wrapper: Load libraries globally instead of relative to $PWD
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarijnS95 committed May 22, 2024
1 parent d472559 commit cbffe8a
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<PathBuf>) -> Result<Self> {
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,
Expand Down Expand Up @@ -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<PathBuf>) -> Result<Self> {
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 {
Expand Down

0 comments on commit cbffe8a

Please sign in to comment.