Optional Code: Problem Definition, OPTION()
¶
Problem¶
hello-second
licensing requirementsMust pay extra $$ to be able to greet “Joerg” (and a number of other blacklisted names)
⟶ separate builds
One that ignores a blacklist (pay extra $$)
One in the public domain, with restrictions
Solution: Add An Option¶
Goal: add option
USE_BLACKLIST
to be used like$ cmake -DUSE_BLACKLIST=ON ... $ cmake -DUSE_BLACKLIST=OFF ...
Implementation: manyfold; see below
Define Option USE_BLACKLIST
; Option Usage, Cache¶
Define option
OPTION(USE_BLACKLIST "Refuse to greet blacklisted names" ON)
Default:
ON
$ cmake -L ~/work/jfasch-home/trainings/material/soup/cmake/code/
...
USE_BLACKLIST:BOOL=ON
...
Set to
OFF
$ cmake -DUSE_BLACKLIST=OFF ~/work/jfasch-home/trainings/material/soup/cmake/code/
...
Value of
USE_BLACKLIST
is persisted (“cached”)
$ cmake -L ~/work/jfasch-home/trainings/material/soup/cmake/code/
...
USE_BLACKLIST:BOOL=OFF
...
Option Handling: Many Approaches¶
Question: what to when
USE_BLACKLIST
isON
orOFF
Note that this is programming!
⟶ should not be taken lightly
Here we present two alternative approaches
Approach 1: globally define flags at CMake and C (macro) levels
CMake variable
USE_BLACKLIST
from option, re-used downwardsC macro
MACRO_USE_BLACKLIST
inDemoConfig.h.in
Optionally descend into
blacklist
directoryConditionals at CMake and C level everywhere
Approach 2: optionally make
blacklist
anINTERFACE
libraryIf not
USE_BLACKLIST
, makeblacklist
anINTERFACE
libraryUse
TARGET_COMPILE_DEFINITIONS()
andTARGET_INCLUDE_DIRECTORIES()
to announce (non-)presence ofblacklist