Skip to main content

miden_node_rocksdb_cxx_linkage_fix/
lib.rs

1//! A temporary solution to missing c++ std library linkage when using a precompile static library
2//!
3//! For more information see: <https://github.com/rust-rocksdb/rust-rocksdb/pull/1029>
4
5use std::env;
6
7pub fn configure() {
8    println!("cargo:rerun-if-env-changed=ROCKSDB_COMPILE");
9    println!("cargo:rerun-if-env-changed=ROCKSDB_LIB_DIR");
10    println!("cargo:rerun-if-env-changed=ROCKSDB_STATIC");
11    println!("cargo:rerun-if-env-changed=CXXSTDLIB");
12    let target = env::var("TARGET").unwrap_or_default();
13    if should_link_cpp_stdlib() {
14        link_cpp_stdlib(&target);
15    }
16}
17
18fn should_compile() -> bool {
19    // in sync with
20    // <https://github.com/rust-rocksdb/rust-rocksdb/blob/master/librocksdb-sys/build.rs#L348-L352>
21    if let Ok(v) = env::var("ROCKSDB_COMPILE") {
22        if v.to_lowercase() == "true" || v == "1" {
23            return true;
24        }
25    }
26    false
27}
28
29fn should_link_cpp_stdlib() -> bool {
30    if should_compile() {
31        return false;
32    }
33    // the value doesn't matter
34    // <https://github.com/rust-rocksdb/rust-rocksdb/blob/master/librocksdb-sys/build.rs#L359>
35    env::var("ROCKSDB_STATIC").is_ok()
36    // `ROCKSDB_LIB_DIR` is not really discriminative, it only adds extra lookup dirs for the linker
37}
38
39fn link_cpp_stdlib(target: &str) {
40    // aligned with
41    // <https://github.com/rust-rocksdb/rust-rocksdb/blob/master/librocksdb-sys/build.rs#L399-L411>
42    if let Ok(stdlib) = env::var("CXXSTDLIB") {
43        println!("cargo:rustc-link-lib=dylib={stdlib}");
44    } else if target.contains("apple") || target.contains("freebsd") || target.contains("openbsd") {
45        println!("cargo:rustc-link-lib=dylib=c++");
46    } else if target.contains("linux") {
47        println!("cargo:rustc-link-lib=dylib=stdc++");
48    } else if target.contains("aix") {
49        println!("cargo:rustc-link-lib=dylib=c++");
50        println!("cargo:rustc-link-lib=dylib=c++abi");
51    }
52}