将CMake变量传递给ExternalProject_Add

时间:2014-08-27 17:46:45

标签: c++ cmake external-project

我正在使用CMake和ExternalProject_Add构建一个外部库。我已经使用答案here生成以下内容(它捕获命令行输入到CMake以便将其传递给ExternalProject_Add命令):

get_cmake_property(CACHE_VARS CACHE_VARIABLES)
foreach(CACHE_VAR ${CACHE_VARS})
  get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
  if(CACHE_VAR_HELPSTRING STREQUAL "No help, variable specified on the command line.")
    get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
    if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
      set(CACHE_VAR_TYPE)
    else()
      set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
    endif()
    set(CMAKE_ARGS "${CMAKE_ARGS} -D${CACHE_VAR}${CACHE_VAR_TYPE}=\"${${CACHE_VAR}}\"")
  endif()
endforeach()

ExternalProject_Add命令如下所示:

ExternalProject_Add(external_lib
  URL ${EXTERNALLIB_SOURCE_DIR}
  PREFIX ${EXTERNALLIB_PREFIX}
  CMAKE_ARGS "${CMAKE_ARGS};-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  INSTALL_DIR ${EXTERNALLIB_INSTALL_DIR}
  BINARY_DIR "${EXTERNALLIB_PREFIX}/lib" 
  )

我无法弄清楚如何将CMAKE_ARGS变量正确传递给此命令。显然,其他变量工作正常,但CMAKE_ARGS似乎没有正确扩展,因为我知道它包含我想要它包含的内容。我用CMake语法做错了吗?

1 个答案:

答案 0 :(得分:5)

set(CMAKE_ARGS "${CMAKE_ARGS} -D${CACHE_VAR}${CACHE_VAR_TYPE}=\"${${CACHE_VAR}}\"")

您想要从所有选项中创建一个大字符串。请尝试使用简单列表:

list(APPEND CMAKE_ARGS "-D${CACHE_VAR}${CACHE_VAR_TYPE}=${${CACHE_VAR}}")