How to capture CMake command line arguments?

前端 未结 3 1694
礼貌的吻别
礼貌的吻别 2020-12-06 01:46

I want to record the arguments passed to cmake in my generated scripts. E.g., \"my-config.in\" will be processed by cmake, it has definition like this:

<
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 01:54

    I don't know of any variable which provides this information, but you can generate it yourself (with a few provisos).

    Any -D arguments passed to CMake are added to the cache file CMakeCache.txt in the build directory and are reapplied during subsequent invocations without having to be specified on the command line again.

    So in your example, if you first execute CMake as

    cmake ../.. -DCMAKE_INSTALL_PREFIX:PATH=/usr
    

    then you will find that subsequently running simply

    cmake .
    

    will still have CMAKE_INSTALL_PREFIX set to /usr


    If what you're looking for from CMAKE_ARGS is the full list of variables defined on the command line from every invocation of CMake then the following should do the trick:

    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()
    message("CMAKE_ARGS: ${CMAKE_ARGS}")
    

    This is a bit fragile as it depends on the fact that each variable which has been set via the command line has the phrase "No help, variable specified on the command line." specified as its HELPSTRING property. If CMake changes this default HELPSTRING, you'd have to update the if statement accordingly.


    If this isn't what you want CMAKE_ARGS to show, but instead only the arguments from the current execution, then I don't think there's a way to do that short of hacking CMake's source code! However, I expect this isn't what you want since all the previous command line arguments are effectively re-applied every time.

提交回复
热议问题