Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bindgen feature #34

Merged
merged 2 commits into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions fitsio-sys-bindgen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion fitsio-sys-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = comment.iter().map(|&x| x as u8).filter(|&x| x != 0).collect();
let comment: Vec<u8> = comment
.iter()
.map(|&x| x as u8)
.filter(|&x| x != 0)
.collect();
let comment = String::from_utf8(comment).unwrap();
assert_eq!(comment, "Double value");
}
Expand Down
2 changes: 0 additions & 2 deletions fitsio-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ extern crate libc;

use self::libc::*;

pub static MAX_VALUE_LENGTH: usize = 71;

pub type LONGLONG = c_longlong;

#[repr(C)]
Expand Down
6 changes: 4 additions & 2 deletions fitsio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
14 changes: 10 additions & 4 deletions fitsio/src/fitsfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
Expand Down Expand Up @@ -755,7 +757,7 @@ impl ReadsKey for String {
fn read_key(f: &FitsFile, name: &str) -> FitsResult<Self> {
let c_name = ffi::CString::new(name).unwrap();
let mut status = 0;
let mut value: Vec<libc::c_char> = vec![0; sys::MAX_VALUE_LENGTH];
let mut value: Vec<libc::c_char> = vec![0; MAX_VALUE_LENGTH];

unsafe {
sys::ffgkys(f.fptr as *mut _,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion fitsio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]
Expand Down