Reputation: 5925
I want to parse/process passed console parameters in CMake, so that if I run this in console:
cmake -DCMAKE_BUILD_TYPE=Release -DSOME_FLAG=1 ..
I want to obtain the -DCMAKE_BUILD_TYPE=Release
and -DSOME_FLAG=1
from this inside CMake script (and every other parameter passed) and save them somewhere.
The reason I want it is to pass all parameters through custom CMake script (which calls execute_process(cmake <something>)
after) e.g.
cmake -DCMAKE_BUILD_TYPE=Release -P myscript.cmake
Upvotes: 2
Views: 1870
Reputation: 5925
There is CMAKE_ARGC
variable which contains amount of variables passed to CMake (divided by whitespaces), and CMAKE_ARGV0
, CMAKE_ARGV1
, ... which contains actual values.
It's common for languages for C++ that first (zero) variable holds the command you called (cmake
in this situation), so we will need everything except CMAKE_ARGV0
. Let's make a simple loop then:
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()
(Answering my own question, didn't find anything like that in SO, maybe it'll help someone)
Upvotes: 7