From e0b5392aea86e38bf3bf6d6b5233dbe46fba5796 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 10 Oct 2020 17:44:39 +0300 Subject: [PATCH] Remove superfluous use of build.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should make building of this crate significantly faster. At the same time we also correctly annotate the linkage on the `extern` block, the way it ought to be. Sadly there's a regression in tests – the helper scripts no longer specify the target for which tests are being built. This shall be addressed in a subsequent commit. --- Cargo.toml | 1 - build.rs | 70 ---------------------------------------------- src/os/unix/mod.rs | 25 ++++++++++++++--- tests/functions.rs | 10 +++---- 4 files changed, 26 insertions(+), 80 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 1dfd507..9839306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ readme = "README.mkd" description = "A safer binding to platform’s dynamic library loading utilities" keywords = ["dlopen", "load", "shared", "dylib"] categories = ["api-bindings"] -build = "build.rs" [target.'cfg(windows)'.dependencies.winapi] version = "0.3" diff --git a/build.rs b/build.rs deleted file mode 100644 index e217f80..0000000 --- a/build.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::io::Write; -use std::env; - -fn dlerror_is_mtsafe(target_os: &str) { - match target_os { - // Confirmed MT-safe: - "linux" - | "android" - | "openbsd" - | "macos" - | "ios" - | "solaris" - | "illumos" - | "redox" - | "fuchsia" => { - println!("cargo:rustc-cfg=mtsafe_dlerror"); - } - // Confirmed not MT-safe: - "freebsd" - | "dragonfly" - | "netbsd" - | "bitrig" - | "haiku" => {} - // Unknown: - _ => {} - } -} - -fn link_libraries(target_os: &str) { - match target_os { - "linux" | "android" => println!("cargo:rustc-link-lib=dl"), - "freebsd" | "dragonfly" => println!("cargo:rustc-link-lib=c"), - // netbsd claims dl* will be available to any dynamically linked binary, but I haven’t - // found any libraries that have to be linked to on other platforms. - // What happens if the executable is not linked up dynamically? - "openbsd" | "bitrig" | "netbsd" | "macos" | "ios" => {} - "solaris" | "illumos" => {} - "haiku" => {} - "redox" => {} - "fuchsia" => {} - // dependencies come with winapi - "windows" => {} - tos => { - writeln!(::std::io::stderr(), - "Building for an unknown target_os=`{:?}`!\nPlease report an issue ", - tos).expect("could not report the error"); - ::std::process::exit(0xfc); - } - } -} - -fn main() { - match env::var("CARGO_CFG_TARGET_OS") { - Ok(target_os) => { - dlerror_is_mtsafe(&target_os); - link_libraries(&target_os); - } - Err(e) => { - writeln!(::std::io::stderr(), - "Unable to get target_os=`{}`!", e).expect("could not report the error"); - ::std::process::exit(0xfd); - } - } - - // For tests - println!( - "cargo:rustc-env=LIBLOADING_TEST_TARGET={}", - std::env::var("TARGET").expect("$TARGET is not set") - ); -} diff --git a/src/os/unix/mod.rs b/src/os/unix/mod.rs index 785c58f..71d2c5f 100644 --- a/src/os/unix/mod.rs +++ b/src/os/unix/mod.rs @@ -220,10 +220,25 @@ impl Library { /// care about, consider using the [`Library::get_singlethreaded`] call. #[inline(always)] pub unsafe fn get(&self, symbol: &[u8]) -> Result, crate::Error> { - #[cfg(mtsafe_dlerror)] - { self.get_singlethreaded(symbol) } - #[cfg(not(mtsafe_dlerror))] - { self.get_impl(symbol, || Err(crate::Error::DlSymUnknown)) } + extern crate cfg_if; + cfg_if::cfg_if! { + // These targets are known to have MT-safe `dlerror`. + if #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "openbsd", + target_os = "macos", + target_os = "ios", + target_os = "solaris", + target_os = "illumos", + target_os = "redox", + target_os = "fuchsia" + ))] { + self.get_singlethreaded(symbol) + } else { + self.get_impl(symbol, || Err(crate::Error::DlSymUnknown)) + } + } } /// Get a pointer to function or static variable by symbol name. @@ -387,6 +402,8 @@ impl fmt::Debug for Symbol { } // Platform specific things +#[cfg_attr(any(target_os = "linux", target_os = "android"), link(name="dl"))] +#[cfg_attr(any(target_os = "freebsd", target_os = "dragonfly"), link(name="c"))] extern { fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void; fn dlclose(handle: *mut raw::c_void) -> raw::c_int; diff --git a/tests/functions.rs b/tests/functions.rs index b642478..45a5bb2 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -4,19 +4,19 @@ extern crate winapi; extern crate libloading; use libloading::{Symbol, Library}; -const LIBPATH: &'static str = concat!(env!("OUT_DIR"), "/libtest_helpers.module"); +const LIBPATH: &'static str = "target/libtest_helpers.module"; fn make_helpers() { static ONCE: ::std::sync::Once = ::std::sync::Once::new(); ONCE.call_once(|| { - let rustc = option_env!("RUSTC").unwrap_or_else(|| { "rustc".into() }); + let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| { "rustc".into() }); let mut cmd = ::std::process::Command::new(rustc); cmd .arg("src/test_helpers.rs") .arg("-o") .arg(LIBPATH) - .arg("--target") - .arg(env!("LIBLOADING_TEST_TARGET")) + // .arg("--target") + // .arg(env!("LIBLOADING_TEST_TARGET")) .arg("-O"); cmd @@ -67,7 +67,7 @@ fn test_0_no_0() { #[test] fn wrong_name_fails() { - Library::new(concat!(env!("OUT_DIR"), "/libtest_help")).err().unwrap(); + Library::new("target/this_location_is_definitely_non existent:^~").err().unwrap(); } #[test]