Passing CMake Variables to ExternalProject_Add

sklum picture sklum · Aug 27, 2014 · Viewed 7k times · Source

I'm building an external library using CMake with ExternalProject_Add. I've used the answer here to generate the following (which captures command line input to CMake in order to pass it to the ExternalProject_Add command):

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()

The ExternalProject_Add command looks like this:

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

I cannot figure out how to properly pass the CMAKE_ARGS variable to this command. Obviously, the other variables work fine, but the CMAKE_ARGS one seems not to be expanded properly as I know it contains what I want it to contain. Am I doing something wrong syntacticly with CMake?

Answer

user2288008 picture user2288008 · Aug 27, 2014

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

You want to create one big string from all options. Try simple list instead:

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