简体   繁体   中英

Have CMake select MSVC platform toolset explicitly

Hi, I have a problem, that I have a C++ project that uses select C++11/14 features that for the MSVC compiler are at best only present in the CTP_Nov2013 compiler toolset. I am aware that there is a very similar topic here , and that the most relevant answer was that the toolset must not be tinkered with from the script, as the end-user will want to tinker with it.

  1. First of all, I do not agree that the selection of the toolset is the privilage of the end-user (whomever that might refer to in case a library under development).
  2. Secondly, the only thing I'd like to accomplish is that CMake detects if various compiler capabilities are present, and if they are missing, AND the platform is MSVC, then depending on the version of MSVC CMake attempts to change the platform toolset to the CTP version. I know for a fact, that VS 12 will not recieve any more compiler updates (at least not in CTPs), as this is a privilage of the VS 13 Beta from here on, so it is safe to assume that MSVC_VER 1700 (if that is the version corresponding to VS 12) toolsets can be selected using the script.
  3. Thirdly, manually updating the generated Visual Studio project files often result in defunct project files. Not considering that it is tedious to change the toolset of roughly a dozen targets one by one, if the source path contains unicode characters (which they do in my case), then cl.exe will fail to find them. This is an issue of CMake, and is related to character encoding of the project file and the way CMake writes paths. There is nothing I can do about this. The project files MUST be ready enough for me to only have to hit F7.

I have found a way to achieve what I wanted with the commands

set(CMAKE_GENERATOR_TOOLSET "CTP_Nov2013" CACHE STRING "Platform Toolset" FORCE) 
set(CMAKE_VS_PLATFORM_TOOLSET "CTP_Nov2013" CACHE STRING "Platform Toolset" FORCE)

However the problem with this is that the script needs to be run twice in order for this to take effect. Since the cmake command-line is invoked using

cmake -G"Visual Studio 12 2013 Win64"

There is already a toolset selected, but I immediately wish to override the toolset incorporated into the x64 Configuration of the selected generator. Ultimately the user selects 32/64-bit builds from the command line as usual using the generator specification, but I wish to hide the incapabilities of the MSVC compiler from the "end-user" in case he/she has the CTP installed.

How can I write the script and invoke it from the command line so it works for both the Visual Studio and NMake Makefiles generators and for the first invocation of CMake?

Using the reply of @Tanuki and set_target_properties , you can force the usage of the platform toolset in your target

if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "CTP_Nov2013")
  set_target_properties(${your_target}
  PROPERTIES
  PLATFORM_TOOLSET "${CMAKE_VS_PLATFORM_TOOLSET}")
endif()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM