Skip to content

Commit 333b920

Browse files
committed
Auto merge of rust-lang#109421 - mhammerly:extern-force-option, r=petrochenkov
Add `force` option for `--extern` flag When `--extern force:foo=libfoo.so` is passed to `rustc` and `foo` is not actually used in the crate, ~inject an `extern crate foo;` statement into the AST~ force it to be resolved anyway in `CrateLoader::postprocess()`. This allows you to, for instance, inject a `#[panic_handler]` implementation into a `#![no_std]` crate without modifying its source so that it can be built as a `dylib`. It may also be useful for `#![panic_runtime]` or `#[global_allocator]`/`#![default_lib_allocator]` implementations. My work previously involved integrating Rust into an existing C/C++ codebase which was built with Buck and shipped on, among other platforms, Android. When targeting Android, Buck builds all "native" code with shared linkage* so it can be loaded from Java/Kotlin. My project was not itself `#![no_std]`, but many of our dependencies were, and they would fail to build with shared linkage due to a lack of a panic handler. With this change, that project can add the new `force` option to the `std` dependency it already explicitly provides to every crate to solve this problem. *This is an oversimplification - Buck has a couple features for aggregating dependencies into larger shared libraries, but none that I think sustainably solve this problem. ~The AST injection happens after macro expansion around where we similarly inject a test harness and proc-macro harness. The resolver's list of actually-used extern flags is populated during macro expansion, and if any of our `--extern` arguments have the `force` option and weren't already used, we inject an `extern crate` statement for them. The injection logic was added in `rustc_builtin_macros` as that's where similar injections for tests, proc-macros, and std/core already live.~ (New contributor - grateful for feedback and guidance!)
2 parents 151a070 + 812f2d7 commit 333b920

File tree

7 files changed

+78
-2
lines changed

7 files changed

+78
-2
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ where
6969
is_private_dep: false,
7070
add_prelude: true,
7171
nounused_dep: false,
72+
force: false,
7273
}
7374
}
7475

compiler/rustc_metadata/src/creader.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,17 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
865865
}
866866
}
867867

868+
fn inject_forced_externs(&mut self) {
869+
for (name, entry) in self.sess.opts.externs.iter() {
870+
if entry.force {
871+
let name_interned = Symbol::intern(name);
872+
if !self.used_extern_options.contains(&name_interned) {
873+
self.resolve_crate(name_interned, DUMMY_SP, CrateDepKind::Explicit);
874+
}
875+
}
876+
}
877+
}
878+
868879
fn inject_dependency_if(
869880
&self,
870881
krate: CrateNum,
@@ -913,7 +924,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
913924
// Don't worry about pathless `--extern foo` sysroot references
914925
continue;
915926
}
916-
if entry.nounused_dep {
927+
if entry.nounused_dep || entry.force {
917928
// We're not worried about this one
918929
continue;
919930
}
@@ -942,6 +953,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
942953
}
943954

944955
pub fn postprocess(&mut self, krate: &ast::Crate) {
956+
self.inject_forced_externs();
945957
self.inject_profiler_runtime(krate);
946958
self.inject_allocator_crate(krate);
947959
self.inject_panic_runtime(krate);

compiler/rustc_session/src/config.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ pub struct ExternEntry {
518518
/// `--extern nounused:std=/path/to/lib/libstd.rlib`. This is used to
519519
/// suppress `unused-crate-dependencies` warnings.
520520
pub nounused_dep: bool,
521+
/// If the extern entry is not referenced in the crate, force it to be resolved anyway.
522+
///
523+
/// Allows a dependency satisfying, for instance, a missing panic handler to be injected
524+
/// without modifying source:
525+
/// `--extern force:extras=/path/to/lib/libstd.rlib`
526+
pub force: bool,
521527
}
522528

523529
#[derive(Clone, Debug)]
@@ -556,7 +562,13 @@ impl Externs {
556562

557563
impl ExternEntry {
558564
fn new(location: ExternLocation) -> ExternEntry {
559-
ExternEntry { location, is_private_dep: false, add_prelude: false, nounused_dep: false }
565+
ExternEntry {
566+
location,
567+
is_private_dep: false,
568+
add_prelude: false,
569+
nounused_dep: false,
570+
force: false,
571+
}
560572
}
561573

562574
pub fn files(&self) -> Option<impl Iterator<Item = &CanonicalizedPath>> {
@@ -2261,6 +2273,7 @@ pub fn parse_externs(
22612273
let mut is_private_dep = false;
22622274
let mut add_prelude = true;
22632275
let mut nounused_dep = false;
2276+
let mut force = false;
22642277
if let Some(opts) = options {
22652278
if !is_unstable_enabled {
22662279
early_error(
@@ -2283,6 +2296,7 @@ pub fn parse_externs(
22832296
}
22842297
}
22852298
"nounused" => nounused_dep = true,
2299+
"force" => force = true,
22862300
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
22872301
}
22882302
}
@@ -2293,6 +2307,8 @@ pub fn parse_externs(
22932307
entry.is_private_dep |= is_private_dep;
22942308
// likewise `nounused`
22952309
entry.nounused_dep |= nounused_dep;
2310+
// and `force`
2311+
entry.force |= force;
22962312
// If any flag is missing `noprelude`, then add to the prelude.
22972313
entry.add_prelude |= add_prelude;
22982314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(lang_items)]
2+
#![no_std]
3+
4+
// Since `rustc` generally passes `-nodefaultlibs` to the linker,
5+
// Rust programs link necessary system libraries via `#[link()]`
6+
// attributes in the `libc` crate. `libc` is a dependency of `std`,
7+
// but as we are `#![no_std]`, we need to include it manually.
8+
#![feature(rustc_private)]
9+
extern crate libc;
10+
11+
#[panic_handler]
12+
pub fn begin_panic_handler(_info: &core::panic::PanicInfo<'_>) -> ! {
13+
loop {}
14+
}
15+
16+
#[lang = "eh_personality"]
17+
extern "C" fn eh_personality() {}

tests/ui/extern-flag/force-extern.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
// ignore-cross-compile (needs dylibs and compiletest doesn't have a more specific header)
3+
// aux-crate:force:panic_handler=panic_handler.rs
4+
// compile-flags: -Zunstable-options --crate-type dylib
5+
// edition:2018
6+
7+
#![no_std]
8+
9+
fn foo() {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// aux-crate:panic_handler=panic_handler.rs
2+
// ignore-cross-compile (needs dylibs and compiletest doesn't have a more specific header)
3+
// compile_flags: -Zunstable-options --crate-type dylib
4+
// error-pattern: `#[panic_handler]` function required, but not found
5+
// dont-check-compiler-stderr
6+
// edition: 2018
7+
8+
#![no_std]
9+
10+
fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
// ignore-cross-compile (needs dylibs and compiletest doesn't have a more specific header)
3+
// aux-crate:force:panic_handler=panic_handler.rs
4+
// compile-flags: -Zunstable-options --crate-type dylib
5+
// edition:2018
6+
7+
#![no_std]
8+
9+
extern crate panic_handler;
10+
11+
fn foo() {}

0 commit comments

Comments
 (0)