如何向cmake添加自定义构建类型? (定位制作)

时间:2012-07-11 17:00:03

标签: cmake

我正在尝试为我的cmake构建添加自定义构建类型,目标是代码覆盖率。

我找到了关于它的常见问题解答条目:http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_specify_my_own_configurations_.28for_generators_that_allow_it.29_.3F

然而,我无法使其发挥作用。

这是我的代码,使用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自定义构建类型......”......

1 个答案:

答案 0 :(得分:23)

发现问题:添加自定义版本和自定义配置之间存在混淆:

  • 配置适用于Visual Studio或XCode等特殊工具
  • 构建类型是一个更原始的功能

因此,要添加自定义构建类型,根本不需要操作CMAKE_CONFIGURATION_TYPES变量。只需设置相应的变量并开始使用它,如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 )

我们可能还希望更新帮助字符串(顺便提一下setting the default build type):

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}")

关于尝试从另一个构建类型继承默认选项的一些警告:

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 )

这是一个错误的主意,因为每次再次获取配置时,每次更改内容时都会追加额外的选项。