[CMake] Capturing command line arguments passed to CMake

Timothy Wrona tjwrona1992 at gmail.com
Sat Apr 13 14:18:29 EDT 2019


I am working on a CMake project that uses ExternalProject_Add to add an
external dependency. The issue is, I need all of my command line arguments
from my project to be propagated to the external project so it will build
using the same build configuration and with the correct compiler settings.

Is there a simple way to extract all of the command line arguments into a
variable so I can simply pass them to ExternalProject_Add with the
"CMAKE_ARGS" parameter?

The only solution I have found so far is a function that parses the CMake
Cache and extracts all of the variables with a help message that says "No
help, variable specified on the command line." While this does work, it
feels like a hacky workaround at best and seems like it may break in a
future update of CMake if they decide to change the default cache help
messages.

Here is the code to make this hack work:

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

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

      endif()

    endforeach()


Is there any easy way to just get a list of all of the command line
arguments without parsing the cache and filtering by help messages? This
solution just really feels wrong... there must be a better way.

Thanks,
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190413/40068970/attachment.html>


More information about the CMake mailing list