@@ -793,6 +793,10 @@ impl Build {
793793 /// When enabled on systems that support dynamic linking, this prevents
794794 /// linking with the shared libraries.
795795 ///
796+ /// If not specified, this falls back to:
797+ /// - `-Ctarget-features=+crt-static` when compiling in a build script.
798+ /// - A target-specific default.
799+ ///
796800 /// # Example
797801 ///
798802 /// ```no_run
@@ -1301,6 +1305,9 @@ impl Build {
13011305 /// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
13021306 ///
13031307 /// This option defaults to `false`, and affect only msvc targets.
1308+ ///
1309+ /// If not specified, this falls back to `-Ctarget-features=+crt-static`
1310+ /// when compiling in a build script.
13041311 pub fn static_crt ( & mut self , static_crt : bool ) -> & mut Build {
13051312 self . static_crt = Some ( static_crt) ;
13061313 self
@@ -1944,18 +1951,10 @@ impl Build {
19441951 ToolFamily :: Msvc { .. } => {
19451952 cmd. push_cc_arg ( "-nologo" . into ( ) ) ;
19461953
1947- let crt_flag = match self . static_crt {
1948- Some ( true ) => "-MT" ,
1949- Some ( false ) => "-MD" ,
1950- None => {
1951- let features = self . getenv ( "CARGO_CFG_TARGET_FEATURE" ) ;
1952- let features = features. as_deref ( ) . unwrap_or_default ( ) ;
1953- if features. to_string_lossy ( ) . contains ( "crt-static" ) {
1954- "-MT"
1955- } else {
1956- "-MD"
1957- }
1958- }
1954+ let crt_flag = if self . static_crt . unwrap_or_else ( || target. crt_static ( ) ) {
1955+ "-MT"
1956+ } else {
1957+ "-MD"
19591958 } ;
19601959 cmd. push_cc_arg ( crt_flag. into ( ) ) ;
19611960
@@ -2142,12 +2141,8 @@ impl Build {
21422141 cmd. args . push ( "-finput-charset=utf-8" . into ( ) ) ;
21432142 }
21442143
2145- if self . static_flag . is_none ( ) {
2146- let features = self . getenv ( "CARGO_CFG_TARGET_FEATURE" ) ;
2147- let features = features. as_deref ( ) . unwrap_or_default ( ) ;
2148- if features. to_string_lossy ( ) . contains ( "crt-static" ) {
2149- cmd. args . push ( "-static" . into ( ) ) ;
2150- }
2144+ if self . static_flag . is_none ( ) && target. crt_static ( ) {
2145+ cmd. args . push ( "-static" . into ( ) ) ;
21512146 }
21522147
21532148 // armv7 targets get to use armv7 instructions
0 commit comments