Include file name collisions PIO and third party libs

I have some include file name collisions between two third party libraries and a PlatformIO provided framework package

(NOTE: names are for illustration only)

Normal PlatformIO provided package

 /home/user/.platformio/packages/framework-arduinopackage/core/nrf52/foo.h

My project:
myproject/platformio.ini

build_flags = 
; does not add any "-I" flags

My projects lib directory contains:

myproject/lib/thirdparty1/util/dog/foo.h
myproject/lib/thirdparty1/util/library.json
myproject/lib/thirdparty1/util/extra_script

myproject/lib/thirdparty2/util/dog/foo.h
myproject/lib/thirdparty2/util/library.json
myproject/lib/thirdparty2/util/extra_script

The problem I have is with the PIO generated CPPPATH content. At a minimum it looks like PIO auto creates the include path as follows:

  1. gathering up all found directories from the project’s lib directory
  2. add directories defined within platformio.ini build_flags variable
  3. add in all .platformio/package/framework… directories

so the result in my case is

 -I myproject/lib/thirdparty1/util/dog \
 -I myproject/lib/thirdparty2/util/dog \
 -I /home/user/.platformio/packages/framework-arduinopackage/core/nrf52

Again there is a foo.h in all three include search locations. I need the foo.h to be picked up from the appropriate directory of the library currently being built.

when building lib/thirdparty1  i need -I myproject/lib/thirdparty1/util/dog/foo.h
when building lib/thirdparty2  i need -I myproject/lib/thirdparty2/util/dog/foo.h
when building the frame-work  i need -I /home/user/.platformio/packages/framework-arduinoadafruitnrf52/core/nrf52/foo.h

When building the thirdparty libs I can, via the library.json “extraScript” content, filter out the unwanted paths from CPPPATH and get those to pick up the desired foo.h files.

The problem comes when building the PIO package framework library files. I do not see a way to filter the CPPPATH that is passed when building the PIO included framework files.

I would like to remove the includes of thirdparty1 and thirdparty2 libraries or just tell PIO to not search those at all during it’s prebuild process.

Renaming/moving/modifying the thirdparty files really should be considered as only a temporary workaround.

Thanks for any pointers on how this might be accomplished.

This can be partially resolved on a library by library basis by implementing a library.json with an appropriate extra_scripts.py file to filter out the unwanted include directories from the CPPPATH variable used when compiling the library.

This still appears to be a problem for files in the src directory.

For files in the src directory I added an extra_script.py at the top level, same level as platformio,ini, added a script with an AddPreAction(). This allowed me to filter out some troublesome include paths during the compile of main.cpp only.
I sort of wanted to do the entire src directory, but not sure how that’s done as wild card in the AddPreAction filter do not seem to work. Turns out I cannot do that anyway as other files could not have the includes filtered.

added the srcbld.py to my existing scripts in platformio.ini:

extra_scripts =
        srcbld.py
        pre:pre_check_files.py
        post:post_build.py

in srcbld.py

Import("env")
import fnmatch

def callback(source, target, env):
    print("ENTER MAIN Extra script")
    print("Processing Includes for: " + source[0].name)

    # throw away any path containing "nrf5_sdk", but keep all others
    pattern="*nrf5_sdk*"
    NEW_INCS = []

    for item in env.get("CPPPATH", []):
         if not fnmatch.fnmatch(item, pattern):
            #print("Found one to keep: " + item)
            NEW_INCS.append(item)

    env['CPPPATH'] = NEW_INCS

env.AddPreAction("$BUILD_DIR/src/main.cpp.o", callback)