Skip to content

Commit 9ca58b7

Browse files
committed
rewrite return-non-c-like-enum-from-c to rmake
1 parent 5d76a13 commit 9ca58b7

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

src/tools/run-make-support/src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
3131
pub use clang::{clang, Clang};
3232
pub use diff::{diff, Diff};
3333
pub use llvm::{
34-
llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmFilecheck, LlvmObjdump,
35-
LlvmProfdata, LlvmReadobj,
34+
llvm_ar, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmFilecheck,
35+
LlvmObjdump, LlvmProfdata, LlvmReadobj,
3636
};
3737
pub use run::{cmd, run, run_fail, run_with_args};
3838
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
@@ -317,6 +317,26 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
317317
count
318318
}
319319

320+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
321+
#[track_caller]
322+
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
323+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
324+
let src = format!("{lib_name}.c");
325+
let lib_path = static_lib_name(lib_name);
326+
if is_msvc() {
327+
cc().arg("-c").out_exe(&obj_file).input(src).run();
328+
} else {
329+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
330+
};
331+
let mut obj_file = PathBuf::from(format!("{lib_name}.o"));
332+
if is_msvc() {
333+
obj_file.set_extension("");
334+
obj_file.set_extension("obj");
335+
}
336+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
337+
path(lib_path)
338+
}
339+
320340
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
321341
/// available on the platform!
322342
#[track_caller]

src/tools/run-make-support/src/llvm.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ pub fn llvm_objdump() -> LlvmObjdump {
2929
LlvmObjdump::new()
3030
}
3131

32+
/// Construct a new `llvm-ar` invocation. This assumes that `llvm-ar` is available
33+
/// at `$LLVM_BIN_DIR/llvm-ar`.
34+
pub fn llvm_ar() -> LlvmAr {
35+
LlvmAr::new()
36+
}
37+
3238
/// A `llvm-readobj` invocation builder.
3339
#[derive(Debug)]
3440
#[must_use]
@@ -57,10 +63,18 @@ pub struct LlvmObjdump {
5763
cmd: Command,
5864
}
5965

66+
/// A `llvm-ar` invocation builder.
67+
#[derive(Debug)]
68+
#[must_use]
69+
pub struct LlvmAr {
70+
cmd: Command,
71+
}
72+
6073
crate::impl_common_helpers!(LlvmReadobj);
6174
crate::impl_common_helpers!(LlvmProfdata);
6275
crate::impl_common_helpers!(LlvmFilecheck);
6376
crate::impl_common_helpers!(LlvmObjdump);
77+
crate::impl_common_helpers!(LlvmAr);
6478

6579
/// Generate the path to the bin directory of LLVM.
6680
#[must_use]
@@ -204,3 +218,26 @@ impl LlvmObjdump {
204218
self
205219
}
206220
}
221+
222+
impl LlvmAr {
223+
/// Construct a new `llvm-ar` invocation. This assumes that `llvm-ar` is available
224+
/// at `$LLVM_BIN_DIR/llvm-ar`.
225+
pub fn new() -> Self {
226+
let llvm_ar = llvm_bin_dir().join("llvm-ar");
227+
let cmd = Command::new(llvm_ar);
228+
Self { cmd }
229+
}
230+
231+
pub fn obj_to_ar(&mut self) -> &mut Self {
232+
self.cmd.arg("rcus");
233+
self
234+
}
235+
236+
/// Provide an output, then an input file. Bundled in one function, as llvm-ar has
237+
/// no "--output"-style flag.
238+
pub fn output_input(&mut self, out: impl AsRef<Path>, input: impl AsRef<Path>) -> &mut Self {
239+
self.cmd.arg(out.as_ref());
240+
self.cmd.arg(input.as_ref());
241+
self
242+
}
243+
}

src/tools/tidy/src/allowed_run_make_makefiles.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ run-make/no-builtins-attribute/Makefile
9292
run-make/no-duplicate-libs/Makefile
9393
run-make/obey-crate-type-flag/Makefile
9494
run-make/panic-abort-eh_frame/Makefile
95-
run-make/pass-non-c-like-enum-to-c/Makefile
9695
run-make/pdb-buildinfo-cl-cmd/Makefile
9796
run-make/pgo-gen-lto/Makefile
9897
run-make/pgo-gen-no-imp-symbols/Makefile

tests/run-make/return-non-c-like-enum-from-c/Makefile

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// A reversed version of the `return-non-c-like-enum` test, though
2+
// this time, the C code is the library, and the Rust code compiles
3+
// into the executable. Once again, enum variants should be treated
4+
// like an union of structs, which should prevent segfaults or
5+
// unexpected results.
6+
// See https://github.com/rust-lang/rust/issues/68190
7+
8+
//@ ignore-cross-compile
9+
// Reason: the compiled binary is executed
10+
11+
use run_make_support::{build_native_static_lib, run, rustc};
12+
13+
fn main() {
14+
build_native_static_lib("test");
15+
rustc().input("nonclike.rs").arg("-ltest").run();
16+
run("nonclike");
17+
}

0 commit comments

Comments
 (0)