In CMake, how can I define an option that is not configurable by the user, but automatically calculated?
I would like to do the following:
if (FOO AND NOT BAR)
option(BOO "Foo and not bar" ON)
endif()
And then I can use BOO
in different CMake files:
if (BOO)
# do something
endif()
And also in sources:
#ifdef BOO
// do something
#endif
So BOO
behaves like a regular option, but is automatically calculated and not configurable by user running cmake
.
EDIT: I now realize that options are not implicitly available in sources, but need to be defined explicitly with target_add_definitions
or other means. So now it becomes obvious that the solution is to define BOO
as a CMake variable, rather than an option.
CodePudding user response:
Your "non-user-configurable" option sounds like a variable, which you can create like:
if(FOO AND NOT BAR)
set(BOO TRUE)
else()
set(BOO FALSE)
endif()
To use this variable in C code, you need to tell CMake to create a C file which defines this information. Take a look at CMake's configure_file
function. Here's the official documentation, and here's part of the official CMake tutorial which walks through a simple usage of this function. I would type out some example code, but it would be a lot of boilerplate, and the linked documents do the job and should be better at answering your questions.
CodePudding user response:
You can just use variable with set(BOO <what you want>)
.
You can find the documentation here: https://cmake.org/cmake/help/latest/command/set.html
But there is no option
that will restrict the value of your variable for an internal settings. Only you.