Skip to content

Commit 8f641b1

Browse files
committed
rewrite foreign-double-unwind to rmake
1 parent 5367673 commit 8f641b1

File tree

7 files changed

+130
-14
lines changed

7 files changed

+130
-14
lines changed

Diff for: src/tools/run-make-support/src/external_deps/c_build.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
use std::path::PathBuf;
22

3+
<<<<<<< HEAD
34
use super::cygpath::get_windows_path;
45
use crate::artifact_names::{dynamic_lib_name, static_lib_name};
56
use crate::external_deps::cc::cc;
7+
=======
8+
use crate::artifact_names::static_lib_name;
9+
use crate::external_deps::cc::{cc, cxx};
10+
>>>>>>> e3cf7e53339 (rewrite foreign-double-unwind to rmake)
611
use crate::external_deps::llvm::llvm_ar;
712
use crate::path_helpers::path;
813
use crate::targets::{is_darwin, is_msvc, is_windows};
914

1015
// FIXME(Oneirical): These native build functions should take a Path-based generic.
1116

1217
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
18+
/// Built from a C file.
1319
#[track_caller]
1420
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
1521
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
@@ -58,3 +64,24 @@ pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
5864
}
5965
path(lib_path)
6066
}
67+
68+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
69+
/// Built from a C++ file.
70+
#[track_caller]
71+
pub fn build_native_static_lib_cxx(lib_name: &str) -> PathBuf {
72+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
73+
let src = format!("{lib_name}.cpp");
74+
let lib_path = static_lib_name(lib_name);
75+
if is_msvc() {
76+
cxx().arg("-EHs").arg("-c").out_exe(&obj_file).input(src).run();
77+
} else {
78+
cxx().arg("-c").out_exe(&obj_file).input(src).run();
79+
};
80+
let obj_file = if is_msvc() {
81+
PathBuf::from(format!("{lib_name}.obj"))
82+
} else {
83+
PathBuf::from(format!("{lib_name}.o"))
84+
};
85+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
86+
path(lib_path)
87+
}

Diff for: src/tools/run-make-support/src/external_deps/cc.rs

+38
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,41 @@ pub fn extra_cxx_flags() -> Vec<&'static str> {
214214
}
215215
}
216216
}
217+
218+
/// `EXTRARSCXXFLAGS`
219+
pub fn extra_rs_cxx_flags() -> Vec<&'static str> {
220+
// Adapted from tools.mk (trimmed):
221+
//
222+
// ```makefile
223+
// ifdef IS_WINDOWS
224+
// ifdef IS_MSVC
225+
// else
226+
// EXTRARSCXXFLAGS := -lstatic:-bundle=stdc++
227+
// endif
228+
// else
229+
// ifeq ($(UNAME),Darwin)
230+
// EXTRARSCXXFLAGS := -lc++
231+
// else
232+
// ifeq ($(UNAME),FreeBSD)
233+
// else
234+
// ifeq ($(UNAME),SunOS)
235+
// else
236+
// ifeq ($(UNAME),OpenBSD)
237+
// else
238+
// EXTRARSCXXFLAGS := -lstdc++
239+
// endif
240+
// endif
241+
// endif
242+
// endif
243+
// endif
244+
// ```
245+
if is_windows() {
246+
if is_msvc() { vec![] } else { vec!["-lstatic:-bundle=stdc++"] }
247+
} else {
248+
match &uname()[..] {
249+
"Darwin" => vec!["-lc++"],
250+
"FreeBSD" | "SunOS" | "OpenBSD" => vec![],
251+
_ => vec!["-lstdc++"],
252+
}
253+
}
254+
}

Diff for: src/tools/run-make-support/src/external_deps/rustc.rs

+43
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::command::Command;
55
use crate::env::env_var;
66
use crate::path_helpers::cwd;
77
use crate::util::set_host_rpath;
8+
use crate::{is_msvc, is_windows, uname};
89

910
/// Construct a new `rustc` invocation. This will automatically set the library
1011
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -314,4 +315,46 @@ impl Rustc {
314315
self.cmd.arg(format!("-Clinker-flavor={linker_flavor}"));
315316
self
316317
}
318+
319+
/// `EXTRARSCXXFLAGS`
320+
pub fn extra_rs_cxx_flags(&mut self) -> &mut Self {
321+
// Adapted from tools.mk (trimmed):
322+
//
323+
// ```makefile
324+
// ifdef IS_WINDOWS
325+
// ifdef IS_MSVC
326+
// else
327+
// EXTRARSCXXFLAGS := -lstatic:-bundle=stdc++
328+
// endif
329+
// else
330+
// ifeq ($(UNAME),Darwin)
331+
// EXTRARSCXXFLAGS := -lc++
332+
// else
333+
// ifeq ($(UNAME),FreeBSD)
334+
// else
335+
// ifeq ($(UNAME),SunOS)
336+
// else
337+
// ifeq ($(UNAME),OpenBSD)
338+
// else
339+
// EXTRARSCXXFLAGS := -lstdc++
340+
// endif
341+
// endif
342+
// endif
343+
// endif
344+
// endif
345+
// ```
346+
let flag = if is_windows() {
347+
if is_msvc() { None } else { Some("-lstatic:-bundle=stdc++") }
348+
} else {
349+
match &uname()[..] {
350+
"Darwin" => Some("-lc++"),
351+
"FreeBSD" | "SunOS" | "OpenBSD" => None,
352+
_ => Some("-lstdc++"),
353+
}
354+
};
355+
if let Some(flag) = flag {
356+
self.cmd.arg(flag);
357+
}
358+
self
359+
}
317360
}

Diff for: src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub use wasmparser;
4444
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};
4545

4646
// These rely on external dependencies.
47-
pub use c_build::{build_native_dynamic_lib, build_native_static_lib};
4847
pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc};
48+
pub use c_build::{build_native_dynamic_lib, build_native_static_lib, build_native_static_lib_cxx};
4949
pub use clang::{clang, Clang};
5050
pub use htmldocck::htmldocck;
5151
pub use llvm::{

Diff for: src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ run-make/dep-info-spaces/Makefile
99
run-make/dep-info/Makefile
1010
run-make/emit-to-stdout/Makefile
1111
run-make/extern-fn-reachable/Makefile
12-
run-make/foreign-double-unwind/Makefile
1312
run-make/foreign-exceptions/Makefile
1413
run-make/incr-add-rust-src-component/Makefile
1514
run-make/issue-36710/Makefile

Diff for: tests/run-make/foreign-double-unwind/Makefile

-12
This file was deleted.

Diff for: tests/run-make/foreign-double-unwind/rmake.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// When using foreign function interface (FFI) with C++, it is possible
2+
// to run into a "double unwind" if either both Rust and C++ run into a panic
3+
// and exception at the same time, or C++ encounters two exceptions. In this case,
4+
// one of the panic unwinds would be leaked and the other would be kept, leading
5+
// to undefined behaviour. After this was fixed in #92911, this test checks that
6+
// the keyword "unreachable" indicative of this bug triggering in this specific context
7+
// does not appear after successfully compiling and executing the program.
8+
// See https://github.com/rust-lang/rust/pull/92911
9+
10+
//@ needs-unwind
11+
// Reason: this test exercises panic unwinding
12+
//@ ignore-cross-compile
13+
// Reason: the compiled binary is executed
14+
15+
use run_make_support::{build_native_static_lib_cxx, run, rustc};
16+
17+
fn main() {
18+
build_native_static_lib_cxx("foo");
19+
rustc().input("foo.rs").arg("-lfoo").extra_rs_cxx_flags().run();
20+
run("foo").assert_stdout_not_contains("unreachable");
21+
}

0 commit comments

Comments
 (0)