-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.rs
More file actions
410 lines (371 loc) · 12.4 KB
/
build.rs
File metadata and controls
410 lines (371 loc) · 12.4 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::env;
use std::path::{Path, PathBuf};
const PREBUILT_LIB_DIR: &str = ".cache/lbug-prebuilt/lib";
fn link_mode() -> &'static str {
if env::var("LBUG_SHARED").is_ok() {
"dylib"
} else {
"static"
}
}
fn get_target() -> String {
env::var("PROFILE").unwrap()
}
fn link_libraries(link_bundled_deps: bool, prebuilt_lbug_root: Option<&Path>) {
// This also needs to be set by any crates using it if they want to use extensions
if !cfg!(windows) && link_mode() == "static" {
println!("cargo:rustc-link-arg=-rdynamic");
}
if cfg!(windows) && link_mode() == "dylib" {
println!("cargo:rustc-link-lib=dylib=lbug_shared");
} else if link_mode() == "dylib" {
println!("cargo:rustc-link-lib={}=lbug", link_mode());
} else if rustversion::cfg!(since(1.82)) {
println!("cargo:rustc-link-lib=static:+whole-archive=lbug");
} else {
println!("cargo:rustc-link-lib=static=lbug");
}
if link_mode() == "static" {
if cfg!(windows) {
println!("cargo:rustc-link-lib=dylib=msvcrt");
println!("cargo:rustc-link-lib=dylib=shell32");
println!("cargo:rustc-link-lib=dylib=ole32");
} else if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=dylib=c++");
} else {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
if !link_bundled_deps {
if let Some(lbug_root) = prebuilt_lbug_root {
link_prebuilt_libraries(lbug_root);
}
return;
}
for lib in [
"utf8proc",
"antlr4_cypher",
"antlr4_runtime",
"re2",
"fastpfor",
"parquet",
"thrift",
"snappy",
"zstd",
"miniz",
"mbedtls",
"brotlidec",
"brotlicommon",
"lz4",
"roaring_bitmap",
"simsimd",
"yyjson",
] {
if rustversion::cfg!(since(1.82)) {
println!("cargo:rustc-link-lib=static:+whole-archive={lib}");
} else {
println!("cargo:rustc-link-lib=static={lib}");
}
}
}
}
fn link_prebuilt_libraries(lbug_root: &Path) {
let build_dir = lbug_root.join("build").join("release");
for (dir, lib) in [
("antlr4_cypher", "antlr4_cypher"),
("antlr4_runtime", "antlr4_runtime"),
("brotli", "brotlidec"),
("brotli", "brotlicommon"),
("utf8proc", "utf8proc"),
("re2", "re2"),
("fastpfor", "fastpfor"),
("parquet", "parquet"),
("snappy", "snappy"),
("thrift", "thrift"),
("yyjson", "yyjson"),
("zstd", "zstd"),
("miniz", "miniz"),
("mbedtls", "mbedtls"),
("lz4", "lz4"),
("roaring_bitmap", "roaring_bitmap"),
("simsimd", "simsimd"),
] {
let lib_dir = build_dir.join("third_party").join(dir);
let lib_path = lib_dir.join(format!("lib{lib}.a"));
if lib_path.exists() {
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib=static={lib}");
}
}
}
fn manifest_dir() -> PathBuf {
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
}
fn static_lbug_file_name() -> &'static str {
if cfg!(windows) {
"lbug.lib"
} else {
"liblbug.a"
}
}
fn try_download_prebuilt_lbug(manifest_dir: &Path) -> bool {
if link_mode() != "static" {
return false;
}
if env::var("LBUG_BUILD_FROM_SOURCE").is_ok() || env::var("LBUG_RUST_BUILD_FROM_SOURCE").is_ok()
{
println!("cargo:warning=Skipping prebuilt liblbug because source build was requested");
return false;
}
let lib_dir = manifest_dir.join(PREBUILT_LIB_DIR);
let lib_path = lib_dir.join(static_lbug_file_name());
if lib_path.exists() {
return true;
}
let script = manifest_dir.join("scripts").join("download_lbug.sh");
if !script.exists() {
return false;
}
println!("cargo:warning=Downloading prebuilt liblbug static archive...");
let status = std::process::Command::new("sh")
.arg(&script)
.current_dir(manifest_dir)
.status();
match status {
Ok(status) if status.success() && lib_path.exists() => true,
Ok(status) => {
println!(
"cargo:warning=Prebuilt liblbug download failed with status {status}; building from source"
);
false
}
Err(error) => {
println!(
"cargo:warning=Could not run prebuilt liblbug downloader ({error}); building from source"
);
false
}
}
}
fn use_prebuilt_lbug(manifest_dir: &Path) -> Option<(Vec<PathBuf>, PathBuf)> {
if !try_download_prebuilt_lbug(manifest_dir) {
return None;
}
let lib_dir = manifest_dir.join(PREBUILT_LIB_DIR);
let lbug_root = get_lbug_root();
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rerun-if-changed={}", lib_dir.display());
println!(
"cargo:warning=Using prebuilt liblbug from {}",
lib_dir.join(static_lbug_file_name()).display()
);
Some((vec![lib_dir, lbug_root.join("src/include")], lbug_root))
}
fn get_lbug_root() -> PathBuf {
let manifest_dir = manifest_dir();
if let Ok(lbug_source_dir) = std::env::var("LBUG_SOURCE_DIR") {
let root = PathBuf::from(lbug_source_dir);
if root.is_symlink() || root.is_dir() {
return root;
}
}
let sibling_root = manifest_dir.join("../ladybug");
if sibling_root.is_symlink() || sibling_root.is_dir() {
return sibling_root;
}
let bundled_root = manifest_dir.join("lbug-src");
if bundled_root.is_symlink() || bundled_root.is_dir() {
return bundled_root;
}
if cfg!(windows) {
return manifest_dir.join("../..");
}
let lbug_dir = manifest_dir.join("lbug-src");
if !lbug_dir.exists() {
let version = std::env::var("LBUG_VERSION").unwrap_or_else(|_| "main".to_string());
println!("Downloading ladybug source version {version}...");
let url = if version.starts_with('v') {
format!(
"https://github.com/LadybugDB/ladybug/archive/refs/tags/{}.tar.gz",
version
)
} else if version == "main" {
"https://github.com/LadybugDB/ladybug/archive/refs/heads/main.tar.gz".to_string()
} else {
format!(
"https://github.com/LadybugDB/ladybug/archive/refs/tags/v{}.tar.gz",
version
)
};
let output = std::process::Command::new("curl")
.args(["-sL", &url])
.arg("-o")
.arg("ladybug.tar.gz")
.current_dir(&manifest_dir)
.output()
.expect("Failed to download ladybug source");
if !output.status.success() {
panic!("Failed to download ladybug source from {}", url);
}
std::fs::create_dir_all(&lbug_dir).expect("Failed to create lbug-src directory");
std::process::Command::new("tar")
.args([
"-xzf",
"ladybug.tar.gz",
"--strip-components=1",
"-C",
"lbug-src",
])
.current_dir(&manifest_dir)
.status()
.expect("Failed to extract ladybug source");
std::fs::remove_file(manifest_dir.join("ladybug.tar.gz")).ok();
}
lbug_dir
}
fn build_bundled_cmake() -> Vec<PathBuf> {
let lbug_root = get_lbug_root();
let mut build = cmake::Config::new(&lbug_root);
build
.no_build_target(true)
.define("BUILD_SHELL", "OFF")
.define("BUILD_SINGLE_FILE_HEADER", "OFF")
.define("AUTO_UPDATE_GRAMMAR", "OFF");
if cfg!(windows) {
build.generator("Ninja");
build.cxxflag("/EHsc");
build.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL");
build.define("CMAKE_POLICY_DEFAULT_CMP0091", "NEW");
}
if let Ok(jobs) = std::env::var("NUM_JOBS") {
std::env::set_var("CMAKE_BUILD_PARALLEL_LEVEL", jobs);
}
let build_dir = build.build();
let lbug_lib_path = build_dir.join("build").join("src");
println!("cargo:rustc-link-search=native={}", lbug_lib_path.display());
for dir in [
"utf8proc",
"antlr4_cypher",
"antlr4_runtime",
"re2",
"brotli",
"alp",
"fastpfor",
"parquet",
"thrift",
"snappy",
"zstd",
"miniz",
"mbedtls",
"lz4",
"roaring_bitmap",
"simsimd",
"yyjson",
] {
let lib_path = build_dir
.join("build")
.join("third_party")
.join(dir)
.canonicalize()
.unwrap_or_else(|_| {
panic!(
"Could not find {}/build/third_party/{}",
build_dir.display(),
dir
)
});
println!("cargo:rustc-link-search=native={}", lib_path.display());
}
vec![
lbug_root.join("src/include"),
build_dir.join("build/src"),
build_dir.join("build/src/include"),
lbug_root.join("third_party/nlohmann_json"),
lbug_root.join("third_party/fastpfor"),
lbug_root.join("third_party/alp/include"),
]
}
fn build_ffi(
bridge_file: &str,
out_name: &str,
source_file: &str,
bundled: bool,
include_paths: &Vec<PathBuf>,
) {
let mut build = cxx_build::bridge(bridge_file);
build.file(source_file);
if bundled {
build.define("LBUG_BUNDLED", None);
}
if get_target() == "debug" || get_target() == "relwithdebinfo" {
build.define("ENABLE_RUNTIME_CHECKS", "1");
}
if link_mode() == "static" {
build.define("LBUG_STATIC_DEFINE", None);
}
build.includes(include_paths);
println!("cargo:rerun-if-env-changed=LBUG_SHARED");
println!("cargo:rerun-if-changed=include/lbug_rs.h");
println!("cargo:rerun-if-changed=src/lbug_rs.cpp");
// Note that this should match the lbug-src/* entries in the package.include list in Cargo.toml
// Unfortunately they appear to need to be specified individually since the symlink is
// considered to be changed each time.
println!("cargo:rerun-if-changed=lbug-src/src");
println!("cargo:rerun-if-changed=lbug-src/cmake");
println!("cargo:rerun-if-changed=lbug-src/third_party");
println!("cargo:rerun-if-changed=lbug-src/CMakeLists.txt");
println!("cargo:rerun-if-changed=lbug-src/tools/CMakeLists.txt");
if cfg!(windows) {
build.flag("/std:c++20");
build.flag("/MD");
} else {
build.flag("-std=c++2a");
}
build.compile(out_name);
}
fn main() {
if env::var("DOCS_RS").is_ok() {
// Do nothing; we're just building docs and don't need the C++ library
return;
}
let manifest_dir = manifest_dir();
let mut bundled = false;
let mut link_bundled_deps = false;
let mut prebuilt_lbug_root = None;
let mut include_paths = vec![manifest_dir.join("include")];
if let (Ok(lbug_lib_dir), Ok(lbug_include)) =
(env::var("LBUG_LIBRARY_DIR"), env::var("LBUG_INCLUDE_DIR"))
{
println!("cargo:rustc-link-search=native={lbug_lib_dir}");
println!("cargo:rustc-link-arg=-Wl,-rpath,{lbug_lib_dir}");
include_paths.push(Path::new(&lbug_include).to_path_buf());
} else if let Some((prebuilt_include_paths, lbug_root)) = use_prebuilt_lbug(&manifest_dir) {
include_paths.extend(prebuilt_include_paths);
prebuilt_lbug_root = Some(lbug_root);
} else {
include_paths.extend(build_bundled_cmake());
bundled = true;
link_bundled_deps = true;
}
if link_mode() == "static" {
link_libraries(link_bundled_deps, prebuilt_lbug_root.as_deref());
}
build_ffi(
"src/ffi.rs",
"lbug_rs",
"src/lbug_rs.cpp",
bundled,
&include_paths,
);
if cfg!(feature = "arrow") {
build_ffi(
"src/ffi/arrow.rs",
"lbug_arrow_rs",
"src/lbug_arrow.cpp",
bundled,
&include_paths,
);
}
if link_mode() == "dylib" {
link_libraries(link_bundled_deps, prebuilt_lbug_root.as_deref());
}
}