Injection of version of the computed libraries

Again through Advanced scripting. Here, the object returned by env.PioPlatform() returns the needed info.

You can add this to the bottom of the script:

from platformio.package.version import get_original_version

platform = env.PioPlatform()
used_packages = platform.dump_used_packages()
for package in used_packages:
    pio_package_version = package["version"] # e.g. "1.70300.191015"
    pio_package_name = package["name"] # e.g. "toolchain-atmelavr"
    # can fail at decoding and return None if package version is not in semver format
    # in these cases the pio package version is already the decoded version
    # e.g. 1.70300.191015 => 7.3.0
    # e.g. 5.1.0 => None
    pio_decoded_version = get_original_version(pio_package_version)
    print("Name '%s' Version: %s" % (package["name"], str(get_original_version(package["version"]))))
    name_converter = lambda name: name.upper().replace(" ", "_").replace("-", "_")
    projenv.Append(CPPDEFINES=[
     ("PIO_PACKAGE_%s_PKG_VERSION" % name_converter(pio_package_name) , "\\\"" + pio_package_version + "\\\"")
    ])
    if pio_decoded_version is not None: 
        projenv.Append(CPPDEFINES=[
        ("PIO_PACKAGE_%s_DECODED_VERSION" % name_converter(pio_package_name) , "\\\"" + pio_decoded_version + "\\\"")
        ])

The code was derived by looking how PlatformIO generates the output for “PACKAGES: …” at the start of the compilation output which lsits all used packages.

PACKAGES:
 - framework-arduinoespressif32 3.10005.210223 (1.0.5)
 - tool-esptoolpy 1.30000.201119 (3.0.0) 
 - toolchain-xtensa32 2.50200.97 (5.2.0)

That’s found here.

And that will generate

-DPIO_PACKAGE_TOOLCHAIN_ATMELAVR_PKG_VERSION="1.70300.191015" -DPIO_PACKAGE_TOOLCHAIN_ATMELAVR_DECODED_VERSION="7.3.0" -DPIO_PACKAGE_FRAMEWORK_ARDUINO_AVR_PKG_VERSION="5.1.0"

As compiler flags / macros for an Arduino Uno project.

For platform = espressfi32 and board = esp32dev it would generate

-DPIO_PACKAGE_TOOLCHAIN_XTENSA32_PKG_VERSION="2.50200.97" -DPIO_PACKAGE_TOOLCHAIN_XTENSA32_DECODED_VERSION="5.2.0" -DPIO_PACKAGE_FRAMEWORK_ARDUINOESPRESSIF32_PKG_VERSION="3.10005.210223" -DPIO_PACKAGE_FRAMEWORK_ARDUINOESPRESSIF32_DECODED_VERSION="1.0.5" -DPIO_PACKAGE_TOOL_ESPTOOLPY_PKG_VERSION="1.30000.201119" -DPIO_PACKAGE_TOOL_ESPTOOLPY_DECODED_VERSION="3.0.0"

I’m accessing internal APIs here from the PlatformIO core. The platformio.builder.tools.piolib package path maps to platformio-core/platformio/builder/tools/piolib.py at develop · platformio/platformio-core · GitHub.

I used this file because it outputs the dependency graph

So I just had to look how it’s stored in those internal objects and did the same logic as in the linked file. Thus it’s only to be found in the open-source implementation, but not regular docs. This was more a searching, experimenting and debugging cycle.