| 1 |
fn main() { |
| 2 |
println!("cargo:rustc-check-cfg=cfg(use_instrument_hooks)"); |
| 3 |
|
| 4 |
println!("cargo:rerun-if-changed=instrument-hooks/dist/core.c"); |
| 5 |
println!("cargo:rerun-if-changed=instrument-hooks/includes/core.h"); |
| 6 |
println!("cargo:rerun-if-changed=build.rs"); |
| 7 |
|
| 8 |
collect_rustc_info(); |
| 9 |
collect_cargo_info(); |
| 10 |
collect_build_profile_info(); |
| 11 |
|
| 12 |
let mut build = cc::Build::new(); |
| 13 |
build |
| 14 |
.flag("-std=c11") |
| 15 |
.file("instrument-hooks/dist/core.c") |
| 16 |
.include("instrument-hooks/includes") |
| 17 |
// We generated the C code from Zig, which contains some warnings |
| 18 |
// that can be safely ignored. |
| 19 |
.flag("-Wno-format") |
| 20 |
.flag("-Wno-format-security") |
| 21 |
.flag("-Wno-unused-but-set-variable") |
| 22 |
.flag("-Wno-unused-const-variable") |
| 23 |
.flag("-Wno-type-limits") |
| 24 |
.flag("-Wno-uninitialized") |
| 25 |
// Ignore warnings when cross-compiling: |
| 26 |
.flag("-Wno-overflow") |
| 27 |
.flag("-Wno-unused-function") |
| 28 |
.flag("-Wno-constant-conversion") |
| 29 |
.flag("-Wno-incompatible-pointer-types") |
| 30 |
// Disable warnings, as we will have lots of them |
| 31 |
.warnings(false) |
| 32 |
.extra_warnings(false) |
| 33 |
.cargo_warnings(false) |
| 34 |
.opt_level(3); |
| 35 |
|
| 36 |
let result = build.try_compile("instrument_hooks"); |
| 37 |
match result { |
| 38 |
Ok(_) => println!("cargo:rustc-cfg=use_instrument_hooks"), |
| 39 |
Err(e) => { |
| 40 |
let compiler = build.try_get_compiler().expect("Failed to get C compiler"); |
| 41 |
|
| 42 |
eprintln!("\n\nWARNING: Failed to compile instrument-hooks native library with cc-rs."); |
| 43 |
eprintln!("The library will still compile, but instrument-hooks functionality will be disabled."); |
| 44 |
eprintln!("Compiler information: {compiler:?}"); |
| 45 |
eprintln!("Compilation error: {e}\n"); |
| 46 |
|
| 47 |
println!("cargo:warning=Failed to compile instrument-hooks native library with cc-rs. Continuing with noop implementation."); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// Collect build profile and performance-relevant compiler options at build time. |
| 53 |
/// These env var names must be kept in sync with `src/instrument_hooks/mod.rs`. |
| 54 |
/// |
| 55 |
/// Reference: <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts> |
| 56 |
fn collect_build_profile_info() { |
| 57 |
// PROFILE is set by Cargo to the name of the profile being built (e.g. "release", "bench", "debug"). |
| 58 |
if let Ok(profile) = std::env::var("PROFILE") { |
| 59 |
println!("cargo:rustc-env=CODSPEED_BUILD_PROFILE={profile}"); |
| 60 |
} |
| 61 |
|
| 62 |
// OPT_LEVEL is set by Cargo to the optimization level (0, 1, 2, 3, s, z). |
| 63 |
if let Ok(opt_level) = std::env::var("OPT_LEVEL") { |
| 64 |
println!("cargo:rustc-env=CODSPEED_BUILD_OPT_LEVEL={opt_level}"); |
| 65 |
} |
| 66 |
|
| 67 |
// Extract performance-relevant codegen flags from rustflags. |
| 68 |
// |
| 69 |
// Cargo uses two mutually exclusive sources for rustflags (see |
| 70 |
// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads>): |
| 71 |
// - RUSTFLAGS env var (space-separated, takes priority) |
| 72 |
// - CARGO_ENCODED_RUSTFLAGS env var (0x1f-separated, used when RUSTFLAGS is not set) |
| 73 |
// We check both in priority order to accurately represent the flags used to build. |
| 74 |
let rustflags: Vec<String> = if let Ok(raw) = std::env::var("RUSTFLAGS") { |
| 75 |
raw.split_whitespace().map(String::from).collect() |
| 76 |
} else if let Ok(encoded) = std::env::var("CARGO_ENCODED_RUSTFLAGS") { |
| 77 |
encoded.split('\x1f').map(String::from).collect() |
| 78 |
} else { |
| 79 |
Vec::new() |
| 80 |
}; |
| 81 |
|
| 82 |
if let Some(target_cpu) = extract_codegen_flag(&rustflags, "target-cpu") { |
| 83 |
println!("cargo:rustc-env=CODSPEED_BUILD_TARGET_CPU={target_cpu}"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/// Collect rustc toolchain info at build time and expose as env vars. |
| 88 |
/// These env var names must be kept in sync with `src/instrument_hooks/mod.rs`. |
| 89 |
fn collect_rustc_info() { |
| 90 |
let Ok(output) = std::process::Command::new("rustc") |
| 91 |
.args(["--version", "--verbose"]) |
| 92 |
.output() |
| 93 |
else { |
| 94 |
return; |
| 95 |
}; |
| 96 |
if !output.status.success() { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 101 |
for line in stdout.lines() { |
| 102 |
if let Some(rest) = line.strip_prefix("rustc ") { |
| 103 |
println!("cargo:rustc-env=CODSPEED_RUSTC_VERSION={rest}"); |
| 104 |
} else if let Some((key, value)) = line.split_once(':') { |
| 105 |
let key = key.trim(); |
| 106 |
let value = value.trim(); |
| 107 |
let env_key = match key { |
| 108 |
"release" => Some("CODSPEED_RUSTC_RELEASE"), |
| 109 |
"LLVM version" => Some("CODSPEED_RUSTC_LLVM_VERSION"), |
| 110 |
_ => None, |
| 111 |
}; |
| 112 |
if let Some(env_key) = env_key { |
| 113 |
println!("cargo:rustc-env={env_key}={value}"); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/// Collect cargo version at build time and expose as an env var. |
| 120 |
/// This env var name must be kept in sync with `src/instrument_hooks/mod.rs`. |
| 121 |
fn collect_cargo_info() { |
| 122 |
let Ok(output) = std::process::Command::new("cargo") |
| 123 |
.arg("--version") |
| 124 |
.output() |
| 125 |
else { |
| 126 |
return; |
| 127 |
}; |
| 128 |
if !output.status.success() { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 133 |
if let Some(rest) = stdout.trim().strip_prefix("cargo ") { |
| 134 |
println!("cargo:rustc-env=CODSPEED_CARGO_VERSION={rest}"); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/// Extract a codegen option value from a list of rustc flags. |
| 139 |
/// |
| 140 |
/// Handles all three forms that rustc accepts: |
| 141 |
/// - `-Ckey=value` (no space) |
| 142 |
/// - `-C` `key=value` (space between -C and the option, split into two entries) |
| 143 |
/// - `-C` `key` `value` (space between key and value, split into three entries) — only for flags that accept it |
| 144 |
/// |
| 145 |
/// Returns the value for the first match of `key`. |
| 146 |
fn extract_codegen_flag(flags: &[String], key: &str) -> Option<String> { |
| 147 |
let mut iter = flags.iter(); |
| 148 |
while let Some(flag) = iter.next() { |
| 149 |
// Form: -Ckey=value |
| 150 |
if let Some(rest) = flag.strip_prefix("-C") { |
| 151 |
if let Some(value) = rest.strip_prefix(key).and_then(|s| s.strip_prefix('=')) { |
| 152 |
return Some(value.to_string()); |
| 153 |
} |
| 154 |
// rest is not our key, skip |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
// Form: -C key=value or -C key value |
| 159 |
if flag == "-C" { |
| 160 |
if let Some(next) = iter.next() { |
| 161 |
if let Some(value) = next.strip_prefix(key).and_then(|s| s.strip_prefix('=')) { |
| 162 |
return Some(value.to_string()); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
None |
| 168 |
} |