From c651bfe19ae0c785ddea74575fabeb8ba6416db9 Mon Sep 17 00:00:00 2001 From: "Christopher H. Jordan" Date: Sun, 1 Nov 2020 20:01:47 +0800 Subject: [PATCH] Expose the fitsio and fitsio-sys crates used. This allows callers to use whatever version of fitsio and fitsio-sys that is used by mwalib, in turn ensuring that other dependent libraries aren't using different versions of these crates. And, along with the other change to this crate, means that statically-linking cfitsio from other crates is simpler. The `infer_static` function introduced to build.rs is a workaround for pkg-config-rs being too restrictive when static linking (https://github.com/rust-lang/pkg-config-rs/issues/102). Basically, if we decide to statically link, we emit a message to cargo, and it'll work. Hopefully this hack can be removed in the future when pkg-config-rs is a little more liberal. --- README.md | 4 ++-- build.rs | 24 +++++++++++++++++++----- src/lib.rs | 4 ++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b746d54..1b64f6c 100644 --- a/README.md +++ b/README.md @@ -77,8 +77,8 @@ You can build mwalib from source: `cargo build --release` -- MWALIB_LINK_STATIC_CFITSIO environment variable - If this environment variable exists and is set to a non-zero value, rustc will statically link libcfitsio. If the environment variable does not exist or is 0, rustc will dynamically link libcfitsio. This is an attempt to give developers the choice of having a static or dynamic link of the fits library to ease the build and deploy process. +- Statically-linking cfitsio + If any of the MWALIB_LINK_STATIC_CFITSIO, STATIC_CFITSIO or PKG_CONFIG_ALL_STATIC environment variables exist and are set to a non-zero value, rustc will statically link libcfitsio. The default is to dynamically link libcfitsio. This is an attempt to give developers the choice of having a static or dynamic link of the fits library to ease the build and deploy process. - Use the dynamic-shared and/or static objects in the `target/release` directory diff --git a/build.rs b/build.rs index 8fa573a..21f4bc5 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,22 @@ use std::env; +// This code is adapted from pkg-config-rs +// (https://github.com/rust-lang/pkg-config-rs). +fn infer_static(name: &str) -> bool { + #[allow(clippy::if_same_then_else, clippy::needless_bool)] + if env::var(&format!("{}_STATIC", name.to_uppercase())).is_ok() { + true + } else if env::var(&format!("{}_DYNAMIC", name.to_uppercase())).is_ok() { + false + } else if env::var("PKG_CONFIG_ALL_STATIC").is_ok() { + true + } else if env::var("PKG_CONFIG_ALL_DYNAMIC").is_ok() { + false + } else { + false + } +} + fn main() { // // Link to shared or static CFITSIO @@ -11,10 +28,7 @@ fn main() { // AND // 2. libcfitsio.a needs to have been built with the following ./configure statement: // ./configure --disable-curl --prefix=/usr/local --enable-reentrant - if let Ok(val) = env::var("MWALIB_LINK_STATIC_CFITSIO") { - match val.as_str() { - "0" => (), - _ => println!("cargo:rustc-link-lib=static=cfitsio"), - } + if env::var("MWALIB_LINK_STATIC_CFITSIO") == Ok("1".to_string()) || infer_static("cfitsio") { + println!("cargo:rustc-link-lib=static=cfitsio"); } } diff --git a/src/lib.rs b/src/lib.rs index b48aaff..3e9bb62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,3 +32,7 @@ pub use context::{mwalibContext, CorrelatorVersion}; pub use error::MwalibError; pub use fits_read::*; pub use rfinput::*; + +// So that callers don't use a different version of fitsio, export them here. +pub use fitsio; +pub use fitsio_sys;