Commit 19bedb31 authored by Jay's avatar Jay Committed by GitHub

support static link (#26)

parent ebf9de0f
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
name = "librocksdb_sys" name = "librocksdb_sys"
version = "0.1.0" version = "0.1.0"
authors = ["Jay Lee <busyjaylee@gmail.com>"] authors = ["Jay Lee <busyjaylee@gmail.com>"]
build = "build.rs"
[dependencies] [dependencies]
libc = "0.1.8" libc = "0.1.8"
......
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
macro_rules! t {
($e:expr) => (match $e {
Ok(n) => n,
Err(e) => panic!("\n{} failed with {}\n", stringify!($e), e),
})
}
fn main() {
let want_static = env::var("ROCKSDB_SYS_STATIC").map(|s| s == "1").unwrap_or(false);
if !want_static {
return;
}
let target = env::var("TARGET").unwrap();
if !target.contains("linux") && !target.contains("darwin") {
// only linux and apple support static link right now
return;
}
let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let build = dst.join("build");
t!(fs::create_dir_all(&build));
let fest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let p = PathBuf::from(fest_dir).join("build.sh");
for lib in &["z", "snappy", "bz2", "lz4", "rocksdb"] {
let lib_name = format!("lib{}.a", lib);
let src = build.join(&lib_name);
let dst = dst.join(&lib_name);
if dst.exists() {
continue;
}
if !src.exists() {
let mut cmd = Command::new(p.as_path());
cmd.current_dir(&build).args(&[format!("compile_{}", lib)]);
if *lib == "rocksdb" {
if let Some(s) = env::var("ROCKSDB_SYS_PORTABLE").ok() {
cmd.env("PORTABLE", s);
}
}
run(&mut cmd);
}
if let Err(e) = fs::rename(src.as_path(), dst.as_path()) {
panic!("failed to move {} to {}: {:?}", src.display(), dst.display(), e);
}
}
println!("cargo:rustc-link-lib=static=rocksdb");
println!("cargo:rustc-link-lib=static=z");
println!("cargo:rustc-link-lib=static=bz2");
println!("cargo:rustc-link-lib=static=lz4");
println!("cargo:rustc-link-lib=static=snappy");
println!("cargo:rustc-link-search=native={}", dst.display());
let mut cpp_linked = false;
if let Ok(libs) = env::var("ROCKSDB_OTHER_STATIC") {
for lib in libs.split(":") {
if lib == "stdc++" {
cpp_linked = true;
}
println!("cargo:rustc-link-lib=static={}", lib);
}
if let Ok(pathes) = env::var("ROCKSDB_OTHER_STATIC_PATH") {
for p in pathes.split(":") {
println!("cargo:rustc-link-search=native={}", p);
}
}
}
// TODO: find a reliable way to link stdc++ statically.
if !cpp_linked {
println!("cargo:rustc-link-lib=stdc++");
}
}
fn run(cmd: &mut Command) {
println!("running: {:?}", cmd);
let status = match cmd.status() {
Ok(s) => s,
Err(e) => panic!("{:?} failed: {}", cmd, e),
};
if !status.success() {
panic!("{:?} failed: {}", cmd, status);
}
}
#!/usr/bin/env bash
set -e
function panic() {
echo $@ >&2
exit 1
}
function download() {
if which wget &>/dev/null; then
wget $1 -O $2
elif which curl &>/dev/null; then
curl -L $1 -o $2
else
panic can\'t find wget and curl.
fi
if which md5sum &>/dev/null; then
hash=`md5sum $2 | cut -d ' ' -f 1`
elif which openssl &>/dev/null; then
hash=`openssl md5 -hex $2 | cut -d ' ' -f 2`
else
panic can\'t find hash tool.
fi
[[ "$hash" == "$3" ]] || panic $2: hash not correct, expect $3, got $hash
}
function compile_z() {
if [[ -f libz.a ]]; then
return
fi
rm -rf zlib-1.2.8
download http://zlib.net/zlib-1.2.8.tar.gz zlib-1.2.8.tar.gz 44d667c142d7cda120332623eab69f40
tar xf zlib-1.2.8.tar.gz
cd zlib-1.2.8
CFLAGS='-fPIC' ./configure --static
make
cp libz.a ../
cd ..
}
function compile_bz2() {
if [[ -f libbz2.a ]]; then
return
fi
rm -rf bzip2-1.0.6
download http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz bzip2-1.0.6.tar.gz 00b516f4704d4a7cb50a1d97e6e8e15b
tar xvzf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make CFLAGS='-fPIC -O2 -g -D_FILE_OFFSET_BITS=64'
cp libbz2.a ../
cd ..
}
function compile_snappy() {
if [[ -f libsnappy.a ]]; then
return
fi
rm -rf snappy-1.1.1
download http://pkgs.fedoraproject.org/repo/pkgs/snappy/snappy-1.1.1.tar.gz/8887e3b7253b22a31f5486bca3cbc1c2/snappy-1.1.1.tar.gz snappy-1.1.1.tar.gz 8887e3b7253b22a31f5486bca3cbc1c2
tar xvzf snappy-1.1.1.tar.gz
cd snappy-1.1.1
./configure --with-pic --enable-static
make
mv .libs/libsnappy.a ../
cd ..
}
function compile_lz4() {
if [[ -f liblz4.a ]]; then
return
fi
rm -rf lz4-r127
download https://github.com/Cyan4973/lz4/archive/r131.tar.gz lz4-r131.tar.gz 42b09fab42331da9d3fb33bd5c560de9
tar xvzf lz4-r131.tar.gz
cd lz4-r131/lib
make CFLAGS='-fPIC' all
mv liblz4.a ../../
cd ../..
}
function compile_rocksdb() {
if [[ -f librocksdb.a ]]; then
return
fi
version=4.9.fb
echo building rocksdb-$version
rm -rf rocksdb-$version
download https://github.com/facebook/rocksdb/archive/$version.tar.gz rocksdb-$version.tar.gz 75f00635d4dcf0200db54a9244ac5f1d
tar xf rocksdb-$version.tar.gz
cd rocksdb-$version
EXTRA_CFLAGS="-fPIC -I./zlib-1.2.8 -I./bzip2-1.0.6 -I./snappy-1.1.1 -I./lz4-r127/lib" EXTRA_CXXFLAGS="$EXTRA_CFLAGS" make static_lib
mv librocksdb.a ../
}
if [[ $# -ne 1 ]]; then
panic $0 [compile_bz2|compile_z|compile_lz4|compile_rocksdb|compile_snappy]
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