Skip to content

Commit 7ceebd9

Browse files
committed
Auto merge of #69519 - 12101111:remove-proc-macro-check, r=nagisa
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. Fix rust-lang/cargo#7563
2 parents 660326e + afd374f commit 7ceebd9

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

src/librustc_codegen_ssa/back/link.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
502502
cmd.args(args);
503503
}
504504
if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) {
505-
if sess.crt_static() {
505+
if sess.crt_static(Some(crate_type)) {
506506
cmd.args(args);
507507
}
508508
}
@@ -528,7 +528,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
528528
cmd.arg(get_file_path(sess, obj));
529529
}
530530

531-
if crate_type == config::CrateType::Executable && sess.crt_static() {
531+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
532532
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
533533
cmd.arg(get_file_path(sess, obj));
534534
}
@@ -572,7 +572,7 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
572572
for obj in &sess.target.target.options.post_link_objects {
573573
cmd.arg(get_file_path(sess, obj));
574574
}
575-
if sess.crt_static() {
575+
if sess.crt_static(Some(crate_type)) {
576576
for obj in &sess.target.target.options.post_link_objects_crt {
577577
cmd.arg(get_file_path(sess, obj));
578578
}
@@ -1302,7 +1302,8 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13021302
let more_args = &sess.opts.cg.link_arg;
13031303
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
13041304

1305-
if is_pic(sess) && !sess.crt_static() && !args.any(|x| *x == "-static") {
1305+
if is_pic(sess) && !sess.crt_static(Some(crate_type)) && !args.any(|x| *x == "-static")
1306+
{
13061307
position_independent_executable = true;
13071308
}
13081309
}
@@ -1387,7 +1388,7 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13871388
if crate_type != config::CrateType::Executable {
13881389
cmd.build_dylib(out_filename);
13891390
}
1390-
if crate_type == config::CrateType::Executable && sess.crt_static() {
1391+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
13911392
cmd.build_static_executable();
13921393
}
13931394

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
@@ -555,25 +555,34 @@ impl Session {
555555
.unwrap_or(self.opts.debug_assertions)
556556
}
557557

558-
pub fn crt_static(&self) -> bool {
558+
/// Check whether this compile session and crate type use static crt.
559+
pub fn crt_static(&self, crate_type: Option<config::CrateType>) -> bool {
559560
// If the target does not opt in to crt-static support, use its default.
560561
if self.target.target.options.crt_static_respected {
561-
self.crt_static_feature()
562+
self.crt_static_feature(crate_type)
562563
} else {
563564
self.target.target.options.crt_static_default
564565
}
565566
}
566567

567-
pub fn crt_static_feature(&self) -> bool {
568+
/// Check whether this compile session and crate type use `crt-static` feature.
569+
pub fn crt_static_feature(&self, crate_type: Option<config::CrateType>) -> bool {
568570
let requested_features = self.opts.cg.target_feature.split(',');
569571
let found_negative = requested_features.clone().any(|r| r == "-crt-static");
570572
let found_positive = requested_features.clone().any(|r| r == "+crt-static");
571573

572-
// If the target we're compiling for requests a static crt by default,
573-
// then see if the `-crt-static` feature was passed to disable that.
574-
// Otherwise if we don't have a static crt by default then see if the
575-
// `+crt-static` feature was passed.
576-
if self.target.target.options.crt_static_default { !found_negative } else { found_positive }
574+
if found_positive || found_negative {
575+
found_positive
576+
} else if crate_type == Some(config::CrateType::ProcMacro)
577+
|| crate_type == None && self.opts.crate_types.contains(&config::CrateType::ProcMacro)
578+
{
579+
// FIXME: When crate_type is not available,
580+
// we use compiler options to determine the crate_type.
581+
// We can't check `#![crate_type = "proc-macro"]` here.
582+
false
583+
} else {
584+
self.target.target.options.crt_static_default
585+
}
577586
}
578587

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// ignore-wasm32
6+
// build-pass
7+
#![crate_type = "proc-macro"]
8+
9+
extern crate proc_macro;
10+
11+
use proc_macro::TokenStream;
12+
13+
#[proc_macro_derive(Foo)]
14+
pub fn derive_foo(input: TokenStream) -> TokenStream {
15+
input
16+
}

0 commit comments

Comments
 (0)