Library extraScript can't add include paths to build flags

I’m developing a library for a HAL for several of our boards. It works on STM32(stm32cube) and ESP32 (IDF).
I want some source files to be compiled only for some targets. In the future I may split the library in multiple libraries, but for now I want one library, since there is a lot of common code
In my library.json i’ve set my extraScript to a python script, where I handle advanced platform depended things such as dependencies.
This works well. But there is one thing I cannot move from library.json to extraScript.

“build”: {
“flags”: “-Isrc/stm32”
}

I cannot get this include path flag working in my script and I don’t want the include folder for stm32 to be visible for esp32 to avoid possible header-file naming conflicts. I cannot edit some of these files, because I’ve generated them with STM32CubeMX and don’t want to change those too much.
I’m getting this error if I remove the flags and try to add them in my script:

fatal error: usbd_conf.h: No such file or directory

Here is what I’ve tried so far:

env.Append(build_flags=[“-Isrc/stm32”])

env.Append(BUILD_FLAGS=[“-Isrc/stm32”])

env.Append(LINKFLAGS=[“-Isrc/stm32”])

env.Append(src_build_flags=[“-Isrc/stm32”])

How can I add include folder paths in my python script?

Are you basing your work off of the documented HAL based library example?

Looking at the above example, I think you should rather use CPPPATH (and maybe SRC_FILTER for exclusion) and not build_flags. Also LINKFLAGS should not be needed – the final linker script involves only linking the compiled object files and archive files, no include files are needed there.

It is loosely based on the lib found in HAL-based library. It is a seperate library, not in the lib folder.
All sources build. Only the header files in the src/stm32 folder are not seen by the compiler (the other folders still have their build flags).
generate_headers.py:

try:
    import configparser
except ImportError:
    import ConfigParser as configparser

Import('env')

config = configparser.ConfigParser()
config.read(env.get("PROJECT_CONFIG"))

if env.get("PIOPLATFORM") == 'ststm32':

    print("platform is ststm32!")
 
    env.Append(CPPPATH=["src/stm32"])
    env.Append(CPATH=["src/stm32"])> 
    env.Append(srcFilter=["+<src/stm32>"])
    
    
    # lib_extra_dirs
    env.Append(lib_extra_dirs=["src/Middlewares/ST/STM32_USB_Device_Library/Class"])
    env.Append(lib_extra_dirs=["src/Middlewares/ST/STM32_USB_Device_Library"])
    env.Append(lib_extra_dirs=["src/stm32"])

    # lib_deps
    env.Append(lib_deps=["CDC"])
    env.Append(lib_deps=["Core"])
    env.Append(lib_deps=["STM32Cube Middleware-FreeRTOS"])
    env.Append(lib_deps=["stm32"])

library.json:

{
    "name": "lib_name",
    "version": "0.0",
    "authors": [
        {
            "maintainer": true,
            "name": "author name"
        }
    ],
    "frameworks": [
        "stm32cube",
        "espidf"
    ],
    "platforms": [
        "ststm32",
        "espressif32"
    ],
    "build": {
        "flags": "-DSTM32 -Isrc/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -Isrc/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc",
        "extraScript": "generate_headers.py",
        "libArchive": false
    }   
}

So from where is this file included? Where does the file itself reside? Note that with

you change the build environment for your library – not the global ones for other libraries or the main src/ folder of the project. See Redirecting... and Redirecting....

src/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h
includes
src/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h
which includes src/stm32/usbd_conf.h but it cannot be found

I do not want to change the source files or folder structure of these generated files. Changing the folder location or adding extra files (like library.json) is fine to me.

I’ve now changed the library so that the sub folders are no longer included as extra libraries but simply as source files with srcFilter. It still doesn’t work without the global build flags in library.json. I still don’t know how to get it to work with the script.
I really hope there is a solution or else platformio is fundamentally broken, since it doesn’t allow splitting code into different folders and doesn’t allow dynamic building.

"build": {
    "srcDir":"",
    "srcFilter":[
        "+<src/*.cpp>",
        "+<include/*.h>",
        "+<src/stm32/*.c>",
        "+<src/stm32/*.h>",
        "+<src/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/*.h>",
        "+<src/Middlewares/ST/STM32_USB_Device_Library/Core/Src/*.c>",
        "+<src/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/*.h>",
        "+<src/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/*.c>"
    ],
    "flags": [
        "-DSTM32",
        "-Isrc/stm32", 
        "-Isrc/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc",
        "-Isrc/Middlewares/ST/STM32_USB_Device_Library/Core/Inc"
    ],
    "extraScript": "generate_headers.py",
    "libArchive": false
}

Why doesn’t env.Append(build_flags… append the build_flags to the environment?

Edit: It looks like a known issue:

I hope there is a workaround

Well the issue is from 2018. Have you tried modifying the global environment as noted above?

I was able to get it to work with:

env.ProcessFlags(“-Isrc/stm32”)

I can now combine above with:

env.Append(srcFilter=[“-<pathtoexclude>”])

to add the include paths I need and exclude the folders I don’t need. This is what I needed.

Now I’m looking for a way to add the following in my library (as opposed to requiring the user to add it):

build_flags = -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp

The following does NOT work:

env.ProcessFlags(["-mthumb","-mfpu=fpv4-sp-d16","-mfloat-abi=softfp"])

Any advice?
Maybe it is better to create our own board definitions and add it there.

And I would like to move custom_freertos_config_location variable from the ini to the library as well. Right now it is located in the user project. I was able to remove the file and change the value with:

config.set(“env:” + env.get(“PIOENV”), “custom_freertos_config_location”,“src/stm32/FreeRTOSConfig.h”)