如何捕获 CMake 命令行参数? [英] How to capture CMake command line arguments?

查看:50
本文介绍了如何捕获 CMake 命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在生成的脚本中记录传递给 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_ARGSCMAKE_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆