Skip to content

Commit

Permalink
Expose the fitsio and fitsio-sys crates used.
Browse files Browse the repository at this point in the history
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 (rust-lang/pkg-config-rs#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.
  • Loading branch information
cjordan committed Nov 1, 2020
1 parent 47cd333 commit c651bfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 19 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit c651bfe

Please sign in to comment.