Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/msvc
diff options
context:
space:
mode:
authorJohan Lorensson <lateralusx.github@gmail.com>2019-05-10 11:33:43 +0300
committerGitHub <noreply@github.com>2019-05-10 11:33:43 +0300
commit5f41fb86be4cc1bcc3b65383d47d9d5ea605fcfe (patch)
tree124df2bb0ecb2a19a53403c9694853b0c7761373 /msvc
parent643b2ebfc6ad8a7cb2c21d3cac1a89ca3ceb3c9f (diff)
parent6fc4fe419d2913fe03633ad49f9cd207188a3405 (diff)
Merge pull request #14332 from lateralusX/lateralusX/add-llvm-windows-msvc-build-packaging-support
Add LLVM Windows MSVC build/packaging/archive/download support.
Diffstat (limited to 'msvc')
-rwxr-xr-x[-rw-r--r--]msvc/build-external-btls.bat0
-rwxr-xr-x[-rw-r--r--]msvc/build-external-llvm.bat29
-rw-r--r--msvc/build-external-llvm.vcxproj13
-rwxr-xr-xmsvc/install-llvm-mono-build.bat77
-rwxr-xr-x[-rw-r--r--]msvc/install.bat0
-rwxr-xr-x[-rw-r--r--]msvc/jaystuff.bat0
-rwxr-xr-x[-rw-r--r--]msvc/libmono.bat0
-rw-r--r--msvc/llvm-cmake-config.bat52
-rwxr-xr-x[-rw-r--r--]msvc/mono-sgen-msvc.bat208
-rwxr-xr-xmsvc/mono-sgen-msvc.sh42
-rw-r--r--msvc/mono.external.targets4
-rw-r--r--msvc/mono.vcxproj20
-rwxr-xr-x[-rw-r--r--]msvc/package.bat0
-rwxr-xr-x[-rw-r--r--]msvc/run-msbuild.bat159
-rwxr-xr-xmsvc/run-msbuild.sh33
-rwxr-xr-xmsvc/setup-vs-msbuild-env.bat104
-rwxr-xr-xmsvc/setup-vs-msvcbuild-env.bat155
-rwxr-xr-xmsvc/setup-windows-env.bat64
-rwxr-xr-x[-rw-r--r--]msvc/test-config-setup.bat0
-rwxr-xr-xmsvc/winsetup.bat98
20 files changed, 547 insertions, 511 deletions
diff --git a/msvc/build-external-btls.bat b/msvc/build-external-btls.bat
index d4de955a5a0..d4de955a5a0 100644..100755
--- a/msvc/build-external-btls.bat
+++ b/msvc/build-external-btls.bat
diff --git a/msvc/build-external-llvm.bat b/msvc/build-external-llvm.bat
index 22a0ad50938..bac098b49bf 100644..100755
--- a/msvc/build-external-llvm.bat
+++ b/msvc/build-external-llvm.bat
@@ -275,37 +275,10 @@ if exist "%LLVM_BUILD_DIR%\install.vcxproj" (
"%MSBUILD%" "%LLVM_BUILD_DIR%\install.vcxproj" /p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% /v:m /nologo
)
-if not exist "%LLVM_INSTALL_DIR%\bin\opt.exe" (
- echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\opt.exe"
+call "install-llvm-mono-build.bat" "%LLVM_INSTALL_DIR%" "%MONO_DIST_DIR%" || (
goto ON_ERROR
)
-if not exist "%LLVM_INSTALL_DIR%\bin\llc.exe" (
- echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llc.exe"
- goto ON_ERROR
-)
-
-if not exist "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" (
- echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe"
- goto ON_ERROR
-)
-
-if not exist "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" (
- echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe"
- goto ON_ERROR
-)
-
-if not exist "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" (
- echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-as.exe"
- goto ON_ERROR
-)
-
-copy /Y "%LLVM_INSTALL_DIR%\bin\opt.exe" "%MONO_DIST_DIR%" >nul 2>&1
-copy /Y "%LLVM_INSTALL_DIR%\bin\llc.exe" "%MONO_DIST_DIR%" >nul 2>&1
-copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" "%MONO_DIST_DIR%" >nul 2>&1
-copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" "%MONO_DIST_DIR%" >nul 2>&1
-copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" "%MONO_DIST_DIR%" >nul 2>&1
-
goto ON_SUCCESS
:ON_CLEAN_LLVM
diff --git a/msvc/build-external-llvm.vcxproj b/msvc/build-external-llvm.vcxproj
index c851755ef2c..9b06ed7f69a 100644
--- a/msvc/build-external-llvm.vcxproj
+++ b/msvc/build-external-llvm.vcxproj
@@ -162,16 +162,23 @@
<_LLVMAdditionalCMakeArgs>$(_LLVMEnableAsserts)</_LLVMAdditionalCMakeArgs>
<_LLVMBuildCommand>build-external-llvm.bat &quot;$(_LLVMSourceDir)&quot; &quot;$(_LLVMBuildDir)&quot; &quot;$(_LLVMInstallDir)&quot; &quot;$(_MonoOutputDir)&quot; &quot;$(_LLVMCFlags)&quot; &quot;$(_LLVMAdditionalCMakeArgs)&quot; &quot;$(Platform)&quot; &quot;$(Configuration)&quot;</_LLVMBuildCommand>
</PropertyGroup>
- <Target Name="_AfterBuildExternalLLVM" Condition="'$(MONO_ENABLE_LLVM)' == 'true' and '$(_MonoEnableInternalLLVM)' == 'true'">
- <Exec Command="$(_LLVMBuildCommand) &quot;Build&quot; &quot;$(MSBuildBinPath)\&quot; &quot;$(_MonoExternalBuildForceMSBuild)&quot;">
+ <Target Name="_LLVMBuild" Condition="'$(MONO_ENABLE_LLVM)' == 'true' and '$(_MonoEnableInternalLLVM)' == 'true'">
+ <Exec Command="$(_LLVMBuildCommand) &quot;Build&quot; &quot;$(MSBuildBinPath)\&quot; &quot;$(_MonoExternalBuildForceMSBuild)&quot;" Condition="'$(_MonoEnableInternalLLVM)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="_BuildExternalLLVMExitCode" />
</Exec>
</Target>
- <Target Name="_AfterCleanExternalLLVM" Condition="'$(MONO_ENABLE_LLVM)' == 'true' and '$(_MonoEnableInternalLLVM)' == 'true'">
+ <Target Name="_LLVMInstallBuild" Condition="'$(MONO_ENABLE_LLVM)' == 'true' and '$(_MonoEnableExternalLLVM)' == 'true'" DependsOnTargets="_SetupMonoLLVMBuildProperties">
+ <Exec Command="install-llvm-mono-build.bat &quot;$(MONO_LLVM_BIN_DIR)\..&quot; &quot;$(_MonoOutputDir)&quot;">
+ <Output TaskParameter="ExitCode" PropertyName="_BuildExternalLLVMExitCode" />
+ </Exec>
+ </Target>
+ <Target Name="_LLVMCleanBuild" Condition="'$(MONO_ENABLE_LLVM)' == 'true' and '$(_MonoEnableInternalLLVM)' == 'true'">
<Exec Command="$(_LLVMBuildCommand) &quot;Clean&quot; &quot;$(MSBuildBinPath)\&quot; &quot;$(_MonoExternalBuildForceMSBuild)&quot;">
<Output TaskParameter="ExitCode" PropertyName="_CleanExternalLLVMExitCode" />
</Exec>
</Target>
+ <Target Name="_AfterBuildExternalLLVM" Condition="'$(MONO_ENABLE_LLVM)' == 'true'" DependsOnTargets="_LLVMBuild;_LLVMInstallBuild" />
+ <Target Name="_AfterCleanExternalLLVM" Condition="'$(MONO_ENABLE_LLVM)' == 'true'" DependsOnTargets="_LLVMCleanBuild" />
<Target Name="AfterBuild" DependsOnTargets="_ConfigureExternalMonoLLVMBuildEnvironment;_AfterBuildExternalLLVM" />
<Target Name="AfterClean" DependsOnTargets="_ConfigureExternalMonoLLVMBuildEnvironment;_AfterCleanExternalLLVM" />
diff --git a/msvc/install-llvm-mono-build.bat b/msvc/install-llvm-mono-build.bat
new file mode 100755
index 00000000000..ebb1a86780c
--- /dev/null
+++ b/msvc/install-llvm-mono-build.bat
@@ -0,0 +1,77 @@
+:: --------------------------------------------------
+:: Install needed LLVM binaries from LLVM install directory
+:: into Mono build output directory.
+::
+:: %1 LLVM install root directory (internal or external LLVM build).
+:: %2 Mono distribution root directory.
+:: --------------------------------------------------
+
+@echo off
+setlocal
+
+set BUILD_RESULT=1
+
+set LLVM_INSTALL_DIR=%~1
+shift
+set MONO_DIST_DIR=%~1
+shift
+
+if "%LLVM_INSTALL_DIR%" == "" (
+ echo Missing LLVM install directory argument.
+ goto ECHO_USAGE
+)
+
+if "%MONO_DIST_DIR%" == "" (
+ echo Missing Mono dist directory argument.
+ goto ECHO_USAGE
+)
+
+if not exist "%LLVM_INSTALL_DIR%\bin\opt.exe" (
+ echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\opt.exe"
+ goto ON_ERROR
+)
+
+if not exist "%LLVM_INSTALL_DIR%\bin\llc.exe" (
+ echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llc.exe"
+ goto ON_ERROR
+)
+
+if not exist "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" (
+ echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe"
+ goto ON_ERROR
+)
+
+if not exist "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" (
+ echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe"
+ goto ON_ERROR
+)
+
+if not exist "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" (
+ echo Missing LLVM build output, "%LLVM_INSTALL_DIR%\bin\llvm-as.exe"
+ goto ON_ERROR
+)
+
+copy /Y "%LLVM_INSTALL_DIR%\bin\opt.exe" "%MONO_DIST_DIR%" >nul 2>&1
+copy /Y "%LLVM_INSTALL_DIR%\bin\llc.exe" "%MONO_DIST_DIR%" >nul 2>&1
+copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-dis.exe" "%MONO_DIST_DIR%" >nul 2>&1
+copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-mc.exe" "%MONO_DIST_DIR%" >nul 2>&1
+copy /Y "%LLVM_INSTALL_DIR%\bin\llvm-as.exe" "%MONO_DIST_DIR%" >nul 2>&1
+
+goto ON_SUCCESS
+
+:ON_SUCCESS
+
+set BUILD_RESULT=0
+goto ON_EXIT
+
+:ECHO_USAGE:
+ ECHO Usage: install-llvm-mono-build.bat [llvm_install_dir] [mono_dist_dir].
+
+:ON_ERROR
+ echo Failed to install LLVM binaries into Mono build output directory.
+ goto ON_EXIT
+
+:ON_EXIT
+ exit /b %BUILD_RESULT%
+
+@echo on
diff --git a/msvc/install.bat b/msvc/install.bat
index 1a433991a7d..1a433991a7d 100644..100755
--- a/msvc/install.bat
+++ b/msvc/install.bat
diff --git a/msvc/jaystuff.bat b/msvc/jaystuff.bat
index a8736d85b08..a8736d85b08 100644..100755
--- a/msvc/jaystuff.bat
+++ b/msvc/jaystuff.bat
diff --git a/msvc/libmono.bat b/msvc/libmono.bat
index 4c68bc1648d..4c68bc1648d 100644..100755
--- a/msvc/libmono.bat
+++ b/msvc/libmono.bat
diff --git a/msvc/llvm-cmake-config.bat b/msvc/llvm-cmake-config.bat
deleted file mode 100644
index 43bf8d6b58a..00000000000
--- a/msvc/llvm-cmake-config.bat
+++ /dev/null
@@ -1,52 +0,0 @@
-@ECHO OFF
-
-SET TEMP_PATH=%PATH%
-
-REM Update PATH to include local cmake and phython installations.
-REM SET PATH="C:\tools\cmake-3.10.2-win32-x86\bin";"C:\tools\python2.2.7.15\tools\";%PATH%
-
-SET TOP=%1
-IF "" == "%TOP%" (
- ECHO Error, first script parameter should be LLVM source folder.
- GOTO ON_ERROR
-)
-
-IF NOT EXIST "%TOP%" (
- ECHO Error, could not find "%TOP%".
- GOTO ON_ERROR
-)
-
-IF NOT EXIST "%~dp0mono.sln" (
- ECHO Error, script bust be located in same directory as mono.sln file.
- GOTO ON_ERROR
-)
-
-SET LLVM_SRC_PATH=%TOP%
-SET LLVM_BUILD_PATH=%TOP%\llvm-build
-
-REM Update to reflect value used in mono.props, MONO_LLVM_INSTALL_DIR_PREFIX property.
-SET LLVM_INSTALL_PATH=%~dp0dist\llvm
-
-SET CROSS_CMAKE_FLAGS=^
--DCMAKE_INSTALL_PREFIX="%LLVM_INSTALL_PATH%" ^
--DCMAKE_BUILD_TYPE=Release ^
--DLLVM_ENABLE_ZLIB=OFF ^
--DLLVM_TARGETS_TO_BUILD="X86" ^
--DCMAKE_CROSSCOMPILING=False ^
--DCMAKE_SYSTEM_NAME=Windows
-
-SET TEMP_WD=%CD%
-cd %LLVM_BUILD_PATH%
-ECHO cmake.exe -G "Visual Studio 14 2015 Win64" %CROSS_CMAKE_FLAGS% %LLVM_SRC_PATH%
-cmake.exe -G "Visual Studio 14 2015 Win64" %CROSS_CMAKE_FLAGS% %LLVM_SRC_PATH%
-cd %TEMP_WD%
-
-:ON_ERROR
- SET CONFIG_RESULT=ERRORLEVEL
- GOTO ON_EXIT
-
-:ON_EXIT
- SET PATH=%TEMP_PATH%
- EXIT /b %CONFIG_RESULT%
-
-@ECHO ON \ No newline at end of file
diff --git a/msvc/mono-sgen-msvc.bat b/msvc/mono-sgen-msvc.bat
index 975859abec3..5458a8fe7a1 100644..100755
--- a/msvc/mono-sgen-msvc.bat
+++ b/msvc/mono-sgen-msvc.bat
@@ -4,58 +4,21 @@
:: from VS setup for the corresponding architecture.
@echo off
-
setlocal
set EXECUTE_RESULT=1
-:: Make sure we can restore current working directory after setting up environment.
-:: Some of the VS scripts can change the current working directory.
-set CALLER_WD=%CD%
-
:: Get path for current running script.
set RUN_MONO_SGEN_MSVC_SCRIPT_PATH=%~dp0
-:: If we are running from none Windows shell we will need to restore a clean PATH
-:: before setting up VS MSVC build environment. If not there is a risk we will pick up
-:: for example cygwin binaries when running toolchain commands not explicitly setup by VS MSVC build environment.
-set HKCU_ENV_PATH=
-set HKLM_ENV_PATH=
-if "%SHELL%" == "/bin/bash" (
- for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKCU\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
- SET HKCU_ENV_PATH=%%b
- )
- for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
- SET HKLM_ENV_PATH=%%b
- )
-)
-
-:: Restore default path, if we are running from none Windows shell.
-if "%SHELL%" == "/bin/bash" (
- call :restore_default_path "%HKCU_ENV_PATH%" "%HKLM_ENV_PATH%"
+:: Optimization, check if we need to setup full build environment, only needed when running mono-sgen.exe as AOT compiler.
+if "%MONO_AS_AOT_COMPILER%" == "1" (
+ goto SETUP_VS_ENV
)
-:: NOTE, MSVC build mono-sgen.exe AOT compiler currently support 64-bit AMD codegen. Below will only setup
-:: amd64 versions of VS MSVC build environment and corresponding ClangC2 compiler.
-
-set VS_2015_TOOLCHAIN_ARCH=amd64
-set VS_2015_VCVARS_ARCH=%VS_2015_TOOLCHAIN_ARCH%\vcvars64.bat
-set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
-set VS_2017_VCVARS_ARCH=vcvars64.bat
-set VS_2017_CLANGC2_ARCH=HostX64
-
-:: 32-bit AOT toolchains for MSVC build mono-sgen.exe is currently not supported.
-:: set VS_2015_TOOLCHAIN_ARCH=x86
-:: set VS_2015_VCVARS_ARCH=vcvars32.bat
-:: set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
-:: set VS_2017_VCVARS_ARCH=vcvars32.bat
-:: set VS_2017_CLANGC2_ARCH=HostX86
-
set MONO_AS_AOT_COMPILER=0
-set VS_CLANGC2_TOOLS_BIN_PATH=
-
-:: Optimization, check if we need to setup full build environment, only needed when running mono-sgen.exe as AOT compiler.
-echo.%* | findstr /c:"--aot=" > nul && (
+:: Look for --aot or --aot=, --aot-path alone should not trigger setup of VS env.
+echo.%* | findstr /r /c:".*--aot[^-a-zA-Z0-9].*" > nul && (
set MONO_AS_AOT_COMPILER=1
)
@@ -69,7 +32,8 @@ goto ON_EXECUTE
:: Try setting up VS MSVC build environment.
:SETUP_VS_ENV
-:: Optimization, check if we have something that looks like a MSVC build environment already available.
+:: Optimization, check if we have something that looks like a VS MSVC build environment
+:: already available.
if /i not "%VCINSTALLDIR%" == "" (
if /i not "%INCLUDE%" == "" (
if /i not "%LIB%" == "" (
@@ -78,152 +42,56 @@ if /i not "%VCINSTALLDIR%" == "" (
)
)
-:: Visual Studio 2015 == 14.0
-if "%VisualStudioVersion%" == "14.0" (
- goto SETUP_VS_2015
-)
-
-:: Visual Studio 2017 == 15.0
-if "%VisualStudioVersion%" == "15.0" (
- goto SETUP_VS_2017
-)
-
-:SETUP_VS_2015
-
-set VS_2015_VCINSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\
-
-:: Try to locate installed VS2015 Clang/C2.
-SET VS_2015_CLANGC2_TOOLS_BIN_PATH=%VS_2015_VCINSTALL_DIR%ClangC2\bin\%VS_2015_CLANGC2_ARCH%\
-SET VS_2015_CLANGC2_TOOLS_BIN=%VS_2015_CLANGC2_TOOLS_BIN_PATH%clang.exe
-
-if not exist "%VS_2015_CLANGC2_TOOLS_BIN%" (
- goto SETUP_VS_2017
-)
-
-:SETUP_VS_2015_BUILD_TOOLS
-
-:: Try to locate VS2015 build tools installation.
-set VS_2015_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\
-set VS_2015_BUILD_TOOLS_CMD=%VS_2015_BUILD_TOOLS_INSTALL_DIR%vcbuildtools.bat
-
-:: Setup VS2015 VC development environment using build tools installation.
-call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%VS_2015_TOOLCHAIN_ARCH%" "%CALLER_WD%" && (
- set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
- goto ON_EXECUTE
-)
-
-:SETUP_VS_2015_VC
+:: Setup Windows environment.
+call %RUN_MONO_SGEN_MSVC_SCRIPT_PATH%setup-windows-env.bat
-:: Try to locate installed VS2015 VC environment.
-set VS_2015_DEV_CMD=%VS_2015_VCINSTALL_DIR%bin\%VS_2015_VCVARS_ARCH%
-
-call :setup_build_env "%VS_2015_DEV_CMD%" "" "%CALLER_WD%" && (
- set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
- goto ON_EXECUTE
+if "%MONO_VS_MSVCBUILD_ENV_FILE%" == "" (
+ set MONO_VS_MSVCBUILD_ENV_FILE=%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%mono-sgen.exe.env
)
-:SETUP_VS_2017
-
-:: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
-set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
-set VS_2017_VCINSTALL_DIR=
-
-:: Try to locate installed VS2017 VC environment.
-if exist "%VSWHERE_TOOLS_BIN%" (
- for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
- set VS_2017_VCINSTALL_DIR=%%a\VC\
+:: Check import of VS MSVC build environment using a file instead of running all commands.
+:: NOTE, this is an optimization since setting up a development command
+:: prompt could take some time.
+if /i "%MONO_IMPORT_VS_MSVCBUILD_ENV_FILE%" == "true" (
+ if exist "%MONO_VS_MSVCBUILD_ENV_FILE%" (
+ for /f "delims=" %%a in (%MONO_VS_MSVCBUILD_ENV_FILE%) do SET %%a
)
)
-:: Try to locate installed VS2017 Clang/C2.
-SET VS_2017_CLANGC2_VERSION_FILE=%VS_2017_VCINSTALL_DIR%Auxiliary/Build/Microsoft.ClangC2Version.default.txt
-if not exist "%VS_2017_CLANGC2_VERSION_FILE%" (
- goto ON_ENV_ERROR
-)
-
-set /p VS_2017_CLANGC2_VERSION=<"%VS_2017_CLANGC2_VERSION_FILE%"
-set VS_2017_CLANGC2_TOOLS_BIN_PATH=%VS_2017_VCINSTALL_DIR%Tools\ClangC2\%VS_2017_CLANGC2_VERSION%\bin\%VS_2017_CLANGC2_ARCH%\
-set VS_2017_CLANGC2_TOOLS_BIN=%VS_2017_CLANGC2_TOOLS_BIN_PATH%clang.exe
-if not exist "%VS_2017_CLANGC2_TOOLS_BIN%" (
- goto ON_ENV_ERROR
-)
-
-:SETUP_VS_2017_BUILD_TOOLS
-
-:: Try to locate VS2017 build tools installation.
-set VS_2017_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\
-set VS_2017_BUILD_TOOLS_CMD=%VS_2017_BUILD_TOOLS_INSTALL_DIR%VC\Auxiliary\Build\%VS_2017_VCVARS_ARCH%
-
-:: Setup VS2017 VC development environment using build tools installation.
-call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "" "%CALLER_WD%" && (
- set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
+if not "%MONO_MSVC_PATH%" == "" (
+ set "PATH=%MONO_MSVC_PATH%;%PATH%"
goto ON_EXECUTE
)
-:SETUP_VS_2017_VC
-
-:: Try to locate installed VS2017 VC environment.
-set VS_2017_DEV_CMD=%VS_2017_VCINSTALL_DIR%Auxiliary\Build\%VS_2017_VCVARS_ARCH%
+:: Setup VS MSVC build environment.
+set TEMP_PATH=%PATH%
+call %RUN_MONO_SGEN_MSVC_SCRIPT_PATH%setup-vs-msvcbuild-env.bat
+call set MONO_MSVC_PATH=%%PATH:%TEMP_PATH%=%%
-:: Setup VS2017 VC development environment using VS installation.
-call :setup_build_env "%VS_2017_DEV_CMD%" "" "%CALLER_WD%" && (
- set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
- goto ON_EXECUTE
+:: Check if msvc env should be exported into file for later import.
+for /f %%a in ('uuidgen.exe') do set NEW_UUID=%%a
+if /i "%MONO_EXPORT_VS_MSVCBUILD_ENV_FILE%" == "true" (
+ SET VCINSTALLDIR >> "%TEMP%\%NEW_UUID%.env"
+ SET INCLUDE >> "%TEMP%\%NEW_UUID%.env"
+ SET LIB >> "%TEMP%\%NEW_UUID%.env"
+ SET MONO_MSVC_PATH >> "%TEMP%\%NEW_UUID%.env"
+ move /Y "%TEMP%\%NEW_UUID%.env" "%MONO_VS_MSVCBUILD_ENV_FILE%" >nul 2>&1
)
-:ON_ENV_ERROR
-
-echo Warning, failed to setup build environment needed by MSVC build mono-sgen.exe running as an AOT compiler.
-echo Incomplete build environment can cause AOT compiler build due to missing compiler, linker and platform libraries.
-
:ON_EXECUTE
-:: Add mono.sgen.exe (needed for optional LLVM tooling) and ClangC2 folders to PATH
-set "PATH=%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%;%VS_CLANGC2_TOOLS_BIN_PATH%;%PATH%"
+:: Add mono.sgen.exe (needed for optional LLVM tooling) to PATH
+set "PATH=%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%;%PATH%"
call "%RUN_MONO_SGEN_MSVC_SCRIPT_PATH%mono-sgen.exe" %* && (
- set EXCEUTE_RESULT=0
+ set EXECUTE_RESULT=0
) || (
- set EXCEUTE_RESULT=1
+ set EXECUTE_RESULT=1
if not %ERRORLEVEL% == 0 (
- set EXCEUTE_RESULT=%ERRORLEVEL%
- )
-)
-
-exit /b %EXCEUTE_RESULT%
-
-:setup_build_env
-
-:: Check if VS build environment script exists.
-if not exist "%~1" (
- goto setup_build_env_error
-)
-
-:: Run VS build environment script.
-call "%~1" %~2 > NUL
-
-:: Restore callers working directory in case it has been changed by VS scripts.
-cd /d "%~3"
-
-goto setup_build_env_exit
-
-:setup_build_env_error
-exit /b 1
-
-:setup_build_env_exit
-goto :EOF
-
-:restore_default_path
-
-:: Restore default PATH.
-if not "%~2" == "" (
- if not "%~1" == "" (
- set "PATH=%~2;%~1"
- ) else (
- set "PATH=%~2"
+ set EXECUTE_RESULT=%ERRORLEVEL%
)
)
-goto :EOF
+exit /b %EXECUTE_RESULT%
-@echo on
+@echo on \ No newline at end of file
diff --git a/msvc/mono-sgen-msvc.sh b/msvc/mono-sgen-msvc.sh
index 03757366d09..a7e9ba44e9b 100755
--- a/msvc/mono-sgen-msvc.sh
+++ b/msvc/mono-sgen-msvc.sh
@@ -11,11 +11,47 @@
# Optimization, only run full build environment when running mono-sgen.exe as AOT compiler.
# If not, just run mono-sgen.exe with supplied arguments.
+function win32_format_path {
+ local formatted_path=$1
+ local host_win32_wsl=0
+ local host_win32_cygwin=0
+
+ host_uname="$(uname -a)"
+ case "$host_uname" in
+ *Microsoft*)
+ host_win32_wsl=1
+ ;;
+ CYGWIN*)
+ host_win32_cygwin=1
+ ;;
+ esac
+
+ if [[ $host_win32_wsl = 1 ]] && [[ $1 == "/mnt/"* ]]; then
+ formatted_path="$(wslpath -a -w "$1")"
+ elif [[ $host_win32_cygwin = 1 ]] && [[ $1 == "/cygdrive/"* ]]; then
+ formatted_path="$(cygpath -a -w "$1")"
+ fi
+
+ echo "$formatted_path"
+}
+
MONO_SGEN_MSVC_SCRIPT_PATH=$(cd "$(dirname "$0")"; pwd)
-if [[ "$@" != *"--aot="* ]]; then
+MONO_AS_AOT_COMPILER=0
+if [[ "$@" =~ .*--aot[^-a-zA-Z0-9].*|.*--aot$ ]]; then
+ MONO_AS_AOT_COMPILER=1
+fi
+
+if [[ $MONO_AS_AOT_COMPILER = 0 ]]; then
"$MONO_SGEN_MSVC_SCRIPT_PATH/mono-sgen.exe" "$@"
else
- MONO_SGEN_MSVC_SCRIPT_PATH=$(cygpath -w "$MONO_SGEN_MSVC_SCRIPT_PATH/mono-sgen-msvc.bat")
- "$WINDIR/System32/cmd.exe" /c "$MONO_SGEN_MSVC_SCRIPT_PATH" "$@"
+ MONO_SGEN_MSVC_SCRIPT_PATH=$(win32_format_path "$MONO_SGEN_MSVC_SCRIPT_PATH/mono-sgen-msvc.bat")
+
+ WINDOWS_CMD=$(which cmd.exe)
+ if [ ! -f $WINDOWS_CMD ]; then
+ WINDOWS_CMD=$WINDIR/System32/cmd.exe
+ fi
+
+ export MONO_AS_AOT_COMPILER
+ "$WINDOWS_CMD" /c "$MONO_SGEN_MSVC_SCRIPT_PATH" "$@"
fi
diff --git a/msvc/mono.external.targets b/msvc/mono.external.targets
index 6b732ac35de..c3603ec06d5 100644
--- a/msvc/mono.external.targets
+++ b/msvc/mono.external.targets
@@ -113,8 +113,8 @@
<PropertyGroup>
<_LLVMSourceDir Condition="'$(_LLVMSourceDir)' == ''">$(MONO_INTERNAL_LLVM_SOURCE_DIR)</_LLVMSourceDir>
<_LLVMSourceDir Condition="'$(_LLVMSourceDir)' == ''">$(_MonoSourceDir)\external\llvm</_LLVMSourceDir>
- <_LLVMBuildDir>$([System.IO.Path]::GetFullPath('$(MONO_BUILD_DIR_PREFIX)$(Platform)\obj\external\llvm-build\$(Configuration)'))</_LLVMBuildDir>
- <_LLVMInstallDir>$(_LLVMBuildDir)\install</_LLVMInstallDir>
+ <_LLVMBuildDir Condition="'$(_LLVMBuildDir)' == ''">$([System.IO.Path]::GetFullPath('$(MONO_BUILD_DIR_PREFIX)$(Platform)\obj\external\llvm-build\$(Configuration)'))</_LLVMBuildDir>
+ <_LLVMInstallDir Condition="'$(_LLVMInstallDir)' == ''">$(_LLVMBuildDir)\install</_LLVMInstallDir>
<_MonoLLVMConfig>$(_LLVMInstallDir)\bin\llvm-config.exe</_MonoLLVMConfig>
</PropertyGroup>
diff --git a/msvc/mono.vcxproj b/msvc/mono.vcxproj
index 7c95a2d6f63..4602e6c91f5 100644
--- a/msvc/mono.vcxproj
+++ b/msvc/mono.vcxproj
@@ -113,7 +113,10 @@
</ProjectReference>
<PostBuildEvent>
<Command>xcopy "$(SolutionDir)mono-sgen-msvc.sh" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
-xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
+
+xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-windows-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-vs-msvcbuild-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -141,7 +144,10 @@ xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</
</Link>
<PostBuildEvent>
<Command>xcopy "$(SolutionDir)mono-sgen-msvc.sh" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
-xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
+
+xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-windows-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-vs-msvcbuild-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -165,7 +171,10 @@ xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</
</Link>
<PostBuildEvent>
<Command>xcopy "$(SolutionDir)mono-sgen-msvc.sh" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
-xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
+
+xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-windows-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-vs-msvcbuild-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -190,7 +199,10 @@ xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</
</Link>
<PostBuildEvent>
<Command>xcopy "$(SolutionDir)mono-sgen-msvc.sh" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
-xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
+
+xcopy "$(SolutionDir)mono-sgen-msvc.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-windows-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1
+xcopy "$(SolutionDir)setup-vs-msvcbuild-env.bat" "$(OutDir)" /q /y &gt;nul 2&gt;&amp;1</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(MONO_COMPILE_AS_CPP)'=='true'">
diff --git a/msvc/package.bat b/msvc/package.bat
index df29a8ba6f2..df29a8ba6f2 100644..100755
--- a/msvc/package.bat
+++ b/msvc/package.bat
diff --git a/msvc/run-msbuild.bat b/msvc/run-msbuild.bat
index 033739f5699..fd5633a2346 100644..100755
--- a/msvc/run-msbuild.bat
+++ b/msvc/run-msbuild.bat
@@ -9,55 +9,13 @@
:: -------------------------------------------------------
@echo off
-
setlocal
set BUILD_RESULT=1
-:: Make sure we can restore current working directory after setting up environment.
-:: Some of the VS scripts can change the current working directory.
-set CALLER_WD=%CD%
-
:: Get path for current running script.
set RUN_MSBUILD_SCRIPT_PATH=%~dp0
-:: If we are running from none Windows shell we will need to restore a clean PATH
-:: before setting up VS MSVC build environment. If not there is a risk we will pick up
-:: for example cygwin binaries when running toolchain commands not explicitly setup by VS MSVC build environment.
-set HKCU_ENV_PATH=
-set HKLM_ENV_PATH=
-if "%SHELL%" == "/bin/bash" (
- for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKCU\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
- SET HKCU_ENV_PATH=%%b
- )
- for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
- SET HKLM_ENV_PATH=%%b
- )
-)
-
-:: Restore default path, if we are running from none Windows shell.
-if "%SHELL%" == "/bin/bash" (
- call :restore_default_path "%HKCU_ENV_PATH%" "%HKLM_ENV_PATH%"
-)
-
-:: There is still a scenario where the default path can include cygwin\bin folder. If that's the case
-:: there is still a big risk that build tools will be incorrectly resolved towards cygwin bin folder.
-:: Make sure to adjust path and drop all cygwin paths.
-set NEW_PATH=
-call where /Q "cygpath.exe" && (
- echo Warning, PATH includes cygwin bin folders. This can cause build errors due to incorrectly
- echo located build tools. Build script will drop all cygwin folders from used PATH.
- for %%a in ("%PATH:;=";"%") do (
- if not exist "%%~a\cygpath.exe" (
- call :add_to_new_path "%%~a"
- )
- )
-)
-
-if not "%NEW_PATH%" == "" (
- set "PATH=%NEW_PATH%"
-)
-
:: Configure all known build arguments.
set VS_BUILD_ARGS=""
set VS_TARGET=build
@@ -86,74 +44,11 @@ if /i not "%~1" == "" (
set VS_ADDITIONAL_ARGUMENTS=%~1
)
-:: Visual Studio 2015 == 14.0
-if "%VisualStudioVersion%" == "14.0" (
- goto SETUP_VS_2015
-)
-
-:: Visual Studio 2017 == 15.0
-if "%VisualStudioVersion%" == "15.0" (
- goto SETUP_VS_2017
-)
-
-:SETUP_VS_2015
-
-:SETUP_VS_2015_BUILD_TOOLS
-
-:: Try to locate VS2015 build tools installation.
-set VS_2015_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\vcbuildtools_msbuild.bat
-
-:: Setup VS2015 VC development environment using build tools installation.
-call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2015_VC
-
-:: Try to locate installed VS2015 VC environment.
-set VS_2015_DEV_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat
-
-:: Setup VS2015 VC development environment using VS installation.
-call :setup_build_env "%VS_2015_DEV_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2017
-
-:SETUP_VS_2017_BUILD_TOOLS
-
-:: Try to locate VS2017 build tools installation.
-set VS_2017_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\VsMSBuildCmd.bat
-
-:: Setup VS2017 VC development environment using build tools installation.
-call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2017_VC
-
-:: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
-set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
-set VS_2017_DEV_CMD=
-
-:: Try to locate installed VS2017 VC environment.
-if exist "%VSWHERE_TOOLS_BIN%" (
- for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
- set VS_2017_DEV_CMD=%%a\Common7\Tools\VsMSBuildCmd.bat
- )
-)
-
-:: Setup VS2017 VC development environment using VS installation.
-call :setup_build_env "%VS_2017_DEV_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:ON_ENV_ERROR
-
-echo Warning, failed to setup build environment needed by msbuild.exe.
-echo Incomplete build environment can cause build error's due to missing compiler, linker and platform libraries.
+:: Setup Windows environment.
+call %RUN_MSBUILD_SCRIPT_PATH%setup-windows-env.bat
-:ON_BUILD
+:: Setup VS msbuild environment.
+call %RUN_MSBUILD_SCRIPT_PATH%setup-vs-msbuild-env.bat
set VS_BUILD_ARGS=/p:Configuration=%VS_CONFIGURATION% /p:Platform=%VS_PLATFORM% %VS_ADDITIONAL_ARGUMENTS% /t:%VS_TARGET%
call msbuild.exe %VS_BUILD_ARGS% "%RUN_MSBUILD_SCRIPT_PATH%mono.sln" && (
@@ -167,48 +62,4 @@ call msbuild.exe %VS_BUILD_ARGS% "%RUN_MSBUILD_SCRIPT_PATH%mono.sln" && (
exit /b %BUILD_RESULT%
-:setup_build_env
-
-:: Check if VS build environment script exists.
-if not exist "%~1" (
- goto setup_build_env_error
-)
-
-:: Run VS build environment script.
-call "%~1" > NUL
-
-:: Restore callers working directory in case it has been changed by VS scripts.
-cd /d "%~2"
-
-goto setup_build_env_exit
-
-:setup_build_env_error
-exit /b 1
-
-:setup_build_env_exit
-goto :EOF
-
-:restore_default_path
-
-:: Restore default PATH.
-if not "%~2" == "" (
- if not "%~1" == "" (
- set "PATH=%~2;%~1"
- ) else (
- set "PATH=%~2"
- )
-)
-
-goto :EOF
-
-:add_to_new_path
-
-if "%NEW_PATH%" == "" (
- set "NEW_PATH=%~1"
-) else (
- SET "NEW_PATH=%NEW_PATH%;%~1"
-)
-
-goto :EOF
-
-@echo on
+@echo on \ No newline at end of file
diff --git a/msvc/run-msbuild.sh b/msvc/run-msbuild.sh
index 7de93f0d9f4..d90d8ac1615 100755
--- a/msvc/run-msbuild.sh
+++ b/msvc/run-msbuild.sh
@@ -8,7 +8,36 @@
# $4 Additional arguments passed to msbuild, needs to be quoted if multiple.
#-------------------------------------------------------
+function win32_format_path {
+ local formatted_path=$1
+ local host_win32_wsl=0
+ local host_win32_cygwin=0
+
+ host_uname="$(uname -a)"
+ case "$host_uname" in
+ *Microsoft*)
+ host_win32_wsl=1
+ ;;
+ CYGWIN*)
+ host_win32_cygwin=1
+ ;;
+ esac
+
+ if [[ $host_win32_wsl = 1 ]] && [[ $1 == "/mnt/"* ]]; then
+ formatted_path="$(wslpath -a -w "$1")"
+ elif [[ $host_win32_cygwin = 1 ]] && [[ $1 == "/cygdrive/"* ]]; then
+ formatted_path="$(cygpath -a -w "$1")"
+ fi
+
+ echo "$formatted_path"
+}
+
RUN_MSBUILD_SCRIPT_PATH=$(cd "$(dirname "$0")"; pwd)
-RUN_MSBUILD_SCRIPT_PATH=$(cygpath -w "$RUN_MSBUILD_SCRIPT_PATH/run-msbuild.bat")
+RUN_MSBUILD_SCRIPT_PATH=$(win32_format_path "$RUN_MSBUILD_SCRIPT_PATH/run-msbuild.bat")
+
+WINDOWS_CMD=$(which cmd.exe)
+if [ ! -f $WINDOWS_CMD ]; then
+ WINDOWS_CMD=$WINDIR/System32/cmd.exe
+fi
-"$WINDIR/System32/cmd.exe" /c "$RUN_MSBUILD_SCRIPT_PATH" "$@"
+"$WINDOWS_CMD" /c "$RUN_MSBUILD_SCRIPT_PATH" "$@"
diff --git a/msvc/setup-vs-msbuild-env.bat b/msvc/setup-vs-msbuild-env.bat
new file mode 100755
index 00000000000..aefeb124966
--- /dev/null
+++ b/msvc/setup-vs-msbuild-env.bat
@@ -0,0 +1,104 @@
+:: Set up VS msbuild environment depending on installed VS versions.
+:: Script will setup environment variables directly in callers environment.
+
+:: Make sure we can restore current working directory after setting up environment.
+:: Some of the VS scripts can change the current working directory.
+set CALLER_WD=%CD%
+
+:: Get path for current running script.
+set RUN_SETUP_VS_MSBUILD_ENV_SCRIPT_PATH=%~dp0
+
+:: Visual Studio 2015 == 14.0
+if "%VisualStudioVersion%" == "14.0" (
+ goto SETUP_VS_2015
+)
+
+:: Visual Studio 2017 == 15.0
+if "%VisualStudioVersion%" == "15.0" (
+ goto SETUP_VS_2017
+)
+
+:SETUP_VS_2015
+
+:SETUP_VS_2015_BUILD_TOOLS
+
+:: Try to locate VS2015 build tools installation.
+set VS_2015_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\vcbuildtools_msbuild.bat
+
+:: Setup VS2015 VC development environment using build tools installation.
+call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
+ goto ON_EXIT
+)
+
+:SETUP_VS_2015_VC
+
+:: Try to locate installed VS2015 VC environment.
+set VS_2015_DEV_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat
+
+:: Setup VS2015 VC development environment using VS installation.
+call :setup_build_env "%VS_2015_DEV_CMD%" "%CALLER_WD%" && (
+ goto ON_EXIT
+)
+
+:SETUP_VS_2017
+
+:SETUP_VS_2017_BUILD_TOOLS
+
+:: Try to locate VS2017 build tools installation.
+set VS_2017_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\VsMSBuildCmd.bat
+
+:: Setup VS2017 VC development environment using build tools installation.
+call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
+ goto ON_EXIT
+)
+
+:SETUP_VS_2017_VC
+
+:: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
+set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
+set VS_2017_DEV_CMD=
+
+:: Try to locate installed VS2017 VC environment.
+if exist "%VSWHERE_TOOLS_BIN%" (
+ for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
+ set VS_2017_DEV_CMD=%%a\Common7\Tools\VsMSBuildCmd.bat
+ )
+)
+
+:: Setup VS2017 VC development environment using VS installation.
+call :setup_build_env "%VS_2017_DEV_CMD%" "%CALLER_WD%" && (
+ goto ON_EXIT
+)
+
+:ON_ENV_ERROR
+
+echo Warning, failed to setup VS build environment needed by VS tooling.
+echo Incomplete build environment can cause build error's due to missing compiler,
+echo linker and platform libraries.
+
+exit /b 1
+
+:ON_EXIT
+
+exit /b 0
+
+:setup_build_env
+
+:: Check if VS build environment script exists.
+if not exist "%~1" (
+ goto setup_build_env_error
+)
+
+:: Run VS build environment script.
+call "%~1" > NUL
+
+:: Restore callers working directory in case it has been changed by VS scripts.
+cd /d "%~2"
+
+goto setup_build_env_exit
+
+:setup_build_env_error
+exit /b 1
+
+:setup_build_env_exit
+goto :EOF \ No newline at end of file
diff --git a/msvc/setup-vs-msvcbuild-env.bat b/msvc/setup-vs-msvcbuild-env.bat
new file mode 100755
index 00000000000..88557fe0486
--- /dev/null
+++ b/msvc/setup-vs-msvcbuild-env.bat
@@ -0,0 +1,155 @@
+:: Set up VS MSVC environment depending on installed VS versions.
+:: Script will setup environment variables directly in callers environment.
+
+:: Make sure we can restore current working directory after setting up environment.
+:: Some of the VS scripts can change the current working directory.
+set CALLER_WD=%CD%
+
+:: Get path for current running script.
+set RUN_SETUP_VS_MSVCBUILD_ENV_SCRIPT_PATH=%~dp0
+
+:: NOTE, MSVC build Mono full AOT tooling currently support 64-bit AMD codegen. Below will only setup
+:: amd64 versions of VS MSVC build environment and corresponding ClangC2 compiler.
+set VS_2015_TOOLCHAIN_ARCH=amd64
+set VS_2015_VCVARS_ARCH=%VS_2015_TOOLCHAIN_ARCH%\vcvars64.bat
+set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
+set VS_2017_VCVARS_ARCH=vcvars64.bat
+set VS_2017_CLANGC2_ARCH=HostX64
+
+:: 32-bit AOT toolchains for MSVC build mono-sgen.exe is currently not supported.
+:: set VS_2015_TOOLCHAIN_ARCH=x86
+:: set VS_2015_VCVARS_ARCH=vcvars32.bat
+:: set VS_2015_CLANGC2_ARCH=%VS_2015_TOOLCHAIN_ARCH%
+:: set VS_2017_VCVARS_ARCH=vcvars32.bat
+:: set VS_2017_CLANGC2_ARCH=HostX86
+
+set VS_CLANGC2_TOOLS_BIN_PATH=
+
+:: Visual Studio 2015 == 14.0
+if "%VisualStudioVersion%" == "14.0" (
+ goto SETUP_VS_2015
+)
+
+:: Visual Studio 2017 == 15.0
+if "%VisualStudioVersion%" == "15.0" (
+ goto SETUP_VS_2017
+)
+
+:SETUP_VS_2015
+
+set VS_2015_VCINSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\
+
+:: Try to locate installed VS2015 Clang/C2.
+SET VS_2015_CLANGC2_TOOLS_BIN_PATH=%VS_2015_VCINSTALL_DIR%ClangC2\bin\%VS_2015_CLANGC2_ARCH%\
+SET VS_2015_CLANGC2_TOOLS_BIN=%VS_2015_CLANGC2_TOOLS_BIN_PATH%clang.exe
+
+if not exist "%VS_2015_CLANGC2_TOOLS_BIN%" (
+ goto SETUP_VS_2017
+)
+
+:SETUP_VS_2015_BUILD_TOOLS
+
+:: Try to locate VS2015 build tools installation.
+set VS_2015_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\
+set VS_2015_BUILD_TOOLS_CMD=%VS_2015_BUILD_TOOLS_INSTALL_DIR%vcbuildtools.bat
+
+:: Setup VS2015 VC development environment using build tools installation.
+call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%VS_2015_TOOLCHAIN_ARCH%" "%CALLER_WD%" && (
+ set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
+ goto ON_EXIT
+)
+
+:SETUP_VS_2015_VC
+
+:: Try to locate installed VS2015 VC environment.
+set VS_2015_DEV_CMD=%VS_2015_VCINSTALL_DIR%bin\%VS_2015_VCVARS_ARCH%
+
+call :setup_build_env "%VS_2015_DEV_CMD%" "" "%CALLER_WD%" && (
+ set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2015_CLANGC2_TOOLS_BIN_PATH%"
+ goto ON_EXIT
+)
+
+:SETUP_VS_2017
+
+:: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
+set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
+set VS_2017_VCINSTALL_DIR=
+
+:: Try to locate installed VS2017 VC environment.
+if exist "%VSWHERE_TOOLS_BIN%" (
+ for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
+ set VS_2017_VCINSTALL_DIR=%%a\VC\
+ )
+)
+
+:: Try to locate installed VS2017 Clang/C2.
+SET VS_2017_CLANGC2_VERSION_FILE=%VS_2017_VCINSTALL_DIR%Auxiliary/Build/Microsoft.ClangC2Version.default.txt
+if not exist "%VS_2017_CLANGC2_VERSION_FILE%" (
+ goto ON_ENV_ERROR
+)
+
+set /p VS_2017_CLANGC2_VERSION=<"%VS_2017_CLANGC2_VERSION_FILE%"
+set VS_2017_CLANGC2_TOOLS_BIN_PATH=%VS_2017_VCINSTALL_DIR%Tools\ClangC2\%VS_2017_CLANGC2_VERSION%\bin\%VS_2017_CLANGC2_ARCH%\
+set VS_2017_CLANGC2_TOOLS_BIN=%VS_2017_CLANGC2_TOOLS_BIN_PATH%clang.exe
+if not exist "%VS_2017_CLANGC2_TOOLS_BIN%" (
+ goto ON_ENV_ERROR
+)
+
+:SETUP_VS_2017_BUILD_TOOLS
+
+:: Try to locate VS2017 build tools installation.
+set VS_2017_BUILD_TOOLS_INSTALL_DIR=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\
+set VS_2017_BUILD_TOOLS_CMD=%VS_2017_BUILD_TOOLS_INSTALL_DIR%VC\Auxiliary\Build\%VS_2017_VCVARS_ARCH%
+
+:: Setup VS2017 VC development environment using build tools installation.
+call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "" "%CALLER_WD%" && (
+ set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
+ goto ON_EXIT
+)
+
+:SETUP_VS_2017_VC
+
+:: Try to locate installed VS2017 VC environment.
+set VS_2017_DEV_CMD=%VS_2017_VCINSTALL_DIR%Auxiliary\Build\%VS_2017_VCVARS_ARCH%
+
+:: Setup VS2017 VC development environment using VS installation.
+call :setup_build_env "%VS_2017_DEV_CMD%" "" "%CALLER_WD%" && (
+ set "VS_CLANGC2_TOOLS_BIN_PATH=%VS_2017_CLANGC2_TOOLS_BIN_PATH%"
+ goto ON_EXIT
+)
+
+:ON_ENV_ERROR
+
+echo Warning, failed to setup VS build environment needed by VS tooling.
+echo Incomplete build environment can cause build error's due to missing compiler,
+echo linker and platform libraries.
+
+exit /b 1
+
+:ON_EXIT
+
+:: Add ClangC2 folders to PATH
+set "PATH=%VS_CLANGC2_TOOLS_BIN_PATH%;%PATH%"
+
+exit /b 0
+
+:setup_build_env
+
+:: Check if VS build environment script exists.
+if not exist "%~1" (
+ goto setup_build_env_error
+)
+
+:: Run VS build environment script.
+call "%~1" %~2 > NUL
+
+:: Restore callers working directory in case it has been changed by VS scripts.
+cd /d "%~3"
+
+goto setup_build_env_exit
+
+:setup_build_env_error
+exit /b 1
+
+:setup_build_env_exit
+goto :EOF \ No newline at end of file
diff --git a/msvc/setup-windows-env.bat b/msvc/setup-windows-env.bat
new file mode 100755
index 00000000000..7b47754e0b1
--- /dev/null
+++ b/msvc/setup-windows-env.bat
@@ -0,0 +1,64 @@
+:: Script will setup environment variables directly in callers environment.
+
+:: If we are running from none Windows shell we will need to restore a clean PATH
+:: before setting up VS MSVC build environment. If not there is a risk we will pick up
+:: for example cygwin binaries when running toolchain commands not explicitly setup by
+:: VS MSVC build environment.
+set HKCU_ENV_PATH=
+set HKLM_ENV_PATH=
+if "%SHELL%" == "/bin/bash" (
+ for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKCU\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
+ SET HKCU_ENV_PATH=%%b
+ )
+ for /f "tokens=2,*" %%a in ('%WINDIR%\System32\reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" ^| %WINDIR%\System32\find.exe /i "REG_"') do (
+ SET HKLM_ENV_PATH=%%b
+ )
+)
+
+:: Restore default path, if we are running from none Windows shell.
+if "%SHELL%" == "/bin/bash" (
+ call :restore_default_path "%HKCU_ENV_PATH%" "%HKLM_ENV_PATH%"
+)
+
+:: There is still a scenario where the default path can include cygwin\bin folder. If that's the case
+:: there is still a big risk that build tools will be incorrectly resolved towards cygwin bin folder.
+:: Make sure to adjust path and drop all cygwin paths.
+set NEW_PATH=
+call where /Q "cygpath.exe" && (
+ echo Warning, PATH includes cygwin bin folders. This can cause build errors due to incorrectly
+ echo located build tools. Build script will drop all cygwin folders from used PATH.
+ for %%a in ("%PATH:;=";"%") do (
+ if not exist "%%~a\cygpath.exe" (
+ call :add_to_new_path "%%~a"
+ )
+ )
+)
+
+if not "%NEW_PATH%" == "" (
+ set "PATH=%NEW_PATH%"
+)
+
+exit /b 0
+
+:restore_default_path
+
+:: Restore default PATH.
+if not "%~2" == "" (
+ if not "%~1" == "" (
+ set "PATH=%~2;%~1"
+ ) else (
+ set "PATH=%~2"
+ )
+)
+
+goto :EOF
+
+:add_to_new_path
+
+if "%NEW_PATH%" == "" (
+ set "NEW_PATH=%~1"
+) else (
+ SET "NEW_PATH=%NEW_PATH%;%~1"
+)
+
+goto :EOF \ No newline at end of file
diff --git a/msvc/test-config-setup.bat b/msvc/test-config-setup.bat
index adb174d8093..adb174d8093 100644..100755
--- a/msvc/test-config-setup.bat
+++ b/msvc/test-config-setup.bat
diff --git a/msvc/winsetup.bat b/msvc/winsetup.bat
index 28f1bbd2f1f..aa6c882155c 100755
--- a/msvc/winsetup.bat
+++ b/msvc/winsetup.bat
@@ -3,80 +3,13 @@ setlocal
set BUILD_RESULT=1
-:: Make sure we can restore current working directory after setting up environment.
-:: Some of the VS scripts can change the current working directory.
-set CALLER_WD=%CD%
+:: Get path for current running script.
+set RUN_WINSETUP_SCRIPT_PATH=%~dp0
-:: Visual Studio 2015 == 14.0
-if "%VisualStudioVersion%" == "14.0" (
- goto SETUP_VS_2015
-)
-
-:: Visual Studio 2017 == 15.0
-if "%VisualStudioVersion%" == "15.0" (
- goto SETUP_VS_2017
-)
-
-:SETUP_VS_2015
-
-:SETUP_VS_2015_BUILD_TOOLS
-
-:: Try to locate VS2015 build tools installation.
-set VS_2015_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual C++ Build Tools\vcbuildtools_msbuild.bat
-
-:: Setup VS2015 VC development environment using build tools installation.
-call :setup_build_env "%VS_2015_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2015_VC
-
-:: Try to locate installed VS2015 VC environment.
-set VS_2015_DEV_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat
-
-:: Setup VS2015 VC development environment using VS installation.
-call :setup_build_env "%VS_2015_DEV_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2017
-
-:SETUP_VS_2017_BUILD_TOOLS
-
-:: Try to locate VS2017 build tools installation.
-set VS_2017_BUILD_TOOLS_CMD=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\VsMSBuildCmd.bat
-
-:: Setup VS2017 VC development environment using build tools installation.
-call :setup_build_env "%VS_2017_BUILD_TOOLS_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:SETUP_VS_2017_VC
+:: Setup VS msbuild environment.
+call %RUN_WINSETUP_SCRIPT_PATH%setup-vs-msbuild-env.bat
-:: VS2017 includes vswhere.exe that can be used to locate current VS2017 installation.
-set VSWHERE_TOOLS_BIN=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
-set VS_2017_DEV_CMD=
-
-:: Try to locate installed VS2017 VC environment.
-if exist "%VSWHERE_TOOLS_BIN%" (
- for /f "tokens=*" %%a in ('"%VSWHERE_TOOLS_BIN%" -latest -property installationPath') do (
- set VS_2017_DEV_CMD=%%a\Common7\Tools\VsMSBuildCmd.bat
- )
-)
-
-:: Setup VS2017 VC development environment using VS installation.
-call :setup_build_env "%VS_2017_DEV_CMD%" "%CALLER_WD%" && (
- goto ON_BUILD
-)
-
-:ON_ENV_ERROR
-
-echo Warning, failed to setup build environment needed by msbuild.exe.
-echo Incomplete build environment can cause build error's due to missing compiler, linker and platform libraries.
-
-:ON_BUILD
-
-call "msbuild.exe" /t:RunWinConfigSetup mono.winconfig.targets && (
+call "msbuild.exe" /t:RunWinConfigSetup %RUN_WINSETUP_SCRIPT_PATH%mono.winconfig.targets && (
set BUILD_RESULT=0
) || (
set BUILD_RESULT=1
@@ -87,25 +20,4 @@ call "msbuild.exe" /t:RunWinConfigSetup mono.winconfig.targets && (
exit /b %BUILD_RESULT%
-:setup_build_env
-
-:: Check if VS build environment script exists.
-if not exist "%~1" (
- goto setup_build_env_error
-)
-
-:: Run VS build environment script.
-call "%~1" > NUL
-
-:: Restore callers working directory in case it has been changed by VS scripts.
-cd /d "%~2"
-
-goto setup_build_env_exit
-
-:setup_build_env_error
-exit /b 1
-
-:setup_build_env_exit
-goto :EOF
-
@echo on