Screenplay: Making SQLite3 Optional (configure_file())

Alternative: configure_file() Instead Of target_compile_definitions()

  • In Screenplay: Making SQLite3 Optional (target_compile_definitions()), we used target_compile_definitions() to propagate HAVE_SINK_SQLITE3 from toolcase/base/ right into firmware/data-logger.cpp

  • If done much, this can clutter compiler commandlines with macro definitions

  • What if every user like firmware/data-logger.cpp pulls in that information via a dedicated - generated - header file?

  • configure_file()

  • Beware: this is not less a massacre!

Make It So

  • Remove propagated commandline macro from toolcase/data-logger/

    # if (SQLite3_FOUND)
    #   target_compile_definitions(data-logger INTERFACE HAVE_SQLITE3=1)
    # endif()
    
  • firmware/data-logger.cpp still builds - it just does not see HAVE_SQLITE3 defined

  • firmware/data-logger.cpp to act differently

  • ⟶ pull in a header file that contains HAVE_SQLITE3 (and possibly related) definitions

#include <project-config.h>     // <--- defines HAVE_SQLITE3
#if HAVE_SQLITE3==1
#  include <sink-sqlite3.h>
#else
#  include <sink-terminal.h>
#endif
.../firmware/data-logger.cpp:4:10: fatal error: project-config.h: No such file or directory
    4 | #include <project-config.h>     // <--- defines HAVE_SQLITE3
      |          ^~~~~~~~~~~~~~

“Configured Files”: configure_file()

  • Toplevel (any other directory is fine, but lets keep such things together prominently), add project-config.h.in

    #pragma once
    #define HAVE_SQLITE3 @CONFIG_HAVE_SQLITE3@
    
  • In toplevel CMakeLists..txt, add

    find_package(SQLite3)      # <--- already in place
    
    if (SQLite3_FOUND)
      set(CONFIG_HAVE_SQLITE3 1)
    else()
      set(CONFIG_HAVE_SQLITE3 0)
    endif()
    configure_file(project-config.h.in project-config.h)
    include_directories(${CMAKE_CURRENT_BINARY_DIR})
    
  • @CONFIG_HAVE_SQLITE3@ will be substituted with variable value

  • project-config.h in corresponding directory (toplevel in our case) in the build tree

  • include_directories(${CMAKE_CURRENT_BINARY_DIR}) adds that to the include paths of the entire build

What Else To Put In project-config.h.in

  • Version numbers maybe?

  • In toplevel CMakeLists.txt

    project(Gluehweinkochen VERSION 42.666)
    
    message("Major: ${Demo_VERSION_MAJOR}")   # <--- 42
    message("Major: ${Demo_VERSION_MINOR}")   # <--- 666
    
  • In project-config.h.in, add …

    #define DEMO_MAJOR @Demo_VERSION_MAJOR@
    #define DEMO_MINOR @Demo_VERSION_MINOR@
    
  • In firmware/data-logger.cpp and firmware/boiling-pot.cpp, make use of those.