Optional Code: Problem Definition, OPTION()#
Problem#
hello-secondlicensing 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_BLACKLISTto be used like$ cmake -DUSE_BLACKLIST=ON ... $ cmake -DUSE_BLACKLIST=OFF ...
Implementation: manyfold; see below
Define Option USE_BLACKLIST; Option Usage, Cache#
Define option
ToplevelCMakeLists.txt#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_BLACKLISTis 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_BLACKLISTisONorOFFNote 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_BLACKLISTfrom option, re-used downwardsC macro
MACRO_USE_BLACKLISTinDemoConfig.h.inOptionally descend into
blacklistdirectoryConditionals at CMake and C level everywhere
Approach 2: optionally make
blacklistanINTERFACElibraryIf not
USE_BLACKLIST, makeblacklistanINTERFACElibraryUse
TARGET_COMPILE_DEFINITIONS()andTARGET_INCLUDE_DIRECTORIES()to announce (non-)presence ofblacklist