Rename bin file on build

Is there a way to rename the bin file to include the DBUILD_TAG when targeting a specific build?

e.g. in the platformio.ini I have the a entry version = -DBUILD_TAG=2.2. I would like this to be concatenated to the bin file when doing a release build so that firmware.bin becomes firmware_v2.2.bin

Sure, take a look at Advanced Scripting. You need an extra script

Import("env")

build_tag = "v2.2"

env.Replace(PROGNAME="firmware_%s" % build_tag)
2 Likes

Thanks Ivan

I got it working using a python script that renames it and moves it to a release location. Working like a well oiled machine now :slight_smile:

1 Like

I can’t get this to work.
This is show in the output
warning: Calling missing SConscript without error is deprecated.
Transition by adding must_exist=0 to SConscript calls.
Missing SConscript ‘extra_script.py’

So what exactly is your project file structure and the content of your extra script?

Thanks Max for your help, here the info

the content of extrascript.py:

Import("env", "projenv")
# access to global construction environment
print(env)
# access to project construction environment
print(projenv)
my_flags = env.ParseFlags(env['BUILD_FLAGS'])
defines = {k: v for (k, v) in my_flags.get("CPPDEFINES")}
print(defines)
env.Replace(PROGNAME="firmware_test%s" % defines.get("VERSION")) 

location of the extrascript.py file
Screenshot 2020-04-10 09.26.18

my platform.ini

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino
lib_deps =
    PubSubClient
    EspSoftwareSerial
build_flags = -D VERSION=1.1.3
extra_scripts = pre:extra_script.py
monitor_speed = 115200

output

So in your platformio.ini you instruct PIO to execute extra_script.py but in your project there’s no such file, but a extrascript.py instead. You just have a typo :sweat_smile:.

2 Likes

:blush: thanks a lot

Did you use this script:

Import(“env”)
my_flags = env.ParseFlags(env[‘BUILD_FLAGS’])
defines = {k: v for (k, v) in my_flags.get(“CPPDEFINES”)}

print(defines)

env.Replace(PROGNAME=“firmware_%s” % defines.get(“VERSION”))

I just don’t get this to work(New to platformio)
Where do I put what?
Any advice will be greatly appreciated,
Thank you in advance.

Just wanted to post this for the next person. I was building some Tasmota firmware and the CPPDEFINES includes nested KVP’s so doing: defines = {k: v for (k, v) in my_flags.get(“CPPDEFINES”)} will fail. Can’t have a key that has the syntax of a KVP. Instead, just do this to parse those nested KVPs.

Import("env")
my_flags = env.ParseFlags(env['BUILD_FLAGS'])
defines = dict()
for b in my_flags.get("CPPDEFINES"):
    if isinstance(b, list):
        defines[b[0]] = b[1]
    else:
        defines[b] = b
        
env.Replace(PROGNAME="%s" % defines.get("IMAGE_NAME"))

Then I have this in my custom platform IO env: platformio_tasmota_cenv.ini

[env:AkumA-S31]
build_flags             = ${env.build_flags} -DAkumA_S31 -DIMAGE_NAME="tasmota-custom-s31-v6"
lib_extra_dirs          = lib/lib_basic, lib/lib_i2c, lib/lib_display, lib/lib_ssl, lib/lib_audio, lib/lib_rf, lib/lib_div
extra_scripts           = pre:extra_script.py

This renames my bin file to IMAGE_NAME.

Dear,
I use this:

[env:env_custom_prog_name]
platform = espressif32
framework = arduino
board = esp32dev
build_flags =
    -DVERSION=${this.custom_prog_version}
extra_scripts = pre:extra_script.py
custom_prog_version = 1.2.3

and this:

Import("env")

env.Replace(PROGNAME="firmware_%s" % env.GetProjectOption("custom_prog_version"))

Is possible to use a define or variable directly from main.cpp ??

example:
custom_prog_version = 1.2.3

mod

custom_prog_version =" my define or varialbe in main.cpp"

thank you

Yes, see Stringification.

Dear, @ivankravets
thank you for your reply,
this is regular?
image

Don’t work for me!!

There is an example for your use case
https://docs.platformio.org/en/latest/scripting/examples/custom_program_name.html

Dear @ivankravets , thank you for your reply,
I try this

[env:env_custom_prog_name]
platform = espressif32
framework = arduino
board = esp32dev
build_flags =
    -DVERSION=${this.custom_prog_version}
extra_scripts = pre:extra_script.py
custom_prog_version = 1.2.3

and this

Import("env")

env.Replace(PROGNAME="firmware_%s" % env.GetProjectOption("custom_prog_version"))

but i need to set use variable in main.cpp in line “custom_prog_version”

Is possible?
I try but i don’t have good result.
Thank you for your support and for your work.

Adding

env.Append(CPPDEFINES=[
    ("CUSTOM_PROG_VERSION", env.StringifyMacro(env.GetProjectOption("custom_prog_version"))),
])

to the script and Serial.println(CUSTOM_PROG_VERSION); does not work?

(Updated to use env.StringifyMacro)

1 Like

https://docs.platformio.org/en/latest/projectconf/section_env_build.html#stringification

This is the right link:
build_flags — PlatformIO latest documentation

Is it somehow possible to get a #define set in the code, to use by PlatformIO e.g. in the filename of the binary?
So a define which is set in a header file.