我想在CMake中解析/处理传递的控制台参数,这样如果我在控制台中运行它:
cmake -DCMAKE_BUILD_TYPE=Release -DSOME_FLAG=1 ..
我想从这个内部CMake脚本(以及传递的所有其他参数)中获取-DCMAKE_BUILD_TYPE=Release
和-DSOME_FLAG=1
并将它们保存在某处。
我想要它的原因是通过自定义CMake脚本(之后调用execute_process(cmake <something>)
)传递所有参数,例如
cmake -DCMAKE_BUILD_TYPE=Release -P myscript.cmake
答案 0 :(得分:6)
有CMAKE_ARGC
变量,其中包含传递给CMake的变量数量(除以空格),CMAKE_ARGV0
,CMAKE_ARGV1
,...包含实际值。
对于C ++的语言来说,第一个(零)变量保存你在这种情况下调用的命令(cmake
)是很常见的,因此我们需要除CMAKE_ARGV0
之外的所有内容。让我们做一个简单的循环:
set(PASSED_PARAMETERS "") # it will contain all params string
set(ARG_NUM 1) # current index, starting with 1 (0 ignored)
# you can subtract something from that if you want to ignore some last variables
# using "${CMAKE_ARGC}-1" for example
math(EXPR ARGC_COUNT "${CMAKE_ARGC}")
while(ARG_NUM LESS ARGC_COUNT)
# making current arg named "CMAKE_ARGV" + "CURRENT_INDEX"
# in CMake like in Bash it's easy
set(CURRENT_ARG ${CMAKE_ARGV${ARG_NUM}})
message(STATUS "Doing whatever with ${CURRENT_ARG}")
# adding current param to the list
set(PASSED_PARAMETERS ${PASSED_PARAMETERS} ${CURRENT_ARG})
math(EXPR ARG_NUM "${ARG_NUM}+1") # incrementing current index
endwhile()
(回答我自己的问题,在SO中找不到类似的东西,也许它会帮助某人)