rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
fn main() {
|
2017-02-23 09:49:54 -06:00
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
2016-09-25 10:59:12 -05:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
if cfg!(feature = "llvm-libunwind")
|
|
|
|
&& ((target.contains("linux") && !target.contains("musl")) || target.contains("fuchsia"))
|
|
|
|
{
|
2019-03-10 21:27:59 -05:00
|
|
|
// Build the unwinding from libunwind C/C++ source code.
|
|
|
|
llvm_libunwind::compile();
|
2020-06-23 12:18:34 -05:00
|
|
|
} else if target.contains("x86_64-fortanix-unknown-sgx") {
|
|
|
|
llvm_libunwind::compile();
|
2019-03-10 21:27:59 -05:00
|
|
|
} else if target.contains("linux") {
|
2020-09-20 23:19:34 -05:00
|
|
|
// linking for Linux is handled in lib.rs
|
2018-01-12 17:22:06 -06:00
|
|
|
if target.contains("musl") {
|
2019-07-30 14:31:26 -05:00
|
|
|
llvm_libunwind::compile();
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
}
|
|
|
|
} else if target.contains("freebsd") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_s");
|
|
|
|
} else if target.contains("rumprun") {
|
|
|
|
println!("cargo:rustc-link-lib=unwind");
|
|
|
|
} else if target.contains("netbsd") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_s");
|
|
|
|
} else if target.contains("openbsd") {
|
2019-08-15 08:34:23 -05:00
|
|
|
if target.contains("sparc64") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc");
|
|
|
|
} else {
|
|
|
|
println!("cargo:rustc-link-lib=c++abi");
|
|
|
|
}
|
2017-02-11 11:24:33 -06:00
|
|
|
} else if target.contains("solaris") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_s");
|
2020-04-13 18:37:22 -05:00
|
|
|
} else if target.contains("illumos") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_s");
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
} else if target.contains("dragonfly") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_pic");
|
2019-05-27 09:38:21 -05:00
|
|
|
} else if target.contains("pc-windows-gnu") {
|
2020-03-03 14:41:37 -06:00
|
|
|
// This is handled in the target spec with late_link_args_[static|dynamic]
|
2019-05-27 09:38:21 -05:00
|
|
|
} else if target.contains("uwp-windows-gnu") {
|
|
|
|
println!("cargo:rustc-link-lib=unwind");
|
2016-10-18 15:43:18 -05:00
|
|
|
} else if target.contains("fuchsia") {
|
|
|
|
println!("cargo:rustc-link-lib=unwind");
|
2017-04-21 20:47:36 -05:00
|
|
|
} else if target.contains("haiku") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc_s");
|
2017-08-03 23:13:44 -05:00
|
|
|
} else if target.contains("redox") {
|
2019-04-07 09:39:54 -05:00
|
|
|
// redox is handled in lib.rs
|
2017-12-26 16:32:42 -06:00
|
|
|
} else if target.contains("cloudabi") {
|
|
|
|
println!("cargo:rustc-link-lib=unwind");
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-10 21:27:59 -05:00
|
|
|
|
|
|
|
mod llvm_libunwind {
|
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
/// Compile the libunwind C/C++ source code.
|
|
|
|
pub fn compile() {
|
2020-06-23 12:18:34 -05:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
2019-03-10 21:27:59 -05:00
|
|
|
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
|
|
|
|
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
|
2019-10-30 11:51:43 -05:00
|
|
|
let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
|
2019-03-10 21:27:59 -05:00
|
|
|
let cfg = &mut cc::Build::new();
|
|
|
|
|
|
|
|
cfg.cpp(true);
|
|
|
|
cfg.cpp_set_stdlib(None);
|
|
|
|
cfg.warnings(false);
|
|
|
|
|
2019-10-30 11:51:43 -05:00
|
|
|
// libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765
|
|
|
|
if target_endian_little {
|
|
|
|
cfg.define("__LITTLE_ENDIAN__", Some("1"));
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:27:59 -05:00
|
|
|
if target_env == "msvc" {
|
|
|
|
// Don't pull in extra libraries on MSVC
|
|
|
|
cfg.flag("/Zl");
|
|
|
|
cfg.flag("/EHsc");
|
|
|
|
cfg.define("_CRT_SECURE_NO_WARNINGS", None);
|
|
|
|
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
|
2020-06-23 12:18:34 -05:00
|
|
|
} else if target.contains("x86_64-fortanix-unknown-sgx") {
|
|
|
|
cfg.cpp(false);
|
|
|
|
|
|
|
|
cfg.static_flag(true);
|
|
|
|
cfg.opt_level(3);
|
|
|
|
|
|
|
|
cfg.flag("-nostdinc++");
|
|
|
|
cfg.flag("-fno-exceptions");
|
|
|
|
cfg.flag("-fno-rtti");
|
|
|
|
cfg.flag("-fstrict-aliasing");
|
|
|
|
cfg.flag("-funwind-tables");
|
|
|
|
cfg.flag("-fvisibility=hidden");
|
|
|
|
cfg.flag("-fno-stack-protector");
|
|
|
|
cfg.flag("-ffreestanding");
|
|
|
|
cfg.flag("-fexceptions");
|
|
|
|
|
|
|
|
// easiest way to undefine since no API available in cc::Build to undefine
|
|
|
|
cfg.flag("-U_FORTIFY_SOURCE");
|
|
|
|
cfg.define("_FORTIFY_SOURCE", "0");
|
|
|
|
|
|
|
|
cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
|
|
|
|
|
|
|
|
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
|
|
|
|
cfg.define("RUST_SGX", "1");
|
|
|
|
cfg.define("__NO_STRING_INLINES", None);
|
|
|
|
cfg.define("__NO_MATH_INLINES", None);
|
|
|
|
cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
|
|
|
|
cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
|
|
|
|
cfg.define("NDEBUG", None);
|
2019-03-10 21:27:59 -05:00
|
|
|
} else {
|
|
|
|
cfg.flag("-std=c99");
|
|
|
|
cfg.flag("-std=c++11");
|
|
|
|
cfg.flag("-nostdinc++");
|
2019-05-20 14:27:15 -05:00
|
|
|
cfg.flag("-fno-exceptions");
|
2019-03-10 21:27:59 -05:00
|
|
|
cfg.flag("-fno-rtti");
|
|
|
|
cfg.flag("-fstrict-aliasing");
|
2019-05-20 14:27:15 -05:00
|
|
|
cfg.flag("-funwind-tables");
|
2020-05-05 14:41:23 -05:00
|
|
|
cfg.flag("-fvisibility=hidden");
|
2020-05-29 13:14:27 -05:00
|
|
|
cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
|
|
|
|
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
|
2019-03-10 21:27:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut unwind_sources = vec![
|
|
|
|
"Unwind-EHABI.cpp",
|
|
|
|
"Unwind-seh.cpp",
|
|
|
|
"Unwind-sjlj.c",
|
|
|
|
"UnwindLevel1-gcc-ext.c",
|
|
|
|
"UnwindLevel1.c",
|
|
|
|
"UnwindRegistersRestore.S",
|
|
|
|
"UnwindRegistersSave.S",
|
|
|
|
"libunwind.cpp",
|
|
|
|
];
|
|
|
|
|
|
|
|
if target_vendor == "apple" {
|
|
|
|
unwind_sources.push("Unwind_AppleExtras.cpp");
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:18:34 -05:00
|
|
|
if target.contains("x86_64-fortanix-unknown-sgx") {
|
|
|
|
unwind_sources.push("UnwindRustSgx.c");
|
|
|
|
}
|
|
|
|
|
2020-06-11 21:31:49 -05:00
|
|
|
let root = Path::new("../../src/llvm-project/libunwind");
|
2019-03-10 21:27:59 -05:00
|
|
|
cfg.include(root.join("include"));
|
|
|
|
for src in unwind_sources {
|
|
|
|
cfg.file(root.join("src").join(src));
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:31:26 -05:00
|
|
|
if target_env == "musl" {
|
|
|
|
// use the same C compiler command to compile C++ code so we do not need to setup the
|
|
|
|
// C++ compiler env variables on the builders
|
|
|
|
cfg.cpp(false);
|
|
|
|
// linking for musl is handled in lib.rs
|
|
|
|
cfg.cargo_metadata(false);
|
|
|
|
println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:27:59 -05:00
|
|
|
cfg.compile("unwind");
|
|
|
|
}
|
|
|
|
}
|