Skip to content

Commit

Permalink
Auto merge of rust-lang#125642 - khuey:zstd, r=<try>
Browse files Browse the repository at this point in the history
Enable zstd for debug compression.

Set LLVM_ENABLE_ZSTD alongside LLVM_ENABLE_ZLIB so that --compress-debug-sections=zstd is an option.

See rust-lang#120953
  • Loading branch information
bors committed Jul 17, 2024
2 parents fcc325f + 86f954d commit 8186fdd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
29 changes: 26 additions & 3 deletions compiler/rustc_llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,32 @@ fn main() {
cmd.args(&components);

for lib in output(&mut cmd).split_whitespace() {
let mut is_static = false;
let name = if let Some(stripped) = lib.strip_prefix("-l") {
stripped
} else if let Some(stripped) = lib.strip_prefix('-') {
stripped
} else if Path::new(lib).exists() {
// On MSVC llvm-config will print the full name to libraries, but
// we're only interested in the name part
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
name.trim_end_matches(".lib")
// On Unix when we get a static library llvm-config will print the
// full name and we *are* interested in the path, but we need to
// handle it separately. For example, when statically linking to
// libzstd llvm-config will output something like
// -lrt -ldl -lm -lz /usr/local/lib/libzstd.a -lxml2
// and we transform the zstd part into
// cargo:rustc-link-search-native=/usr/local/lib
// cargo:rustc-link-lib=static=zstd
let path = Path::new(lib);
if lib.ends_with(".a") {
is_static = true;
println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
let name = path.file_stem().unwrap().to_str().unwrap();
name.trim_start_matches("lib")
} else {
let name = path.file_name().unwrap().to_str().unwrap();
name.trim_end_matches(".lib")
}
} else if lib.ends_with(".lib") {
// Some MSVC libraries just come up with `.lib` tacked on, so chop
// that off
Expand All @@ -285,7 +302,13 @@ fn main() {
continue;
}

let kind = if name.starts_with("LLVM") { llvm_kind } else { "dylib" };
let kind = if name.starts_with("LLVM") {
llvm_kind
} else if is_static {
"static"
} else {
"dylib"
};
println!("cargo:rustc-link-lib={kind}={name}");
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/download-ci-llvm-stamp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Change this file to make users of the `download-ci-llvm` configuration download
a new version of LLVM from CI, even if the LLVM submodule hasn’t changed.

Last change is for: https://github.com/rust-lang/rust/pull/126298
Last change is for: https://github.com/rust-lang/rust/pull/125642
19 changes: 10 additions & 9 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,6 @@ impl Step for Llvm {
cfg.define("LLVM_PROFDATA_FILE", path);
}

// Disable zstd to avoid a dependency on libzstd.so.
cfg.define("LLVM_ENABLE_ZSTD", "OFF");

if !target.is_windows() {
cfg.define("LLVM_ENABLE_ZLIB", "ON");
} else {
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
}

// Are we compiling for iOS/tvOS/watchOS/visionOS?
if target.contains("apple-ios")
|| target.contains("apple-tvos")
Expand Down Expand Up @@ -833,6 +824,16 @@ fn configure_llvm(builder: &Builder<'_>, target: TargetSelection, cfg: &mut cmak
}
}

// Libraries for ELF section compression.
if !target.is_windows() {
cfg.define("LLVM_ENABLE_ZLIB", "ON");
cfg.define("LLVM_ENABLE_ZSTD", "ON");
cfg.define("LLVM_USE_STATIC_ZSTD", "TRUE");
} else {
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
}

if let Some(ref linker) = builder.config.llvm_use_linker {
cfg.define("LLVM_USE_LINKER", linker);
}
Expand Down
1 change: 1 addition & 0 deletions src/ci/docker/host-x86_64/x86_64-gnu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
pkg-config \
xz-utils \
mingw-w64 \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*

COPY scripts/sccache.sh /scripts/
Expand Down
4 changes: 4 additions & 0 deletions tests/run-make/rust-lld-compress-debug-sections/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510
// see https://github.com/rust-lang/compiler-team/issues/510 for more info

fn main() {}
39 changes: 39 additions & 0 deletions tests/run-make/rust-lld-compress-debug-sections/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Checks the `compress-debug-sections` option on rust-lld.

//@ needs-rust-lld
//@ only-linux
//@ ignore-cross-compile

// FIXME: This test isn't comprehensive and isn't covering all possible combinations.

use run_make_support::{assert_contains, cmd, llvm_readobj, run_in_tmpdir, rustc};

fn check_compression(compression: &str, to_find: &str) {
run_in_tmpdir(|| {
let out = rustc()
.arg("-Zlinker-features=+lld")
.arg("-Clink-self-contained=+linker")
.arg("-Zunstable-options")
.arg("-Cdebuginfo=full")
.link_arg(&format!("-Wl,--compress-debug-sections={compression}"))
.input("main.rs")
.run_unchecked();
let stderr = out.stderr_utf8();
if stderr.is_empty() {
llvm_readobj().arg("-t").arg("main").run().assert_stdout_contains(to_find);
} else {
assert_contains(
stderr,
format!(
"\
LLVM was not built with LLVM_ENABLE_{to_find} or did not find {compression} at build time"
),
);
}
});
}

fn main() {
check_compression("zlib", "ZLIB");
check_compression("zstd", "ZSTD");
}

0 comments on commit 8186fdd

Please sign in to comment.