如何将C / CXX标志传递到CMake中的外部项目

时间:2019-05-28 12:28:34

标签: cmake

我正在使用CMake来驱动项目的构建,但是有些子项目需要使用自动工具来构建。我使用CMake中的ExternalProject功能进行构建,但是我无法将完整的默认C / CXX标志集传递给子项目的configure脚本。

当前,我只传递了CMAKE_C_FLAGS,CMAKE_CXX_FLAGS等。问题是这不包括特定于build_type的标志,这导致在使用调试信息(例如)进行编译时会出现问题,因为我必须手动指定-g标志

当前外部项目配置如下:

{
  "telegram": {
    "text": "Pick a color",
    "reply_markup": {
      "inline_keyboard": [
        [
          {
            "text": "Red",
            "callback_data": "Red"
          },
          {
            "text": "Blue",
            "callback_data": "Blue"
          },
          {
            "text": "Green",
            "callback_data": "Green"
          }
        ]
      ]
    }
  }
}

问题行如下:

ExternalProject_Add(
  openthread-build
  SOURCE_DIR ${openthread_SOURCE_DIR}
  BINARY_DIR ${openthread_BINARY_DIR}
  CONFIGURE_COMMAND cd ${openthread_SOURCE_DIR}
        COMMAND ${openthread_SOURCE_DIR}/configure ${OPENTHREAD_CONFIGURE_OPTS}
        "CXX=${CMAKE_CXX_COMPILER}"
        "CPP=${CMAKE_C_COMPILER} -E"
        "CC=${CMAKE_C_COMPILER}"
        "AR=${CMAKE_C_COMPILER_AR}"
        "RANLIB=${CMAKE_C_COMPILER_RANLIB}"
        "NM=${CMAKE_NM}"
        "STRIP=${CMAKE_STRIP}"
        "CFLAGS=${CMAKE_C_FLAGS} ${OPENTHREAD_INCLUDE_ARG}"
        "CPPFLAGS=${CMAKE_C_FLAGS} ${OPENTHREAD_INCLUDE_ARG}"
        "CXXFLAGS=${CMAKE_CXX_FLAGS} ${OPENTHREAD_INCLUDE_ARG}"
        "LDFLAGS=${CMAKE_EXE_LINKER_FLAGS}"
        "--host=${MACHINE_NAME}"
  BUILD_COMMAND ${OPENTHREAD_MAKE} ${OPENTHREAD_MAKE_ARGS}
  BUILD_ALWAYS ON
  DOWNLOAD_COMMAND  cd ${openthread_SOURCE_DIR} && ./bootstrap
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

理想情况下,该名称可能类似于"CFLAGS=${CMAKE_C_FLAGS} ${OPENTHREAD_INCLUDE_ARG}" ,但是由于变量名区分大小写而无法正常工作。

${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}

因此,我想最后的选择是编写一个形成"CFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}} ${OPENTHREAD_INCLUDE_ARG}" 的函数,将其转换为upper,然后对其进行扩展,但这似乎很笨拙。

有没有简单的方法可以将C / CXX标志的默认基本集传递给外部项目,而无需对每种构建类型进行手动硬编码或添加功能?我希望将默认的CMAKE_C_FLAGS和CMAKE_C_FLAGS_ *包含在构建类型中。

1 个答案:

答案 0 :(得分:1)

我仍然对其他答案持开放态度,但是现在我决定创建一个大写版本的build_type,并使用它来提取特定于build_type的标志。

# get an uppercase version of the build type, for extracting build_type specific flags
if(CMAKE_BUILD_TYPE)
    string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UC)
endif()

因此各个标志线如下所示:

"CFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BUILD_TYPE_UC}} ${OPENTHREAD_INCLUDE_ARG}"

整个外部项目调用非常拥挤,但是看起来像:

ExternalProject_Add(
  openthread-build
  SOURCE_DIR ${openthread_SOURCE_DIR}
  BINARY_DIR ${openthread_BINARY_DIR}
  CONFIGURE_COMMAND cd ${openthread_SOURCE_DIR}
        COMMAND ${openthread_SOURCE_DIR}/configure ${OPENTHREAD_CONFIGURE_OPTS}
        "CXX=${CMAKE_CXX_COMPILER}"
        "CPP=${CMAKE_C_COMPILER} -E"
        "CC=${CMAKE_C_COMPILER}"
        "AR=${CMAKE_C_COMPILER_AR}"
        "RANLIB=${CMAKE_C_COMPILER_RANLIB}"
        "NM=${CMAKE_NM}"
        "STRIP=${CMAKE_STRIP}"
        "CFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BUILD_TYPE_UC}} ${OPENTHREAD_INCLUDE_ARG}"
        "CPPFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BUILD_TYPE_UC}} ${OPENTHREAD_INCLUDE_ARG}"
        "CXXFLAGS=${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BUILD_TYPE_UC}} ${OPENTHREAD_INCLUDE_ARG}"
        "LDFLAGS=${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${BUILD_TYPE_UC}}"
        "--host=${MACHINE_NAME}"
  BUILD_COMMAND ${OPENTHREAD_MAKE} ${OPENTHREAD_MAKE_ARGS}
  BUILD_ALWAYS ON
  DOWNLOAD_COMMAND  cd ${openthread_SOURCE_DIR} && ./bootstrap
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)