Skip to content

Commit c1781de

Browse files
authored
Update VS detection logic (#5507)
1 parent 7b93285 commit c1781de

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

eng/Build-Native.cmd

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@ echo %__MsgPrefix%Starting Build at %TIME%
99
set __ThisScriptFull="%~f0"
1010
set __ThisScriptDir="%~dp0"
1111

12-
call "%__ThisScriptDir%"\native\init-vs-env.cmd
13-
if NOT '%ERRORLEVEL%' == '0' goto ExitWithError
14-
15-
if defined VS170COMNTOOLS (
16-
set "__VSToolsRoot=%VS170COMNTOOLS%"
17-
set "__VCToolsRoot=%VS170COMNTOOLS%\..\..\VC\Auxiliary\Build"
18-
set __VSVersion=vs2022
19-
)
20-
if defined VS160COMNTOOLS (
21-
set "__VSToolsRoot=%VS160COMNTOOLS%"
22-
set "__VCToolsRoot=%VS160COMNTOOLS%\..\..\VC\Auxiliary\Build"
23-
set __VSVersion=vs2019
24-
) else if defined VS150COMNTOOLS (
25-
set "__VSToolsRoot=%VS150COMNTOOLS%"
26-
set "__VCToolsRoot=%VS150COMNTOOLS%\..\..\VC\Auxiliary\Build"
27-
set __VSVersion=vs2017
28-
)
29-
3012
:: Set the default arguments for build
3113

3214
set __TargetArch=x64
@@ -42,11 +24,11 @@ set __Verbosity=minimal
4224
set __Ninja=0
4325

4426
:: Set the various build properties here so that CMake and MSBuild can pick them up
45-
set "__ProjectDir=%~dp0"
46-
:: remove trailing slash
47-
if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
48-
set "__ProjectDir=%__ProjectDir%\.."
49-
set "__SourceDir=%__ProjectDir%\src"
27+
set "__RepoRootDir=%~dp0"
28+
:: remove trailing slash
29+
if %__RepoRootDir:~-1%==\ set "__RepoRootDir=%__RepoRootDir:~0,-1%"
30+
set "__RepoRootDir=%__RepoRootDir%\.."
31+
set "__SourceDir=%__RepoRootDir%\src"
5032

5133
:: __UnprocessedBuildArgs are args that we pass to msbuild (e.g. /p:OfficialBuildId=xxxxxx)
5234
set "__args=%*"
@@ -89,13 +71,20 @@ if [!processedArgs!] == [] (
8971

9072
:ArgsDone
9173

74+
call "%__RepoRootDir%"\eng\native\init-vs-env.cmd
75+
if NOT '%ERRORLEVEL%' == '0' goto ExitWithError
76+
77+
if defined VCINSTALLDIR (
78+
set "__VCToolsRoot=%VCINSTALLDIR%Auxiliary\Build"
79+
)
80+
9281
if "%__HostArch%" == "" set __HostArch=%__TargetArch%
9382
if /i "%__BuildType%" == "debug" set __BuildType=Debug
9483
if /i "%__BuildType%" == "release" set __BuildType=Release
9584

9685
if "%NUGET_PACKAGES%" == "" (
9786
if %__CI% EQU 1 (
98-
set "NUGET_PACKAGES=%__ProjectDir%\.packages"
87+
set "NUGET_PACKAGES=%__RepoRootDir%\.packages"
9988
) else (
10089
set "NUGET_PACKAGES=%UserProfile%\.nuget\packages"
10190
)
@@ -104,7 +93,7 @@ if "%NUGET_PACKAGES%" == "" (
10493
echo %NUGET_PACKAGES%
10594

10695
:: Set the remaining variables based upon the determined build configuration
107-
set "__RootBinDir=%__ProjectDir%\artifacts"
96+
set "__RootBinDir=%__RepoRootDir%\artifacts"
10897
set "__BinDir=%__RootBinDir%\bin\%__TargetOS%.%__TargetArch%.%__BuildType%"
10998
set "__LogDir=%__RootBinDir%\log\%__TargetOS%.%__TargetArch%.%__BuildType%"
11099
set "__ArtifactsIntermediatesDir=%__RootBinDir%\obj"
@@ -128,7 +117,7 @@ echo %__MsgPrefix%Commencing diagnostics repo build
128117

129118
echo %__MsgPrefix%Checking prerequisites
130119
:: Eval the output from probe-win1.ps1
131-
for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy ByPass "& ""%__ProjectDir%\eng\native\set-cmake-path.ps1"""') do %%a
120+
for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy ByPass "& ""%__RepoRootDir%\eng\native\set-cmake-path.ps1"""') do %%a
132121

133122
REM =========================================================================================
134123
REM ===
@@ -139,7 +128,7 @@ REM ============================================================================
139128
@if defined _echo @echo on
140129

141130
:: Parse the optdata package versions out of msbuild so that we can pass them on to CMake
142-
set __DotNetCli=%__ProjectDir%\dotnet.cmd
131+
set __DotNetCli=%__RepoRootDir%\dotnet.cmd
143132

144133
REM =========================================================================================
145134
REM ===
@@ -179,7 +168,7 @@ if %__BuildNative% EQU 1 (
179168

180169
echo Generating Version Header
181170
set __GenerateVersionLog="%__LogDir%\GenerateVersion.binlog"
182-
powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -File "%__ProjectDir%\eng\common\msbuild.ps1" "%__ProjectDir%\eng\native-prereqs.proj" /bl:!__GenerateVersionLog! /t:BuildPrereqs /restore %__CommonBuildArgs%
171+
powershell -NoProfile -ExecutionPolicy ByPass -NoLogo -File "%__RepoRootDir%\eng\common\msbuild.ps1" "%__RepoRootDir%\eng\native-prereqs.proj" /bl:!__GenerateVersionLog! /t:BuildPrereqs /restore %__CommonBuildArgs%
183172
if not !errorlevel! == 0 (
184173
echo Generate Version Header FAILED
185174
goto ExitWithError
@@ -193,7 +182,7 @@ if %__BuildNative% EQU 1 (
193182
set __ExtraCmakeArgs=!__ExtraCmakeArgs! "-DCMAKE_SYSTEM_VERSION=10.0" "-DCLR_MANAGED_BINARY_DIR=!__ManagedBinaryDir!" "-DCLR_BUILD_TYPE=%__BuildType%" "-DCLR_CMAKE_TARGET_ARCH=%__TargetArch%" "-DNUGET_PACKAGES=%NUGET_PACKAGES:\=/%"
194183

195184
pushd "%__IntermediatesDir%"
196-
call "%__ProjectDir%\eng\native\gen-buildsys.cmd" "%__ProjectDir%" "%__IntermediatesDir%" %__VSVersion% %__HostArch% %__TargetOS% !__ExtraCmakeArgs!
185+
call "%__RepoRootDir%\eng\native\gen-buildsys.cmd" "%__RepoRootDir%" "%__IntermediatesDir%" %VisualStudioVersion% %__HostArch% %__TargetOS% !__ExtraCmakeArgs!
197186
@if defined _echo @echo on
198187
popd
199188

eng/native/configurecompiler.cmake

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ set(CMAKE_TRY_COMPILE_CONFIGURATION Release)
1919

2020
include(CheckCCompilerFlag)
2121
include(CheckCXXCompilerFlag)
22-
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.18")
23-
include(CheckLinkerFlag)
24-
endif()
22+
include(CheckLinkerFlag)
2523

2624
# "configureoptimization.cmake" must be included after CLR_CMAKE_HOST_UNIX has been set.
2725
include(${CMAKE_CURRENT_LIST_DIR}/configureoptimization.cmake)
@@ -672,22 +670,22 @@ if (CLR_CMAKE_HOST_UNIX)
672670
set(DISABLE_OVERRIDING_MIN_VERSION_ERROR -Wno-overriding-t-option)
673671
add_link_options(-Wno-overriding-t-option)
674672
if(CLR_CMAKE_HOST_ARCH_ARM64)
675-
set(CLR_CMAKE_MACCATALYST_COMPILER_TARGET "arm64-apple-ios15.0-macabi")
676-
add_link_options(-target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET})
673+
set(MACOS_VERSION_MIN_FLAGS "-target arm64-apple-ios15.0-macabi")
674+
add_link_options(-target arm64-apple-ios15.0-macabi)
677675
elseif(CLR_CMAKE_HOST_ARCH_AMD64)
678-
set(CLR_CMAKE_MACCATALYST_COMPILER_TARGET "x86_64-apple-ios15.0-macabi")
679-
add_link_options(-target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET})
676+
set(MACOS_VERSION_MIN_FLAGS "-target x86_64-apple-ios15.0-macabi")
677+
add_link_options(-target x86_64-apple-ios15.0-macabi)
680678
else()
681679
clr_unknown_arch()
682680
endif()
683681
# These options are intentionally set using the CMAKE_XXX_FLAGS instead of
684682
# add_compile_options so that they take effect on the configuration functions
685683
# in various configure.cmake files.
686-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
687-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
688-
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
689-
set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS}-target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
690-
set(CMAKE_OBJCXX_FLAGS "${CMAKE_OBJCXX_FLAGS} -target ${CLR_CMAKE_MACCATALYST_COMPILER_TARGET} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
684+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MACOS_VERSION_MIN_FLAGS} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
685+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MACOS_VERSION_MIN_FLAGS} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
686+
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${MACOS_VERSION_MIN_FLAGS} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
687+
set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} ${MACOS_VERSION_MIN_FLAGS} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
688+
set(CMAKE_OBJCXX_FLAGS "${CMAKE_OBJCXX_FLAGS} ${MACOS_VERSION_MIN_FLAGS} ${DISABLE_OVERRIDING_MIN_VERSION_ERROR}")
691689
elseif(CLR_CMAKE_HOST_OSX)
692690
set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0")
693691
if(CLR_CMAKE_HOST_ARCH_ARM64)

eng/native/gen-buildsys.cmd

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if /i "%__Ninja%" == "1" (
2626
set __CmakeGenerator=Ninja
2727
) else (
2828
if /i NOT "%__Arch%" == "wasm" (
29-
if /i "%__VSVersion%" == "vs2022" (set __CmakeGenerator=%__CmakeGenerator% 17 2022)
29+
if /i "%__VSVersion%" == "17.0" (set __CmakeGenerator=%__CmakeGenerator% 17 2022)
3030

3131
if /i "%__Arch%" == "x64" (set __ExtraCmakeParams=%__ExtraCmakeParams% -A x64)
3232
if /i "%__Arch%" == "arm" (set __ExtraCmakeParams=%__ExtraCmakeParams% -A ARM)
@@ -80,6 +80,33 @@ if /i "%__Arch%" == "wasm" (
8080
set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_SYSTEM_VERSION=10.0"
8181
)
8282

83+
if /i "%__Os%" == "android" (
84+
:: Keep in sync with $(AndroidApiLevelMin) in Directory.Build.props in the repository rooot
85+
set __ANDROID_API_LEVEL=21
86+
if "%ANDROID_NDK_ROOT%" == "" (
87+
echo Error: You need to set the ANDROID_NDK_ROOT environment variable pointing to the Android NDK root.
88+
exit /B 1
89+
)
90+
91+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_BUILD=1" "-DANDROID_CPP_FEATURES='no-rtti exceptions'"
92+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_PLATFORM=android-!__ANDROID_API_LEVEL!" "-DANDROID_NATIVE_API_LEVEL=!__ANDROID_API_LEVEL!"
93+
94+
if "%__Arch%" == "x64" (
95+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_ABI=x86_64"
96+
)
97+
if "%__Arch%" == "x86" (
98+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_ABI=x86"
99+
)
100+
if "%__Arch%" == "arm64" (
101+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_ABI=arm64-v8a"
102+
)
103+
if "%__Arch%" == "arm" (
104+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DANDROID_ABI=armeabi-v7a"
105+
)
106+
107+
set __ExtraCmakeParams=!__ExtraCmakeParams! "-DCMAKE_TOOLCHAIN_FILE='%ANDROID_NDK_ROOT:\=/%/build/cmake/android.toolchain.cmake'" "-C %__repoRoot%/eng/native/tryrun.cmake"
108+
)
109+
83110
:loop
84111
if [%6] == [] goto end_loop
85112
set __ExtraCmakeParams=%__ExtraCmakeParams% %6
@@ -111,6 +138,7 @@ if not "%__ConfigureOnly%" == "1" (
111138
if /i "%__UseEmcmake%" == "1" (
112139
call "!EMSDK_PATH!/emsdk_env.cmd" > nul 2>&1 && emcmake "%CMakePath%" %__ExtraCmakeParams% --no-warn-unused-cli -G "%__CmakeGenerator%" -B %__IntermediatesDir% -S %__SourceDir%
113140
) else (
141+
echo "%CMakePath% %__ExtraCmakeParams% --no-warn-unused-cli -G %__CmakeGenerator% -B %__IntermediatesDir% -S %__SourceDir%"
114142
"%CMakePath%" %__ExtraCmakeParams% --no-warn-unused-cli -G "%__CmakeGenerator%" -B %__IntermediatesDir% -S %__SourceDir%
115143
)
116144

eng/native/init-vs-env.cmd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ set "__VSCOMNTOOLS="
5353
set "VSCMD_START_DIR="
5454

5555
:VSDetected
56-
if "%VisualStudioVersion%"=="17.0" (
57-
set __VSVersion=vs2022
58-
set __PlatformToolset=v143
59-
goto :SetVCEnvironment
60-
)
56+
goto :SetVCEnvironment
6157

6258
:VSMissing
6359
echo %__MsgPrefix%Error: Visual Studio 2022 with C++ tools required. ^

0 commit comments

Comments
 (0)