Searching for a solution / pattern to configure libs with preprocessor defines

We (the OSS project OpenKNX) have a bunch of firmware modules encapsulating specific functionallity. They are contained in own repos and a VSC/Plattform I/O project.
They can be built standalone but can also be integrated into another firmware (we use git subtree for that in the /lib dir).

Our modules rely on quite a big set of #defines defining the memory layout and some hardware properties. These lets call them hardware.h and knxmem.h logically belong to the root project.

After some trial&and&error with a fixed path to store that .h files so that every module uses the same one in a build, we came to use the -include build flag so that the #defines are set for the whole build process for all modules (-include hardware.h -include knxmem.h).

That works for the build process if you do a complete build.
But the dependecy walker in PIO struggles with that because it does not know of the defines introduced with -include. And so some changed files are not built because they have somethine like a #ifdef FEATUREA … #endif in them and FEATUREA is defined in the knxmem.h and the build process seems not to know of them.
That puzzles me a little bit because PIO seems to know of defines introduced with -D flag in the plattformio.ini but not the one introduced with -include.

So, that is the description of the problem. We are searching for a solution that allows us to:

  • store the relevant defines in the repository of the root project
  • have a working code highlighting
  • working partial build

The -include is probably pushing the limits of PlatformIO’s library dependency finder.

Files not being recompiled although the file pointed to by -include changes seems like an SCons problem to me (the underlying build system PIO uses).

Both seem to be separate issues – can you report them at Issues · platformio/platformio-core · GitHub, best with a minimal project for reproduction?

what two issues exactly are you refering with “both” ?

I see one: PIO is not building files that should be build because defines in a header file included by compiler flag -include changed

what is the other? Maybe a missunderstanding

I prepare a sample project

To me,

  1. PIO not rebuilding the firmware although a file pointed to by -include <file> has changed is a bug
  2. PIO not detecting a library dependency that is guarded by an #ifdef <macro> with <macro> being defined in a file pointed to by -include is a bug

seem to be be two separate issues. One is general SCons rebuild logic, the other one LDF (library dependency finder).

The first issue is already correctly opened.

As a work around, I can only recommend to not use -include but regular #include statements in the files that need the macros.