Skip to content

Commit 2f59631

Browse files
authored
Rollup merge of rust-lang#69519 - 12101111:remove-proc-macro-check, r=nagisa,smaeul
Don't use static crt by default when build proc-macro Don't check value of `crt-static` when build proc-macro crates, since they are always built dynamically. For more information, see rust-lang/cargo#7563 (comment) I hope this will fix issues about compiling `proc_macro` crates on musl host without bring more issues. Note: I can't test this on my musl host locally, because I encounter this issue when bootstraping from a beta snapshot. Fix rust-lang/cargo#7563
2 parents 5ac1fba + 75e6cfc commit 2f59631

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

src/librustc_codegen_ssa/back/link.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
497497
cmd.args(args);
498498
}
499499
if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) {
500-
if sess.crt_static() {
500+
if sess.crt_static(Some(crate_type)) {
501501
cmd.args(args);
502502
}
503503
}
@@ -523,7 +523,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
523523
cmd.arg(get_file_path(sess, obj));
524524
}
525525

526-
if crate_type == config::CrateType::Executable && sess.crt_static() {
526+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
527527
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
528528
cmd.arg(get_file_path(sess, obj));
529529
}
@@ -558,7 +558,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
558558
for obj in &sess.target.target.options.post_link_objects {
559559
cmd.arg(get_file_path(sess, obj));
560560
}
561-
if sess.crt_static() {
561+
if sess.crt_static(Some(crate_type)) {
562562
for obj in &sess.target.target.options.post_link_objects_crt {
563563
cmd.arg(get_file_path(sess, obj));
564564
}
@@ -1288,7 +1288,8 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
12881288
let more_args = &sess.opts.cg.link_arg;
12891289
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
12901290

1291-
if is_pic(sess) && !sess.crt_static() && !args.any(|x| *x == "-static") {
1291+
if is_pic(sess) && !sess.crt_static(Some(crate_type)) && !args.any(|x| *x == "-static")
1292+
{
12921293
position_independent_executable = true;
12931294
}
12941295
}
@@ -1373,7 +1374,7 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13731374
if crate_type != config::CrateType::Executable {
13741375
cmd.build_dylib(out_filename);
13751376
}
1376-
if crate_type == config::CrateType::Executable && sess.crt_static() {
1377+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
13771378
cmd.build_static_executable();
13781379
}
13791380

src/librustc_codegen_utils/link.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ pub fn invalid_output_for_target(sess: &Session, crate_type: config::CrateType)
167167
if !sess.target.target.options.dynamic_linking {
168168
return true;
169169
}
170-
if sess.crt_static() && !sess.target.target.options.crt_static_allows_dylibs {
170+
if sess.crt_static(Some(crate_type))
171+
&& !sess.target.target.options.crt_static_allows_dylibs
172+
{
171173
return true;
172174
}
173175
}

src/librustc_interface/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn add_configuration(
4949

5050
cfg.extend(codegen_backend.target_features(sess).into_iter().map(|feat| (tf, Some(feat))));
5151

52-
if sess.crt_static_feature() {
52+
if sess.crt_static_feature(None) {
5353
cfg.insert((tf, Some(Symbol::intern("crt-static"))));
5454
}
5555
}

src/librustc_metadata/dependency_format.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
9797

9898
// If the global prefer_dynamic switch is turned off, or the final
9999
// executable will be statically linked, prefer static crate linkage.
100-
config::CrateType::Executable if !sess.opts.cg.prefer_dynamic || sess.crt_static() => {
100+
config::CrateType::Executable
101+
if !sess.opts.cg.prefer_dynamic || sess.crt_static(Some(ty)) =>
102+
{
101103
Linkage::Static
102104
}
103105
config::CrateType::Executable => Linkage::Dynamic,
@@ -129,7 +131,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
129131
// If any are not found, generate some nice pretty errors.
130132
if ty == config::CrateType::Staticlib
131133
|| (ty == config::CrateType::Executable
132-
&& sess.crt_static()
134+
&& sess.crt_static(Some(ty))
133135
&& !sess.target.target.options.crt_static_allows_dylibs)
134136
{
135137
for &cnum in tcx.crates().iter() {

src/librustc_session/session.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -543,25 +543,34 @@ impl Session {
543543
.unwrap_or(self.opts.debug_assertions)
544544
}
545545

546-
pub fn crt_static(&self) -> bool {
546+
/// Check whether this compile session and crate type use static crt.
547+
pub fn crt_static(&self, crate_type: Option<config::CrateType>) -> bool {
547548
// If the target does not opt in to crt-static support, use its default.
548549
if self.target.target.options.crt_static_respected {
549-
self.crt_static_feature()
550+
self.crt_static_feature(crate_type)
550551
} else {
551552
self.target.target.options.crt_static_default
552553
}
553554
}
554555

555-
pub fn crt_static_feature(&self) -> bool {
556+
/// Check whether this compile session and crate type use `crt-static` feature.
557+
pub fn crt_static_feature(&self, crate_type: Option<config::CrateType>) -> bool {
556558
let requested_features = self.opts.cg.target_feature.split(',');
557559
let found_negative = requested_features.clone().any(|r| r == "-crt-static");
558560
let found_positive = requested_features.clone().any(|r| r == "+crt-static");
559561

560-
// If the target we're compiling for requests a static crt by default,
561-
// then see if the `-crt-static` feature was passed to disable that.
562-
// Otherwise if we don't have a static crt by default then see if the
563-
// `+crt-static` feature was passed.
564-
if self.target.target.options.crt_static_default { !found_negative } else { found_positive }
562+
if found_positive || found_negative {
563+
found_positive
564+
} else if crate_type == Some(config::CrateType::ProcMacro)
565+
|| self.opts.crate_types.contains(&config::CrateType::ProcMacro)
566+
{
567+
// FIXME: When crate_type is not available,
568+
// we use compiler options to determine the crate_type.
569+
// We can't check `#![crate_type = "proc-macro"]` here.
570+
false
571+
} else {
572+
self.target.target.options.crt_static_default
573+
}
565574
}
566575

567576
pub fn must_not_eliminate_frame_pointers(&self) -> bool {

src/test/ui/proc-macro/crt-static.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test proc-macro crate can be built without addtional RUSTFLAGS
2+
// on musl target
3+
// override -Ctarget-feature=-crt-static from compiletest
4+
// compile-flags: -Ctarget-feature=
5+
// build-pass
6+
#![crate_type = "proc-macro"]
7+
8+
extern crate proc_macro;
9+
10+
use proc_macro::TokenStream;
11+
12+
#[proc_macro_derive(Foo)]
13+
pub fn derive_foo(input: TokenStream) -> TokenStream {
14+
input
15+
}

0 commit comments

Comments
 (0)