Binary file name depending on the program variable(s)

I see lots of examples where you can use variable FROM platformio.ini into the program file (main.cpp)
But I would like to use opposite: use the variable FROM main.cpp in platformio.ini

Use case: main.cpp is common for many boards and I simply use the macro in main.cpp like this:

#define THIS_BOARD "board1"
// #define THIS_BOARD "board2"
...

To build the binary for the specific board.

Now, I would like to use “post_program_action” to rename the output binary file to be like this:
"board1.bin"
or:
"board2.bin

based on THIS_BOARD value uncommented in the main.cpp before building

How can I achieve this?
thx

Just do the opposite, like the docs say:

; platformio.ini
[my_vars]
; https://docs.platformio.org/en/latest/projectconf/interpolation.html
board_name="board1" ; or "board2"

[env]
; https://docs.platformio.org/en/latest/projectconf/section_env_build.html#build-flags
build_flags =
    ; this will do equivalent of `#define THIS_BOARD "board1"`
    '-D THIS_BOARD="${my_vars.board_name}"'

; use ${my_vars.board_name} to set the output bin file, idk how

thank you brother
but I am not sure you understood me (or I still don’t understand you):
in my config.h file I have defined which board I want to compile, i.e.:

// ********************* choose device - ONLY ONE! *****************************
  // #define DEVICE_ID  26           // "esp32026" - C3, production - TRV4 Printers
  // #define DEVICE_ID  28           // "esp32028" - S2, production - Garage
  // #define DEVICE_ID  31           // "esp32031" - S2, production - Printer room
  // #define DEVICE_ID  32           // "esp32032" - S2, production - Bedroom mot.
  #define DEVICE_ID  33           // "esp32033" - S2, production - Living room
  // #define DEVICE_ID  35           // "esp32035" - S2, production - Tailor room
  // #define DEVICE_ID  36           // "esp32036" - S2, production - Lidia room
  // #define DEVICE_ID  38           // "esp32038" - S2, production - Toilet bedroom
  // #define DEVICE_ID  39           // "esp32032" - S2, production - garage mot.
  // #define DEVICE_ID  49           // "esp32049" - S2, production - kitchen
  // #define DEVICE_ID  100          // "esp32100" - S2, production - Office
  // #define DEVICE_ID  101          // "esp32101" - S2, production - Dining room
  // #define DEVICE_ID  102          // "esp32102" - S2, production - Toilet up
  // #define DEVICE_ID  104          // "esp32104" - S2, production - Kids room
  // #define DEVICE_ID  105          // "esp32105" - S2, production - Garden

depending which line is uncommented, the binary is for this board
and now, depending also on this uncommented line, I would like my binary file to be renamed accordingly
today, every binary file has the same name, so I need to manually rename it to (or to move to another folder) to make sure that the next compiled file does not overwrite the previous one
If I can make that after compilation for DEVICE_ID=1 my binary is “binary1.bin” then my life would be easier

So I don’t want to define which binary I am compiling in platformio.ini but in my config.h as there are many things depending on the DEVICE_ID (i.e. MQTT sensor name etc.)

I did. I don’t know if you can configure pio from code. But you definitely can configure code from pio.

So, instead of defining your board/device_id/whatever in CODE, do that in CONFIG.

Get rid of config.h and define those constants in platformio.ini. If you need those values in the code, pass them via -D build option.

For that I would make an env for each config: [env:device26], [env:device27] or [env:garage], [env:printer_room], etc.
This way each env gets its own build, and your binaries don’t get overwritten. On the minus side you need to have like 20 builds in your case, but who cares in 21. century with gigahertzs of processors and terrabites of disks :wink:

thank you @dvdnwk
I shall probably rethink this way as you say said - at the end I don’t care where it gets “scripted” as long as I don’t need to do too much of manual job
yes, I need separate binaries as each board (so MCU) uses its own config that I am not sure can be common i.e. different GPIOs, even different usage (i.e. some boards are temperature/humidity only while others are also motion sensors and/or light) so based on the DEVICE_ID I have further code in use - not everything is common amongst the boards
then going forward I could have binaries posted also to the final folders or even ftp so that sensors could upgrade firmware automatically from there

But I really thought I could send variable from CODE to PIO

one super good outcome of your proposal would be: no more manual commenting out the #define section - all have its own [env:xxx] so 1 time build and voila - all are compiled at once :wink:
maybe your solution is even simpler :wink:
thank you

@dvdnwk I tried the proposed by your approach - do you think I could make it even more compact?

[platformio]

; default_envs = esp32092

[common]
lib_deps =
    sparkfun/SparkFun MAX1704x Fuel Gauge Arduino Library @ ^1.0.4
    adafruit/Adafruit SHT31 Library @ ^2.2.0
    sparkfun/SparkFun TSL2561 @ ^1.1.0
    adafruit/Adafruit BusIO @ ^1.13.2
    SPI
    WiFi
    ottowinter/ESPAsyncTCP-esphome @ ^1.2.3
    esphome/AsyncTCP-esphome@^1.2.2
    esphome/ESPAsyncWebServer-esphome@^2.1.0
    bblanchon/ArduinoJson @ ^6.19.4
    SPIFFS

monitor_filters = time, colorize, esp32_exception_decoder
platform = espressif32
framework = arduino
build_flags =
    -DCORE_DEBUG_LEVEL=0

[esp32]
board=esp32dev

[esp32s2]
board=esp32-s2-saola-1

[esp32c3]
board=esp32-c3-devkitm-1

[env]
upload_speed = 921600
monitor_speed = 115200
extra_scripts = ${scripts_defaults.extra_scripts}

[scripts_defaults]
extra_scripts =
    post:extra_scripts/output_bins.py

; ********************* PRODUCTION boards definition start here *********************

[env:esp32026]
board                   = ${esp32c3.board}
monitor_filters         = ${common.monitor_filters}
platform                = ${common.platform}
framework               = ${common.framework}
lib_deps                = ${common.lib_deps}
build_flags             = '-D DEVICE_ID=26' ${common.build_flags}

[env:esp32028]
board                   = ${esp32s2.board}
monitor_filters         = ${common.monitor_filters}
platform                = ${common.platform}
framework               = ${common.framework}
lib_deps                = ${common.lib_deps}
build_flags             = '-D DEVICE_ID=28' ${common.build_flags}
........

that brings me at the end the files like this:

You can try extends (inheritance) for less repetition: Platformio.ini common settings for release and debug but not for native - #4 by dvdnwk

@dvdnwk you mean this part could have been avoided in every [env:xxx], right?

monitor_filters         = ${common.monitor_filters}
platform                = ${common.platform}
framework               = ${common.framework}
lib_deps                = ${common.lib_deps}

maybe also this one:

build_flags = ${common.build_flags}

so the final would be (per device):

[env:esp32026]
board                   = ${esp32c3.board}
build_flags             = '-D DEVICE_ID=26'
[platformio]

; default_envs = esp32026

[common]
lib_deps =
    sparkfun/SparkFun MAX1704x Fuel Gauge Arduino Library @ ^1.0.4
    adafruit/Adafruit SHT31 Library @ ^2.2.0
    sparkfun/SparkFun TSL2561 @ ^1.1.0
    adafruit/Adafruit BusIO @ ^1.13.2
    SPI
    WiFi
    ottowinter/ESPAsyncTCP-esphome @ ^1.2.3
    esphome/AsyncTCP-esphome@^1.2.2
    esphome/ESPAsyncWebServer-esphome@^2.1.0
    bblanchon/ArduinoJson @ ^6.19.4
    SPIFFS

monitor_filters = time, colorize, esp32_exception_decoder
platform = espressif32
framework = arduino
build_flags =
    -DCORE_DEBUG_LEVEL=0

[esp32]
board=esp32dev

[esp32s2]
board=esp32-s2-saola-1

[esp32c3]
board=esp32-c3-devkitm-1

[env]
upload_speed = 921600
monitor_speed = 115200
extra_scripts = ${scripts_defaults.extra_scripts}

[scripts_defaults]
extra_scripts =
    post:extra_scripts/output_bins.py

[base]
monitor_filters         = ${common.monitor_filters}
platform                = ${common.platform}
framework               = ${common.framework}
lib_deps                = ${common.lib_deps}
build_flags             = ${common.build_flags}

; ********************* PRODUCTION boards definition start here *********************

[env:esp32026]
extends                 = base
board                   = ${esp32c3.board}
build_flags             = '-D DEVICE_ID=26' 

[env:esp32028]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=28'

[env:esp32031]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=31' 

[env:esp32032]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=32'

[env:esp32033]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=33' 

[env:esp32035]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=35' 

[env:esp32036]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=36'

[env:esp32038]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=38'

[env:esp32039]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=39' 

[env:esp32049]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=49'

[env:esp32100]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=100' 

[env:esp32101]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=101'

[env:esp32102]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=102'

[env:esp32104]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=104' 

[env:esp32105]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=105' 


; ********************* TEST boards definition start here *********************

[env:esp32092]
extends                 = base
board                   = ${esp32s2.board}
build_flags             = '-D DEVICE_ID=92'
...
=========================================================================== [SUCCESS] Took 14.82 seconds ===========================================================================

Environment    Status    Duration
-------------  --------  ------------
esp32026       SUCCESS   00:00:12.370
esp32028       SUCCESS   00:00:14.633
esp32031       SUCCESS   00:00:14.911
esp32032       SUCCESS   00:00:14.559
esp32033       SUCCESS   00:00:14.582
esp32035       SUCCESS   00:00:14.441
esp32036       SUCCESS   00:00:14.412
esp32038       SUCCESS   00:00:14.431
esp32039       SUCCESS   00:00:14.514
esp32049       SUCCESS   00:00:14.568
esp32100       SUCCESS   00:00:14.643
esp32101       SUCCESS   00:00:15.122
esp32102       SUCCESS   00:00:14.400
esp32104       SUCCESS   00:00:14.858
esp32105       SUCCESS   00:00:14.799
esp32092       SUCCESS   00:00:14.819
=========================================================================== 16 succeeded in 00:03:52.062 ===========================================================================
 *  Terminal will be reused by tasks, press any key to close it. 

Like that?
That works!
thank you

yes, with a cavecat – if you override, you must “mention” parent config

[env:esp32026]
extends = env:the_parent_env_whatever_you_inherit_from
board = 'board123'
build_flags =
    ${env:the_parent_env_whatever_you_inherit_from.build_flags}
    '-D DEVICE_ID=26'

@dvdnwk like this:


[base]
monitor_filters         = ${common.monitor_filters}
platform                = ${common.platform}
framework               = ${common.framework}
lib_deps                = ${common.lib_deps}
build_flags             = ${common.build_flags}

; ********************* PRODUCTION boards definition start here *********************

[env:esp32026]
extends                 = base
board                   = ${esp32c3.board}
build_flags             = ${base.build_flags} '-D DEVICE_ID=26'

yes

I guess you can also inline common definitions now :+1:

thank you @dvdnwk but I did not get it