Weird behavior of CMAKE_GENERATOR_TOOLSET
As it known, from version 11 2012 Visual Studio have two default toolsets on Windows:
- v###, which is default, supports new features and targets Windows versions above XP;
-
v###_xp, which is for backward compatibility, lacks new features and targets Windows XP.
where ### is a version of Visual Studio (110 for 11-2012, 120 for 12-2013 and 140 for 14-2015/15-2017)
You can read more about differences here:
http://stackoverflow.com/questions/32580151/what-is-the-difference-between-v120-xp-and-v120-on-vs-2013
http://stackoverflow.com/questions/24240371/vs2013-v120-xp-as-platform-toolset-by-default
CMake was influenced by this functionality by introducing support for toolsets in 2.8.11:
https://cmake.org/Bug/view.php?id=10722
But there's problems with usage of CMAKE_GENERATOR_TOOLSET
.
If I change it not inside if
at all, project generation will fail on any other generator that doesn't support toolsets.
So, I must put it inside something like if (MSVC)
. But then there's another problem:
- If I change
CMAKE_GENERATOR_TOOLSET
afterproject()
, it has no effect - project will be generated with v### toolset, not v###_xp. - If I change
CMAKE_GENERATOR_TOOLSET
beforeproject()
, it will take effect, but all variables with compiler info (e.g.MSVC
,CMAKE_<LANG>_COMPILER_ID
etc.) are still undefined here.
There's also a subproblem - it may be necessary to set needed WinXP compatible toolset accordingly to MSVC version, because project can support some of them. And you can't set, for example, v120_xp as one-stop solution - Visual Studio 14 2015 has v140_xp, but has no v120_xp by default.
Currently the only known for me way to resolve this problem is to parse CMAKE_GENERATOR
with MATCHES
. So it should be?
In the end, I note funny thing that CMAKE_GENERATOR_TOOLSET
, being set after project()
, affects generator if I use cmake-gui and press "Configure" button twice before pressing "Generate".