Skip to content

Commit aff3689

Browse files
committed
fuzz: Add a script for downloading the correct LLVM version
Avoid downloading LLVM as part of the build script, which speeds the rebuild process down significantly. This also simplifies full paths from std.
1 parent 981fad9 commit aff3689

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

etc/download-llvm.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# Download the version of LLVM matching this crate's port version to test
3+
# against.
4+
5+
set -xux
6+
7+
# Find the version and extract everything after the `-` to get the LLVM hash
8+
version="$(
9+
cargo metadata --no-deps --format-version=1 |
10+
jq -r '.packages | .[] | select(.name == "rustc_apfloat") | .version'
11+
)"
12+
llvm_hash="${version##*-}"
13+
14+
target_dir="${CARGO_TARGET_DIR:-target}"
15+
out_dir="$target_dir/llvm-downloads/"
16+
mkdir -p "$out_dir"
17+
18+
tgz_url="https://codeload.github.com/llvm/llvm-project/tar.gz/$llvm_hash"
19+
curl -sS "$tgz_url" | tar -C "$out_dir" -xz

fuzz/build.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use std::env;
2+
use std::fs;
3+
use std::io;
4+
use std::path::PathBuf;
15
use std::process::{Command, ExitCode};
26

37
mod ops;
48

5-
fn main() -> std::io::Result<ExitCode> {
9+
fn main() -> io::Result<ExitCode> {
610
// HACK(eddyb) disable the default of re-running the build script on *any*
711
// change to *the entire source tree* (i.e. the default is roughly `./`).
812
println!("cargo:rerun-if-changed=build.rs");
@@ -11,8 +15,8 @@ fn main() -> std::io::Result<ExitCode> {
1115
let (_, llvm_commit_hash) = env!("CARGO_PKG_VERSION").split_once("+llvm-").unwrap();
1216
assert_eq!(llvm_commit_hash.len(), 12);
1317

14-
let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
15-
std::fs::write(out_dir.join("generated_fuzz_ops.rs"), ops::generate_rust())?;
18+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
19+
fs::write(out_dir.join("generated_fuzz_ops.rs"), ops::generate_rust())?;
1620

1721
// FIXME(eddyb) add a way to disable the C++ build below, or automatically
1822
// disable it if on an unsupported target (e.g. Windows).
@@ -22,27 +26,41 @@ fn main() -> std::io::Result<ExitCode> {
2226
}
2327

2428
let mut cxx_exported_symbols = vec![];
25-
std::fs::write(
29+
fs::write(
2630
out_dir.join("cxx_apf_fuzz.cpp"),
2731
ops::generate_cxx(&mut cxx_exported_symbols),
2832
)?;
2933

34+
let manifest_dir =
35+
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR unset"));
36+
let target_dir = env::var_os("CARGO_TARGET_DIR")
37+
.map(PathBuf::from)
38+
.unwrap_or_else(|| manifest_dir.parent().unwrap().join("target"));
39+
let llvm_dir = target_dir.join(format!("llvm-downloads/llvm-project-{llvm_commit_hash}"));
40+
if !llvm_dir.try_exists().is_ok_and(|val| val) {
41+
panic!(
42+
"llvm dir `{llvm_dir:?}` does not exist or cannot be reached. \
43+
Perhaps you need to run etc/download-llvm.sh?"
44+
)
45+
}
46+
3047
// HACK(eddyb) work around https://github.com/rust-lang/cargo/issues/3676,
3148
// by removing the env vars that Cargo appears to hardcode.
3249
const CARGO_HARDCODED_ENV_VARS: &[(&str, &str)] = &[
3350
("SSL_CERT_DIR", "/etc/pki/tls/certs"),
3451
("SSL_CERT_FILE", "/etc/pki/tls/certs/ca-bundle.crt"),
3552
];
3653
for &(var_name, cargo_hardcoded_value) in CARGO_HARDCODED_ENV_VARS {
37-
if let Ok(value) = std::env::var(var_name) {
54+
if let Ok(value) = env::var(var_name) {
3855
if value == cargo_hardcoded_value {
39-
std::env::remove_var(var_name);
56+
env::remove_var(var_name);
4057
}
4158
}
4259
}
4360

4461
let sh_script_exit_status = Command::new("bash")
4562
.args(["-c", SH_SCRIPT])
63+
.env("llvm", &llvm_dir.join("llvm"))
4664
.envs([
4765
("llvm_project_git_hash", llvm_commit_hash),
4866
("cxx_apf_fuzz_exports", &cxx_exported_symbols.join(",")),
@@ -61,11 +79,6 @@ fn main() -> std::io::Result<ExitCode> {
6179

6280
// HACK(eddyb) should avoid shelling out, but for now this will suffice.
6381
const SH_SCRIPT: &str = r#"
64-
set -e
65-
66-
llvm_project_tgz_url="https://codeload.github.com/llvm/llvm-project/tar.gz/$llvm_project_git_hash"
67-
curl -sS "$llvm_project_tgz_url" | tar -C "$OUT_DIR" -xz
68-
llvm="$OUT_DIR"/llvm-project-"$llvm_project_git_hash"/llvm
6982
set -eux
7083
7184
mkdir -p "$OUT_DIR"/fake-config/llvm/Config

0 commit comments

Comments
 (0)