@@ -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 {
0 commit comments