@@ -215,6 +215,8 @@ fn cfg_alias(alias: &str, has_feature: bool) {
215215#[ derive( Debug ) ]
216216pub struct WindowsAttributes {
217217 window_icon_path : Option < PathBuf > ,
218+ /// Whether to statically link the Visual C++ runtime into the application binary on Windows MSVC targets
219+ static_vc_runtime : Option < bool > ,
218220 /// A string containing an [application manifest] to be included with the application on Windows.
219221 ///
220222 /// Defaults to:
@@ -257,6 +259,7 @@ impl WindowsAttributes {
257259 pub fn new ( ) -> Self {
258260 Self {
259261 window_icon_path : Default :: default ( ) ,
262+ static_vc_runtime : None ,
260263 app_manifest : Some ( include_str ! ( "windows-app-manifest.xml" ) . into ( ) ) ,
261264 append_rc_content : Vec :: new ( ) ,
262265 }
@@ -268,6 +271,7 @@ impl WindowsAttributes {
268271 Self {
269272 app_manifest : None ,
270273 window_icon_path : Default :: default ( ) ,
274+ static_vc_runtime : None ,
271275 append_rc_content : Vec :: new ( ) ,
272276 }
273277 }
@@ -282,6 +286,15 @@ impl WindowsAttributes {
282286 self
283287 }
284288
289+ /// Sets whether to statically link the Visual C++ runtime into the application binary on Windows MSVC targets.
290+ ///
291+ /// If unset, this is read from `build > windows > staticVCRuntime` in the Tauri configuration.
292+ #[ must_use]
293+ pub fn static_vc_runtime ( mut self , static_vc_runtime : bool ) -> Self {
294+ self . static_vc_runtime . replace ( static_vc_runtime) ;
295+ self
296+ }
297+
285298 /// Sets the [application manifest] to be included with the application on Windows.
286299 ///
287300 /// Defaults to:
@@ -489,6 +502,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
489502 json_patch:: merge ( & mut config, & merge_config) ;
490503 }
491504 let config: Config = serde_json:: from_value ( config) ?;
505+ let static_vc_runtime = should_static_link_vc_runtime ( & config, & attributes) ;
492506
493507 let s = config. identifier . split ( '.' ) ;
494508 let last = s. clone ( ) . count ( ) - 1 ;
@@ -705,7 +719,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
705719 }
706720 }
707721 }
708- "msvc" if env :: var_os ( "STATIC_VCRUNTIME" ) . is_some_and ( |v| v == "true" ) => {
722+ "msvc" if static_vc_runtime => {
709723 static_vcruntime:: build ( ) ;
710724 }
711725 _ => ( ) ,
@@ -726,6 +740,20 @@ fn to_winres_version(v: &semver::Version) -> u64 {
726740 ( v. major << 48 ) | ( v. minor << 32 ) | ( v. patch << 16 ) | build
727741}
728742
743+ fn should_static_link_vc_runtime ( config : & Config , attributes : & Attributes ) -> bool {
744+ if let Some ( value) = env:: var_os ( "STATIC_VCRUNTIME" ) {
745+ println ! (
746+ "cargo:warning=STATIC_VCRUNTIME is deprecated; use build.windows.staticVCRuntime in tauri.conf.json or tauri_build::WindowsAttributes::static_vc_runtime instead."
747+ ) ;
748+ value != "false"
749+ } else {
750+ attributes
751+ . windows_attributes
752+ . static_vc_runtime
753+ . unwrap_or ( config. build . windows . static_vc_runtime )
754+ }
755+ }
756+
729757#[ cfg( test) ]
730758mod tests {
731759 use semver:: Version ;
@@ -769,4 +797,86 @@ mod tests {
769797 ( 1 << 48 ) | ( 2 << 32 ) | ( 3 << 16 )
770798 ) ;
771799 }
800+
801+ #[ test]
802+ fn static_vc_runtime_chain ( ) {
803+ // 1. Nothing is set, should default to true
804+ let config = tauri_utils:: config:: Config :: default ( ) ;
805+ let attributes = crate :: Attributes :: new ( ) ;
806+ assert ! ( crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
807+
808+ // 2. Set to anything but "false" in env, should be true
809+ std:: env:: set_var ( "STATIC_VCRUNTIME" , "qweqe" ) ;
810+ let config = tauri_utils:: config:: Config :: default ( ) ;
811+ let attributes = crate :: Attributes :: new ( ) ;
812+ assert ! ( crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
813+ std:: env:: remove_var ( "STATIC_VCRUNTIME" ) ;
814+
815+ // 3. Set to "false" in env, should be false
816+ std:: env:: set_var ( "STATIC_VCRUNTIME" , "false" ) ;
817+ let config = tauri_utils:: config:: Config :: default ( ) ;
818+ let attributes = crate :: Attributes :: new ( ) ;
819+ assert ! ( !crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
820+ std:: env:: remove_var ( "STATIC_VCRUNTIME" ) ;
821+
822+ // 4. Set to true in attributes, should be true
823+ let config = tauri_utils:: config:: Config :: default ( ) ;
824+ let attributes = crate :: Attributes :: new ( )
825+ . windows_attributes ( crate :: WindowsAttributes :: new ( ) . static_vc_runtime ( true ) ) ;
826+ assert ! ( crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
827+
828+ // 5. Set to false in attributes, should be false
829+ let config = tauri_utils:: config:: Config :: default ( ) ;
830+ let attributes = crate :: Attributes :: new ( )
831+ . windows_attributes ( crate :: WindowsAttributes :: new ( ) . static_vc_runtime ( false ) ) ;
832+ assert ! ( !crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
833+
834+ // 6. Set to true in config, should be true
835+ let config = tauri_utils:: config:: Config {
836+ build : tauri_utils:: config:: BuildConfig {
837+ windows : tauri_utils:: config:: WindowsBuildConfig {
838+ static_vc_runtime : true ,
839+ } ,
840+ ..Default :: default ( )
841+ } ,
842+ ..Default :: default ( )
843+ } ;
844+ let attributes = crate :: Attributes :: new ( ) ;
845+ assert ! ( crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
846+
847+ // 7. Set to false in config, should be false
848+ let config = tauri_utils:: config:: Config {
849+ build : tauri_utils:: config:: BuildConfig {
850+ windows : tauri_utils:: config:: WindowsBuildConfig {
851+ static_vc_runtime : false ,
852+ } ,
853+ ..Default :: default ( )
854+ } ,
855+ ..Default :: default ( )
856+ } ;
857+ let attributes = crate :: Attributes :: new ( ) ;
858+ assert ! ( !crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
859+
860+ // 8. Set to true in config and false in attributes, should be false because attributes takes precedence over config
861+ let config = tauri_utils:: config:: Config {
862+ build : tauri_utils:: config:: BuildConfig {
863+ windows : tauri_utils:: config:: WindowsBuildConfig {
864+ static_vc_runtime : true ,
865+ } ,
866+ ..Default :: default ( )
867+ } ,
868+ ..Default :: default ( )
869+ } ;
870+ let attributes = crate :: Attributes :: new ( )
871+ . windows_attributes ( crate :: WindowsAttributes :: new ( ) . static_vc_runtime ( false ) ) ;
872+ assert ! ( !crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
873+
874+ // 9. Set to false in env and true in attributes, should be false because env takes precedence over attributes
875+ std:: env:: set_var ( "STATIC_VCRUNTIME" , "false" ) ;
876+ let config = tauri_utils:: config:: Config :: default ( ) ;
877+ let attributes = crate :: Attributes :: new ( )
878+ . windows_attributes ( crate :: WindowsAttributes :: new ( ) . static_vc_runtime ( true ) ) ;
879+ assert ! ( !crate :: should_static_link_vc_runtime( & config, & attributes) ) ;
880+ std:: env:: remove_var ( "STATIC_VCRUNTIME" ) ;
881+ }
772882}
0 commit comments