Skip to content

Commit 7ca1b2f

Browse files
committed
Don't use static crt by default when build proc-macro.
1 parent 97b3d81 commit 7ca1b2f

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

src/librustc_codegen_ssa/back/link.rs

+5-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,7 @@ 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") {
12921292
position_independent_executable = true;
12931293
}
12941294
}
@@ -1373,7 +1373,7 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13731373
if crate_type != config::CrateType::Executable {
13741374
cmd.build_dylib(out_filename);
13751375
}
1376-
if crate_type == config::CrateType::Executable && sess.crt_static() {
1376+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
13771377
cmd.build_static_executable();
13781378
}
13791379

src/librustc_codegen_utils/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ 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)) && !sess.target.target.options.crt_static_allows_dylibs {
171171
return true;
172172
}
173173
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ 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 if !sess.opts.cg.prefer_dynamic || sess.crt_static(Some(ty)) => {
101101
Linkage::Static
102102
}
103103
config::CrateType::Executable => Linkage::Dynamic,
@@ -129,7 +129,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
129129
// If any are not found, generate some nice pretty errors.
130130
if ty == config::CrateType::Staticlib
131131
|| (ty == config::CrateType::Executable
132-
&& sess.crt_static()
132+
&& sess.crt_static(Some(ty))
133133
&& !sess.target.target.options.crt_static_allows_dylibs)
134134
{
135135
for &cnum in tcx.crates().iter() {

src/librustc_session/session.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -540,25 +540,50 @@ impl Session {
540540
.unwrap_or(self.opts.debug_assertions)
541541
}
542542

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

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

557-
// If the target we're compiling for requests a static crt by default,
558-
// then see if the `-crt-static` feature was passed to disable that.
559-
// Otherwise if we don't have a static crt by default then see if the
560-
// `+crt-static` feature was passed.
561-
if self.target.target.options.crt_static_default { !found_negative } else { found_positive }
559+
if self.target.target.options.crt_static_default {
560+
// `proc-macro` always required to be compiled to dylibs.
561+
// We don't use a static crt unless the `+crt-static` feature was passed.
562+
if !self.target.target.options.crt_static_allows_dylibs {
563+
match crate_type {
564+
Some(config::CrateType::ProcMacro) => found_positive,
565+
Some(_) => !found_negative,
566+
None => {
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+
if self.opts.crate_types.contains(&config::CrateType::ProcMacro) {
571+
found_positive
572+
} else {
573+
!found_negative
574+
}
575+
}
576+
}
577+
} else {
578+
// If the target we're compiling for requests a static crt by default,
579+
// then see if the `-crt-static` feature was passed to disable that.
580+
!found_negative
581+
}
582+
} else {
583+
// If the target we're compiling for don't have a static crt by default then see if the
584+
// `+crt-static` feature was passed.
585+
found_positive
586+
}
562587
}
563588

564589
pub fn must_not_eliminate_frame_pointers(&self) -> bool {
+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+
4+
// run-pass
5+
// compile-flags: --target=x86_64-unknown-linux-musl
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)