@@ -27,14 +27,15 @@ fn main() {
27
27
let bdirs = cmake_build_src_dir ( & sdir, is_msvc) ;
28
28
println ! ( "build directory: {bdirs:?}\n source directory {sdir:?}" ) ;
29
29
30
- if bdirs. is_empty ( ) {
30
+ let native_libs_search_paths = if bdirs. is_empty ( ) {
31
31
panic ! ( "You didn't specify build directory for couchbase-lite-core" ) ;
32
32
} 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 ] )
34
34
} 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( ) ) ;
38
39
}
39
40
40
41
if cfg ! ( feature = "use-couchbase-lite-sqlite" ) {
@@ -43,13 +44,25 @@ fn main() {
43
44
if cfg ! ( feature = "use-couchbase-lite-websocket" ) {
44
45
println ! ( "cargo:rustc-link-lib=static=LiteCoreWebSocket" ) ;
45
46
}
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
+ }
53
66
54
67
if let Ok ( icu_lib_path) = env:: var ( "ICU_LIB_DIR" ) {
55
68
println ! ( "cargo:rustc-link-search=native={icu_lib_path}" ) ;
@@ -113,35 +126,35 @@ fn main() {
113
126
. expect ( "bindgen failed" ) ;
114
127
}
115
128
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" ) ,
132
152
bdir. join( "vendor" )
133
153
. join( "mbedtls" )
134
154
. 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
+ ]
145
158
}
146
159
147
160
#[ cfg( feature = "git-download" ) ]
@@ -817,3 +830,13 @@ impl ParseCallbacks for CollectIncludes {
817
830
self . 0 . lock ( ) . unwrap ( ) . push ( filename. into ( ) ) ;
818
831
}
819
832
}
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