forked from tevador/RandomX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
79 lines (70 loc) · 2.52 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
use std::env;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let target = env::var("TARGET").unwrap();
let n_threads = std::thread::available_parallelism()
.unwrap()
.get()
.to_string();
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let build_dir = &cargo_dir.join("build");
std::fs::create_dir_all(build_dir).unwrap();
env::set_current_dir(build_dir).unwrap();
// Generate CMake cache files
let b = Command::new("cmake")
.arg("-DARCH=native")
.arg("..")
.output()
.expect("Failed to generate Makefile with CMake");
std::io::stdout().write_all(&b.stdout).unwrap();
std::io::stderr().write_all(&b.stderr).unwrap();
assert!(b.status.success());
// Build the library
let b = Command::new("cmake")
.arg("--build")
.arg(".")
.arg("--config")
.arg("Release")
.arg("-j")
.arg(n_threads)
.output()
.expect("Failed to build RandomX library with CMake");
std::io::stdout().write_all(&b.stdout).unwrap();
std::io::stderr().write_all(&b.stderr).unwrap();
assert!(b.status.success());
env::set_current_dir(cargo_dir).unwrap();
// Tell cargo how to find the static library
println!(
"cargo:rustc-link-search=native={}",
build_dir.to_string_lossy()
);
println!("cargo:rustc-link-lib=static=randomx");
if target.contains("apple") {
println!("cargo:rustc-link-lib=dylib=c++");
} else if target.contains("linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
} else {
unimplemented!()
}
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("src/randomx.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}