Skip to content

Commit 2e43be3

Browse files
committed
Use rerun-if-changed for couchbase-lite-core libraries
to rebuild whole archive if couchbase-lite-core libraries were changed
1 parent 54cb119 commit 2e43be3

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

couchbase-lite-core-sys/build.rs

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ fn main() {
2727
let bdirs = cmake_build_src_dir(&sdir, is_msvc);
2828
println!("build directory: {bdirs:?}\nsource directory {sdir:?}");
2929

30-
if bdirs.is_empty() {
30+
let native_libs_search_paths = if bdirs.is_empty() {
3131
panic!("You didn't specify build directory for couchbase-lite-core");
3232
} else if bdirs.len() == 1 {
33-
specify_library_search_dirs_for_std_layout(&bdirs[0]);
33+
specify_library_search_dirs_for_std_layout(&bdirs[0])
3434
} else {
35-
for d in &bdirs {
36-
println!("cargo:rustc-link-search=native={}", d.display());
37-
}
35+
bdirs
36+
};
37+
for path in &native_libs_search_paths {
38+
println!("cargo:rustc-link-search=native={}", path.display());
3839
}
3940

4041
if cfg!(feature = "use-couchbase-lite-sqlite") {
@@ -43,13 +44,25 @@ fn main() {
4344
if cfg!(feature = "use-couchbase-lite-websocket") {
4445
println!("cargo:rustc-link-lib=static=LiteCoreWebSocket");
4546
}
46-
println!("cargo:rustc-link-lib=static=LiteCoreStatic");
47-
println!("cargo:rustc-link-lib=static=FleeceStatic");
48-
println!("cargo:rustc-link-lib=static=SQLite3_UnicodeSN");
49-
println!("cargo:rustc-link-lib=static=BLIPStatic");
50-
println!("cargo:rustc-link-lib=static=mbedcrypto");
51-
println!("cargo:rustc-link-lib=static=mbedtls");
52-
println!("cargo:rustc-link-lib=static=mbedx509");
47+
for lib in [
48+
"LiteCoreStatic",
49+
"FleeceStatic",
50+
"SQLite3_UnicodeSN",
51+
"BLIPStatic",
52+
"mbedcrypto",
53+
"mbedtls",
54+
"mbedx509",
55+
] {
56+
println!("cargo:rustc-link-lib=static={lib}");
57+
match find_full_library_path(&native_libs_search_paths, lib) {
58+
Ok(full_path) => {
59+
println!("cargo:rerun-if-changed={}", full_path.display());
60+
}
61+
Err(err) => {
62+
panic!("{err}");
63+
}
64+
}
65+
}
5366

5467
if let Ok(icu_lib_path) = env::var("ICU_LIB_DIR") {
5568
println!("cargo:rustc-link-search=native={icu_lib_path}");
@@ -113,35 +126,35 @@ fn main() {
113126
.expect("bindgen failed");
114127
}
115128

116-
fn specify_library_search_dirs_for_std_layout(bdir: &Path) {
117-
println!("cargo:rustc-link-search=native={}", bdir.display());
118-
println!(
119-
"cargo:rustc-link-search=native={}",
120-
bdir.join("vendor").join("fleece").display()
121-
);
122-
println!(
123-
"cargo:rustc-link-search=native={}",
124-
bdir.join("Networking").join("BLIP").display()
125-
);
126-
println!(
127-
"cargo:rustc-link-search=native={}",
128-
bdir.join("vendor").join("sqlite3-unicodesn").display()
129-
);
130-
println!(
131-
"cargo:rustc-link-search=native={}",
129+
fn find_full_library_path(
130+
native_libs_search_paths: &[PathBuf],
131+
lib: &str,
132+
) -> Result<PathBuf, Box<dyn std::error::Error>> {
133+
let static_lib_ext = if win_target() { "lib" } else { "a" };
134+
let prefix = "lib";
135+
let file_name = format!("{prefix}{lib}.{static_lib_ext}");
136+
137+
for path in native_libs_search_paths {
138+
let full_path = path.join(&file_name);
139+
if full_path.exists() {
140+
return Ok(full_path);
141+
}
142+
}
143+
Err(format!("Can no find {file_name} in {native_libs_search_paths:?}").into())
144+
}
145+
146+
fn specify_library_search_dirs_for_std_layout(bdir: &Path) -> Vec<PathBuf> {
147+
vec![
148+
bdir.to_path_buf(),
149+
bdir.join("vendor").join("fleece"),
150+
bdir.join("Networking").join("BLIP"),
151+
bdir.join("vendor").join("sqlite3-unicodesn"),
132152
bdir.join("vendor")
133153
.join("mbedtls")
134154
.join("crypto")
135-
.join("library")
136-
.display()
137-
);
138-
println!(
139-
"cargo:rustc-link-search=native={}",
140-
bdir.join("vendor")
141-
.join("mbedtls")
142-
.join("library")
143-
.display()
144-
);
155+
.join("library"),
156+
bdir.join("vendor").join("mbedtls").join("library"),
157+
]
145158
}
146159

147160
#[cfg(feature = "git-download")]
@@ -817,3 +830,13 @@ impl ParseCallbacks for CollectIncludes {
817830
self.0.lock().unwrap().push(filename.into());
818831
}
819832
}
833+
834+
/// Tells whether we're building for Windows. This is more suitable than a plain
835+
/// `cfg!(windows)`, since the latter does not properly handle cross-compilation
836+
///
837+
/// Note that there is no way to know at compile-time which system we'll be
838+
/// targeting, and this test must be made at run-time (of the build script) See
839+
/// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
840+
fn win_target() -> bool {
841+
env::var("CARGO_CFG_WINDOWS").is_ok()
842+
}

0 commit comments

Comments
 (0)