Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
466 lines (436 loc) · 17.4 KB
/
Copy pathbuild.rs
File metadata and controls
466 lines (436 loc) · 17.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Package tidesdb
// Copyright (C) TidesDB
//
// Licensed under the Mozilla Public License, v. 2.0 (the "License");
//! Links the TidesDB C library.
//!
//! The crate's major version maps to a TidesDB major: crate N.x binds TidesDB (N+9).x, so
//! 1.x binds TidesDB 10.x and 2.x will bind TidesDB 11.x. A system install found through
//! pkg-config is used when it belongs to that major and is at least the bundled release;
//! anything else is ignored with a warning, and the source bundled in `tidesdb/` (a git
//! submodule pinned to `TIDESDB_VERSION`) is built with CMake and linked statically.
use std::path::{Path, PathBuf};
/// The TidesDB release the bindings in src/ffi.rs were written against and the submodule is
/// pinned to. Must match `TIDESDB_VERSION` in src/ffi.rs.
const TIDESDB_VERSION: &str = "10.2.1";
/// Crate major N binds TidesDB major N + this.
const TIDESDB_MAJOR_OFFSET: u64 = 9;
fn feature(name: &str) -> bool {
std::env::var(format!("CARGO_FEATURE_{}", name.to_uppercase())).is_ok()
}
fn on_off(enabled: bool) -> &'static str {
if enabled { "ON" } else { "OFF" }
}
/// `major.minor.patch`, ignoring any suffix; missing parts are 0.
fn parse_version(v: &str) -> Option<(u64, u64, u64)> {
let mut parts = v.trim().split(['.', '-', '+']).map(|p| p.parse::<u64>());
let major = parts.next()?.ok()?;
let minor = parts.next().and_then(|p| p.ok()).unwrap_or(0);
let patch = parts.next().and_then(|p| p.ok()).unwrap_or(0);
Some((major, minor, patch))
}
/// The TidesDB major this crate version binds, checked against `TIDESDB_VERSION`.
fn tidesdb_major() -> u64 {
let crate_version = env!("CARGO_PKG_VERSION");
let crate_major: u64 = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap();
let major = crate_major + TIDESDB_MAJOR_OFFSET;
let bound = parse_version(TIDESDB_VERSION).unwrap().0;
assert_eq!(
major, bound,
"tidesdb crate {crate_version} must bind TidesDB {major}.x (crate N.x binds TidesDB \
(N+{TIDESDB_MAJOR_OFFSET}).x), but build.rs pins TidesDB {TIDESDB_VERSION}. Bump the \
crate major or the pinned release so they agree."
);
major
}
fn bundled_source() -> PathBuf {
let dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("tidesdb");
let cmake_lists = dir.join("CMakeLists.txt");
let Ok(text) = std::fs::read_to_string(&cmake_lists) else {
panic!(
"TidesDB source not found at {}. In a git checkout run \
`git submodule update --init`, or install TidesDB {TIDESDB_VERSION} (or a later \
{}.x) with a pkg-config file.",
dir.display(),
parse_version(TIDESDB_VERSION).unwrap().0
);
};
// project(tidesdb VERSION x.y.z ...) -- the submodule must be checked out at the pinned tag
let found = text
.lines()
.position(|l| l.trim_start().starts_with("project("))
.and_then(|start| {
let call: String = text.lines().skip(start).collect::<Vec<_>>().join(" ");
let call = call.split(')').next()?.to_string();
let mut words = call.split_whitespace().skip_while(|w| *w != "VERSION");
words.nth(1).map(str::to_string)
});
if found.as_deref() != Some(TIDESDB_VERSION) {
panic!(
"the bundled TidesDB source at {} is version {}, but this crate binds TidesDB \
{TIDESDB_VERSION}. Check out the pinned release: \
`git -C tidesdb checkout v{TIDESDB_VERSION}`.",
dir.display(),
found.as_deref().unwrap_or("unknown")
);
}
dir
}
fn build_from_source(src: &Path) -> PathBuf {
let mut cfg = cmake::Config::new(src);
cfg.define("TIDESDB_BUILD_TESTS", "OFF")
.define("TIDESDB_BUILD_BENCH", "OFF")
.define("TIDESDB_BUILD_FUZZERS", "OFF")
.define("TIDESDB_WARN_STRICT", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
// set explicitly: the C build picks a platform-dependent default for Snappy
.define("TIDESDB_WITH_SNAPPY", on_off(feature("snappy")))
.define("TIDESDB_WITH_LZ4", on_off(feature("lz4")))
.define("TIDESDB_WITH_ZSTD", on_off(feature("zstd")))
// allocators only change what the final binary links (see link_allocator), so the
// static archive is the same either way; keep CMake from probing for them
.define("TIDESDB_WITH_JEMALLOC", "OFF")
.define("TIDESDB_WITH_MIMALLOC", "OFF")
.define("TIDESDB_WITH_TCMALLOC", "OFF");
cfg.build()
}
/// A system library: pkg-config names to try, the name to link when none is found, and the
/// package to suggest for Debian/Ubuntu, Homebrew and vcpkg.
struct SysLib {
pkg: &'static [&'static str],
lib: &'static str,
debian: &'static str,
brew: &'static str,
vcpkg: &'static str,
}
/// Link each library through pkg-config, falling back to linking it by name with a warning
/// that lists how to install the ones that were not found.
fn link_system_libs(what: &str, libs: &[SysLib]) {
let missing: Vec<&SysLib> = libs
.iter()
.filter(|l| !l.pkg.iter().any(|p| pkg_config::probe_library(p).is_ok()))
.collect();
for l in &missing {
println!("cargo:rustc-link-lib={}", l.lib);
}
if !missing.is_empty() {
let names: Vec<_> = missing.iter().map(|l| l.lib).collect();
let join = |f: fn(&SysLib) -> &'static str| {
missing.iter().map(|l| f(l)).collect::<Vec<_>>().join(" ")
};
println!(
"cargo:warning=Could not find {what} {} via pkg-config, falling back to link by \
name. If linking fails, install them:\
\n\x20 Debian/Ubuntu: sudo apt install {}\
\n\x20 macOS: brew install {}\
\n\x20 Windows: vcpkg install {}",
names.join(", "),
join(|l| l.debian),
join(|l| l.brew),
join(|l| l.vcpkg),
);
}
}
fn link_compression_deps() {
let mut libs = Vec::new();
if feature("zstd") {
libs.push(SysLib {
pkg: &["libzstd"],
lib: "zstd",
debian: "libzstd-dev",
brew: "zstd",
vcpkg: "zstd:x64-windows",
});
}
if feature("lz4") {
libs.push(SysLib {
pkg: &["liblz4"],
lib: "lz4",
debian: "liblz4-dev",
brew: "lz4",
vcpkg: "lz4:x64-windows",
});
}
if feature("snappy") {
libs.push(SysLib {
pkg: &["snappy"],
lib: "snappy",
debian: "libsnappy-dev",
brew: "snappy",
vcpkg: "snappy:x64-windows",
});
}
link_system_libs("compression libraries", &libs);
}
/// Link the alternative allocator selected by a feature. Like TidesDB's own
/// `TIDESDB_WITH_*ALLOC` CMake options, this only links the allocator: it replaces
/// `malloc`/`free` for the whole process, the Rust side included.
fn link_allocator() {
let selected: Vec<&str> = ["jemalloc", "mimalloc", "tcmalloc"]
.into_iter()
.filter(|f| feature(f))
.collect();
if selected.len() > 1 {
panic!(
"the tidesdb features {} each replace the process allocator; enable at most one",
selected.join(", ")
);
}
let Some(&alloc) = selected.first() else {
return;
};
let windows = std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows");
if windows && alloc != "mimalloc" {
panic!("the tidesdb `{alloc}` feature is not supported on Windows; use `mimalloc`");
}
let lib = match alloc {
"jemalloc" => SysLib {
pkg: &["jemalloc"],
lib: "jemalloc",
debian: "libjemalloc-dev",
brew: "jemalloc",
vcpkg: "jemalloc:x64-windows",
},
"mimalloc" => SysLib {
pkg: &["mimalloc"],
lib: "mimalloc",
debian: "libmimalloc-dev",
brew: "mimalloc",
vcpkg: "mimalloc:x64-windows",
},
_ => SysLib {
pkg: &["libtcmalloc", "libtcmalloc_minimal"],
lib: "tcmalloc",
debian: "libgoogle-perftools-dev",
brew: "gperftools",
vcpkg: "gperftools:x64-windows",
},
};
link_system_libs("allocator", &[lib]);
}
/// Use a TidesDB found through pkg-config if it belongs to the bound major and is at least the
/// bundled release. Returns the version linked, or `None` to build the bundled source.
/// The `.pc` file pkg-config resolves `name` to.
fn pc_file(name: &str) -> Option<PathBuf> {
let pkg_config = std::env::var("PKG_CONFIG").unwrap_or_else(|_| "pkg-config".into());
let out = std::process::Command::new(pkg_config)
.args(["--variable=pcfiledir", name])
.output()
.ok()
.filter(|o| o.status.success())?;
let dir = String::from_utf8_lossy(&out.stdout).trim().to_string();
let pc = PathBuf::from(dir).join(format!("{name}.pc"));
pc.exists().then_some(pc)
}
fn system_tidesdb(major: u64) -> Option<String> {
if std::env::var_os("TIDESDB_NO_PKG_CONFIG").is_some() {
return None;
}
// link metadata only once the version is accepted, but rerun when PKG_CONFIG_* changes
let found = pkg_config::Config::new()
.cargo_metadata(false)
.env_metadata(true)
.probe("tidesdb")
.ok()?;
// rebuild when the installed TidesDB changes (an upgrade rewrites its .pc file)
if let Some(pc) = pc_file("tidesdb") {
println!("cargo:rerun-if-changed={}", pc.display());
}
let crate_version = env!("CARGO_PKG_VERSION");
let crate_major = major - TIDESDB_MAJOR_OFFSET;
let usable = match parse_version(&found.version) {
Some(v) => v.0 == major && v >= parse_version(TIDESDB_VERSION).unwrap(),
None => false,
};
if !usable {
println!(
"cargo:warning=Ignoring the system TidesDB {} found via pkg-config: tidesdb crate \
{crate_version} binds TidesDB {major}.x (at least {TIDESDB_VERSION}). Crate \
{crate_major}.x maps to TidesDB {major}.x, crate {}.x to TidesDB {}.x. Building the \
bundled TidesDB {TIDESDB_VERSION} instead.",
found.version,
crate_major + 1,
major + 1,
);
return None;
}
// probe again, this time emitting the link metadata
let lib = pkg_config::Config::new()
.range_version(found.version.as_str()..=found.version.as_str())
.probe("tidesdb")
.ok()?;
if lib.version != TIDESDB_VERSION {
println!(
"cargo:warning=Linking the system TidesDB {} found via pkg-config. The bindings \
were written against TidesDB {TIDESDB_VERSION}; set TIDESDB_NO_PKG_CONFIG=1 to \
build the bundled {TIDESDB_VERSION} instead.",
lib.version
);
}
if feature("snappy") || feature("lz4") || feature("zstd") {
println!(
"cargo:warning=The tidesdb compression features do not change a system TidesDB: it \
has whichever codecs it was built with. Check CompressionAlgorithm::is_available."
);
}
Some(lib.version)
}
/// The width of the C `time_t` on the target, in bits, asked of the target's C compiler.
///
/// It is not fixed per target: 32-bit glibc targets have a 32-bit `time_t` by default but a
/// 64-bit one where the toolchain defines `_TIME_BITS=64` (Debian and Ubuntu armhf since the
/// 64-bit time transition), while Rust's `libc::time_t` stays 32-bit. `time_t` is a field of
/// `tidesdb_commit_op_t` and a TTL parameter, so the bindings must match the compiler that
/// built TidesDB. Each candidate is checked with a compile-time assertion, so this also works
/// when cross-compiling.
fn c_time_t_bits() -> u32 {
let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());
for bits in [64u32, 32] {
let probe = out.join(format!("time_t_{bits}.c"));
std::fs::write(
&probe,
format!(
"#include <time.h>\n\
typedef char tidesdb_time_t_probe[sizeof(time_t) == {} ? 1 : -1];\n",
bits / 8
),
)
.unwrap();
let compiled = cc::Build::new()
.file(&probe)
.cargo_metadata(false)
.cargo_warnings(false)
.warnings(false)
.try_compile_intermediates();
if compiled.is_ok() {
return bits;
}
}
panic!("could not determine the size of the C time_t for this target");
}
/// The platform the crate is being built for, from Cargo's target cfg. A build script's own
/// `#[cfg(...)]` describes the machine running the build, which differs when cross-compiling.
struct Target {
os: String,
family: String,
env: String,
pointer_width: String,
/// Whether the build runs on the target platform itself (not cross-compiling)
native: bool,
}
impl Target {
fn from_env() -> Self {
let var = |k: &str| std::env::var(k).unwrap_or_default();
Target {
os: var("CARGO_CFG_TARGET_OS"),
family: var("CARGO_CFG_TARGET_FAMILY"),
env: var("CARGO_CFG_TARGET_ENV"),
pointer_width: var("CARGO_CFG_TARGET_POINTER_WIDTH"),
native: var("HOST") == var("TARGET"),
}
}
}
/// The system libraries the static TidesDB needs, following the per-platform link recipe in
/// TidesDB's CMakeLists.txt.
fn link_platform_libs(target: &Target) {
let link = |lib: &str| println!("cargo:rustc-link-lib={lib}");
match target.os.as_str() {
// MSVC uses the native Win32 threads in compat.h; MinGW links winpthreads
"windows" => {
if target.env == "gnu" {
link("pthread");
}
}
// Bionic has pthreads and libm in libc and ships no libpthread
"android" => link("m"),
"macos" | "ios" | "freebsd" | "openbsd" | "netbsd" | "dragonfly" | "solaris"
| "illumos" => {
link("m");
link("pthread");
}
_ if target.family == "unix" => {
link("pthread");
link("m");
// 64-bit atomics on 32-bit targets (ARMv7, PowerPC, MIPS, x86) live in libatomic
if target.pointer_width == "32" {
link("atomic");
}
// Snappy is C++; its C++ runtime comes along when Snappy is enabled
if feature("snappy") {
link("stdc++");
}
}
_ => {}
}
}
/// Library directories of the build machine's package managers, where the codec and
/// allocator libraries usually live. Only added for native builds: when cross-compiling
/// they hold libraries for the wrong platform, and the target's libraries are found through
/// pkg-config (`PKG_CONFIG_SYSROOT_DIR`, `PKG_CONFIG_ALLOW_CROSS`) or `RUSTFLAGS="-L ..."`.
fn link_host_search_paths(target: &Target) {
if !target.native {
return;
}
let search = |dir: &str| println!("cargo:rustc-link-search=native={dir}");
match target.os.as_str() {
"macos" => match brew_prefix() {
Some(prefix) => search(&format!("{prefix}/lib")),
None => {
search("/opt/homebrew/lib");
search("/usr/local/lib");
}
},
"windows" => {
// MSYS2 shells set MINGW_PREFIX as a POSIX path (/mingw64); the native linker
// needs it under the default MSYS2 root
if target.env == "gnu" {
let prefix = std::env::var("MINGW_PREFIX").unwrap_or_else(|_| "/mingw64".into());
match prefix.strip_prefix('/') {
Some(rest) => search(&format!("C:/msys64/{rest}/lib")),
None => search(&format!("{prefix}/lib")),
}
}
}
"netbsd" => search("/usr/pkg/lib"),
"solaris" | "illumos" => search("/opt/ooce/lib/amd64"),
_ => search("/usr/local/lib"),
}
}
fn brew_prefix() -> Option<String> {
std::process::Command::new("brew")
.arg("--prefix")
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tidesdb/include/db.h");
println!("cargo:rerun-if-changed=tidesdb/CMakeLists.txt");
println!("cargo:rerun-if-env-changed=TIDESDB_NO_PKG_CONFIG");
println!("cargo:rerun-if-env-changed=MINGW_PREFIX");
println!("cargo:rustc-check-cfg=cfg(tidesdb_system)");
println!("cargo:rustc-check-cfg=cfg(tidesdb_time_t_32)");
let major = tidesdb_major();
let target = Target::from_env();
if c_time_t_bits() == 32 {
println!("cargo:rustc-cfg=tidesdb_time_t_32");
}
if let Some(version) = system_tidesdb(major) {
println!("cargo:rustc-cfg=tidesdb_system");
println!("cargo:rustc-env=TIDESDB_LINKED_VERSION={version}");
link_allocator();
link_host_search_paths(&target);
return;
}
let dst = build_from_source(&bundled_source());
println!("cargo:rustc-env=TIDESDB_LINKED_VERSION={TIDESDB_VERSION}");
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-search=native={}/lib64", dst.display());
println!("cargo:rustc-link-lib=static=tidesdb");
link_compression_deps();
link_allocator();
link_platform_libs(&target);
link_host_search_paths(&target);
}