Skip to content

Commit 120065a

Browse files
committed
build: Set os_release_id="(...)" in build.rs
This enables us to add specific flags known to work on platforms on which we control the toolchain build-time options. Currently, the logic is working well enough that compilation doesn't stop working if you're not on Solus. Tested on fedora. Signed-off-by: Rune Morling <[email protected]>
1 parent 3fbc36b commit 120065a

File tree

7 files changed

+87
-32
lines changed

7 files changed

+87
-32
lines changed

.cargo/config.toml

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
[build]
2-
# - On systems that do not use lld as the system linker (such as Solus) using
3-
# lld directly saves about a second of build time for incremental compiles
4-
# for building boulder (from 2.191s to 1.198s on Reilly's machine).
5-
# - The new symbol mangling format[1] improves the backtrace shown by
6-
# RUST_BACKTRACE=1 and other debug utilities. It should also be helpful once
7-
# we have ABI reports. Upstream hasn't switched to it yet by default due to
8-
# stable distros not having new enough tools, but that doesn't matter for us
9-
# [1]: https://doc.rust-lang.org/rustc/symbol-mangling/v0.html
10-
rustflags = [
11-
"-Clink-arg=-fuse-ld=lld",
12-
"-Csymbol-mangling-version=v0",
13-
]
14-
151
[profile.release]
162
lto = "thin"
173

@@ -20,7 +6,7 @@ lto = "thin"
206
# and full backtraces. Hyperfine tests shows opt-level = 1 to be a good
217
# compromise between compile speed and runtime speed.
228
# During testing, opt-level = 2 caused a non-trivial slowdown in compilation
23-
# iteration speed, but also sped up execution times commensurably.
9+
# iteration speed, but also sped up execution times commensurably. /ermo
2410
[profile.onboarding]
2511
inherits = "dev"
2612
opt-level = 1
@@ -37,29 +23,49 @@ opt-level = 3
3723
strip = "none"
3824
debug = true
3925

40-
# This build profile is for packaging on Serpent, where we control the toolchain.
41-
# Hence, we can get away with adding rustflags here that we know are present
42-
# in the Serpent toolchain builds.
26+
# Having a way to detect on which system we are compiled means we can get
27+
# away with adding rustflags here that we know are present in the Serpent
28+
# toolchain builds.
4329
#
4430
# We can set these extra flags via matching on a target cfg() expression.
4531
#
46-
# This requires that we set a custom cfg value for the Serpent rustc at compile time,
47-
# which the cfg() expression can then match against, so that this target is _only_
48-
# selected for Serpent-native builds
32+
# This requires that we set a custom cfg value for the Serpent rustc at
33+
# compile time, which the cfg() expression can then match against, so that
34+
# this target is _only_ selected for Serpent-native builds
4935
#
50-
# Note that target rustflags take precedence over the build.rustflags specified above.
36+
# - On systems that do not use lld as the system linker (such as Solus) using
37+
# lld directly saves about a second of build time for incremental compiles
38+
# for building boulder (from 2.191s to 1.198s on Reilly's machine).
5139
#
52-
# So in addition to the above [build] settings:
53-
# - Add compression of debug symbols with zstd, which shrinks the dev profile
40+
# - In testing, compression of debug symbols with zstd shrinks the dev profile
5441
# boulder binary from 206.03MB to 81.44MB, a 124.59MB or ~60% savings.
5542
# It doesn't affect the binary size for packaging builds since we strip those,
5643
# but the debug symbols are reduced in size from 113.16MB to 34.63MB.
5744
# It adds about ~152ms to the build times which is less than we gained by
58-
# switching to lld.
45+
# switching to lld. This feature requires a compiler compiled with support
46+
# for zstd debug symbols.
5947
#
60-
# [target.'cfg(all(custom_value = "serpent"))']
61-
# rustflags = [
62-
# "-Clink-arg=-fuse-ld=lld",
63-
# "-Csymbol-mangling-version=v0",
64-
# "-Clink-arg=-Wl,--compress-debug-sections=zstd",
65-
# ]
48+
# - The new symbol mangling format[1] improves the backtrace shown by
49+
# RUST_BACKTRACE=1 and other debug utilities. It should also be helpful once
50+
# we have ABI reports. Upstream hasn't switched to it yet by default due to
51+
# stable distros not having new enough tools, but that doesn't matter for us
52+
# [1]: https://doc.rust-lang.org/rustc/symbol-mangling/v0.html
53+
#
54+
55+
# NB: os_release patterns need to be added to both target configs for this to
56+
# work...
57+
#
58+
# The Solus toolchain supports zstd debug sections currently (Serpent doesn't)
59+
[target.'cfg(any(os_release_id = "solus"))']
60+
rustflags = [
61+
"-Clink-arg=-fuse-ld=lld",
62+
"-Clink-arg=-Wl,--compress-debug-sections=zstd",
63+
"-Csymbol-mangling-version=v0",
64+
]
65+
66+
# Default flags
67+
[target.'cfg(not(any(os_release_id = "solus")))']
68+
rustflags = [
69+
"-Clink-arg=-fuse-ld=lld",
70+
"-Csymbol-mangling-version=v0",
71+
]

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ diesel = { version = "2.2.1", features = ["sqlite", "returning_clauses_for_sqlit
2727
diesel_migrations = "2.2.0"
2828
dirs = "5.0.1"
2929
elf = "0.7.4"
30+
etc-os-release = "0.1.0"
3031
indicatif = "0.17.8"
3132
itertools = "0.13.0"
3233
futures = "0.3.30"

boulder/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ strum.workspace = true
4343
thiserror.workspace = true
4444
tokio.workspace = true
4545
url.workspace = true
46+
47+
[build-dependencies]
48+
etc-os-release.workspace = true

boulder/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use etc_os_release::OsRelease;
2+
use std::error::Error;
3+
use std::path::PathBuf;
4+
5+
/// Set cargo::rustc-cfg=os_release_id="whatever" when /etc/os-release warrants it
6+
fn main() -> Result<(), Box<dyn Error>> {
7+
// only recompile when necessary
8+
let top_level = PathBuf::from("..").canonicalize()?;
9+
println!("cargo::rerun-if-changed={}/build.rs", top_level.display());
10+
// if /etc/os-release doesn't exist, we have a problem big enough that it's OK to crash
11+
let os_release = OsRelease::open()?;
12+
println!("cargo::rustc-cfg=os_release_id=\"{}\"", os_release.id());
13+
14+
Ok(())
15+
}

moss/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ clap.workspace = true
2121
derive_more.workspace = true
2222
diesel.workspace = true
2323
diesel_migrations.workspace = true
24-
itertools.workspace = true
2524
fnmatch = { path = "../crates/fnmatch" }
2625
futures.workspace = true
2726
hex.workspace = true
27+
itertools.workspace = true
2828
libsqlite3-sys.workspace = true
2929
log.workspace = true
3030
nix.workspace = true
@@ -39,6 +39,9 @@ thiserror.workspace = true
3939
url.workspace = true
4040
xxhash-rust.workspace = true
4141

42+
[build-dependencies]
43+
etc-os-release.workspace = true
44+
4245
[package.metadata.cargo-machete]
4346
# Needed for unixepoch() in src/db/state/migrations/2024-03-04-201550_init/up.sql
4447
ignored = ["libsqlite3-sys"]

moss/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use etc_os_release::OsRelease;
2+
use std::error::Error;
3+
use std::path::PathBuf;
4+
5+
/// Set cargo::rustc-cfg=os_release_id="whatever" when /etc/os-release warrants it
6+
fn main() -> Result<(), Box<dyn Error>> {
7+
// only recompile when necessary
8+
let top_level = PathBuf::from("..").canonicalize()?;
9+
println!("cargo::rerun-if-changed={}/build.rs", top_level.display());
10+
// if /etc/os-release doesn't exist, we have a problem big enough that it's OK to crash
11+
let os_release = OsRelease::open()?;
12+
println!("cargo::rustc-cfg=os_release_id=\"{}\"", os_release.id());
13+
14+
Ok(())
15+
}

0 commit comments

Comments
 (0)