cmake arguments passed to scripts on command line

62 Views Asked by At

I was just reading the book Modern CMake for C++, and on page 55, the author says exactly this:

Remember that you can pass arguments to scripts through the command line after a -- token. Values will be stored in the CMAKE_ARGV<n> variable and the count of the passed arguments will be in the CMAKE_ARGC variable.

so I tried the following script:

# file.cmake
message("CMAKE_ARGC=${CMAKE_ARGC}")

set (i 0)
while(i LESS CMAKE_ARGC)
  message("CMAKE_ARGV${i}=${CMAKE_ARGV${i}}")
  math(EXPR i "${i}+1")
endwhile() 

from here, and I ran this command:

cmake -P file.cmake -- arg

but the output was:

CMAKE_ARGC=5
CMAKE_ARGV0=cmake
CMAKE_ARGV1=-P
CMAKE_ARGV2=file.cmake
CMAKE_ARGV3=--
CMAKE_ARGV4=arg

Does the author of Modern CMake for C++ say that the script arguments starts after -- (which he's clearly WRONG) or I'm missing something?

1

There are 1 best solutions below

0
273K On

I don't know the exact text there, maybe the author is wrong, maybe you lost some context and misread that. -- does not mean that the args before -- will not be stored in CMAKE_ARGV<n>. It means only that any options after -- are not parsed by CMake.

If you run you script like cmake -P file.cmake -Dvar=val, you get CMAKE_ARGV3=-Dvar=val in the output and the variable ${var} in the script.

If you run you script like cmake -P file.cmake -- -Dvar=val, you get CMAKE_ARGV4=-Dvar=val in the output and no variable ${var} defined in the script.