Pre-script extra_scripts problem

I have a pre script in my platformio file that I want to bump up a version number in a header file before the build occurs. The issue I’m having is that it fires at random times, not just pre-build - for example at the start of Visual Studio Code it fires, bumping up the version number. Not just when I perform a build.

I’m at a loss as to what I have incorrect as I have read the docs and numerous posts on this issue.

I have in my platform.io file the following defined:

[env:mydebug]
extra_scripts = 
  pre:buildscript_versioning.py

and in my buildscript_versioning.py:

import datetime
Import("env")
FILENAME_BUILDNO = 'versioning'
FILENAME_VERSION_H = 'include/led_version.h'
version = 'v2.0.'

build_no = 0
env.Dump()
try:
    with open(FILENAME_BUILDNO) as f:
        build_no = int(f.readline()) + 1
except:
    print('Starting build number from 1..')
    build_no = 1
with open(FILENAME_BUILDNO, 'w+') as f:
    f.write(str(build_no))
    print('Build number: {}'.format(build_no))

hf = """
#ifndef BUILD_NUMBER
  #define BUILD_NUMBER "{}"
#endif
#ifndef VERSION
  #define VERSION "{} - {}"
#endif
#ifndef VERSION_SHORT
  #define VERSION_SHORT "{}"
#endif
""".format(build_no, version+str(build_no), datetime.datetime.now(), version+str(build_no))
with open(FILENAME_VERSION_H, 'w+') as f:
    f.write(hf)

Any advice on how to fix this so it only runs pre-build?

Welcome!

That’s an interesting idea.

I looked at one of my projects and I run these scripts:

build_flags = !python tools\git_rev.py
extra_scripts = pre:python tools\name_firmware.py

The only thing I noticed is that you are not calling python with the script as an argument. I am not completely sure that’s required, but you can give it a shot and see if it helps.

The buildscript_versioning.py is running as it’s updating the version header file, it’s simply running at not only the prebuilt phase.

For example, it runs and updates the version.h file when Visual Studio first starts, even though I’m. It building the code!

Confused

That’s is very curious.

Have you thought about simplifying the script and seeing what happens? I know you have a fairly strong sense that it’s running all the time, but if you make a passive script, what happens?

Also, I have a similar setup pulling my Git tags for use within my program. That way I am in control of the versioning, but it does make that version available inside my code without having to inline edit. It does sort of make sense that the script runs all the time, because changes to the environment from within those scripts are available dynamically. Maybe that’s the intent rather than running at compile time?

Here’s my setup:

I have the following in my platformio.ini:

build_flags = !python tools\git_rev.py

Then the contents of git_rev.py are:

import subprocess

# Get Git project name
projcmd = "git rev-parse --show-toplevel"
project = subprocess.check_output(projcmd, shell=True).decode().strip()
project = project.split("/")
project = project[len(project)-1]

# Get 0.0.0 version from latest Git tag
tagcmd = "git describe --tags --abbrev=0"
version = subprocess.check_output(tagcmd, shell=True).decode().strip()

# Get latest commit short from Git
revcmd = "git log --pretty=format:'%h' -n 1"
commit = subprocess.check_output(revcmd, shell=True).decode().strip()

# Get branch name from Git
branchcmd = "git rev-parse --abbrev-ref HEAD"
branch = subprocess.check_output(branchcmd, shell=True).decode().strip()

# Make all available for use in the macros
print("-DPIO_SRC_NAM={0}".format(project))
print("-DPIO_SRC_TAG={0}".format(version))
print("-DPIO_SRC_REV={0}".format(commit))
print("-DPIO_SRC_BRH={0}".format(branch))

Then within the code I can access those macro definitions:

#define stringify(s) _stringifyDo(s)
#define _stringifyDo(s) #s

const char* build();
const char* branch();
const char* version();

const char* build() {return stringify(PIO_SRC_REV);}
const char* branch() {return stringify(PIO_SRC_BRH);}
const char* version() {return stringify(PIO_SRC_TAG);}

It’s not automatic as you were looking for, but maybe some of this will help you a little.

1 Like

Thank you, I will give this a shot!

have you ever solved your problem?
I’m experiencing the same problem of yours.

I have not I’m sorry to say.

I released this script to solve the problem.
It increment the version when you upload your software