Optional Code: Problem Definition, OPTION()

Problem

  • hello-second licensing requirements

  • Must 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

    Toplevel CMakeLists.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_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 is ON or OFF

  • Note that this is programming!

  • ⟶ should not be taken lightly

  • Here we present two alternative approaches

  1. Approach 1: globally define flags at CMake and C (macro) levels

    • CMake variable USE_BLACKLIST from option, re-used downwards

    • C macro MACRO_USE_BLACKLIST in DemoConfig.h.in

    • Optionally descend into blacklist directory

    • Conditionals at CMake and C level everywhere

  2. Approach 2: optionally make blacklist an INTERFACE library

    • If not USE_BLACKLIST, make blacklist an INTERFACE library

    • Use TARGET_COMPILE_DEFINITIONS() and TARGET_INCLUDE_DIRECTORIES() to announce (non-)presence of blacklist