How to know if this is the first env?

Hello,

Maybe I’m seeing the problem wrongly, but here is my little issue.
I have a pio script, executed for each environments I have. It generate files that are global to all envs (a auto version.h file with build number inside).
When I execute pio run, I can see the script being executed multiple time, for each environment) that make the generation of the file multiple times in my case.

One solution is to only create the file only if it does not exist (this requires me to manually remove this file)
The best solution is to know when pio run is executing the first environment, so that I know that I can generate the file. But I don’t see how I can get this information.
Maybe there is a better way that I do not see.

Thanks!

Not sure if this an X/Y problem, but okay.

Consider

[env]
platform = atmelavr
framework = arduino
extra_scripts = test.py

[env:uno]
board = uno

[env:nanoatmega328]
board = nanoatmega328

And

Import("env")

this_env = env.subst("$PIOENV")
print("This env name: " + this_env)

platform = env.PioPlatform()
print("The platform config: " + str(platform.config))

for e in platform.config.envs():
    print("Env name: " + str(e))

all_env_names = [str(e) for e in platform.config.envs()]
is_this_the_first_env_in_the_file = this_env == all_env_names[0]
print("Are we currently building in the first environment of the file? " + str(is_this_the_first_env_in_the_file))

Thus

Processing uno (platform: atmelavr; board: uno; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR (5.0.0) > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (avr-stub) External (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr @ 5.2.0
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
This env name: uno
The platform config: <ProjectConfig C:\Users\Max\temp\test_envs\platformio.ini>
Env name: uno
Env name: nanoatmega328
Are we currently building in the first environment of the file? True
Checking size .pio\build\uno\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.4% (used 9 bytes from 2048 bytes)
Flash: [          ]   1.4% (used 444 bytes from 32256 bytes)
======================== [SUCCESS] Took 2.27 seconds ========================

Processing nanoatmega328 (platform: atmelavr; board: nanoatmega328; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/nanoatmega328.html
PLATFORM: Atmel AVR (5.0.0) > Arduino Nano ATmega328
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 30KB Flash
DEBUG: Current (avr-stub) External (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr @ 5.2.0
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
This env name: nanoatmega328
The platform config: <ProjectConfig C:\Users\Max\temp\test_envs\platformio.ini>
Env name: uno
Env name: nanoatmega328
Are we currently building in the first environment of the file? False
Checking size .pio\build\nanoatmega328\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.4% (used 9 bytes from 2048 bytes)
Flash: [          ]   1.4% (used 444 bytes from 30720 bytes)
======================== [SUCCESS] Took 2.22 seconds ========================
1 Like

Great! Thank you so much!