如何捕获CMake命令行参数? [英] How to capture CMake command line arguments?
问题描述
我想记录在我生成的脚本中传递给cmake的参数。例如,my-config.in将由cmake处理,它的定义如下:
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:
config =@ CMAKE_ARGS @
config="@CMAKE_ARGS@"
之后 cmake
, my-config
将包含这样的行:
After cmake
, my-config
will contain a line something like this:
config = - DLINUX -DUSE_FOO = y -DCMAKE_INSTALL_PREFIX = / usr
config="-DLINUX -DUSE_FOO=y -DCMAKE_INSTALL_PREFIX=/usr"
我尝试了 CMAKE_ARGS
, CMAKE_OPTIONS
,但失败。没有文件提到这一点。 : - (
I tried CMAKE_ARGS
, CMAKE_OPTIONS
, but failed. No documents mention this. :-(
推荐答案
我不知道任何提供这些信息的变量,但你可以自己生成
I don't know of any variable which provides this information, but you can generate it yourself (with a few provisos).
传递给CMake的任何 -D
参数都会添加到缓存文件 CMakeCache.txt
,并在后续调用中重新应用,而不必在命令行中再次指定。
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.
例如,如果您首先执行CMake为
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 .
仍然会有 CMAKE_INSTALL_PREFIX
设为 / usr
CMAKE_ARGS
是从CMake的每次调用在命令行上定义的变量的完整列表,那么以下应该做的技巧:
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}")
这有点脆弱,因为它取决于通过命令行设置的每个变量有短语指定为 HELPSTRING
属性的无帮助,在命令行中指定变量。如果CMake更改此默认 HELPSTRING
,则必须相应地更新 if
语句。
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.
如果这不是你想要的 CMAKE_ARGS
,而是只显示当前执行,那么我不认为有一种方法来做这个短缺的黑客CMake的源代码!但是,我预计这不是你想要的,因为所有以前的命令行参数每次都被有效地重新应用。
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.
这篇关于如何捕获CMake命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!