Skip to content

Commit 68d2e8a

Browse files
committed
Auto merge of #125642 - khuey:zstd, r=Kobzol
Enable zstd for debug compression. Set LLVM_ENABLE_ZSTD alongside LLVM_ENABLE_ZLIB so that --compress-debug-sections=zstd is an option. See #120953 try-job: x86_64-gnu-tools
2 parents ca5d25e + 8db318c commit 68d2e8a

File tree

11 files changed

+126
-7
lines changed

11 files changed

+126
-7
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

config.example.toml

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
# library provided by LLVM.
8888
#static-libstdcpp = false
8989

90+
# Enable LLVM to use zstd for compression.
91+
#libzstd = false
92+
9093
# Whether to use Ninja to build LLVM. This runs much faster than make.
9194
#ninja = true
9295

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,7 @@ impl Step for Llvm {
368368
cfg.define("LLVM_PROFDATA_FILE", path);
369369
}
370370

371-
// Disable zstd to avoid a dependency on libzstd.so.
372-
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
373-
371+
// Libraries for ELF section compression.
374372
if !target.is_windows() {
375373
cfg.define("LLVM_ENABLE_ZLIB", "ON");
376374
} else {
@@ -824,6 +822,14 @@ fn configure_llvm(builder: &Builder<'_>, target: TargetSelection, cfg: &mut cmak
824822
}
825823
}
826824

825+
// Libraries for ELF section compression.
826+
if builder.config.llvm_libzstd {
827+
cfg.define("LLVM_ENABLE_ZSTD", "FORCE_ON");
828+
cfg.define("LLVM_USE_STATIC_ZSTD", "TRUE");
829+
} else {
830+
cfg.define("LLVM_ENABLE_ZSTD", "OFF");
831+
}
832+
827833
if let Some(ref linker) = builder.config.llvm_use_linker {
828834
cfg.define("LLVM_USE_LINKER", linker);
829835
}

src/bootstrap/src/core/config/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ pub struct Config {
218218
pub llvm_thin_lto: bool,
219219
pub llvm_release_debuginfo: bool,
220220
pub llvm_static_stdcpp: bool,
221+
pub llvm_libzstd: bool,
221222
/// `None` if `llvm_from_ci` is true and we haven't yet downloaded llvm.
222223
#[cfg(not(test))]
223224
llvm_link_shared: Cell<Option<bool>>,
@@ -878,6 +879,7 @@ define_config! {
878879
plugins: Option<bool> = "plugins",
879880
ccache: Option<StringOrBool> = "ccache",
880881
static_libstdcpp: Option<bool> = "static-libstdcpp",
882+
libzstd: Option<bool> = "libzstd",
881883
ninja: Option<bool> = "ninja",
882884
targets: Option<String> = "targets",
883885
experimental_targets: Option<String> = "experimental-targets",
@@ -1153,6 +1155,7 @@ impl Config {
11531155
llvm_optimize: true,
11541156
ninja_in_file: true,
11551157
llvm_static_stdcpp: false,
1158+
llvm_libzstd: false,
11561159
backtrace: true,
11571160
rust_optimize: RustOptimize::Bool(true),
11581161
rust_optimize_tests: true,
@@ -1791,6 +1794,7 @@ impl Config {
17911794
plugins,
17921795
ccache,
17931796
static_libstdcpp,
1797+
libzstd,
17941798
ninja,
17951799
targets,
17961800
experimental_targets,
@@ -1825,6 +1829,7 @@ impl Config {
18251829
set(&mut config.llvm_thin_lto, thin_lto);
18261830
set(&mut config.llvm_release_debuginfo, release_debuginfo);
18271831
set(&mut config.llvm_static_stdcpp, static_libstdcpp);
1832+
set(&mut config.llvm_libzstd, libzstd);
18281833
if let Some(v) = link_shared {
18291834
config.llvm_link_shared.set(Some(v));
18301835
}
@@ -1875,6 +1880,7 @@ impl Config {
18751880
check_ci_llvm!(optimize_toml);
18761881
check_ci_llvm!(thin_lto);
18771882
check_ci_llvm!(release_debuginfo);
1883+
check_ci_llvm!(libzstd);
18781884
check_ci_llvm!(targets);
18791885
check_ci_llvm!(experimental_targets);
18801886
check_ci_llvm!(clang_cl);

src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
220220
severity: ChangeSeverity::Warning,
221221
summary: "For tarball sources, default value for `rust.channel` will be taken from `src/ci/channel` file.",
222222
},
223+
ChangeInfo {
224+
change_id: 125642,
225+
severity: ChangeSeverity::Info,
226+
summary: "New option `llvm.libzstd` to control whether llvm is built with zstd support.",
227+
},
223228
];

src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ COPY host-x86_64/dist-x86_64-linux/build-clang.sh /tmp/
6262
RUN ./build-clang.sh
6363
ENV CC=clang CXX=clang++
6464

65+
# rustc's LLVM needs zstd.
66+
COPY scripts/zstd.sh /tmp/
67+
RUN ./zstd.sh
68+
6569
COPY scripts/sccache.sh /scripts/
6670
RUN sh /scripts/sccache.sh
6771

@@ -79,6 +83,7 @@ ENV RUST_CONFIGURE_ARGS \
7983
--set target.x86_64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \
8084
--set llvm.thin-lto=true \
8185
--set llvm.ninja=false \
86+
--set llvm.libzstd=true \
8287
--set rust.jemalloc \
8388
--set rust.use-lld=true \
8489
--set rust.lto=thin \

src/ci/docker/host-x86_64/x86_64-gnu/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1717
pkg-config \
1818
xz-utils \
1919
mingw-w64 \
20+
zlib1g-dev \
21+
libzstd-dev \
2022
&& rm -rf /var/lib/apt/lists/*
2123

2224
COPY scripts/sccache.sh /scripts/

src/ci/docker/scripts/zstd.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
hide_output() {
5+
set +x
6+
on_err="
7+
echo ERROR: An error was encountered with the build.
8+
cat /tmp/zstd_build.log
9+
exit 1
10+
"
11+
trap "$on_err" ERR
12+
bash -c "while true; do sleep 30; echo \$(date) - building ...; done" &
13+
PING_LOOP_PID=$!
14+
"$@" &> /tmp/zstd_build.log
15+
trap - ERR
16+
kill $PING_LOOP_PID
17+
rm /tmp/zstd_build.log
18+
set -x
19+
}
20+
21+
ZSTD=1.5.6
22+
curl -L https://github.com/facebook/zstd/releases/download/v$ZSTD/zstd-$ZSTD.tar.gz | tar xzf -
23+
24+
cd zstd-$ZSTD
25+
CFLAGS=-fPIC hide_output make -j$(nproc) VERBOSE=1
26+
hide_output make install
27+
28+
cd ..
29+
rm -rf zstd-$ZSTD
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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+
"LLVM was not built with LLVM_ENABLE_{to_find} \
29+
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)