Remove superfluous use of build.rs

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.
This commit is contained in:
Simonas Kazlauskas
2020-10-10 17:44:39 +03:00
parent f1053ec0c0
commit e0b5392aea
4 changed files with 26 additions and 80 deletions
-1
View File
@@ -12,7 +12,6 @@ readme = "README.mkd"
description = "A safer binding to platforms dynamic library loading utilities"
keywords = ["dlopen", "load", "shared", "dylib"]
categories = ["api-bindings"]
build = "build.rs"
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
-70
View File
@@ -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 havent
// 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")
);
}
+21 -4
View File
@@ -220,10 +220,25 @@ impl Library {
/// care about, consider using the [`Library::get_singlethreaded`] call.
#[inline(always)]
pub unsafe fn get<T>(&self, symbol: &[u8]) -> Result<Symbol<T>, 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<T> fmt::Debug for Symbol<T> {
}
// 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;
+5 -5
View File
@@ -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]