This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
166 lines (138 loc) · 5.42 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use core::arch::x86_64::__cpuid_count;
use rerun_except::rerun_except;
use std::env;
use std::fs;
use std::os::unix::fs as unix_fs;
use std::path::{Path, PathBuf};
use std::process::Command;
const FEATURE_CHECKS_PATH: &str = "feature_checks";
const C_DEPS_DIR: &str = "c_deps";
const C_DEPS_MAKEFILE: &str = "c_deps.mk";
/// Simple feature check, returning `true` if we have the feature.
///
/// The checks themselves are in files under `FEATURE_CHECKS_PATH`.
fn feature_check(filename: &str, output_file: &str) -> bool {
let mut path = PathBuf::new();
path.push(FEATURE_CHECKS_PATH);
path.push(filename);
let mut check_build = cc::Build::new();
check_build.file(path).try_compile(output_file).is_ok()
}
fn make_c_deps_dir() -> PathBuf {
let out_dir = env::var("OUT_DIR").unwrap();
let mut c_deps_dir = PathBuf::from(out_dir);
c_deps_dir.push(C_DEPS_DIR);
if !c_deps_dir.exists() {
fs::create_dir(&c_deps_dir).unwrap();
let mut dest = c_deps_dir.clone();
dest.push(C_DEPS_MAKEFILE);
let mut src = env::current_dir().unwrap();
src.push(C_DEPS_MAKEFILE);
unix_fs::symlink(src, dest).unwrap();
}
c_deps_dir
}
fn build_libipt(c_deps_dir: &Path) {
eprintln!("Building libipt...");
let prev_dir = env::current_dir().unwrap();
env::set_current_dir(c_deps_dir).unwrap();
let res = Command::new("make")
.arg("-f")
.arg(C_DEPS_MAKEFILE)
.output()
.unwrap_or_else(|_| panic!("Fatal error when building libipt"));
if !res.status.success() {
eprintln!("libipt build failed\n>>> stdout");
eprintln!("stdout: {}", String::from_utf8_lossy(&res.stdout));
eprintln!("\n>>> stderr");
eprintln!("stderr: {}", String::from_utf8_lossy(&res.stderr));
panic!();
}
env::set_current_dir(&prev_dir).unwrap();
}
// We always fetch libipt regardless of if we will build our own libipt. This is becuase there are
// a couple of private CPU configuration files that we need to borrow from libipt.
fn fetch_libipt(c_deps_dir: &Path) {
eprintln!("Fetch libipt...");
let prev_dir = env::current_dir().unwrap();
env::set_current_dir(c_deps_dir).unwrap();
let res = Command::new("make")
.arg("libipt") // target just fetches the code.
.output()
.unwrap_or_else(|_| panic!("Fatal error when fetching libipt"));
if !res.status.success() {
eprintln!("libipt fetch failed\n>>> stdout");
eprintln!("stdout: {}", String::from_utf8_lossy(&res.stdout));
eprintln!("\n>>> stderr");
eprintln!("stderr: {}", String::from_utf8_lossy(&res.stderr));
panic!();
}
env::set_current_dir(&prev_dir).unwrap();
}
// Checks if the CPU supports Intel Processor Trace.
fn cpu_supports_pt() -> bool {
let res = unsafe { __cpuid_count(0x7, 0x0) };
(res.ebx & (1 << 25)) != 0
}
fn main() {
let mut c_build = cc::Build::new();
let c_deps_dir = make_c_deps_dir();
let c_deps_dir_s = c_deps_dir.display();
c_build.file("src/util.c");
// Check if we should build the perf collector.
if cfg!(all(target_os = "linux", target_arch = "x86_64"))
&& feature_check("check_perf.c", "check_perf")
{
c_build.file("src/collect/perf/collect.c");
println!("cargo:rustc-cfg=collector_perf");
}
// FIXME: libipt support is unconditionally built-in for now.
if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
c_build.file("src/decode/libipt/decode.c");
// Decide whether to build our own libipt.
if let Ok(val) = env::var("IPT_PATH") {
let mut inc_path = PathBuf::from(val.clone());
inc_path.push("include");
c_build.include(inc_path);
c_build.flag(&format!("-L{}/lib", val));
println!("cargo:rustc-link-search={}/lib", val);
println!("cargo:rustc-env=PTXED={}/bin/ptxed", val);
} else {
build_libipt(&c_deps_dir);
c_build.include(&format!("{}/inst/include/", c_deps_dir_s));
c_build.flag(&format!("-L{}/inst/lib", c_deps_dir_s));
println!("cargo:rustc-link-search={}/inst/lib", c_deps_dir_s);
println!("cargo:rustc-env=PTXED={}/inst/bin/ptxed", c_deps_dir_s);
}
// We borrow the CPU detection functions from libipt (they are not exposed publicly).
// If we built our own libipt above, then the fetch is a no-op.
fetch_libipt(&c_deps_dir);
c_build.include(&format!("{}/libipt/libipt/internal/include", c_deps_dir_s));
c_build.file(&format!("{}/libipt/libipt/src/pt_cpu.c", c_deps_dir_s));
c_build.file(&format!(
"{}/libipt/libipt/src/posix/pt_cpuid.c",
c_deps_dir_s
));
println!("cargo:rustc-cfg=decoder_libipt");
if cpu_supports_pt() {
println!("cargo:rustc-cfg=decoder_libipt_test");
}
println!("cargo:rustc-link-lib=static=ipt");
}
#[cfg(target_arch = "x86_64")]
println!("cargo:rustc-cfg=decoder_ykpt");
c_build.include("src/util");
c_build.include("src"); // to find `hwtracer_private.h`.
c_build.compile("hwtracer_c");
// Additional circumstances under which to re-run this build.rs.
println!("cargo:rerun-if-env-changed=IPT_PATH");
rerun_except(&[
"README.md",
"deny.toml",
"LICENSE-*",
"COPYRIGHT",
"bors.toml",
".buildbot.sh",
])
.unwrap();
}