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:
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.
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: