-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
105 lines (93 loc) · 3.16 KB
/
Copy pathbuild.rs
File metadata and controls
105 lines (93 loc) · 3.16 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
use std::{
env,
path::{Path, PathBuf},
};
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let librdb_root = manifest_dir
.join("librdb")
.canonicalize()
.expect("librdb-sys/librdb not found — run `git submodule update --init`");
println!("cargo:rerun-if-changed=wrapper.h");
println!(
"cargo:rerun-if-changed={}",
librdb_root.join("api/librdb-api.h").display()
);
println!(
"cargo:rerun-if-changed={}",
librdb_root.join("api/librdb-ext-api.h").display()
);
for dir in &[
librdb_root.join("src/lib"),
librdb_root.join("src/ext"),
librdb_root.join("deps/redis"),
] {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().is_some_and(|e| e == "c" || e == "h") {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
}
let dynamic = env::var("CARGO_FEATURE_DYNAMIC_LINKING").is_ok();
let r#static = env::var("CARGO_FEATURE_STATIC_LINKING").is_ok();
if dynamic && r#static {
panic!("features `dynamic-linking` and `static-linking` are mutually exclusive");
}
if dynamic {
println!("cargo:rustc-link-lib=rdb");
} else if r#static {
let root = env::var("DEP_LIBRDB_STATIC_ROOT")
.expect("static-linking requires DEP_LIBRDB_STATIC_ROOT env var");
println!("cargo:rustc-link-search=native={root}");
println!("cargo:rustc-link-lib=static=rdb");
} else {
build_from_source(&librdb_root, &out_dir);
}
}
fn build_from_source(librdb_root: &Path, out_dir: &Path) {
let lib_src = librdb_root.join("src/lib");
let ext_src = librdb_root.join("src/ext");
let redis_deps = librdb_root.join("deps/redis");
let mut build = cc::Build::new();
let _ = build
.warnings(false)
.flag("-std=c99")
.flag("-fvisibility=hidden")
.define("NDEBUG", "1")
.include(&lib_src)
.include(&ext_src)
.include(librdb_root.join("src"))
.include(librdb_root.join("api"))
.include(&redis_deps);
for entry in std::fs::read_dir(&lib_src).unwrap() {
let path = entry.unwrap().path();
if path.extension().is_some_and(|e| e == "c") {
let _ = build.file(&path);
}
}
let ext_includes = [
"extCommon.c",
"handlersFilter.c",
"handlersToJson.c",
"handlersToPrint.c",
"handlersToResp.c",
"readerFile.c",
"readerFileDesc.c",
];
for name in &ext_includes {
let _ = build.file(ext_src.join(name));
}
for entry in std::fs::read_dir(&redis_deps).unwrap() {
let path = entry.unwrap().path();
if path.extension().is_some_and(|e| e == "c") {
let _ = build.file(&path);
}
}
build.compile("rdb");
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=rdb");
}