Commit 6c32ab80 authored by Jay's avatar Jay Committed by GitHub

use feature to control static link (#52)

parent 9e6db6fc
...@@ -15,8 +15,10 @@ exclude = [ ...@@ -15,8 +15,10 @@ exclude = [
] ]
[features] [features]
default=[] default = []
valgrind=[] valgrind = []
static-link = ["librocksdb_sys/static-link"]
portable = ["librocksdb_sys/portable"]
[[test]] [[test]]
......
...@@ -7,3 +7,10 @@ build = "build.rs" ...@@ -7,3 +7,10 @@ build = "build.rs"
[dependencies] [dependencies]
libc = "0.2.11" libc = "0.2.11"
tempdir = "0.3" tempdir = "0.3"
[features]
default = []
static-link = []
# portable doesn't require static link, though it's meaningless
# when not using with static-link right now in this crate.
portable = []
...@@ -10,13 +10,10 @@ macro_rules! t { ...@@ -10,13 +10,10 @@ macro_rules! t {
} }
fn main() { fn main() {
let want_static = env::var("ROCKSDB_SYS_STATIC").map(|s| s == "1").unwrap_or(false); if !cfg!(feature = "static-link") {
if !want_static {
return; return;
} }
if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") {
let target = env::var("TARGET").unwrap();
if !target.contains("linux") && !target.contains("darwin") {
// only linux and apple support static link right now // only linux and apple support static link right now
return; return;
} }
...@@ -31,24 +28,31 @@ fn main() { ...@@ -31,24 +28,31 @@ fn main() {
let lib_name = format!("lib{}.a", lib); let lib_name = format!("lib{}.a", lib);
let src = build.join(&lib_name); let src = build.join(&lib_name);
let dst = dst.join(&lib_name); let dst = dst.join(&lib_name);
if dst.exists() { if dst.exists() && *lib != "rocksdb" {
continue; continue;
} }
if *lib == "rocksdb" && src.exists() {
fs::remove_dir_all(&src).unwrap();
}
if !src.exists() { if !src.exists() {
let mut cmd = Command::new(p.as_path()); let mut cmd = Command::new(p.as_path());
cmd.current_dir(&build).args(&[format!("compile_{}", lib)]); cmd.current_dir(&build).args(&[format!("compile_{}", lib)]);
if *lib == "rocksdb" { if *lib == "rocksdb" {
if let Some(s) = env::var("ROCKSDB_SYS_PORTABLE").ok() { if cfg!(feature = "portable") {
cmd.env("PORTABLE", s); cmd.env("PORTABLE", "1");
} }
} }
run(&mut cmd); run(&mut cmd);
} }
if let Err(e) = fs::rename(src.as_path(), dst.as_path()) { if let Err(e) = fs::rename(src.as_path(), dst.as_path()) {
panic!("failed to move {} to {}: {:?}", src.display(), dst.display(), e); panic!("failed to move {} to {}: {:?}",
src.display(),
dst.display(),
e);
} }
} }
...@@ -60,9 +64,15 @@ fn main() { ...@@ -60,9 +64,15 @@ fn main() {
println!("cargo:rustc-link-search=native={}", dst.display()); println!("cargo:rustc-link-search=native={}", dst.display());
let mut cpp_linked = false; let mut cpp_linked = false;
let std_lib_name = if cfg!(target_os = "linux") {
"libstdc++.a"
} else {
"libc++.a"
};
let short_std_lib_name = &std_lib_name[3..std_lib_name.len() - 2];
if let Ok(libs) = env::var("ROCKSDB_OTHER_STATIC") { if let Ok(libs) = env::var("ROCKSDB_OTHER_STATIC") {
for lib in libs.split(":") { for lib in libs.split(":") {
if lib == "stdc++" { if lib == short_std_lib_name {
cpp_linked = true; cpp_linked = true;
} }
println!("cargo:rustc-link-lib=static={}", lib); println!("cargo:rustc-link-lib=static={}", lib);
...@@ -74,19 +84,23 @@ fn main() { ...@@ -74,19 +84,23 @@ fn main() {
} }
} }
if !cpp_linked { if !cpp_linked {
let output = Command::new(p.as_path()).arg("find_stdcxx").output().unwrap(); let output =
Command::new(p.as_path()).args(&["find_library", std_lib_name]).output().unwrap();
if output.status.success() && !output.stdout.is_empty() { if output.status.success() && !output.stdout.is_empty() {
if let Ok(path_str) = str::from_utf8(&output.stdout) { if let Ok(path_str) = str::from_utf8(&output.stdout) {
let path = PathBuf::from(path_str); let path = PathBuf::from(path_str);
if path.is_absolute() { if path.is_absolute() {
println!("cargo:rustc-link-lib=static=stdc++"); println!("cargo:rustc-link-lib=static=stdc++");
println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display()); println!("cargo:rustc-link-search=native={}",
path.parent().unwrap().display());
return; return;
} }
} }
} }
println!("failed to detect libstdc++.a: {:?}, fallback to dynamic", output); println!("failed to detect {}: {:?}, fallback to dynamic",
println!("cargo:rustc-link-lib=stdc++"); std_lib_name,
output);
println!("cargo:rustc-link-lib={}", &short_std_lib_name);
} }
} }
......
...@@ -119,7 +119,7 @@ function compile_rocksdb() { ...@@ -119,7 +119,7 @@ function compile_rocksdb() {
mv librocksdb.a ../ mv librocksdb.a ../
} }
function find_stdcxx() { function find_library() {
if [[ "$CXX" = "" ]]; then if [[ "$CXX" = "" ]]; then
if g++ --version &>/dev/null; then if g++ --version &>/dev/null; then
CXX=g++ CXX=g++
...@@ -130,11 +130,11 @@ function find_stdcxx() { ...@@ -130,11 +130,11 @@ function find_stdcxx() {
fi fi
fi fi
$CXX --print-file-name libstdc++.a $CXX --print-file-name $1
} }
if [[ $# -ne 1 ]]; then if [[ $# -eq 0 ]]; then
error $0 [compile_bz2\|compile_z\|compile_lz4\|compile_rocksdb\|compile_snappy\|find_stdcxx] error $0 [compile_bz2\|compile_z\|compile_lz4\|compile_rocksdb\|compile_snappy\|find_library]
fi fi
$1 $@
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment