Skip to content

Commit 9dd7dd4

Browse files
committed
Auto merge of rust-lang#125642 - khuey:zstd, r=<try>
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 try-job: x86_64-gnu
2 parents cb12b52 + 36ccc08 commit 9dd7dd4

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

compiler/rustc_llvm/build.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,32 @@ fn main() {
259259
cmd.args(&components);
260260

261261
for lib in output(&mut cmd).split_whitespace() {
262+
let mut is_static = false;
262263
let name = if let Some(stripped) = lib.strip_prefix("-l") {
263264
stripped
264265
} else if let Some(stripped) = lib.strip_prefix('-') {
265266
stripped
266267
} else if Path::new(lib).exists() {
267268
// On MSVC llvm-config will print the full name to libraries, but
268269
// we're only interested in the name part
269-
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
270-
name.trim_end_matches(".lib")
270+
// On Unix when we get a static library llvm-config will print the
271+
// full name and we *are* interested in the path, but we need to
272+
// handle it separately. For example, when statically linking to
273+
// libzstd llvm-config will output something like
274+
// -lrt -ldl -lm -lz /usr/local/lib/libzstd.a -lxml2
275+
// and we transform the zstd part into
276+
// cargo:rustc-link-search-native=/usr/local/lib
277+
// cargo:rustc-link-lib=static=zstd
278+
let path = Path::new(lib);
279+
if lib.ends_with(".a") {
280+
is_static = true;
281+
println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
282+
let name = path.file_stem().unwrap().to_str().unwrap();
283+
name.trim_start_matches("lib")
284+
} else {
285+
let name = path.file_name().unwrap().to_str().unwrap();
286+
name.trim_end_matches(".lib")
287+
}
271288
} else if lib.ends_with(".lib") {
272289
// Some MSVC libraries just come up with `.lib` tacked on, so chop
273290
// that off
@@ -285,7 +302,13 @@ fn main() {
285302
continue;
286303
}
287304

288-
let kind = if name.starts_with("LLVM") { llvm_kind } else { "dylib" };
305+
let kind = if name.starts_with("LLVM") {
306+
llvm_kind
307+
} else if is_static {
308+
"static"
309+
} else {
310+
"dylib"
311+
};
289312
println!("cargo:rustc-link-lib={kind}={name}");
290313
}
291314

src/bootstrap/download-ci-llvm-stamp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Change this file to make users of the `download-ci-llvm` configuration download
22
a new version of LLVM from CI, even if the LLVM submodule hasn’t changed.
33

4-
Last change is for: https://github.com/rust-lang/rust/pull/126298
4+
Last change is for: https://github.com/rust-lang/rust/pull/125642

src/bootstrap/src/core/build_steps/llvm.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,6 @@ impl Step for Llvm {
377377
cfg.define("LLVM_PROFDATA_FILE", path);
378378
}
379379

380-
// Disable zstd to avoid a dependency on libzstd.so.
381-
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
382-
383-
if !target.is_windows() {
384-
cfg.define("LLVM_ENABLE_ZLIB", "ON");
385-
} else {
386-
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
387-
}
388-
389380
// Are we compiling for iOS/tvOS/watchOS/visionOS?
390381
if target.contains("apple-ios")
391382
|| target.contains("apple-tvos")
@@ -833,6 +824,16 @@ fn configure_llvm(builder: &Builder<'_>, target: TargetSelection, cfg: &mut cmak
833824
}
834825
}
835826

827+
// Libraries for ELF section compression.
828+
if !target.is_windows() {
829+
cfg.define("LLVM_ENABLE_ZLIB", "ON");
830+
cfg.define("LLVM_ENABLE_ZSTD", "ON");
831+
cfg.define("LLVM_USE_STATIC_ZSTD", "TRUE");
832+
} else {
833+
cfg.define("LLVM_ENABLE_ZLIB", "OFF");
834+
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
835+
}
836+
836837
if let Some(ref linker) = builder.config.llvm_use_linker {
837838
cfg.define("LLVM_USE_LINKER", linker);
838839
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510
2+
// see https://github.com/rust-lang/compiler-team/issues/510 for more info
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Checks the `compress-debug-sections` option on rust-lld.
2+
3+
//@ needs-rust-lld
4+
//@ only-linux
5+
//@ ignore-cross-compile
6+
7+
// FIXME: This test isn't comprehensive and isn't covering all possible combinations.
8+
9+
use run_make_support::{assert_contains, cmd, llvm_readobj, run_in_tmpdir, rustc};
10+
11+
fn check_compression(compression: &str, to_find: &str) {
12+
run_in_tmpdir(|| {
13+
let out = rustc()
14+
.arg("-Zlinker-features=+lld")
15+
.arg("-Clink-self-contained=+linker")
16+
.arg("-Zunstable-options")
17+
.arg("-Cdebuginfo=full")
18+
.link_arg(&format!("-Wl,--compress-debug-sections={compression}"))
19+
.input("main.rs")
20+
.run_unchecked();
21+
let stderr = out.stderr_utf8();
22+
if stderr.is_empty() {
23+
llvm_readobj().arg("-t").arg("main").run().assert_stdout_contains(to_find);
24+
} else {
25+
assert_contains(
26+
stderr,
27+
format!(
28+
"\
29+
LLVM was not built with LLVM_ENABLE_{to_find} or did not find {compression} at build time"
30+
),
31+
);
32+
}
33+
});
34+
}
35+
36+
fn main() {
37+
check_compression("zlib", "ZLIB");
38+
check_compression("zstd", "ZSTD");
39+
}

0 commit comments

Comments
 (0)