forked from nervosnetwork/ckb-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
96 lines (81 loc) · 3.33 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
92
93
94
95
96
// Due to this bug: https://github.com/rust-lang/cargo/issues/4866, we cannot
// specify different features based on different targets now in cargo file. We
// have to keep features always on, and do conditional compilation within the
// source code
fn main() {
use std::env;
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let is_windows = target_family == "windows";
let is_unix = target_family == "unix";
let is_x86_64 = target_arch == "x86_64";
let is_aarch64 = target_arch == "aarch64";
let x64_asm = is_x86_64 && (is_windows || is_unix);
let aarch64_asm = is_aarch64 && is_unix;
let can_enable_asm = x64_asm || aarch64_asm;
if cfg!(feature = "asm") && (!can_enable_asm) {
panic!(
"Asm feature is not available for target {} on {}!",
target_arch, target_family
);
}
if cfg!(any(feature = "asm", feature = "detect-asm")) && can_enable_asm {
println!("cargo:rerun-if-changed=src/machine/asm/execute_x64.S");
println!("cargo:rerun-if-changed=src/machine/asm/execute_aarch64.S");
println!("cargo:rerun-if-changed=src/machine/asm/cdefinitions_generated.h");
use cc::Build;
use std::path::Path;
use std::process::Command;
fn run_command(mut c: Command) {
println!("Running Command[{:?}]", c);
let output = c.output().unwrap_or_else(|e| {
panic!("Error running Command[{:?}], error: {:?}", c, e);
});
if !output.status.success() {
use std::io::{self, Write};
io::stdout()
.write_all(&output.stdout)
.expect("stdout write");
io::stderr()
.write_all(&output.stderr)
.expect("stderr write");
panic!(
"Command[{:?}] exits with non-success status: {:?}",
c, output.status
);
}
}
let mut build = Build::new();
if is_windows && x64_asm {
let out_dir = env::var("OUT_DIR").unwrap();
let expand_path = Path::new(&out_dir).join("execute_x64-expanded.S");
let mut expand_command = Command::new("clang");
expand_command
.arg("-E")
.arg("src/machine/asm/execute_x64.S")
.arg("-o")
.arg(&expand_path);
run_command(expand_command);
let compile_path = Path::new(&out_dir).join("execute_x64.o");
let mut compile_command = Command::new("yasm");
compile_command
.arg("-p")
.arg("gas")
.arg("-f")
.arg("x64")
.arg("-m")
.arg("amd64")
.arg(&expand_path)
.arg("-o")
.arg(&compile_path);
run_command(compile_command);
build.object(&compile_path);
} else if x64_asm {
build.file("src/machine/asm/execute_x64.S");
} else if aarch64_asm {
build.file("src/machine/asm/execute_aarch64.S");
}
build.include("src/machine/asm").compile("asm");
println!("cargo:rustc-cfg=has_asm")
}
}