Skip to content

Commit fa07d48

Browse files
authored
Merge pull request #29 from Dushistov/static-linking
feature: link couchbase-lite-core statically
2 parents d07343c + 6b07bdd commit fa07d48

File tree

3 files changed

+46
-73
lines changed

3 files changed

+46
-73
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "couchbase-lite-core-sys/couchbase-lite-core"]
22
path = couchbase-lite-core-sys/couchbase-lite-core
3-
url = https://github.com/couchbase/couchbase-lite-core
3+
url = https://github.com/Dushistov/couchbase-lite-core

couchbase-lite-core-sys/build.rs

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bindgen::{Builder, RustTarget};
22
use std::{
3-
env, fs,
3+
env,
44
io::{Read, Write},
55
path::{Path, PathBuf},
66
process::Stdio,
@@ -11,69 +11,51 @@ fn main() {
1111
let dst = cmake::Config::new(Path::new("couchbase-lite-core"))
1212
.define("DISABLE_LTO_BUILD", "True")
1313
.define("SANITIZE_FOR_DEBUG_ENABLED", "False")
14-
.build_target("LiteCore")
14+
.define("ENABLE_TESTING", "False")
15+
.define("LITECORE_BUILD_TESTS", "False")
16+
.build_target("all")
1517
.build()
1618
.join("build");
1719

18-
let out_dir = getenv_unwrap("OUT_DIR");
19-
let out_dir = Path::new(&out_dir);
20-
21-
let lib_name = if cfg!(target_os = "windows") {
22-
"LiteCore.dll"
23-
} else if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
24-
"libLiteCore.dylib"
25-
} else {
26-
"libLiteCore.so"
27-
};
28-
// so it is possible to use from project that uses Rust library,
29-
// but not on Rust language
30-
let target_dir = target_directory(out_dir);
20+
println!("cargo:rustc-link-search=native={}", dst.display());
21+
println!(
22+
"cargo:rustc-link-search=native={}",
23+
dst.join("vendor").join("fleece").display()
24+
);
25+
println!(
26+
"cargo:rustc-link-search=native={}",
27+
dst.join("Networking").join("BLIP").display()
28+
);
29+
println!(
30+
"cargo:rustc-link-search=native={}",
31+
dst.join("vendor").join("sqlite3-unicodesn").display()
32+
);
33+
println!(
34+
"cargo:rustc-link-search=native={}",
35+
dst.join("vendor")
36+
.join("mbedtls")
37+
.join("crypto")
38+
.join("library")
39+
.display()
40+
);
41+
println!("cargo:rustc-link-lib=static=FleeceStatic");
42+
println!("cargo:rustc-link-lib=static=LiteCoreStatic");
43+
println!("cargo:rustc-link-lib=static=SQLite3_UnicodeSN");
44+
println!("cargo:rustc-link-lib=static=BLIPStatic");
45+
println!("cargo:rustc-link-lib=static=Support");
46+
println!("cargo:rustc-link-lib=static=mbedcrypto");
3147

32-
if cfg!(target_os = "windows") {
33-
let msvc_cmake_profile = match &getenv_unwrap("PROFILE")[..] {
34-
"debug" => "Debug",
35-
"release" | "bench" => "Release",
36-
unknown => {
37-
eprintln!(
38-
"Warning: unknown Rust profile={}; defaulting to a release build.",
39-
unknown
40-
);
41-
"Release"
42-
}
43-
};
44-
let dst = dst.join(msvc_cmake_profile);
45-
if let Err(err) = fs::copy(dst.join(lib_name), target_dir.join(lib_name)) {
46-
panic!(
47-
"copy {} from '{}' to '{}' faied: {}",
48-
lib_name,
49-
dst.display(),
50-
target_dir.display(),
51-
err
52-
);
53-
}
54-
let lib_lib_name = "LiteCore.lib";
55-
if let Err(err) = fs::copy(dst.join(lib_lib_name), target_dir.join(lib_lib_name)) {
56-
panic!(
57-
"copy {} from '{}' to '{}' faied: {}",
58-
lib_lib_name,
59-
dst.display(),
60-
target_dir.display(),
61-
err
62-
);
63-
}
64-
} else {
65-
if let Err(err) = fs::copy(dst.join(lib_name), target_dir.join(lib_name)) {
66-
panic!(
67-
"copy {} from '{}' to '{}' faied: {}",
68-
lib_name,
69-
dst.display(),
70-
target_dir.display(),
71-
err
72-
);
73-
}
48+
if cfg!(target_os = "linux") {
49+
println!("cargo:rustc-link-lib=icuuc");
50+
println!("cargo:rustc-link-lib=icui18n");
51+
println!("cargo:rustc-link-lib=icudata");
52+
println!("cargo:rustc-link-lib=z");
53+
} else if cfg!(target_os = "macos") {
54+
println!("cargo:rustc-link-lib=z");
55+
//TODO: remove this dependicies
56+
println!("cargo:rustc-link-lib=framework=CoreFoundation");
57+
println!("cargo:rustc-link-lib=framework=Foundation");
7458
}
75-
println!("cargo:rustc-link-search=native={}", target_dir.display());
76-
println!("cargo:rustc-link-lib=dylib=LiteCore");
7759

7860
let mut includes = vec![
7961
Path::new("couchbase-lite-core").join("C").join("include"),
@@ -92,6 +74,9 @@ fn main() {
9274
includes.append(&mut addon_include_dirs);
9375
framework_dirs.append(&mut addon_framework_dirs);
9476

77+
let out_dir = getenv_unwrap("OUT_DIR");
78+
let out_dir = Path::new(&out_dir);
79+
9580
run_bindgen_for_c_headers(
9681
&target,
9782
&includes,
@@ -120,18 +105,6 @@ fn main() {
120105
.compile("couch_lite_log_retrans");
121106
}
122107

123-
/// Convert something like
124-
/// project/target/release/build/lib-couchbase-lite-core-sys-9daa760ee41fe7b8/out
125-
/// to
126-
/// project/target/release
127-
/// Waiting https://github.com/rust-lang/cargo/issues/5457 for proper mechanizm
128-
fn target_directory(out_dir: &Path) -> &Path {
129-
out_dir
130-
.ancestors()
131-
.nth(3)
132-
.expect("Can not find ancestor during cargo_target_dir search")
133-
}
134-
135108
fn run_bindgen_for_c_headers<P: AsRef<Path>>(
136109
target: &str,
137110
include_dirs: &[P],

0 commit comments

Comments
 (0)