如何向 cmake 添加自定义构建类型?(目标品牌) [英] How to add a custom build type to cmake ? (targeting make)
问题描述
我正在尝试为我的 cmake 构建添加自定义构建类型,目标是代码覆盖率.
I'm trying to add a custom build type for my cmake build, targetted at code coverage.
我找到了关于它的 FAQ 条目:https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-can-i-specify-my-own-configurations-for-generators-that-allow-it-
I've found the FAQ entry about it : https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-can-i-specify-my-own-configurations-for-generators-that-allow-it-
但是,我无法让它工作.
However, I can't make it work.
这是我的代码,使用 cmake 2.8.5 :
Here is my code, using cmake 2.8.5 :
message("* Adding build types...")
if(CMAKE_CONFIGURATION_TYPES)
list(APPEND CMAKE_CONFIGURATION_TYPES CodeCoverage)
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
"Add the configurations that we need"
FORCE)
message(" Available build types are now : ${CMAKE_CONFIGURATION_TYPES}")
else()
message(" XXX custom build types are not allowed...")
endif()
我得到不允许 XXX 自定义构建类型..."...
And I get "XXX custom build types are not allowed..."...
推荐答案
发现问题:添加自定义构建和自定义配置之间存在混淆:
Found the problem : there is a confusion between adding custom builds and custom configurations :
- 配置适用于 Visual Studio 或 XCode 等特殊工具
- 构建类型是一个更原始的功能
- configurations are for special tools like Visual Studio or XCode
- build types are a much rawer feature
所以要添加自定义构建类型,根本不需要操作 CMAKE_CONFIGURATION_TYPES 变量.只需设置相应的变量并开始使用它,如 解释 :
So to add a custom build type, there is no need to manipulate the CMAKE_CONFIGURATION_TYPES variable at all. Just set the corresponding variables and start using it, as explained :
SET(GCC_DEBUG_FLAGS "-g -Wall")
# Add new build types
message("* Adding build types...")
SET(CMAKE_CXX_FLAGS_COVERAGE
"${GCC_DEBUG_FLAGS} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
SET(CMAKE_C_FLAGS_COVERAGE
"${GCC_DEBUG_FLAGS} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
我们可能还想更新帮助字符串(以及 顺便设置默认构建类型):
We may also want to udate the help string (along with setting the default build type by the way) :
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Debug
CACHE STRING "Choose the type of build : None Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)
message("* Current build type is : ${CMAKE_BUILD_TYPE}")
关于尝试从另一个构建类型继承默认选项的警告:
A bit of warning about trying to inherit default options from another build type doing this :
SET(CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
这是一个糟糕的想法,因为每次再次获取配置(每次更改其中的内容时)都会附加额外的选项.
This is a bad idea because the extra options keep being appended every time the config is sourced again (every time you change something in it).
这篇关于如何向 cmake 添加自定义构建类型?(目标品牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!