Per-file #define pattern in CMake (log-level/file-hash,etc)

Hi everyone,

I lead a team of embedded C software engineers and we’re seeking a more suitable build system than what we have had grow organically over the years. I’ve been reading lots of CMake documentation, and testing how we’d implement some of our existing build system patterns in CMake. For the most part I’ve found a sensible mechanism for doing things.

We operate a mono repo, with our executables structured similar to:

  • Product
    • MCU
      • Debug/Release

There are a handful of bits of functionality that we currently utilise which I can’t see a direct mapping (or alternative) for CMake. These are:

  • FILE_HASH - A SHA256 hash passed as a build define to each source file
  • FILENAME_HASH - A SHA256 hash passed as a build define to each source file
  • LOG_LEVEL - The log level for this file passed as a build define

Ultimately they’re all following a similar method of establish file state → set file build define. I see there are some ways to do this in CMake (set_source_file_properties), but they don’t feel like the “CMake way”.

I’m not necessarily looking to replicate our precise mechanisms in CMake, but I’d like a suitable substitute that suits the CMake paradigm.

Could someone point me towards some material that might help?

Thanks!

set_source_file_properties is pretty much the only thing available for per-source settings (there’s no concept of “source for target” that can affect usage requirements in a suitable way). Note that the file hash will be tricky as it will either be baked in at configure time (slowing configure down and putting it in the path of incremental builds). I think I would write a script that generates a response file with the -D flags and then have OBJECT_DEPENDS include that response file as a dependency.