Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
50 lines (44 loc) · 1.99 KB
/
Copy pathbuild.rs
File metadata and controls
50 lines (44 loc) · 1.99 KB
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
use std::{env, io, process::Command};
fn main() -> io::Result<()> {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
let version = Command::new(rustc).arg("-vV").output()?;
let nightly = String::from_utf8_lossy(&version.stdout)
.lines()
.any(|line| {
line.starts_with("release:")
&& (line.contains("-nightly") || line.contains("-dev"))
});
println!("cargo::rustc-check-cfg=cfg(nightly)");
if nightly {
println!("cargo::rustc-cfg=nightly");
}
// These flags only apply to the microfetch binary, not to proc-macro crates
// or other host-compiled artifacts.
// macOS cannot link statically, requires the standard C runtime startup, and
// uses Mach-O (not ELF), so none of the flags below apply. The default
// linker driver produces a correct (and ad-hoc code-signed) binary there.
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if target_os == "macos" {
return Ok(());
}
// No C runtime, we provide _start ourselves
println!("cargo:rustc-link-arg-bin=microfetch=-nostartfiles");
// Static PIE, relocated by `entry_rust`.
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,-static");
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--no-dynamic-linker");
// Remove unreferenced input sections
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--gc-sections");
// Strip all symbol table entries
let debug = env::var("DEBUG").as_deref() == Ok("true");
if !debug {
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--strip-all");
}
// Omit the .note.gnu.build-id section, which split debug info is found by
let build_id = if debug { "sha1" } else { "none" };
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--build-id={build_id}");
// Nothing unwinds with panic=abort, so skip the unwinder's lookup table
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--no-eh-frame-hdr");
// Disable RELRO (removes relro_padding)
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,-z,norelro");
Ok(())
}