-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
91 lines (79 loc) · 3.05 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
use std::error::Error;
use std::path::Path;
use std::path::PathBuf;
const BINDGEN_OUTPUT_FILENAME: &str = "ctanker.rs";
fn main() -> Result<(), Box<dyn Error>> {
let target_triplet = std::env::var("TARGET")?;
let manifest_dir = std::env::var_os("CARGO_MANIFEST_DIR").unwrap();
let mut bindings_folder = PathBuf::from(manifest_dir);
bindings_folder.push("native");
bindings_folder.push(&target_triplet);
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY")?;
let lib_filename = "libctanker.a";
if !bindings_folder.exists() {
panic!(
"Target platform {} is not supported ({} does not exist)",
target_triplet,
bindings_folder.display()
);
}
if target_family != "windows" && !bindings_folder.join(lib_filename).exists() {
panic!(
"Couldn't find {} in {}",
lib_filename,
bindings_folder.display()
);
}
if !bindings_folder.join(BINDGEN_OUTPUT_FILENAME).exists() {
panic!(
"Couldn't find the bindgen-generated {} in {}",
BINDGEN_OUTPUT_FILENAME,
bindings_folder.display()
);
}
let bindings_folder = bindings_folder.to_str().expect("Invalid character in path");
println!(
"cargo:rerun-if-changed={}/{}",
bindings_folder, BINDGEN_OUTPUT_FILENAME
);
println!(
"cargo:rerun-if-changed={}/{}",
bindings_folder, lib_filename
);
// Paths can contain anything, but env vars are a liiitle more restricted. Sanity checks!
assert!(!bindings_folder.contains(&"="));
assert!(!bindings_folder.contains(&"\0"));
assert!(!bindings_folder.contains(&"\n"));
// Export an env var so we can include ctanker.rs in the source code
println!("cargo:rustc-env=NATIVE_BINDINGS_FOLDER={}", bindings_folder);
// Tell cargo to link with our native library
if target_family != "windows" {
println!("cargo:rustc-link-search={}", bindings_folder);
println!("cargo:rustc-link-lib=static=ctanker",);
match target_triplet.as_str() {
"x86_64-unknown-linux-gnu" => println!("cargo:rustc-link-lib=dylib=stdc++"),
"x86_64-apple-darwin" => {
println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-lib=dylib=c++abi");
}
_ => (),
}
}
if target_family == "windows" {
let build_type = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
let tankersdk_bin_path = format!("native/{}", target_triplet);
let tankersdk_bin_path = Path::new(&tankersdk_bin_path);
let unit_test_path = format!("target/{}/{}/deps/", target_triplet, build_type);
let unit_test_path = Path::new(&unit_test_path);
std::fs::create_dir_all(unit_test_path)?;
let target_path = unit_test_path.join("ctanker.dll");
if !target_path.exists() {
std::fs::copy(tankersdk_bin_path.join("ctanker.dll"), target_path)?;
}
}
Ok(())
}