Platform-specific source files in a library

I’m currently trying to implement a library that can be built for different platforms. However some source code should only be included based on the actual platform used. From lots of reading I currently try to implement something based on extra_script.py and ended up with the following:

Directory structure

+ library.json
+ scripts/
  + extra_script.py
+ src/
  + platform/
    + windows/
      + platform_win.c
    + esp32/
      + platform_esp32.c

library.json:

{
  "build": {
    "srcDir": "src",
    "srcFilter": "+<*> -<platform/*>",
    "extraScript": "scripts/extra_script.py"
  }
}

extra_script.py:

Import("env")
import os.path

platform = env.PioPlatform().name
if platform.startswith("windows"):
    env.Append(SRC_FILTER=["+<platform/windows>"])
elif platform == "espressif32":
    esp32_path = os.path.realpath("../src/platform/esp32")
    env.Append(SRC_FILTER=[f"+<{esp32_path}>"])

You can see that I have tried several alternatives such as using the notation from library.json inside extra_script.py (here only in the windows platform path) or using the absolute path (only in the ESP path). I also tried setting srcFilter instead of SRC_FILTER but always with the same result. It looks like these platform specific sources are never really added to the library.

Is there something I’m missing in this setup? Should this be possible?

OMG, I’m very sorry for potentially wasting anyones time :sweat_smile: I just figured it out myself:

  1. It’s not possible to use "srcFilter" and SRC_FILTER at the same time. If you want to use an extraScript, then you have to do everything in that Python file and remove the "srcFilter" in library.json
  2. To use multiple entries in the SRC_FILTER, they have to be added as a list:
env.Append(SRC_FILTER=["+<*>", "-<platform/*>", "+<platform/windows/*>"])