Skip to content

Commit

Permalink
feat: add musl target support (#111)
Browse files Browse the repository at this point in the history
* feat: add musl target support

Changes:
- explicitly declare deps for `bzip2` and `lzma` feature.
- add musl target support using `cc` crate.

Though I choose to link native deps with the `bzip2-sys` and `lzma-sys` crates,
note that the deps could be done with cargo's `target` selector.
Let me know if you do not want to link it by default.

/cc #80

* chore: not force install cargo-travis to save time

* fix: try to fix default target error

* fix(CI): fix cargo-travis(kcov) error

Error description(see https://travis-ci.org/rust-bio/rust-htslib/builds/424648253):

```
kcov: warning: kcov: WARNING: kcov has been built without libbfd-dev (or
kcov: binutils-dev), so the --verify option will not do anything.
Can't set personality: Operation not permitted
kcov: error: Can't start/attach to /home/travis/build/rust-bio/rust-htslib/target/debug/deps/rust_htslib-b4e39d181972c8d6
Child hasn't stopped: ff00
kcov: error: Can't start/attach to /home/travis/build/rust-bio/rust-htslib/target/debug/deps/rust_htslib-b4e39d181972c8d6
error: test failed
```

Reference: roblabla/cargo-travis#38
  • Loading branch information
zitsen authored and johanneskoester committed Sep 12, 2018
1 parent 9cf1c52 commit 1f9736e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@ addons:
- libelf-dev
- libdw-dev
- binutils-dev
- libiberty-dev
- cmake
- musl
- musl-dev
- musl-tools
sources:
- kalakris-cmake

before_script:
- cargo install --force cargo-travis
- cargo install cargo-travis || true
- export PATH=$HOME/.cargo/bin:$PATH
- rustup component add rustfmt-preview
- rustup target add x86_64-unknown-linux-musl

script:
- cargo fmt --version
- cargo fmt -- --check
- cargo build
- cargo test
- cargo build --no-default-features
- cargo test --no-default-features
- cargo build --target x86_64-unknown-linux-musl
- cargo test --target x86_64-unknown-linux-musl
- cargo build --target x86_64-unknown-linux-musl --no-default-features
- cargo test --target x86_64-unknown-linux-musl --no-default-features

after_success:
- cargo coveralls
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ documentation = "https://docs.rs/rust-htslib"

[dependencies]
libc = "0.2"
libz-sys = "1.0"
bzip2-sys = { version = "0.1.6", git = "https://github.com/alexcrichton/bzip2-rs.git", optional = true }
lzma-sys = { version = "0.1.10", optional = true }
itertools = "0.6"
quick-error = "1.2"
newtype_derive = "0.1"
Expand All @@ -28,8 +31,8 @@ bio-types = ">=0.1.1"

[features]
default = ["bzip2", "lzma"]
bzip2 = []
lzma = []
bzip2 = ["bzip2-sys"]
lzma = ["lzma-sys"]

[dev-dependencies]
tempdir = "0.3"
Expand All @@ -40,3 +43,4 @@ pretty_assertions = "0.5.1"
[build-dependencies]
fs-utils = "1.0"
bindgen = "0.36"
cc = "1.0"
70 changes: 35 additions & 35 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
// except according to those terms.

extern crate bindgen;
extern crate cc;
extern crate fs_utils;

use fs_utils::copy::copy_directory;

use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;

Expand All @@ -31,6 +33,13 @@ fn sed_htslib_makefile(out: &PathBuf, patterns: &Vec<&str>, feature: &str) {

fn main() {
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut cfg = cc::Build::new();
cfg.warnings(false).static_flag(true).pic(true);

if let Ok(z_inc) = env::var("DEP_Z_INCLUDE") {
cfg.include(z_inc);
}

if !out.join("htslib").exists() {
copy_directory("htslib", &out).unwrap();
}
Expand All @@ -39,17 +48,28 @@ fn main() {
if !use_bzip2 {
let bzip2_patterns = vec!["s/ -lbz2//", "/#define HAVE_LIBBZ2/d"];
sed_htslib_makefile(&out, &bzip2_patterns, "bzip2");
} else if let Ok(inc) = env::var("DEP_BZIP2_ROOT")
.map(PathBuf::from)
.map(|path| path.join("include"))
{
cfg.include(inc);
}

let use_lzma = env::var("CARGO_FEATURE_LZMA").is_ok();
if !use_lzma {
let lzma_patterns = vec!["s/ -llzma//", "/#define HAVE_LIBLZMA/d"];
sed_htslib_makefile(&out, &lzma_patterns, "lzma");
} else if let Ok(inc) = env::var("DEP_LZMA_INCLUDE").map(PathBuf::from) {
cfg.include(inc);
}

let tool = cfg.get_compiler();
let (cc_path, cflags_env) = (tool.path(), tool.cflags_env());
let cc_cflags = cflags_env.to_string_lossy().replace("-O0", "");
if Command::new("make")
.current_dir(out.join("htslib"))
.arg("CFLAGS=-g -Wall -O2 -fPIC")
.arg(format!("CC={}", cc_path.display()))
.arg(format!("CFLAGS={}", cc_cflags))
.arg("lib-static")
.arg("-B")
.status()
Expand All @@ -59,47 +79,27 @@ fn main() {
panic!("failed to build htslib");
}

let bindings = bindgen::Builder::default()
cfg.file("wrapper.c").compile("wrapper");

bindgen::Builder::default()
.header("wrapper.h")
.generate_comments(false)
.blacklist_type("max_align_t")
.generate()
.expect("Unable to generate bindings.");
bindings
.expect("Unable to generate bindings.")
.write_to_file(out.join("bindings.rs"))
.expect("Could not write bindings.");

// we additionally build and link to the wrapper in order to enable redefinition of inline
// functions
if Command::new("gcc")
.arg("-static")
.arg("-fPIC")
.arg("-c")
.arg("wrapper.c")
.arg("-o")
.arg(out.join("wrapper.o"))
.status()
.unwrap()
.success() != true
{
panic!("failed to build wrapper.o");
}
if Command::new("ar")
.current_dir(&out)
.arg("rcs")
.arg("libwrapper.a")
.arg("wrapper.o")
.status()
.unwrap()
.success() != true
{
panic!("failed to build wrapper.a");
let include = out.join("include");
fs::create_dir_all(&include).unwrap();
if include.join("htslib").exists() {
fs::remove_dir_all(include.join("htslib")).expect("remove exist include dir");
}
copy_directory(out.join("htslib").join("htslib"), &include).unwrap();
fs::copy(out.join("htslib").join("libhts.a"), out.join("libhts.a")).unwrap();

println!(
"cargo:rustc-flags=-L {out}/htslib -L {out} -l static=hts -l static=wrapper -l z {lzma} {bzip2}",
out = out.to_str().unwrap(),
lzma = if use_lzma { "-l lzma" } else { "" },
bzip2 = if use_bzip2 { "-l bz2" } else { "" },
);
println!("cargo:root={}", out.display());
println!("cargo:include={}", include.display());
println!("cargo:libdir={}", out.display());
println!("cargo:rustc-link-lib=static=hts");
}
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ extern crate serde_json;

extern crate bio_types;

extern crate libz_sys;

#[cfg(feature = "bzip2")]
extern crate bzip2_sys;
#[cfg(feature = "lzma")]
extern crate lzma_sys;

pub mod bam;
pub mod bcf;
pub mod htslib;
Expand Down

0 comments on commit 1f9736e

Please sign in to comment.