Closed HamzaHajeir closed 3 years ago
Dynamic build flags can be used to achieve a similar result. An example:
platformio.ini
:
[common]
build_flags =
!python3 define-git-revision.py
define-git-revision.py
:
#!/usr/bin/env python3
"""Git revision"""
import subprocess
REVISION = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()
print("-D SRC_REVISION='\"{}\"'".format(REVISION.decode("UTF-8")))
src/main.cpp
:
#ifndef SRC_REVISION
#define SRC_REVISION "(revision not defined)"
#endif
void loop()
{
Serial.println(SRC_REVISION);
}
I've not used python before, But I've modified your solution and used the following code, Which gives the same error again :
#!/usr/bin/env python3
"""Git revision"""
import subprocess
PROJECT_TAG = (
subprocess.run(['git', 'describe','--tags','--abbrev=0'], capture_output=True, text=True).stdout
)
PROJECT_TAG_NO = PROJECT_TAG.replace('.','0')
REVISION = (
subprocess.run(['git', 'rev-parse','HEAD','--short=10'], capture_output=True, text=True).stdout
)
gitDescribeOutput = (
subprocess.run(['git', 'describe','--tags'], capture_output=True, text=True).stdout
)
info = gitDescribeOutput.split('-')
print("-D GIT_REV='\"{}\"'".format(REVISION))
print("-D MEEZANI_VERSION='\"{}\"'".format(PROJECT_TAG))
print("-D MEEZANI_VERSION_NO='{}'".format(PROJECT_TAG_NO))
print("-D AHEAD_COMMITS='{}'".format(info[1]))
Where I could be mistaken ?
It was because I'm fresh to python, This worked with me achieving my goal:
#!/usr/bin/env python3
"""Git revision"""
import subprocess
revision = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode("utf-8")
REVISION = revision[:10]
gitDescribeOutput = subprocess.check_output(["git", "describe", "--tags"]).strip().decode("utf-8")
info = gitDescribeOutput.split('-')
if len(info) == 3:
PROJECT_TAG = info[0]
AHEAD_COMMITS = info[1]
else:
PROJECT_TAG = info[0]+"-"+info[1]
AHEAD_COMMITS = info[2]
PROJECT_TAG_NO = PROJECT_TAG.replace('.','0')
print("-D GIT_REV='\"{}\"'".format(REVISION))
print("-D MEEZANI_VERSION='\"{}\"'".format(PROJECT_TAG))
print("-D MEEZANI_VERSION_NO='{}'".format(PROJECT_TAG_NO))
print("-D AHEAD_COMMITS='{}'".format(AHEAD_COMMITS))
Configuration
Operating system:
PlatformIO Version (
platformio --version
):Description of problem
Hi I’m trying to start versioning my own project using platformio pre script
I’ve followed 2 methods, both ends with the same error. First one by adding !python:my_version_file.py into build_flags. (I’ll skip to the second method)
and the other method is by using extra_scripts.
I use git describe output to make version of my project. I have 2 issues (3rd is less important now) :
When I hit pio run -v, It shows me less defines by 1 in verbose. For example it should define :
what I really see is only the first 2.
The second (more important issue, I get this error if appended CPP Defines (by this method or the first one) :
xtensa-esp32-elf-g++: fatal error: no input files
A full log with verbose :
However I’ve created a sample project to reproduce the issue, It’s uploaded here.
For a quick eye :
git_rev_macro.py :
platformio.ini
:main.cpp: