From e2b032100d5516aeb909ebce6cee139c9c9ceaf8 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Sun, 7 May 2017 19:42:30 +0100 Subject: [PATCH 1/2] Add bindgen feature This swaps out compilation with `fitsio-sys` for compilation with `fitsio-sys-bindgen`. This relies on the user having clang>=3.9 installed on their system. Set up compilation for switching features (build error) Ensure code compiles and tests pass This mostly revolved around changing pointer types which were hard coded to cast to libc::c_void, whereas in `fitsio-sys-bindgen` they were `std::os::raw::c_void`. By using a wildcard cast (`_`) the code automatically casts to the correct value for both libraries. The other change was to move `MAX_VALUE_LENGTH` to `fitsio` rather than the low level library. --- fitsio-sys-bindgen/build.rs | 4 ++-- fitsio-sys-bindgen/src/lib.rs | 6 +++++- fitsio-sys/src/lib.rs | 2 -- fitsio/Cargo.toml | 6 ++++-- fitsio/src/fitsfile.rs | 14 ++++++++++---- fitsio/src/lib.rs | 9 ++++++++- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/fitsio-sys-bindgen/build.rs b/fitsio-sys-bindgen/build.rs index 0f211499..2563b6a7 100644 --- a/fitsio-sys-bindgen/build.rs +++ b/fitsio-sys-bindgen/build.rs @@ -20,14 +20,14 @@ fn main() { bindings .write_to_file(out_path.join("bindings.rs")) .expect("Couldn't write bindings"); - }, + } Err(Error::Failure { output, .. }) => { // Handle the case where the user has not installed cfitsio, and thusly it is not on // the PKG_CONFIG_PATH let stderr = String::from_utf8(output.stderr).unwrap(); if stderr.contains::<&str>(format!("{} was not found in the pkg-config search path", package_name) - .as_ref()) { + .as_ref()) { let err_msg = format!(" Cannot find {} on the pkg-config search path. Consider installing the library for your system (e.g. through homebrew, apt-get etc.). Alternatively if it is installed, then add diff --git a/fitsio-sys-bindgen/src/lib.rs b/fitsio-sys-bindgen/src/lib.rs index 377a185a..7b4214af 100644 --- a/fitsio-sys-bindgen/src/lib.rs +++ b/fitsio-sys-bindgen/src/lib.rs @@ -186,7 +186,11 @@ mod test { assert_eq!(double_value, 3. / 32.); // TODO Hacky way of getting a string out. This should be simplified. - let comment: Vec = comment.iter().map(|&x| x as u8).filter(|&x| x != 0).collect(); + let comment: Vec = comment + .iter() + .map(|&x| x as u8) + .filter(|&x| x != 0) + .collect(); let comment = String::from_utf8(comment).unwrap(); assert_eq!(comment, "Double value"); } diff --git a/fitsio-sys/src/lib.rs b/fitsio-sys/src/lib.rs index c228598c..c5cb7368 100644 --- a/fitsio-sys/src/lib.rs +++ b/fitsio-sys/src/lib.rs @@ -58,8 +58,6 @@ extern crate libc; use self::libc::*; -pub static MAX_VALUE_LENGTH: usize = 71; - pub type LONGLONG = c_longlong; #[repr(C)] diff --git a/fitsio/Cargo.toml b/fitsio/Cargo.toml index c64be35d..af775e71 100644 --- a/fitsio/Cargo.toml +++ b/fitsio/Cargo.toml @@ -12,11 +12,13 @@ categories = ["external-ffi-bindings", "science"] [dependencies] libc = "0.2.11" -fitsio-sys = { version = "0.3.0", path = "../fitsio-sys" } +fitsio-sys = { version = "0.3.0", path = "../fitsio-sys", optional = true} +fitsio-sys-bindgen = { version = "0.0.2", path = "../fitsio-sys-bindgen", optional = true } clippy = { version = "0.0.108", optional = true } [dev-dependencies] tempdir = "0.3.4" [features] -default = [] +default = ["fitsio-sys"] +bindgen = ["fitsio-sys-bindgen"] diff --git a/fitsio/src/fitsfile.rs b/fitsio/src/fitsfile.rs index 9a43a047..a819ddec 100644 --- a/fitsio/src/fitsfile.rs +++ b/fitsio/src/fitsfile.rs @@ -16,6 +16,8 @@ use std::ffi; use std::ptr; use std::ops::Range; +pub static MAX_VALUE_LENGTH: usize = 71; + /// Macro to return a fits error if the fits file is not open in readwrite mode macro_rules! fits_check_readwrite { ($fitsfile: expr) => ( @@ -755,7 +757,7 @@ impl ReadsKey for String { fn read_key(f: &FitsFile, name: &str) -> FitsResult { let c_name = ffi::CString::new(name).unwrap(); let mut status = 0; - let mut value: Vec = vec![0; sys::MAX_VALUE_LENGTH]; + let mut value: Vec = vec![0; MAX_VALUE_LENGTH]; unsafe { sys::ffgkys(f.fptr as *mut _, @@ -908,7 +910,7 @@ macro_rules! read_write_image_impl { (start + 1) as i64, nelements as i64, ptr::null_mut(), - out.as_mut_ptr() as *mut libc::c_void, + out.as_mut_ptr() as *mut _, ptr::null_mut(), &mut status); } @@ -984,7 +986,7 @@ macro_rules! read_write_image_impl { lpixel.as_mut_ptr(), inc.as_mut_ptr(), ptr::null_mut(), - out.as_mut_ptr() as *mut libc::c_void, + out.as_mut_ptr() as *mut _, ptr::null_mut(), &mut status); @@ -1051,7 +1053,7 @@ macro_rules! read_write_image_impl { $data_type.into(), fpixel.as_mut_ptr(), lpixel.as_mut_ptr(), - data.as_ptr() as *mut libc::c_void, + data.as_ptr() as *mut _, &mut status); } @@ -1376,7 +1378,11 @@ impl<'open> FitsHdu<'open> { #[cfg(test)] mod test { + #[cfg(feature = "default")] extern crate fitsio_sys as sys; + #[cfg(feature = "bindgen")] + extern crate fitsio_sys_bindgen as sys; + extern crate tempdir; use FitsHdu; diff --git a/fitsio/src/lib.rs b/fitsio/src/lib.rs index 91eb8f14..d04096d8 100644 --- a/fitsio/src/lib.rs +++ b/fitsio/src/lib.rs @@ -62,7 +62,10 @@ //! //! ```rust //! # extern crate fitsio; -//! # extern crate fitsio_sys; +//! #[cfg(feature = "default")] +//! # extern crate fitsio_sys as sys; +//! #[cfg(feature = "bindgen")] +//! # extern crate fitsio_sys_bindgen as sys; //! # use fitsio::{FitsFile, HduInfo}; //! # //! # fn main() { @@ -390,7 +393,11 @@ #![cfg_attr(feature="clippy", feature(plugin))] #![cfg_attr(feature="clippy", plugin(clippy))] +#[cfg(feature = "default")] extern crate fitsio_sys as sys; +#[cfg(feature = "bindgen")] +extern crate fitsio_sys_bindgen as sys; + extern crate libc; #[macro_use] From b1bcc17fd9ff204d2453f8fee1a245d00d13dd19 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Sun, 7 May 2017 21:47:08 +0100 Subject: [PATCH 2/2] Update readme with bindgen feature --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c6f9c0d..4ae3ea4e 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,24 @@ Or pin a specific version: fitsio = "0.2.0" ``` -This repository contains `fitsio-sys-bindgen` which generates the C wrapper using `bindgen` at build time. This requires clang to build, and as this is likely to not be available in general, I do not recommend using it. It is contained here but is not actively developed, and untested. Use at your own peril. +This repository contains `fitsio-sys-bindgen` which generates the C +wrapper using `bindgen` at build time. This requires clang to build, and +as this is likely to not be available in general, I do not recommend +using it. It is contained here but is not actively developed, and +untested. Use at your own peril. To opt in to building with `bindgen`, +compile as: + +```sh +cargo build --no-default-features --features bindgen +``` + +or use from your `Cargo.toml` as such: + +```toml +[dependencies] +fitsio = { version = "0.2.0", default-features = false, features = ["bindgen"] } +``` + ## Documentation