diff --git a/.cargo/config.toml b/.cargo/config.toml index 90c10b2b..d54f0c96 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,22 +1,42 @@ -[target."x86_64-unknown-linux-gnu"] -# - On systems that do not use lld as the system linker (such as Solus) using lld directly saves about a second -# of build time for incremental compiles for building boulder (from 2.191s to 1.198s on my machine). -# - Compressing debug symbols with zstd shrinks the dev profile boulder binary from 206.03MB to 81.44MB, a 124.59MB -# or ~60% savings. It doesn't affect the binary size for packaging builds since we strip those, but the debug symbols -# are reduced in size from 113.16MB to 34.63MB. It adds about ~152ms to the build times which is less than we gained -# by switching to lld -# - The new symbol mangling format (https://doc.rust-lang.org/rustc/symbol-mangling/v0.html) improves the backtrace -# shown by RUST_BACKTRACE=1 and other debug utilities. It should also be helpful once we have ABI reports. Upstream -# hasn't switched to it yet by default due to stable distros not having new enough tools, but that doesn't matter for us +# Having a way to detect on which system we are compiled means we can get +# away with adding rustflags here that we know are present in the system +# toolchain builds. +# +# We can set these extra flags via matching on a target cfg() expression. +# +# - On systems that do not use lld as the system linker (such as Solus) using +# lld directly saves about a second of build time for incremental compiles +# for building boulder (from 2.191s to 1.198s on Reilly's machine). +# +# - In testing, compression of debug symbols with zstd shrinks the dev profile +# boulder binary from 206.03MB to 81.44MB, a 124.59MB or ~60% savings. +# It doesn't affect the binary size for packaging builds since we strip those, +# but the debug symbols are reduced in size from 113.16MB to 34.63MB. +# It adds about ~152ms to the build times which is less than we gained by +# switching to lld. This feature requires a compiler compiled with support +# for zstd debug symbols. +# +# - The new symbol mangling format[1] improves the backtrace shown by +# RUST_BACKTRACE=1 and other debug utilities. It should also be helpful once +# we have ABI reports. Upstream hasn't switched to it yet by default due to +# stable distros not having new enough tools, but that doesn't matter for us +# [1]: https://doc.rust-lang.org/rustc/symbol-mangling/v0.html +# + +# NB: os_release patterns need to be added to both target configs for this to +# work... +# +# The Solus toolchain supports zstd debug sections currently (Serpent doesn't) +[target.'cfg(os_release_id = "solus")'] rustflags = [ "-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--compress-debug-sections=zstd", "-Csymbol-mangling-version=v0", ] -[target."aarch64-unknown-linux-gnu"] +# Default flags +[target.'cfg(not(os_release_id = "solus"))'] rustflags = [ "-Clink-arg=-fuse-ld=lld", - "-Clink-arg=-Wl,--compress-debug-sections=zstd", "-Csymbol-mangling-version=v0", ] diff --git a/Cargo.lock b/Cargo.lock index 1ece4eb6..e9d83e3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -229,6 +229,7 @@ dependencies = [ "derive_more", "dirs", "elf", + "etc-os-release", "futures", "glob", "hex", @@ -794,6 +795,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "etc-os-release" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3ab3b560ce06f55db2a56928ad3e051680eb1995feb21c855619e08cb7f0a2b" +dependencies = [ + "indexmap", + "thiserror", +] + [[package]] name = "fastrand" version = "2.1.0" @@ -1405,6 +1416,7 @@ dependencies = [ "derive_more", "diesel", "diesel_migrations", + "etc-os-release", "fnmatch", "futures", "hex", diff --git a/Cargo.toml b/Cargo.toml index ec40977f..7d817cad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ diesel = { version = "2.2.1", features = ["sqlite", "returning_clauses_for_sqlit diesel_migrations = "2.2.0" dirs = "5.0.1" elf = "0.7.4" +etc-os-release = "0.1.0" indicatif = "0.17.8" itertools = "0.13.0" futures = "0.3.30" @@ -54,6 +55,19 @@ url = { version = "2.5.2", features = ["serde"] } xxhash-rust = { version = "0.8.11", features = ["xxh3"] } zstd = { version = "0.13.2", features = ["zstdmt"] } +# We want people who use the onboarding steps to get a nice compromise +# between fast compilation and fast runtime, but with checks in place +# and full backtraces. Hyperfine tests shows opt-level = 1 to be a good +# compromise between compile speed and runtime speed. +# During testing, opt-level = 2 caused a non-trivial slowdown in compilation +# iteration speed, but also sped up execution times commensurably. /ermo +[profile.onboarding] +inherits = "dev" +opt-level = 1 +lto = "thin" +debug = true +strip = "none" + [profile.release] lto = "thin" @@ -66,13 +80,4 @@ opt-level = 3 strip = "none" debug = true -# We want people who use the onboarding steps to get a nice compromise -# between fast compilation and fast runtime, but with checks in place -# and full backtraces. Hyperfine tests shows opt-level = 1 to be a good -# compromise between compile speed and runtime speed. -[profile.onboarding] -inherits = "dev" -opt-level = 1 -lto = "thin" -debug = true -strip = "none" +# NB: Consult .cargo/config.toml to read more about the conditional [target] rustflags we use! diff --git a/boulder/Cargo.toml b/boulder/Cargo.toml index 3ae914e5..f3971030 100644 --- a/boulder/Cargo.toml +++ b/boulder/Cargo.toml @@ -43,3 +43,6 @@ strum.workspace = true thiserror.workspace = true tokio.workspace = true url.workspace = true + +[build-dependencies] +etc-os-release.workspace = true diff --git a/boulder/build.rs b/boulder/build.rs new file mode 100644 index 00000000..5e2e4597 --- /dev/null +++ b/boulder/build.rs @@ -0,0 +1,42 @@ +use etc_os_release::OsRelease; +use std::error::Error; + +/// Set cargo::rustc-cfg=os_release_id="whatever" when /etc/os-release warrants it. +/// The intent is to enable trivial conditional compilation via [target.'cfg(...)'] +/// stanzas. +fn main() -> Result<(), Box> { + // only recompile when necessary + println!("cargo::rerun-if-changed=./build.rs"); + + // if /etc/os-release doesn't exist, we have a problem big enough that it's OK to crash + let os_release = OsRelease::open()?; + // this is only here for visibility during compilation + let os_release_id = os_release.id(); + println!("cargo::rustc-cfg=os_release_id=\"{}\"", os_release_id); + + match os_release_id { + "solus" => advanced_link_args(), + "fedora" | "serpentos" => default_link_args(), + _ => conservative_link_args(), + }; + + Ok(()) +} + +fn advanced_link_args() { + println!("-fuse-ld=lld"); + println!("-Wl,--compress-debug-sections=zstd"); + println!("-Csymbol-mangling-version=v0"); +} + +fn default_link_args() { + println!("-fuse-ld=lld"); + //println!("-Wl,--compress-debug-sections=zstd"); + println!("-Csymbol-mangling-version=v0"); +} + +fn conservative_link_args() { + println!("-fuse-ld=lld"); + //println!("-Wl,--compress-debug-sections=zstd"); + //println!("-Csymbol-mangling-version=v0"); +} diff --git a/justfile b/justfile index df7c4e0f..0af6a7bb 100644 --- a/justfile +++ b/justfile @@ -13,7 +13,7 @@ help: [private] build package: - cargo build --profile {{build-mode}} -p {{package}} + cargo build --verbose --profile {{build-mode}} -p {{package}} # Compile boulder boulder: (build "boulder") diff --git a/moss/Cargo.toml b/moss/Cargo.toml index e28045c6..93bb6c29 100644 --- a/moss/Cargo.toml +++ b/moss/Cargo.toml @@ -21,10 +21,10 @@ clap.workspace = true derive_more.workspace = true diesel.workspace = true diesel_migrations.workspace = true -itertools.workspace = true fnmatch = { path = "../crates/fnmatch" } futures.workspace = true hex.workspace = true +itertools.workspace = true libsqlite3-sys.workspace = true log.workspace = true nix.workspace = true @@ -39,6 +39,9 @@ thiserror.workspace = true url.workspace = true xxhash-rust.workspace = true +[build-dependencies] +etc-os-release.workspace = true + [package.metadata.cargo-machete] # Needed for unixepoch() in src/db/state/migrations/2024-03-04-201550_init/up.sql ignored = ["libsqlite3-sys"]