Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "rocksdb-sys"
version = "0.2.2"
authors = ["Jeremy Fitzhardinge <[email protected]>"]
license = "MIT/Apache-2.0/BSD-3-Clause"
description = "Native bindings to the rocksdb library"
description = "Native bindings to the RocksDB library"
readme = "README.md"
repository = "https://github.com/jsgf/rocksdb-sys.git"
keywords = [ "ffi", "rocksdb" ]
Expand Down
17 changes: 8 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ fn main() {
cmd.arg(format!("-j{}", jobs));
}

cmd.arg("install-static");
cmd.arg("install");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, rocksdb was using its own allocator (tcmalloc), which conflicted with jemalloc used by Rust. The only way I could resolve it was by statically linking rocksdb into the crate so that it uses the Rust allocator.

Also I prefer static linking to avoid more dependencies for the executable. The c<->rust binding is pretty fragile, and I'm sure that the RocksDB ABI doesn't change from version to version, so independently updating rocksdb from the Rust code using it seems risky.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. The problem with static linking is that there is no static libstdc++ on OS X...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, can you statically link rocksdb and dynamically link libstdc++? I think that's what happens on Linux.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid you can't dynamically link to a library from a static library. Unless you do it at run-time (rather than load-time), using dlopen or something. This is definitely a problem, although I see why you wanted to avoid bringing two different allocators into play.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid you can't dynamically link to a library from a static library.

Sure, but that's not quite what I'm talking about.

RocksDB is written in C++, so it has a dependency on libstdc++, but the library itself (the .rlib) doesn't have a direct dependency on it - it just needs to make sure when your executable is finally linked it includes a linkage to libstdc++. And because the rest of RocksDB is static, it effectively becomes part of your main executable.

I believe that rustc uses clang++ as its linker on Mac OS, so the requirement for libstdc++ will be automatically fulfilled.

Have you tried this and had it fail? If so, how did it fail?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been told rustc uses ld as the linker, in fact, but not 100% sure. Anyway, I got lots of "undefined symbol" errors if I tried to link it statically.


match cmd.output() {
Ok(out) => if !out.status.success() {
let _ = writeln!(&mut stderr, "build failed:\nstdout:\n{}\nstderr:\n{}",
String::from_utf8(out.stdout).unwrap(), String::from_utf8(out.stderr).unwrap());
let _ = writeln!(&mut stderr, "build failed:\nstdout:\n{}\nstderr:\n{}", String::from_utf8(out.stdout).unwrap(), String::from_utf8(out.stderr).unwrap());
exit(1);
},
Err(e) => { let _ = writeln!(&mut stderr, "command execution failed: {:?}", e); exit(1) }
}

let config = match File::open("rocksdb/make_config.mk") {
Ok(c) => c,
Err(e) => { let _ = writeln!(&mut stderr, "Failed to open rocksdb/make_config.mk: {}", e); exit(1) }
Err(e) => { let _ = writeln!(&mut stderr, "Failed to open `rocksdb/make_config.mk`: {}", e); exit(1) }
};
let config = BufReader::new(config);

Expand All @@ -47,21 +46,21 @@ fn main() {
let words: Vec<_> = line.split_whitespace().collect();

if !words[0].starts_with("PLATFORM_LDFLAGS=") {
continue
continue;
}

lz4 = words.iter().any(|w| *w == "-llz4");
snappy = words.iter().any( |w| *w == "-lsnappy");
zlib = words.iter().any(|w| *w == "-lz");
bzip2 = words.iter().any(|w| *w == "-lbz2");
break;
}

println!("cargo:rustc-link-search=native={}/lib", out_dir);
println!("cargo:rustc-link-lib=static=rocksdb");
println!("cargo:rustc-link-lib=stdc++");
println!("cargo:rustc-link-lib=rocksdb");
if lz4 { println!("cargo:rustc-link-lib=lz4"); }
if snappy { println!("cargo:rustc-link-lib=snappy"); }
if zlib { println!("cargo:rustc-link-lib=z"); }
if bzip2 { println!("cargo:rustc-link-lib=bz2"); }
println!("cargo:rustc-link-lib=stdc++");
}
2 changes: 1 addition & 1 deletion rocksdb
Submodule rocksdb updated 799 files
Loading