-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
build.rs
80 lines (73 loc) · 2.65 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
// build.rs
#![forbid(unexpected_cfgs)]
fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os != "ios" {
cc::Build::new().file("src/gettls.c").compile("libgettls.a");
}
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
// Force linking against libc++
if target_family == "unix" {
println!("cargo:rustc-link-lib=dylib=c++");
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=test_harness.cpp");
println!("cargo:rerun-if-changed=src/gettls.c");
// Build the test harness
// clang++ -shared -fPIC -O0 -o test_harness.so test_harness.cpp
// Check if we have clang++ installed
if target_family == "windows" {
let compiler = cc::Build::new()
.cpp(true)
.file("test_harness.cpp")
.get_compiler();
let mut cmd = std::process::Command::new(compiler.path());
let cmd = cmd
.args(compiler.args())
.arg("test_harness.cpp")
.arg("/link");
#[cfg(unix)]
let cmd = cmd
.arg(format!(
"/libpath:{}/.cache/cargo-xwin/xwin/crt/lib/x86_64/",
std::env::var("HOME").unwrap()
))
.arg(format!(
"/libpath:{}/.cache/cargo-xwin/xwin/sdk/lib/ucrt/x86_64/",
std::env::var("HOME").unwrap()
))
.arg(format!(
"/libpath:{}/.cache/cargo-xwin/xwin/sdk/lib/um/x86_64/",
std::env::var("HOME").unwrap()
));
cmd.arg("/dll").arg("/OUT:test_harness.dll");
let output = cmd.output().expect("Failed to link test_harness.dll");
let output_str = format!(
"{:?}\nstatus: {}\nstdout: {}\nstderr: {}",
cmd,
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
// std::fs::write("compiler_output.txt", output_str.clone()).expect("Unable to write file");
assert!(
output.status.success(),
"Failed to link test_harness.dll\n {:?}",
output_str.as_str()
);
} else {
let compiler = cc::Build::new()
.cpp(true)
.opt_level(0)
.shared_flag(true)
.get_compiler();
let clangpp = compiler.path();
let mut cmd = std::process::Command::new(clangpp);
cmd.args(compiler.args())
.arg("test_harness.cpp")
.arg("-o")
.arg("test_harness.so")
.status()
.expect("Failed to link test_harness");
}
}