bpo-32507: Change Windows install to include app-local UCRT (#5119)

diff --git a/Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp b/Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp
index e88981f..2e468b7 100644
--- a/Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp
+++ b/Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp
@@ -281,10 +281,6 @@
         case ID_INSTALL_BUTTON:
             SavePageSettings();
 
-            if (!WillElevate() && !QueryElevateForCrtInstall()) {
-                break;
-            }
-
             hr = BalGetNumericVariable(L"InstallAllUsers", &installAllUsers);
             ExitOnFailure(hr, L"Failed to get install scope");
 
@@ -332,10 +328,6 @@
                 ReleaseStr(targetDir);
             }
 
-            if (!WillElevate() && !QueryElevateForCrtInstall()) {
-                break;
-            }
-
             OnPlan(_command.action);
             break;
 
@@ -2642,30 +2634,6 @@
         return result;
     }
 
-    BOOL QueryElevateForCrtInstall() {
-        // Called to prompt the user that even though they think they won't need
-        // to elevate, they actually will because of the CRT install.
-        if (IsCrtInstalled()) {
-            // CRT is already installed - no need to prompt
-            return TRUE;
-        }
-        
-        LONGLONG elevated;
-        HRESULT hr = BalGetNumericVariable(L"WixBundleElevated", &elevated);
-        if (SUCCEEDED(hr) && elevated) {
-            // Already elevated - no need to prompt
-            return TRUE;
-        }
-
-        LOC_STRING *locStr;
-        hr = LocGetString(_wixLoc, L"#(loc.ElevateForCRTInstall)", &locStr);
-        if (FAILED(hr)) {
-            BalLogError(hr, "Failed to get ElevateForCRTInstall string");
-            return FALSE;
-        }
-        return ::MessageBoxW(_hWnd, locStr->wzText, _theme->sczCaption, MB_YESNO) != IDNO;
-    }
-
     HRESULT EvaluateConditions() {
         HRESULT hr = S_OK;
         BOOL result = FALSE;
diff --git a/Tools/msi/bundle/bundle.targets b/Tools/msi/bundle/bundle.targets
index e0dae21..f882d2e 100644
--- a/Tools/msi/bundle/bundle.targets
+++ b/Tools/msi/bundle/bundle.targets
@@ -71,6 +71,7 @@
         <Package Include="..\tcltk\tcltk*.wixproj" />
         <Package Include="..\test\test*.wixproj" />
         <Package Include="..\tools\tools*.wixproj" />
+        <Package Include="..\ucrt\ucrt*.wixproj" />
     </ItemGroup>
     
     <PropertyGroup>
diff --git a/Tools/msi/bundle/packagegroups/crt.wxs b/Tools/msi/bundle/packagegroups/crt.wxs
index dc40475..61b756f 100644
--- a/Tools/msi/bundle/packagegroups/crt.wxs
+++ b/Tools/msi/bundle/packagegroups/crt.wxs
@@ -2,48 +2,22 @@
 <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
     <Fragment>
         <PackageGroup Id="crt">
-            <PackageGroupRef Id="crt_14.0_v6.0" />
-            <PackageGroupRef Id="crt_14.0_v6.1" />
-            <PackageGroupRef Id="crt_14.0_v6.2" />
-            <PackageGroupRef Id="crt_14.0_v6.3" />
-        </PackageGroup>
-    </Fragment>
-
-    <?foreach ver in v6.0;v6.1;v6.2;v6.3 ?>
-    <?if "$(var.ver)" = "v6.0" ?>
-    <?define msuver=6.0 ?>
-    <?elseif "$(var.ver)" = "v6.1" ?>
-    <?define msuver=6.1 ?>
-    <?elseif "$(var.ver)" = "v6.2" ?>
-    <?define msuver=8-RT ?>
-    <?elseif "$(var.ver)" = "v6.3" ?>
-    <?define msuver=8.1 ?>
-    <?else ?>
-    <?error unknown version $(var.ver) ?>
-    <?endif ?>
-
-    <Fragment>
-        <PackageGroup Id="crt_14.0_$(var.ver)">
-            <MsuPackage Id="crt_14.0_$(var.ver)_x86"
-                        KB="2999226"
-                        SourceFile="!(bindpath.redist)\Windows$(var.msuver)-KB2999226-x86.msu"
-                        DisplayName="!(loc.CRTDescription)"
-                        Description="!(loc.CRTDescription)"
+            <MsiPackage Id="ucrt_AllUsers"
+                        SourceFile="ucrt.msi"
                         Compressed="$(var.CompressMSI)"
                         DownloadUrl="$(var.DownloadUrl)"
-                        InstallCondition="not CRTInstalled and VersionNT = $(var.ver) and not VersionNT64 and (Include_core or Include_exe or Include_launcher or Include_pip) and not LauncherOnly" />
-
-            <MsuPackage Id="crt_14.0_$(var.ver)_x64"
-                        KB="2999226"
-                        SourceFile="!(bindpath.redist)\Windows$(var.msuver)-KB2999226-x64.msu"
-                        DisplayName="!(loc.CRTDescription)"
-                        Description="!(loc.CRTDescription)"
+                        ForcePerMachine="yes"
+                        InstallCondition="InstallAllUsers and not CRTInstalled and (Include_core or Include_exe or Include_pip) and not LauncherOnly">
+                <MsiProperty Name="TARGETDIR" Value="[TargetDir]" />
+            </MsiPackage>
+            <MsiPackage Id="ucrt_JustForMe"
+                        SourceFile="ucrt.msi"
                         Compressed="$(var.CompressMSI)"
                         DownloadUrl="$(var.DownloadUrl)"
-                        InstallCondition="not CRTInstalled and VersionNT64 = $(var.ver) and (Include_core or Include_exe or Include_launcher or Include_pip) and not LauncherOnly" />
+                        ForcePerMachine="no"
+                        InstallCondition="not InstallAllUsers and not CRTInstalled and (Include_core or Include_exe or Include_pip) and not LauncherOnly">
+                <MsiProperty Name="TARGETDIR" Value="[TargetDir]" />
+            </MsiPackage>
         </PackageGroup>
     </Fragment>
-
-    <?undef msuver ?>
-    <?endforeach ?>
 </Wix>
\ No newline at end of file
diff --git a/Tools/msi/exe/exe.wxs b/Tools/msi/exe/exe.wxs
index 03d43c6..98d31b3 100644
--- a/Tools/msi/exe/exe.wxs
+++ b/Tools/msi/exe/exe.wxs
@@ -3,10 +3,10 @@
     <Product Id="*" Language="!(loc.LCID)" Name="!(loc.Title)" Version="$(var.Version)" Manufacturer="!(loc.Manufacturer)" UpgradeCode="$(var.UpgradeCode)">
         <Package InstallerVersion="300" Compressed="yes" InstallScope="perUser" Platform="$(var.Platform)" />
         <MediaTemplate EmbedCab="yes" CompressionLevel="high" />
-        
+
         <PropertyRef Id="UpgradeTable" />
         <PropertyRef Id="REGISTRYKEY" />
-        
+
         <Feature Id="DefaultFeature" AllowAdvertise="no" Title="!(loc.Title)" Description="!(loc.Description)">
             <ComponentGroupRef Id="exe_python" Primary="yes" />
             <ComponentGroupRef Id="exe_reg" Primary="yes" />
@@ -14,7 +14,7 @@
             <ComponentGroupRef Id="exe_icons" />
             <ComponentRef Id="OptionalFeature" />
         </Feature>
-            
+
         <Feature Id="Shortcuts" AllowAdvertise="no" Title="!(loc.Title)" Description="!(loc.Description)">
             <ComponentGroupRef Id="exe_python" />
             <Component Id="exe_shortcut" Directory="MenuDir" Guid="*">
diff --git a/Tools/msi/get_externals.bat b/Tools/msi/get_externals.bat
index 913512a..d66781d 100644
--- a/Tools/msi/get_externals.bat
+++ b/Tools/msi/get_externals.bat
@@ -55,7 +55,7 @@
 set binaries=%binaries%     gpg
 set binaries=%binaries%     htmlhelp
 set binaries=%binaries%     nuget
-set binaries=%binaries%     redist
+set binaries=%binaries%     redist-1
 set binaries=%binaries%     wix
 
 for %%b in (%binaries%) do (
diff --git a/Tools/msi/msi.props b/Tools/msi/msi.props
index 0318f7a..0fe822a 100644
--- a/Tools/msi/msi.props
+++ b/Tools/msi/msi.props
@@ -54,8 +54,8 @@
         <OutputPath Condition="!HasTrailingSlash($(OutputPath))">$(OutputPath)\</OutputPath>
         <OutDir>$(OutputPath)</OutDir>
         <ReuseCabinetCache>true</ReuseCabinetCache>
-        <CRTRedist Condition="'$(CRTRedist)' == ''">$(ExternalsDir)\windows-installer\redist</CRTRedist>
-        <CRTRedist Condition="!Exists($(CRTRedist))"></CRTRedist>
+        <CRTRedist Condition="'$(CRTRedist)' == ''">$(ExternalsDir)\windows-installer\redist-1\$(Platform)</CRTRedist>
+        <CRTRedist>$([System.IO.Path]::GetFullPath($(CRTRedist)))</CRTRedist>
         <DocFilename>python$(MajorVersionNumber)$(MinorVersionNumber)$(MicroVersionNumber)$(ReleaseLevelName).chm</DocFilename>
 
         <InstallerVersion>$(MajorVersionNumber).$(MinorVersionNumber).$(Field3Value).0</InstallerVersion>
@@ -123,10 +123,7 @@
         <LinkerBindInputPaths Include="$(tcltkDir)">
             <BindName>tcltk</BindName>
         </LinkerBindInputPaths>
-        <LinkerBindInputPaths Include="$(CRTRedist)" Condition="'$(CRTRedist)' != ''">
-            <BindName>redist</BindName>
-        </LinkerBindInputPaths>
-        <LinkerBindInputPaths Include="$(VS140COMNTOOLS)\..\..\VC\redist\$(Platform)\Microsoft.VC140.CRT">
+        <LinkerBindInputPaths Include="$(CRTRedist)">
             <BindName>redist</BindName>
         </LinkerBindInputPaths>
         <LinkerBindInputPaths Include="$(BuildPath32)">
diff --git a/Tools/msi/ucrt/ucrt.wixproj b/Tools/msi/ucrt/ucrt.wixproj
new file mode 100644
index 0000000..52a7e0e
--- /dev/null
+++ b/Tools/msi/ucrt/ucrt.wixproj
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+    <PropertyGroup>
+        <ProjectGuid>{E233091D-2EE3-49D8-A7F1-6E266CE1B6B1}</ProjectGuid>
+        <SchemaVersion>2.0</SchemaVersion>
+        <OutputName>ucrt</OutputName>
+        <OutputType>Package</OutputType>
+    </PropertyGroup>
+    <Import Project="..\msi.props" />
+    <ItemGroup>
+        <Compile Include="ucrt.wxs" />
+    </ItemGroup>
+    <ItemGroup>
+        <EmbeddedResource Include="*.wxl" />
+    </ItemGroup>
+    <ItemGroup>
+        <InstallFiles Include="$(CRTRedist)\ucrt\*.dll">
+            <SourceBase>$(CRTRedist)</SourceBase>
+            <Source>!(bindpath.redist)\</Source>
+            <TargetBase>$(CRTRedist)\ucrt</TargetBase>
+            <Target_>InstallDirectory\</Target_>
+            <Group>ucrt_dll</Group>
+            <IncludeInCat>false</IncludeInCat>
+        </InstallFiles>
+    </ItemGroup>
+    
+    <Import Project="..\msi.targets" />
+</Project>
\ No newline at end of file
diff --git a/Tools/msi/ucrt/ucrt.wxs b/Tools/msi/ucrt/ucrt.wxs
new file mode 100644
index 0000000..76e5682
--- /dev/null
+++ b/Tools/msi/ucrt/ucrt.wxs
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
+    <Product Id="*" Language="!(loc.LCID)" Name="!(loc.Title)" Version="$(var.Version)" Manufacturer="!(loc.Manufacturer)" UpgradeCode="$(var.UpgradeCode)">
+        <Package InstallerVersion="300" Compressed="yes" InstallScope="perUser" Platform="$(var.Platform)" />
+        <MediaTemplate EmbedCab="yes" CompressionLevel="high" />
+        
+        <PropertyRef Id="UpgradeTable" />
+        <PropertyRef Id="REGISTRYKEY" />
+        
+        <Feature Id="DefaultFeature" AllowAdvertise="no" Title="!(loc.Title)" Description="!(loc.Description)">
+            <ComponentGroupRef Id="ucrt_dll" />
+        </Feature>
+    </Product>
+</Wix>
diff --git a/Tools/msi/ucrt/ucrt_en-US.wxl b/Tools/msi/ucrt/ucrt_en-US.wxl
new file mode 100644
index 0000000..f42dd5f
--- /dev/null
+++ b/Tools/msi/ucrt/ucrt_en-US.wxl
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<WixLocalization Culture="en-us" xmlns="http://schemas.microsoft.com/wix/2006/localization">
+    <String Id="Descriptor">C Runtime Library</String>
+    <String Id="ShortDescriptor">ucrt</String>
+</WixLocalization>
diff --git a/Tools/msi/uploadrelease.proj b/Tools/msi/uploadrelease.proj
index 96b99a4..e70ae1d 100644
--- a/Tools/msi/uploadrelease.proj
+++ b/Tools/msi/uploadrelease.proj
@@ -21,7 +21,7 @@
     </PropertyGroup>
     
     <ItemGroup>
-        <File Include="$(OutputPath)\*.msi;$(OutputPath)\*.msu">
+        <File Include="$(OutputPath)\*.msi">
             <CopyTo>$(MSITarget)</CopyTo>
         </File>
         <File Include="$(OutputPath)\*.exe;$(OutputPath)\*.zip">