:: build-windows-toolchain.bat
::
:: This source file is part of the Swift.org open source project
::
:: Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
:: Licensed under Apache License v2.0 with Runtime Library Exception
::
:: See https://swift.org/LICENSE.txt for license information
:: See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

setlocal enableextensions enabledelayedexpansion

:: Work around CI invocation in vsdevcmd
:: The build relies on build.ps1, which should not be called in a vs dev cmd
if "%VSCMD_ARG_HOST_ARCH%"=="" goto Start
echo This script should not be called in a vs developer command prompt
echo Reeinvoking script in the default environment
set TEMP=%~dp0..\..\tmp
mkdir %TEMP% 2>&1 1>nul
echo set PYTHON_HOME=%PYTHON_HOME%> %TEMP%\call-build.cmd
echo set SKIP_TESTS=%SKIP_TESTS%>> %TEMP%\call-build.cmd
echo set INCLUDE_PACKAGING=%INCLUDE_PACKAGING%>> %TEMP%\call-build.cmd
echo set SKIP_UPDATE_CHECKOUT=%SKIP_UPDATE_CHECKOUT%>> %TEMP%\call-build.cmd
echo set REPO_SCHEME=%REPO_SCHEME%>> %TEMP%\call-build.cmd
echo set WINDOWS_SDKS=%WINDOWS_SDKS%>> %TEMP%\call-build.cmd
echo set HOST_ARCH_NAME=%HOST_ARCH_NAME%>> %TEMP%\call-build.cmd
echo "%~f0">> %TEMP%\call-build.cmd
start /i /b /wait cmd.exe /env=default /c "%TEMP%\call-build.cmd"
set ec=%errorlevel%
del %TEMP%\call-build.cmd
exit /b %ec%

:Start

:: Work around CI invocation with PYTHON_HOME containing double quotes
if defined PYTHON_HOME path !Path!;!PYTHON_HOME:"=!

:: Identify the SourceRoot
:: Normalize the SourceRoot to make it easier to read the output.
cd %~dp0\..\..
set SourceRoot=%CD%

:: Identify the BuildRoot
set BuildRoot=%SourceRoot%\build

md %BuildRoot%
subst T: /d
subst T: %BuildRoot% || (exit /b 1)
set BuildRoot=T:

:: Identify the PackageRoot
set PackageRoot=%BuildRoot%\artifacts

md %PackageRoot%

:: Setup temporary directories
md %BuildRoot%\tmp
set TEMP=%BuildRoot%\tmp
set TMP=%BuildRoot%\tmp
set TMPDIR=%BuildRoot%\tmp

set NINJA_STATUS=[%%f/%%t][%%p][%%es] 

:: Build the -Test argument, if any, by subtracting skipped tests
set TestArg=-Test lld,lldb,swift,dispatch,foundation,xctest,swift-format,sourcekit-lsp,
for %%I in (%SKIP_TESTS%) do (call set TestArg=%%TestArg:%%I,=%%)
if "%TestArg:~-1%"=="," (set TestArg=%TestArg:~0,-1%) else (set TestArg= )

:: Build the packaging arguments (skipped for normal PRs and an added stage for toolchain PRs)
set "PackagingArg=-SkipPackaging"
if not "%INCLUDE_PACKAGING%"=="" set "PackagingArg=-Stage %PackageRoot%"

:: Build the arguments related to Windows SDK builds
set "WindowsSDKArgs=-Windows"
if "%INCLUDE_PACKAGING%"=="" set "WindowsSDKArgs=%WindowsSDKArgs% -WindowsSDKLinkModes dynamic"
if not "%WINDOWS_SDKS%"=="" set "WindowsSDKArgs=%WindowsSDKArgs% -WindowsSDKArchitectures %WINDOWS_SDKS%"

:: Build the -HostArchName argument, if any.
set "HostArchNameArg="
if not "%HOST_ARCH_NAME%"=="" set "HostArchNameArg=-HostArchName %HOST_ARCH_NAME%"

call :CloneRepositories || (exit /b 1)

:: We only have write access to BuildRoot, so use that as the image root.
powershell.exe -ExecutionPolicy RemoteSigned -File %~dp0build.ps1 ^
  %HostArchNameArg% ^
  -SourceCache %SourceRoot% ^
  -BinaryCache %BuildRoot% ^
  -ImageRoot %BuildRoot% ^
  %WindowsSDKArgs% ^
  %PackagingArg% ^
  %TestArg% ^
  -IncludeSBoM ^
  -Summary || (exit /b 1)

:: Clean up the module cache
rd /s /q %LocalAppData%\clang\ModuleCache

goto :end
endlocal

:CloneRepositories
setlocal enableextensions enabledelayedexpansion

if defined SKIP_UPDATE_CHECKOUT goto :eof

if defined REPO_SCHEME set "args=--scheme %REPO_SCHEME%"

:: Always enable symbolic links
git config --global core.symlink true

:: Ensure that we have the files in the original line endings, the swift tests
:: depend on this being the case.
rem git -C "%SourceRoot%\swift" config --local core.autocrlf input
rem git -C "%SourceRoot%\swift" checkout-index --force --all

set "args=%args% --skip-repository swift"
set "args=%args% --skip-repository ninja"
set "args=%args% --skip-repository swift-integration-tests"
set "args=%args% --skip-repository swift-stress-tester"

call "%SourceRoot%\swift\utils\update-checkout.cmd" %args% --clone --skip-history --reset-to-remote --github-comment "%ghprbCommentBody%"

goto :eof
endlocal

:end
