Skip to content

Commit

Permalink
hack: in libc-test/build.rs, copy src to src-hotfix and replace…
Browse files Browse the repository at this point in the history
… `crate::` with `::`

This is needed because ctest2 cannot parse `crate::` in paths

(backport <rust-lang#4132>)
(cherry picked from commit fda6017)
  • Loading branch information
eduardosm authored and tgross35 committed Nov 27, 2024
1 parent 0d227d1 commit 8cb131f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ default-features = false
cc = "1.0.83"
# FIXME: Use fork ctest until the maintainer gets back.
ctest2 = "0.4.3"
regex = "1.11.1"

[features]
default = ["std"]
Expand Down
72 changes: 51 additions & 21 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::{env, io};

fn src_hotfix_dir() -> PathBuf {
Path::new(&env::var_os("OUT_DIR").unwrap()).join("src-hotfix")
}

fn do_cc() {
let target = env::var("TARGET").unwrap();
if cfg!(unix) {
Expand Down Expand Up @@ -150,11 +154,37 @@ fn main() {
// Avoid unnecessary re-building.
println!("cargo:rerun-if-changed=build.rs");

let hotfix_dir = src_hotfix_dir();
if std::fs::exists(&hotfix_dir).unwrap() {
std::fs::remove_dir_all(&hotfix_dir).unwrap();
}

// FIXME(ctest): ctest2 cannot parse `crate::` in paths, so replace them with `::`
let re = regex::bytes::Regex::new(r"(?-u:\b)crate::").unwrap();
copy_dir_hotfix(Path::new("../src"), &hotfix_dir, &re, b"::");

do_cc();
do_ctest();
do_semver();
}

fn copy_dir_hotfix(src: &Path, dst: &Path, regex: &regex::bytes::Regex, replace: &[u8]) {
std::fs::create_dir(&dst).unwrap();
for entry in src.read_dir().unwrap() {
let entry = entry.unwrap();
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
if entry.file_type().unwrap().is_dir() {
copy_dir_hotfix(&src_path, &dst_path, regex, replace);
} else {
// Replace "crate::" with "::"
let src_data = std::fs::read(&src_path).unwrap();
let dst_data = regex.replace_all(&src_data, b"::");
std::fs::write(&dst_path, &dst_data).unwrap();
}
}
}

macro_rules! headers {
($cfg:ident: [$m:expr]: $header:literal) => {
if $m {
Expand Down Expand Up @@ -464,7 +494,7 @@ fn test_apple(target: &str) {
"uuid_t" | "vol_capabilities_set_t" => true,
_ => false,
});
cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_openbsd(target: &str) {
Expand Down Expand Up @@ -651,7 +681,7 @@ fn test_openbsd(target: &str) {
}
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_windows(target: &str) {
Expand Down Expand Up @@ -780,7 +810,7 @@ fn test_windows(target: &str) {
}
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_redox(target: &str) {
Expand Down Expand Up @@ -830,7 +860,7 @@ fn test_redox(target: &str) {
"wchar.h",
}

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_solarish(target: &str) {
Expand Down Expand Up @@ -1108,7 +1138,7 @@ fn test_solarish(target: &str) {
}
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_netbsd(target: &str) {
Expand Down Expand Up @@ -1323,7 +1353,7 @@ fn test_netbsd(target: &str) {
}
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_dragonflybsd(target: &str) {
Expand Down Expand Up @@ -1549,7 +1579,7 @@ fn test_dragonflybsd(target: &str) {
(struct_ == "sigevent" && field == "sigev_notify_thread_id")
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_wasi(target: &str) {
Expand Down Expand Up @@ -1656,7 +1686,7 @@ fn test_wasi(target: &str) {
// doesn't support sizeof.
cfg.skip_field(|s, field| s == "dirent" && field == "d_name");

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_android(target: &str) {
Expand Down Expand Up @@ -2149,7 +2179,7 @@ fn test_android(target: &str) {
}
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");

test_linux_like_apis(target);
}
Expand Down Expand Up @@ -2821,7 +2851,7 @@ fn test_freebsd(target: &str) {
});
}

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_emscripten(target: &str) {
Expand Down Expand Up @@ -3058,7 +3088,7 @@ fn test_emscripten(target: &str) {
].contains(&field))
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_neutrino(target: &str) {
Expand Down Expand Up @@ -3311,7 +3341,7 @@ fn test_neutrino(target: &str) {

cfg.skip_static(move |name| (name == "__dso_handle"));

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_vxworks(target: &str) {
Expand Down Expand Up @@ -3419,7 +3449,7 @@ fn test_vxworks(target: &str) {
_ => false,
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

fn test_linux(target: &str) {
Expand Down Expand Up @@ -4553,7 +4583,7 @@ fn test_linux(target: &str) {
_ => false,
});

cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");

test_linux_like_apis(target);
}
Expand All @@ -4580,7 +4610,7 @@ fn test_linux_like_apis(target: &str) {
})
.skip_const(|_| true)
.skip_struct(|_| true);
cfg.generate("../src/lib.rs", "linux_strerror_r.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_strerror_r.rs");
}

if linux || android || emscripten {
Expand Down Expand Up @@ -4610,7 +4640,7 @@ fn test_linux_like_apis(target: &str) {
t => t.to_string(),
});

cfg.generate("../src/lib.rs", "linux_fcntl.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_fcntl.rs");
}

if linux || android {
Expand All @@ -4634,7 +4664,7 @@ fn test_linux_like_apis(target: &str) {
t if is_union => format!("union {}", t),
t => t.to_string(),
});
cfg.generate("../src/lib.rs", "linux_termios.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_termios.rs");
}

if linux || android {
Expand Down Expand Up @@ -4662,7 +4692,7 @@ fn test_linux_like_apis(target: &str) {
t if is_union => format!("union {}", t),
t => t.to_string(),
});
cfg.generate("../src/lib.rs", "linux_ipv6.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_ipv6.rs");
}

if linux || android {
Expand All @@ -4684,7 +4714,7 @@ fn test_linux_like_apis(target: &str) {
"Elf64_Phdr" | "Elf32_Phdr" => false,
_ => true,
});
cfg.generate("../src/lib.rs", "linux_elf.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_elf.rs");
}

if linux || android {
Expand All @@ -4699,7 +4729,7 @@ fn test_linux_like_apis(target: &str) {
})
.skip_struct(|_| true)
.skip_type(|_| true);
cfg.generate("../src/lib.rs", "linux_if_arp.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_if_arp.rs");
}
}

Expand Down Expand Up @@ -5057,5 +5087,5 @@ fn test_haiku(target: &str) {
s => s.to_string(),
}
});
cfg.generate("../src/lib.rs", "main.rs");
cfg.generate(src_hotfix_dir().join("lib.rs"), "main.rs");
}

0 comments on commit 8cb131f

Please sign in to comment.