23

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

我找到了关于它的常见问题解答条目:https ://gitlab.kitware.com/cmake/community/wikis/FAQ#how-can-i-specify-my-own-configurations-for-generators-that-allow-它-

但是,我无法让它工作。

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

4

1 回答 1

27

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

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

因此,要添加自定义构建类型,根本不需要操作 CMAKE_CONFIGURATION_TYPES 变量。只需设置相应的变量并开始使用它,如下所述

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 )

我们可能还想更新帮助字符串(顺便设置默认构建类型):

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 )

这是一个 主意,因为每次再次获取配置时(每次更改其中的某些内容)都会不断附加额外的选项。

于 2012-07-11T17:00:03.760 回答