The SET command can't accept a variable named CACHE because it conflicts with the command's arguments
Found this by accident when using ${${VARIABLE}}
and reading values from a file.
cmake_minimum_required(VERSION 3.10)
project(test)
set(CACHE "123")
CMake Error at CMakeLists.txt:4 (set): set given invalid arguments for CACHE mode.
CACHE
is not an invalid variable name, it's just the parser for the SET
command that has a problem with it. I can succesfully pass -DCACHE:STRING=123
from the command line and access the variable. Adding the CACHE option also works set(CACHE "FOO" CACHE STRING "Value for Foo")
Adding quotes around the variable name doesn't help: set("CACHE" "123")
. As expected, there is no problem using PARENT_SCOPE
, FORCE
or BOOL
. It's just that when CACHE
is specified there is a check for invalid CACHE options, which is triggered based on the number of arguments.
In cmSetCommand.cxx:113
there is code to detect common problems with CACHE options:
// we should be nice and try to catch some simple screwups if the last or
// next to last args are CACHE then they screwed up. If they used FORCE
// without CACHE they screwed up
if ((args.back() == "CACHE") ||
(args.size() > 1 && args[args.size() - 2] == "CACHE") ||
(force && !cache)) {
status.SetError("given invalid arguments for CACHE mode.");
return false;
}
Because set(CACHE "foo")
only has two arguments, which makes the next to last argument "CACHE", the error is triggered.
As a quick fix, I suggest changing the args.size() check to be greater than 2 instead of greater than 1
// we should be nice and try to catch some simple screwups if the last or
// next to last args are CACHE then they screwed up. If they used FORCE
// without CACHE they screwed up
if ((args.back() == "CACHE") ||
** (args.size() > 2 && args[args.size() - 2] == "CACHE") ||**
(force && !cache)) {
status.SetError("given invalid arguments for CACHE mode.");
return false;
}