-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
306 lines (280 loc) · 10.4 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Couchbase Lite C API bindings generator
//
// Copyright (c) 2020 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This script runs during a Cargo build and generates the raw/unsafe Rust bindings, "bindings.rs",
// in an internal build directory, where they are included by `src/c_api.rs`.
//
// References:
// - https://rust-lang.github.io/rust-bindgen/tutorial-3.html
// - https://doc.rust-lang.org/cargo/reference/build-scripts.html
extern crate bindgen;
extern crate fs_extra;
use std::env;
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use fs_extra::dir;
static CBL_INCLUDE_DIR: &str = "libcblite-3.0.3/include";
static CBL_LIB_DIR: &str = "libcblite-3.0.3/lib";
fn main() -> Result<(), Box<dyn Error>> {
generate_bindings()?;
configure_rustc()?;
// Bypass copying libraries when the build script is called in a cargo check context.
if env::var("ONLY_CARGO_CHECK").unwrap_or_default() != *"true" {
copy_lib().unwrap_or_else(|_| {
panic!(
"can't copy cblite libs, is '{}' a supported target?",
env::var("TARGET").unwrap_or_default()
)
});
}
Ok(())
}
fn bindgen_for_mac(builder: bindgen::Builder) -> Result<bindgen::Builder, Box<dyn Error>> {
let target = env::var("TARGET")?;
if !target.contains("apple") {
return Ok(builder);
}
let sdk = if target.contains("ios") {
if target.contains("x86") || target.contains("sim") {
"iphonesimulator"
} else {
"iphoneos"
}
} else {
"macosx"
};
let sdk = String::from_utf8(
Command::new("xcrun")
.args(["--sdk", sdk, "--show-sdk-path"])
.output()
.expect("failed to execute process")
.stdout,
)?;
Ok(builder.clang_arg(format!("-isysroot{}", sdk.trim())))
}
#[allow(dead_code)]
enum OperatingSystem {
MacOs,
Windows,
Android,
IOs,
}
fn is_target(target: OperatingSystem) -> Result<bool, Box<dyn Error>> {
let target_os = env::var("CARGO_CFG_TARGET_OS")?;
Ok(match target {
OperatingSystem::Android => target_os.contains("android"),
OperatingSystem::Windows => target_os.contains("windows"),
OperatingSystem::IOs => target_os.contains("apple-ios"),
OperatingSystem::MacOs => target_os.contains("apple-darwin"),
})
}
fn is_host(host: OperatingSystem) -> Result<bool, Box<dyn Error>> {
let host_os = env::var("HOST")?;
Ok(match host {
OperatingSystem::MacOs => host_os.contains("apple-darwin"),
OperatingSystem::Windows => host_os.contains("windows"),
OperatingSystem::Android => host_os.contains("android"),
OperatingSystem::IOs => host_os.contains("apple-ios"),
})
}
fn generate_bindings() -> Result<(), Box<dyn Error>> {
let mut bindings = bindgen_for_mac(bindgen::Builder::default())?
.header("src/wrapper.h")
.clang_arg(format!("-I{}", CBL_INCLUDE_DIR));
// Fix cross-compilation from MacOS to Android targets.
// The following clang_arg calls prevent bindgen from trying to include
// MacOS standards headers and returning an error when trying to generate bindings.
// Basically, we specifiy NDK sysroot and usr/include dirs depending on the target arch.
//
// Sample of errors:
//
// /Applications/Xcode.app/.../Developer/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
// /Applications/Xcode.app/.../Developer/SDKs/MacOSX10.15.sdk/usr/include/machine/_types.h:34:2: error: architecture not supported
// FTR: https://github.com/rust-lang/rust-bindgen/issues/1780
if is_host(OperatingSystem::MacOs)? && is_target(OperatingSystem::Android)? {
let ndk_sysroot = format!(
"{}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot",
env::var("NDK_HOME")?,
);
let target_triplet =
if env::var("CARGO_CFG_TARGET_ARCH").expect("Can't read target arch value!") == "arm" {
"arm-linux-androideabi"
} else {
"aarch64-linux-android"
};
bindings = bindings
.clang_arg(format!("--sysroot={}", ndk_sysroot))
.clang_arg(format!("-I{}/usr/include", ndk_sysroot))
.clang_arg(format!("-I{}/usr/include/{}", ndk_sysroot, target_triplet))
.clang_arg(format!("--target={}", target_triplet));
}
// Cross compiling from Mac to Windows
if is_host(OperatingSystem::MacOs)? && is_target(OperatingSystem::Windows)? {
let homebrew_prefix = Command::new("brew")
.arg("--prefix")
.arg("mingw-w64")
.output()?
.stdout;
let homebrew_prefix =
String::from_utf8_lossy(&homebrew_prefix[..homebrew_prefix.len() - 1]);
let mingw_path = format!("{homebrew_prefix}/toolchain-x86_64/x86_64-w64-mingw32");
let mingw_include_path = format!("{mingw_path}/include");
bindings = bindings
.clang_arg(format!("-I{}", mingw_include_path))
.clang_arg(format!("--target={}", "x86_64-pc-windows-gnu"));
}
let out_dir = env::var("OUT_DIR")?;
bindings
.allowlist_type("CBL.*")
.allowlist_type("FL.*")
.allowlist_var("k?CBL.*")
.allowlist_var("k?FL.*")
.allowlist_function("CBL.*")
.allowlist_function("_?FL.*")
.no_copy("FLSliceResult")
.size_t_is_usize(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings")
.write_to_file(PathBuf::from(out_dir).join("bindings.rs"))
.expect("Couldn't write bindings!");
Ok(())
}
fn configure_rustc() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=src/wrapper.h");
println!("cargo:rerun-if-changed={}", CBL_INCLUDE_DIR);
println!("cargo:rerun-if-changed={}", CBL_LIB_DIR);
println!("cargo:rustc-link-search={}", env::var("OUT_DIR")?);
let target_dir = env::var("TARGET")?;
let target_os = env::var("CARGO_CFG_TARGET_OS")?;
if target_os == "ios" {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("Can't read target_arch");
let ios_framework = match target_arch.as_str() {
"aarch64" => "ios-arm64",
"x86_64" => "ios-arm64_x86_64-simulator",
_ => panic!("Unsupported ios target"),
};
println!(
"cargo:rustc-link-search=framework={}/{}/ios/CouchbaseLite.xcframework/{}",
env!("CARGO_MANIFEST_DIR"),
CBL_LIB_DIR,
ios_framework,
);
println!("cargo:rustc-link-lib=framework=CouchbaseLite");
} else if target_os == "macos" {
println!("cargo:rustc-link-lib=dylib=cblite");
println!(
"cargo:rustc-link-search={}/{}/macos",
env!("CARGO_MANIFEST_DIR"),
CBL_LIB_DIR
);
} else {
println!("cargo:rustc-link-lib=dylib=cblite");
println!(
"cargo:rustc-link-search={}/{}/{}",
env!("CARGO_MANIFEST_DIR"),
CBL_LIB_DIR,
target_dir
);
}
Ok(())
}
pub fn copy_lib() -> Result<(), Box<dyn Error>> {
let target_os = env::var("CARGO_CFG_TARGET_OS")?;
let lib_path = PathBuf::from(format!(
"{}/{}/{}/",
env!("CARGO_MANIFEST_DIR"),
CBL_LIB_DIR,
if target_os == "ios" {
"ios".to_string()
} else if target_os == "macos" {
"macos".to_string()
} else {
env::var("TARGET").unwrap()
}
));
let dest_path = PathBuf::from(format!("{}/", env::var("OUT_DIR")?));
match target_os.as_str() {
"android" => {
fs::copy(
lib_path.join("libcblite.stripped.so"),
dest_path.join("libcblite.so"),
)?;
}
"ios" => {
let mut copy_options = dir::CopyOptions::new();
copy_options.overwrite = true;
dir::copy(
lib_path.join("CouchbaseLite.xcframework"),
dest_path,
©_options,
)?;
}
"linux" => {
fs::copy(
lib_path.join("libcblite.so.3"),
dest_path.join("libcblite.so.3"),
)?;
fs::copy(
lib_path.join("libicudata.so.66"),
dest_path.join("libicudata.so.66"),
)?;
fs::copy(
lib_path.join("libicui18n.so.66"),
dest_path.join("libicui18n.so.66"),
)?;
fs::copy(
lib_path.join("libicuio.so.66"),
dest_path.join("libicuio.so.66"),
)?;
fs::copy(
lib_path.join("libicutu.so.66"),
dest_path.join("libicutu.so.66"),
)?;
fs::copy(
lib_path.join("libicuuc.so.66"),
dest_path.join("libicuuc.so.66"),
)?;
// Needed only for build, not required for run
fs::copy(
lib_path.join("libcblite.so.3"),
dest_path.join("libcblite.so"),
)?;
}
"macos" => {
fs::copy(
lib_path.join("libcblite.3.dylib"),
dest_path.join("libcblite.3.dylib"),
)?;
// Needed only for build, not required for run
fs::copy(
lib_path.join("libcblite.3.dylib"),
dest_path.join("libcblite.dylib"),
)?;
}
"windows" => {
fs::copy(lib_path.join("cblite.dll"), dest_path.join("cblite.dll"))?;
// Needed only for build, not required for run
fs::copy(lib_path.join("cblite.lib"), dest_path.join("cblite.lib"))?;
}
_ => {
panic!("Unsupported target: {}", env::var("CARGO_CFG_TARGET_OS")?);
}
}
Ok(())
}