Looking for a place to put a generated header

I have an extra.py script in my library, which generates a board-specific header on-the-fly. It works brilliantly, by saving this file next to some other library headers. But there are some issues with this approach:

  • I have to add this file to .gitgnore to prevent it from getting checked in
  • it breaks when building the library for two different boards at the same time
  • I have to check which version is currently present to re-generate only when the board changes

It would be simpler / cleaner / better, IMO, to place such generated files somewhere in the .pio/build/<envname>/ tree, because that fixes all of the above in one sweep.

I can’t quite decide where to put them, though, as there is no spot inside .pio/ where an include file will be automatically found, as far as I can tell:

arm-none-eabi-g++ -o .pio/build/l432/lib566/jeeh-dev/jee.o -c -std=c++17 -fno-rtti -fno-exceptions -Wall -Wextra -Wno-format -Os -ffunction-sections -fdata-sections -Wall -mthumb -mcpu=cortex-m4 -DPLATFORMIO=60000 -DSTM32L432xx -DF_CPU=80000000L -I/Users/jcw/code/jee/jeeh-dev -I/Users/jcw/.platformio/packages/framework-cmsis@2.50501.200527/CMSIS/Include -I/Users/jcw/.platformio/packages/framework-cmsis-stm32l4/Include /Users/jcw/code/jee/jeeh-dev/jee.cpp

All the -I... settings point into the ~/.platformio/ area, as you can see.

Is there some “natural” spot to put these generated headers? Would I then have to add a somewhat-funky include path to that directory myself? It might be useful to come up with a convention for this, so that custom script-generated files can live near compiler-generated .o files, etc.

Please note that this is for a library (with its own library.json file), which gets included into a PIO project. It’s not about the PIO main project but about the library build. The generated header comes from an extra.py script in the library, not in the main project.

(There’s a working demo in this zip file, which can be run as “cd examples && pio run -e l432”, the generated file is called jee-svn.h - if anyone wants to dig into this …)

Good solution, we also use it internally for different cases. A piece of code from the upcoming PlatformIO Core 6.0 and new Unit Testing engine

    def configure_build_env(self, env):
        env.Replace(
            UNITY_CONFIG_DIR=os.path.join("$BUILD_DIR", "unity_config"),
            BUILD_UNITY_CONFIG_DIR=os.path.join("$BUILD_DIR", "unity_config_build"),
        )
        env.Append(
            CPPDEFINES=["UNITY_INCLUDE_CONFIG_H"],
            CPPPATH=["$UNITY_CONFIG_DIR"],
        )
        self.generate_unity_extras(env.subst("$UNITY_CONFIG_DIR"))
        env.BuildSources("$BUILD_UNITY_CONFIG_DIR", "$UNITY_CONFIG_DIR")

Aha, ok, that gives me a practical starting point. Thank you.

1 Like