Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrapper: Load libraries globally instead of relative to $PWD #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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