cmake-获取使用的命令行标志“ -D” [英] cmake - get the used commandline flags "-D"
问题描述
我最近将一些项目从自动工具切换到了cmake。
我喜欢在自动工具上进行的一件常见操作是-如果我进入src构建目录。这里有config.log / config.status-在顶部的 ./ configure --params
命令被列出-因此很容易重新运行以前使用的命令行标志。
(就像编译了一些东西之后-我想添加另一个-启用此-复制并复制;从config.log / status中粘贴-并重新运行
./ configure --old-params --enable-this
)
cmp中的
-我有一堆 -D
标志-如何使用cmake项目找到config.log / status中使用的命令行? / p>
我知道有CMakeCache ...-但很难提取使用的标志
编辑:
我想出了以下解决方案:
#save命令行来重建它:)
set(USED_CMD_LINE cmake)
set(MY_CMAKE_FLAGS CMAKE_BUILD_TYPE CMAKE_INSTALL_PREFIX ENABLE_SSL ENABLE_LUA ENABLE_SSH ENABLE_SNMP MYSQL_USER MYSQL_PASS INSQL_B_C项目$ {MY_CMAKE_FLAGS})
if($ {cmd_line_loop})
STRING(CONCAT USED_CMD_LINE $ {USED_CMD_LINE} -D $ {cmd_lin e_loop} = $ {$ {cmd_line_loop}})
endif()
ENDFOREACH(cmd_line_loop)
STRING(CONCAT USED_CMD_LINE $ {USED_CMD_LINE} ..)
#将文件存储到一个名为 config.status的文件中
FILE(写$ {CMAKE_CURRENT_BINARY_DIR} /config.status $ {USED_CMD_LINE})
在构建文件夹中创建文件config.status-包含所有设置的cmake参数。
pro:
- 似乎可以解决我的问题
- 似乎可以处理后续的cmake调用
con:
- 无法在
FILE(写
?变量 -
MY_CMAKE_FLAGS
包含已知标志-如果添加了新标志,则需要手动更新
致谢
Cmake不给出您列出所有使用的-D标志(定义)的简便方法。但是,对于正确编写的CMakeList,不需要知道带有所有-D标志的完整命令行来更改一个特定的定义/选项。
请考虑以下代码段:
SET(my_var_1 TRUE CACHE BOOL my var 1)
SET(my_var_2 TRUE CACHE BOOL my var 2 )
消息(状态 my_var_1 $ {my_var_1})
消息(状态 my_var_2 $ {my_var_2})
第一个cmake调用:
> cmake .. -Dmy_var_1 = FALSE
-my_var_1 FALSE
-my_var_2是
-配置完成的
-生成完成的
-构建文件已写入:out
第二个cmake调用:
> cmake .. -Dmy_var_2 = FALSE
-my_var_1 FALSE
-my_var_2 FALSE
-配置完成的
-生成完成的
- -构建文件已写入:out
请注意 my_var_1 = FALSE
即使不是ex明确声明(取自缓存)
i recently switched a few projects from autotools to cmake.
one common thing i liked on autotools is that - if i go into the src build directory. there is config.log/config.status - where at the top the ./configure --params
command is listed - so it is easy to rerun the former used commandline flags.
(like after compiling some stuff - i want to add a another --enable-this
- so copy & paste from config.log/status - and rerun the ./configure --old-params --enable-this
)
in cmake - i have a bunch of -D
flags - how can i find the used commandline like in config.log/status - with a cmake project?
i know there is the CMakeCache... - but its hard to extract the used flags
edit:
i came up with the following solution:
#save commandline to rebuild this :)
set(USED_CMD_LINE "cmake ")
set(MY_CMAKE_FLAGS CMAKE_BUILD_TYPE CMAKE_INSTALL_PREFIX ENABLE_SSL ENABLE_LUA ENABLE_SSH ENABLE_SNMP MYSQL_USER MYSQL_PASS MYSQL_HOST MYSQL_DB FULL_FEATURES USE_COVERAGE)
FOREACH(cmd_line_loop IN ITEMS ${MY_CMAKE_FLAGS})
if(${cmd_line_loop})
STRING(CONCAT USED_CMD_LINE ${USED_CMD_LINE} "-D" ${cmd_line_loop} "=" ${${cmd_line_loop}} " ")
endif()
ENDFOREACH(cmd_line_loop)
STRING(CONCAT USED_CMD_LINE ${USED_CMD_LINE} " .. ")
#store to a file aka "config.status"
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/config.status ${USED_CMD_LINE} )
creates a file config.status in the build folder - containing all set cmake params.
pro:
- seems to solve my problem
- seems to work on subsequent cmake calls
con:
- unable to set chmod on
FILE(write
? the variable MY_CMAKE_FLAGS
contains the known flags - needs to be manually updated if a new flag is added
regards
Cmake does not give you easy way to list all used -D flags (defines). However, for correctly written CMakeLists, it is not needed to know the full command line with all -D flags to change one particular define/option.
Consider this snipplet:
SET(my_var_1 TRUE CACHE BOOL "my var 1")
SET(my_var_2 TRUE CACHE BOOL "my var 2")
message(STATUS "my_var_1 ${my_var_1}")
message(STATUS "my_var_2 ${my_var_2}")
First cmake invocation:
>cmake .. -Dmy_var_1=FALSE
-- my_var_1 FALSE
-- my_var_2 TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: out
Second cmake invocation:
>cmake .. -Dmy_var_2=FALSE
-- my_var_1 FALSE
-- my_var_2 FALSE
-- Configuring done
-- Generating done
-- Build files have been written to: out
Note that my_var_1=FALSE
even it is not explicitely stated (taken from cache)
这篇关于cmake-获取使用的命令行标志“ -D”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!