Some built-in helpers to convert OFF/ON in variable into the 0/1

Some function like to_01( [SRC=]) like #cmakedefine01.
It could be useful in some contexts , e.g. consider some header-only library with its option() for enabling diagnostics which would set some internal #define with another name from another library which we, as library author, don’t control:

option(PROJ_ENABLE_DIAGNOSTICS "Enable diagnostics" ON)
...
add_library(proj_api INTERFACE)
to_01(var PROJ_ENABLE_DIAGNOSTICS) # var now has 0/1 depending on OFF/ON (like in `if()` or like cmakedefine01 but value only
target_compile_definitions(proj_api INTERFACE ENABLE_DIAGNOSTICS=${var}) # the proj_api library now provides new compile definition ENABLE_DIAGNOSTICS=0/1

(it will be easier to make as a function returning value, but it’s currently impossible in CMake language)

Thanks.

Even better:

target_compile_definitions(proj_api INTERFACE
  "ENABLE_DIAGNOSTICS=$<BOOL:${PROJ_ENABLE_DIAGNOSTICS}>")
2 Likes