Skip to content

Commit daaaa9c

Browse files
committed
feat(Vcvars): Add MSVC version mappings by toolset and Visual Studio version
Introduce internal variables mapping known MSVC version values to their corresponding MSVC toolsets (e.g., v143, v142) and Visual Studio versions (e.g., VS 2022, VS 2019). These mappings are derived from: - https://github.com/Kitware/CMake/blob/v4.0.3/Modules/Platform/Windows-MSVC.cmake - https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9271 - https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B#Internal_version_numbering The following variables are now defined: - `Vcvars_TOOLSET_<Toolset>_MSVC_VERSIONS` - `Vcvars_VS<Major>_MSVC_VERSIONS` Update `_Vcvars_SUPPORTED_MSVC_VERSIONS` to reference the toolset-specific lists, improving maintainability and enabling toolset-aware logic. In `tests/CMakeLists.txt`: - Validate that the mappings are defined correctly. - Check that `Vcvars_ConvertMsvcVersionToVsVersion()` returns the expected Visual Studio version string for each supported MSVC version.
1 parent 77b877d commit daaaa9c

File tree

2 files changed

+129
-9
lines changed

2 files changed

+129
-9
lines changed

FindVcvars.cmake

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,50 @@ This module also defines the following functions
9696
``<output_var>``
9797
The name of the variable to be set with the Visual Studio version.
9898
99+
100+
The module also defines the following variables mapping MSVC versions
101+
to their associated toolset or Visual Studio major release:
102+
103+
.. variable:: Vcvars_TOOLSET_<Toolset>_MSVC_VERSIONS
104+
105+
Lists the MSVC version numbers associated with a specific compiler toolset.
106+
These are grouped based on official Microsoft and CMake-maintained mappings.
107+
108+
Example mappings:
109+
110+
- ``Vcvars_TOOLSET_143_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v143`` (Visual Studio 2022)
111+
- ``Vcvars_TOOLSET_142_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v142`` (Visual Studio 2019)
112+
- ``Vcvars_TOOLSET_141_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v141`` (Visual Studio 2017)
113+
- ``Vcvars_TOOLSET_140_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v140`` (Visual Studio 2015)
114+
- ``Vcvars_TOOLSET_120_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v120`` (Visual Studio 2013)
115+
- ``Vcvars_TOOLSET_110_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v110`` (Visual Studio 2012)
116+
- ``Vcvars_TOOLSET_100_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v100`` (Visual Studio 2010)
117+
- ``Vcvars_TOOLSET_90_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v90`` (Visual Studio 2008)
118+
- ``Vcvars_TOOLSET_80_MSVC_VERSIONS`` — MSVC versions associated with toolset ``v80`` (Visual Studio 2005)
119+
120+
.. variable:: Vcvars_VS<Major>_MSVC_VERSIONS
121+
122+
Lists the MSVC version numbers grouped by the corresponding Visual Studio release.
123+
124+
These aliases mirror the toolset mappings above but allow for intuitive access
125+
based on the Visual Studio version number.
126+
127+
Example aliases:
128+
129+
- ``Vcvars_VS17_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_143_MSVC_VERSIONS`` (Visual Studio 2022)
130+
- ``Vcvars_VS16_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_142_MSVC_VERSIONS`` (Visual Studio 2019)
131+
- ``Vcvars_VS15_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_141_MSVC_VERSIONS`` (Visual Studio 2017)
132+
- ``Vcvars_VS14_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_140_MSVC_VERSIONS`` (Visual Studio 2015)
133+
- ``Vcvars_VS12_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_120_MSVC_VERSIONS`` (Visual Studio 2013)
134+
- ``Vcvars_VS11_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_110_MSVC_VERSIONS`` (Visual Studio 2012)
135+
- ``Vcvars_VS10_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_100_MSVC_VERSIONS`` (Visual Studio 2010)
136+
- ``Vcvars_VS9_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_90_MSVC_VERSIONS`` (Visual Studio 2008)
137+
- ``Vcvars_VS8_MSVC_VERSIONS`` — aliases ``Vcvars_TOOLSET_80_MSVC_VERSIONS`` (Visual Studio 2005)
138+
139+
These variables are primarily intended for internal use and test coverage but may also be
140+
useful in advanced configuration or filtering of available toolchains.
141+
142+
99143
This module also supports the following COMPONENTS:
100144
101145
* ``FunctionsOnly``: Only defines the helper functions
@@ -106,19 +150,50 @@ This module also supports the following COMPONENTS:
106150

107151
cmake_minimum_required(VERSION 3.20.6...3.22.6 FATAL_ERROR)
108152

153+
# See https://github.com/Kitware/CMake/blob/v4.0.3/Modules/Platform/Windows-MSVC.cmake#L72-L101
154+
155+
set(Vcvars_TOOLSET_143_MSVC_VERSIONS # VS 2022
156+
1949 1948 1947 1946 1945 1944 1943 1942 1941 1940
157+
1939 1938 1937 1936 1935 1934 1933 1932 1931 1930
158+
)
159+
set(Vcvars_TOOLSET_142_MSVC_VERSIONS # VS 2019
160+
1929 1928 1927 1926 1925 1924 1923 1922 1921 1920
161+
)
162+
set(Vcvars_TOOLSET_141_MSVC_VERSIONS # VS 2017
163+
1916 1915 1914 1913 1912 1911 1910
164+
)
165+
set(Vcvars_TOOLSET_140_MSVC_VERSIONS 1900) # VS 2015
166+
set(Vcvars_TOOLSET_120_MSVC_VERSIONS 1800) # VS 2013
167+
set(Vcvars_TOOLSET_110_MSVC_VERSIONS 1700) # VS 2012
168+
set(Vcvars_TOOLSET_100_MSVC_VERSIONS 1600) # VS 2010
169+
set(Vcvars_TOOLSET_90_MSVC_VERSIONS 1500) # VS 2008
170+
set(Vcvars_TOOLSET_80_MSVC_VERSIONS 1400) # VS 2005
171+
172+
# See https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B#Internal_version_numbering
173+
# and https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9271
174+
set(Vcvars_VS17_MSVC_VERSIONS ${Vcvars_TOOLSET_143_MSVC_VERSIONS}) # VS 2022
175+
set(Vcvars_VS16_MSVC_VERSIONS ${Vcvars_TOOLSET_142_MSVC_VERSIONS}) # VS 2019
176+
set(Vcvars_VS15_MSVC_VERSIONS ${Vcvars_TOOLSET_141_MSVC_VERSIONS}) # VS 2017
177+
set(Vcvars_VS14_MSVC_VERSIONS ${Vcvars_TOOLSET_140_MSVC_VERSIONS}) # VS 2015
178+
set(Vcvars_VS12_MSVC_VERSIONS ${Vcvars_TOOLSET_120_MSVC_VERSIONS}) # VS 2013
179+
set(Vcvars_VS11_MSVC_VERSIONS ${Vcvars_TOOLSET_110_MSVC_VERSIONS}) # VS 2012
180+
set(Vcvars_VS10_MSVC_VERSIONS ${Vcvars_TOOLSET_100_MSVC_VERSIONS}) # VS 2010
181+
set(Vcvars_VS9_MSVC_VERSIONS ${Vcvars_TOOLSET_90_MSVC_VERSIONS}) # VS 2008
182+
set(Vcvars_VS8_MSVC_VERSIONS ${Vcvars_TOOLSET_80_MSVC_VERSIONS}) # VS 2005
183+
109184
# Global variables used only in this script (unset at the end)
110185
set(_Vcvars_MSVC_ARCH_REGEX "^(32|64)$")
111186
set(_Vcvars_MSVC_VERSION_REGEX "^[0-9][0-9][0-9][0-9]$")
112187
set(_Vcvars_SUPPORTED_MSVC_VERSIONS
113-
1949 1948 1947 1946 1945 1944 1943 1942 1941 1940 1939 1938 1937 1936 1935 1934 1933 1932 1931 1930 # VS 2022
114-
1929 1928 1927 1926 1925 1924 1923 1922 1921 1920 # VS 2019
115-
1916 1915 1914 1913 1912 1911 1910 # VS 2017
116-
1900 # VS 2015
117-
1800 # VS 2013
118-
1700 # VS 2012
119-
1600 # VS 2010
120-
1500 # VS 2008
121-
1400 # VS 2005
188+
${Vcvars_TOOLSET_143_MSVC_VERSIONS} # VS 2022
189+
${Vcvars_TOOLSET_142_MSVC_VERSIONS} # VS 2019
190+
${Vcvars_TOOLSET_141_MSVC_VERSIONS} # VS 2017
191+
${Vcvars_TOOLSET_140_MSVC_VERSIONS} # VS 2015
192+
${Vcvars_TOOLSET_120_MSVC_VERSIONS} # VS 2013
193+
${Vcvars_TOOLSET_110_MSVC_VERSIONS} # VS 2012
194+
${Vcvars_TOOLSET_100_MSVC_VERSIONS} # VS 2010
195+
${Vcvars_TOOLSET_90_MSVC_VERSIONS} # VS 2008
196+
${Vcvars_TOOLSET_80_MSVC_VERSIONS} # VS 2005
122197
)
123198

124199
# process component arguments

tests/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,51 @@ check_var_equals("Vcvars_FOUND" "TRUE")
158158
check_function_defined("Vcvars_ConvertMsvcVersionToVsVersion")
159159
check_function_defined("Vcvars_GetVisualStudioPaths")
160160

161+
# VS 2022
162+
check_var_equals(Vcvars_TOOLSET_143_MSVC_VERSIONS "1949;1948;1947;1946;1945;1944;1943;1942;1941;1940;1939;1938;1937;1936;1935;1934;1933;1932;1931;1930")
163+
# VS 2019
164+
check_var_equals(Vcvars_TOOLSET_142_MSVC_VERSIONS "1929;1928;1927;1926;1925;1924;1923;1922;1921;1920")
165+
# VS 2017
166+
check_var_equals(Vcvars_TOOLSET_141_MSVC_VERSIONS "1916;1915;1914;1913;1912;1911;1910")
167+
check_var_equals(Vcvars_TOOLSET_140_MSVC_VERSIONS "1900") # VS 2015
168+
check_var_equals(Vcvars_TOOLSET_120_MSVC_VERSIONS "1800") # VS 2013
169+
check_var_equals(Vcvars_TOOLSET_110_MSVC_VERSIONS "1700") # VS 2012
170+
check_var_equals(Vcvars_TOOLSET_100_MSVC_VERSIONS "1600") # VS 2010
171+
check_var_equals(Vcvars_TOOLSET_90_MSVC_VERSIONS "1500") # VS 2008
172+
check_var_equals(Vcvars_TOOLSET_80_MSVC_VERSIONS "1400") # VS 2005
173+
174+
check_var_equals(Vcvars_VS17_MSVC_VERSIONS "${Vcvars_TOOLSET_143_MSVC_VERSIONS}") # VS 2022
175+
check_var_equals(Vcvars_VS16_MSVC_VERSIONS "${Vcvars_TOOLSET_142_MSVC_VERSIONS}") # VS 2019
176+
check_var_equals(Vcvars_VS15_MSVC_VERSIONS "${Vcvars_TOOLSET_141_MSVC_VERSIONS}") # VS 2017
177+
check_var_equals(Vcvars_VS14_MSVC_VERSIONS "${Vcvars_TOOLSET_140_MSVC_VERSIONS}") # VS 2015
178+
check_var_equals(Vcvars_VS12_MSVC_VERSIONS "${Vcvars_TOOLSET_120_MSVC_VERSIONS}") # VS 2013
179+
check_var_equals(Vcvars_VS11_MSVC_VERSIONS "${Vcvars_TOOLSET_110_MSVC_VERSIONS}") # VS 2012
180+
check_var_equals(Vcvars_VS10_MSVC_VERSIONS "${Vcvars_TOOLSET_100_MSVC_VERSIONS}") # VS 2010
181+
check_var_equals(Vcvars_VS9_MSVC_VERSIONS "${Vcvars_TOOLSET_90_MSVC_VERSIONS}") # VS 2008
182+
check_var_equals(Vcvars_VS8_MSVC_VERSIONS "${Vcvars_TOOLSET_80_MSVC_VERSIONS}") # VS 2005
183+
184+
function(check_msvc_version_to_vs_version_convert msvc_versions_var expected_vs_version)
185+
foreach(msvc_version IN LISTS ${msvc_versions_var})
186+
Vcvars_ConvertMsvcVersionToVsVersion("${msvc_version}" output_var)
187+
if(NOT "${output_var}" STREQUAL "${expected_vs_version}")
188+
message(FATAL_ERROR "Vcvars_ConvertMsvcVersionToVsVersion failed for msvc_version [${msvc_version}]
189+
current_value [${output_var}]
190+
expected_value [${expected_vs_version}]
191+
")
192+
endif()
193+
endforeach()
194+
endfunction()
195+
196+
check_msvc_version_to_vs_version_convert(Vcvars_VS17_MSVC_VERSIONS "17")
197+
check_msvc_version_to_vs_version_convert(Vcvars_VS16_MSVC_VERSIONS "16")
198+
check_msvc_version_to_vs_version_convert(Vcvars_VS15_MSVC_VERSIONS "15")
199+
check_msvc_version_to_vs_version_convert(Vcvars_VS14_MSVC_VERSIONS "14.0")
200+
check_msvc_version_to_vs_version_convert(Vcvars_VS12_MSVC_VERSIONS "12.0")
201+
check_msvc_version_to_vs_version_convert(Vcvars_VS11_MSVC_VERSIONS "11.0")
202+
check_msvc_version_to_vs_version_convert(Vcvars_VS10_MSVC_VERSIONS "10.0")
203+
check_msvc_version_to_vs_version_convert(Vcvars_VS9_MSVC_VERSIONS "9.0")
204+
check_msvc_version_to_vs_version_convert(Vcvars_VS8_MSVC_VERSIONS "8.0")
205+
161206
if(FUNCTIONS_ONLY_COMPONENT_REQUESTED)
162207
check_var_not_defined("Vcvars_MSVC_ARCH")
163208
check_var_not_defined("Vcvars_MSVC_VERSION")

0 commit comments

Comments
 (0)