CMAKE_GENERATOR_TOOLSET and CMAKE_TOOLCHAIN_FILE
2015-09-08 14:50
573 查看
V3.1.3:
Some CMake generators support a toolset name to be given to the native build system to choose a compiler. If the user specifies a toolset name (e.g. via the cmake -T option) the value will be available in this variable.
The value of this variable should never be modified by project code. A toolchain file specified by the CMAKE_TOOLCHAIN_FILE variable
may initialize CMAKE_GENERATOR_TOOLSET. Once a given build tree has been initialized with a particular value for this variable,
changing the value has undefined behavior.
This variable is specified on the command line when cross-compiling with CMake. It is the path to a file which is read early in the CMake run and which specifies locations for compilers and toolchain utilities, and other target platform and compiler related
information.
CMake generates the project files that VS 2012 can load. At the same time, in the project files, the compiler version is automatically set to v110, i.e., Microsoft Visual C++ 11.0 (_MSC_VER 1700).
There are chances that the default platform toolset is not appropriate. For example, a VS2012 project may be configured by the user to be built with different platform toolsets, such as v90(Visual
Studio 2008), v100(Visual Studio 2010), v110_xp(Visual Studio 2012 – Windows XP), and lately, v120_CTP_Nov2012(Microsoft Visual C++ Compiler Nov 2012 CTP). Specifically, projects using certain C++11 features,
such as variadic template, have to be compiled with v120_CTP_Nov2012 in Visual Studio 2012, because v110 does not support those features.
The CMake team saw the need and enabled CMake users to change the platform toolset in a generator, see bugs 0010722: Add PlatformToolset support
to Visual Studio 2010 generator and 0013774: Support v110_xp target with MSVC 2012 Update 1. The latest CMake
2.8.11 Reference mentioned in a few occasions about toolset:
-T <toolset-name> command line option;
CMAKE_GENERATOR_TOOLSET variable;
[align=left]CMAKE_VS_PLATFORM_TOOLSET variable and CMAKE_XCODE_PLATFORM_TOOLSET variable;[/align]
I prefer to capture the project settings in CMakeLists.txt than a command line option, so I skip -T option. I tried both CMAKE_GENERATOR_TOOLSET andCMAKE_VS_PLATFORM_TOOLSET,
and I had better luck with CMAKE_GENERATOR_TOOLSET. Below is a CMakeLists.txt file using the CTP compiler instead:
cmake_minimum_required (VERSION 2.8)
set ( MY_MACRO_INTEGER 42 ) # see configure file below
project (test_sln) # VS solution
if(MSVC OR MSVC_IDE)
if( MSVC_VERSION LESS 1700 ) # VC10-/VS2010-
message(FATAL_ERROR "The project requires C++11 features. "
"You need at least Visual Studio 11 (Microsoft Visual Studio 2012), "
"with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012).")
elseif( MSVC_VERSION EQUAL 1700 ) # VC11/VS2012
message( "VC11: use Microsoft Visual Studio 2012 "
"with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)" )
set(CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012" CACHE STRING "Platform Toolset" FORCE)
else() # VC12+, assuming C++11 supported.
endif()
endif()
configure_file (
"${PROJECT_SOURCE_DIR}/test.h.in"
"${PROJECT_SOURCE_DIR}/test.h"
)
set ( ConfigFiles
test.h.in
)
set ( GeneratedFiles
test.h
)
source_group( "config" FILES ${ConfigFiles} )
source_group( "generated" FILES ${GeneratedFiles} )
add_executable(test_proj test.cpp ${ConfigFiles} ${GeneratedFiles}) # VS project
The CMake file basically detects if MSVC is the generator, and when VC11 is used it changes the platform toolset to v120_CTP_Nov2012.
I have to click Configure twice to clear the red variables in CMake GUI before I can generate the correct project files.
Sometimes when the project files are already generated, but there is some changes in the CMakeLists.txt files and you regenerate the project files, CMake goes wild. For example,
I received this error a few times:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/Program Files (x86)/Microsoft Visual C++ Compiler Nov
2012 CTP/bin/cl.exe" is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Temp/cmake_test/build/CMakeFiles/CMakeTmp
Run Build Command:C:\PROGRA~2\MICROS~3.0\Common7\IDE\devenv.com
CMAKE_TRY_COMPILE.sln /build Debug /project cmTryCompileExec3629620200
…
I have to close CMake GUI and remove the out-of-source build directory entirely, then re-generate the project files from scratch to overcome this problem.
For this reason, I recommend using the out-of-source build directory for easier start-over.
find test.h.in under Filter config, and test.h under Filter generated. Do not forget to add the corresponding files to the project (test_proj here), otherwise this does not work.
Filters are handy. In this example, I know I should never try to modify test.h in Visual Studio because it is generated.
#define MY_MACRO_INTEGER @MY_MACRO_INTEGER@
#define CMAKE_VS_PLATFORM_TOOLSET "@CMAKE_VS_PLATFORM_TOOLSET@"
#define CMAKE_GENERATOR_TOOLSET "@CMAKE_GENERATOR_TOOLSET@"
CMake replaces @VARIABLE_NAME@ with the actual variable value in the configure file, then generates the result file test.h
#define MY_MACRO_INTEGER 42
#define CMAKE_VS_PLATFORM_TOOLSET "v120_CTP_Nov2012"
#define CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012"
As you can see, it is able to access both my custom variable and CMake built-in variables. If you only use the CMake variables as macros, you can use #cmakedefine in the configure file. Or use CMake
command add_definitions to add preprocessor macros.
to for example, v120_CTP_Nov2012, see ThreeTargets.cmake and CMakeLists.txt.
This does not work for me on CMake 2.8.11, and I do not find in CMake 2.8.11 documentation about this property either.
http://www.cmake.org/cmake/help/v3.1/variable/CMAKE_GENERATOR_TOOLSET.html
http://binglongx.com/2013/06/28/set-non-default-platform-toolset-in-cmake/
CMAKE_GENERATOR_TOOLSET
Native build system toolset name specified by user.Some CMake generators support a toolset name to be given to the native build system to choose a compiler. If the user specifies a toolset name (e.g. via the cmake -T option) the value will be available in this variable.
The value of this variable should never be modified by project code. A toolchain file specified by the CMAKE_TOOLCHAIN_FILE variable
may initialize CMAKE_GENERATOR_TOOLSET. Once a given build tree has been initialized with a particular value for this variable,
changing the value has undefined behavior.
CMAKE_TOOLCHAIN_FILE
Path to toolchain file supplied to cmake(1).This variable is specified on the command line when cross-compiling with CMake. It is the path to a file which is read early in the CMake run and which specifies locations for compilers and toolchain utilities, and other target platform and compiler related
information.
Set Non-Default Platform Toolset in CMake
In general, CMake uses the default platform toolset of the generator. The generator mandates the format of the project files. If the user chooses Visual Studio 11 (Microsoft Visual Studio 2012) as the generator,CMake generates the project files that VS 2012 can load. At the same time, in the project files, the compiler version is automatically set to v110, i.e., Microsoft Visual C++ 11.0 (_MSC_VER 1700).
There are chances that the default platform toolset is not appropriate. For example, a VS2012 project may be configured by the user to be built with different platform toolsets, such as v90(Visual
Studio 2008), v100(Visual Studio 2010), v110_xp(Visual Studio 2012 – Windows XP), and lately, v120_CTP_Nov2012(Microsoft Visual C++ Compiler Nov 2012 CTP). Specifically, projects using certain C++11 features,
such as variadic template, have to be compiled with v120_CTP_Nov2012 in Visual Studio 2012, because v110 does not support those features.
The CMake team saw the need and enabled CMake users to change the platform toolset in a generator, see bugs 0010722: Add PlatformToolset support
to Visual Studio 2010 generator and 0013774: Support v110_xp target with MSVC 2012 Update 1. The latest CMake
2.8.11 Reference mentioned in a few occasions about toolset:
-T <toolset-name> command line option;
CMAKE_GENERATOR_TOOLSET variable;
[align=left]CMAKE_VS_PLATFORM_TOOLSET variable and CMAKE_XCODE_PLATFORM_TOOLSET variable;[/align]
I prefer to capture the project settings in CMakeLists.txt than a command line option, so I skip -T option. I tried both CMAKE_GENERATOR_TOOLSET andCMAKE_VS_PLATFORM_TOOLSET,
and I had better luck with CMAKE_GENERATOR_TOOLSET. Below is a CMakeLists.txt file using the CTP compiler instead:
cmake_minimum_required (VERSION 2.8)
set ( MY_MACRO_INTEGER 42 ) # see configure file below
project (test_sln) # VS solution
if(MSVC OR MSVC_IDE)
if( MSVC_VERSION LESS 1700 ) # VC10-/VS2010-
message(FATAL_ERROR "The project requires C++11 features. "
"You need at least Visual Studio 11 (Microsoft Visual Studio 2012), "
"with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012).")
elseif( MSVC_VERSION EQUAL 1700 ) # VC11/VS2012
message( "VC11: use Microsoft Visual Studio 2012 "
"with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)" )
set(CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012" CACHE STRING "Platform Toolset" FORCE)
else() # VC12+, assuming C++11 supported.
endif()
endif()
configure_file (
"${PROJECT_SOURCE_DIR}/test.h.in"
"${PROJECT_SOURCE_DIR}/test.h"
)
set ( ConfigFiles
test.h.in
)
set ( GeneratedFiles
test.h
)
source_group( "config" FILES ${ConfigFiles} )
source_group( "generated" FILES ${GeneratedFiles} )
add_executable(test_proj test.cpp ${ConfigFiles} ${GeneratedFiles}) # VS project
The CMake file basically detects if MSVC is the generator, and when VC11 is used it changes the platform toolset to v120_CTP_Nov2012.
I have to click Configure twice to clear the red variables in CMake GUI before I can generate the correct project files.
Sometimes when the project files are already generated, but there is some changes in the CMakeLists.txt files and you regenerate the project files, CMake goes wild. For example,
I received this error a few times:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/Program Files (x86)/Microsoft Visual C++ Compiler Nov
2012 CTP/bin/cl.exe" is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Temp/cmake_test/build/CMakeFiles/CMakeTmp
Run Build Command:C:\PROGRA~2\MICROS~3.0\Common7\IDE\devenv.com
CMAKE_TRY_COMPILE.sln /build Debug /project cmTryCompileExec3629620200
…
I have to close CMake GUI and remove the out-of-source build directory entirely, then re-generate the project files from scratch to overcome this problem.
For this reason, I recommend using the out-of-source build directory for easier start-over.
Filter in Project in Visual Studio
The CMake file above also shows source_groups, which are used to organize the files in a project under filters in Visual Studio. In the screenshot below, you canfind test.h.in under Filter config, and test.h under Filter generated. Do not forget to add the corresponding files to the project (test_proj here), otherwise this does not work.
Filters are handy. In this example, I know I should never try to modify test.h in Visual Studio because it is generated.
Configure Files
The last thing the CMake file shows is the configure_file feature of CMake. CMake basically uses test.h.in as a template and generates test.h along with project files. This is test.h.in:#define MY_MACRO_INTEGER @MY_MACRO_INTEGER@
#define CMAKE_VS_PLATFORM_TOOLSET "@CMAKE_VS_PLATFORM_TOOLSET@"
#define CMAKE_GENERATOR_TOOLSET "@CMAKE_GENERATOR_TOOLSET@"
CMake replaces @VARIABLE_NAME@ with the actual variable value in the configure file, then generates the result file test.h
#define MY_MACRO_INTEGER 42
#define CMAKE_VS_PLATFORM_TOOLSET "v120_CTP_Nov2012"
#define CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012"
As you can see, it is able to access both my custom variable and CMake built-in variables. If you only use the CMake variables as macros, you can use #cmakedefine in the configure file. Or use CMake
command add_definitions to add preprocessor macros.
Other Reference
three_cpp uses another approach to set the platform toolset. It uses the CMake set_target_properties command to set the build target’s PLATFORM_TOOLSET propertyto for example, v120_CTP_Nov2012, see ThreeTargets.cmake and CMakeLists.txt.
This does not work for me on CMake 2.8.11, and I do not find in CMake 2.8.11 documentation about this property either.
http://www.cmake.org/cmake/help/v3.1/variable/CMAKE_GENERATOR_TOOLSET.html
http://binglongx.com/2013/06/28/set-non-default-platform-toolset-in-cmake/
相关文章推荐
- 解决airserver在Windows下安装失败的问题
- http://elinux.org/Main_Page
- [YARN] 2.2 GB of 2.1 GB virtual memory used. Killing container.
- STL map,vector和pair的混合应用
- FOXMAIL无法响应
- LeetCode 219. Contains Duplicate II
- Customizing AIX Prompt
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- tail命令和head命令查询文件指定行数
- 使用UltraISO制作U盘启动盘
- Liunx 下装openoffice 乱码解决__http://wenku.baidu.com/link?url=3uaBXBwPt_ewQSZEV1vnyhaFrB8Pf7Z-VYJDGLnFvUM
- win8配置最新版flashdevelop v2和 sdk4.6+air18 配置系统未能初始化flashplayer等 解决配置方案
- 【Django】 发邮件 email
- git clone操作出现fatal:index-pack failed错误解决方案
- Latex Tools在OSX EI Captain无法编译的问题
- CPaintDC CDC
- Air中File类获取地址的研究
- leetcode 70 Climbing Stairs(难易度:Easy)
- 怎样写rspec mailer测试
- 【SICP练习】135 练习3.66