Skip to content

Commit

Permalink
🕰️ updated version injection
Browse files Browse the repository at this point in the history
- uses pio's python only
- no more build flags! 
  * those force full recompile after every git change
- now generates a cpp file
  • Loading branch information
t413 committed Oct 17, 2023
1 parent 54aca19 commit a071f93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
13 changes: 1 addition & 12 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
;PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcu-32s]
platform = espressif32
Expand All @@ -17,7 +8,5 @@ lib_deps =
PubSubClient
ModbusMaster
plerup/espsoftwareserial
src_build_flags =
!python utils.py version ;injects version into main
extra_scripts = pre:utils.py
extra_scripts = pre:utils.py ;injects version into main
build_unflags = -fno-rtti ;allow dynamic_cast
43 changes: 25 additions & 18 deletions utils.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,36 @@ def getGitDate():
return shellCmd("git log -1 --date=format:%Y%m%d --format=%ad")
def getVersion():
try:
return getDescribe() + "-" + str(getGitDate())
return getDescribe().replace("-dirty", ".d") + "-" + str(getGitDate())
except Exception as e:
return os.path.basename(os.getcwd())
def prettyPrint():
try: #optional colorful output
from colorama import Fore, Back, Style
print(Back.YELLOW + Fore.BLACK + " git version " + Back.BLACK + Fore.YELLOW + " " + getVersion() + " " + Style.RESET_ALL)
except Exception:
print("git version " + getVersion())


if len(sys.argv) > 1 and sys.argv[1] == "version":
print("-DGIT_VERSION=\\\"%s\\\"" % getVersion())
arg = sys.argv[1] if len(sys.argv) > 1 else ""

elif len(sys.argv) > 1 and sys.argv[1] == "simple":
if arg == "version":
prettyPrint()
elif arg == "simple":
print(getVersion())

elif len(sys.argv) > 1 and sys.argv[0].endswith("scons"): #pre/post extra_script
Import("env")
# print(Back.GREEN + Fore.BLACK + "sys argv" + Style.RESET_ALL + str(sys.argv))
# print(Back.GREEN + Fore.BLACK + "env dump" + Style.RESET_ALL + str(env.Dump()))
# print(Back.GREEN + Fore.BLACK + "projenv" + Style.RESET_ALL + str(projenv.Dump()))
try: #optional colorful output
from colorama import Fore, Back, Style
print(Back.YELLOW + Fore.BLACK + " solar version " + Back.BLACK + Fore.YELLOW + " " + getVersion() + " " + Style.RESET_ALL)
except Exception:
print("solar version " + getVersion())
# -- not working, breaks partition.bin for some reason -- #
# progname = "solar-%s-%s" % (env.get("BOARD"), getVersion())
# log(" program name ", progname)
# env.Replace(PROGPREFIX=progname)
else:
prettyPrint()

try: #if running inside platformio
Import("env")
# print(env.Dump()) # <- can use this to see what's available
bpath = os.path.join(env.subst("$BUILD_DIR"), "generated")
print(" - version injection to " + bpath)

if not os.path.exists(bpath): os.makedirs(bpath)
with open(os.path.join(bpath, "version.cpp"), 'w+') as ofile:
ofile.write("const char* GIT_VERSION(\"" + getVersion() + "\");" + os.linesep)
env.BuildSources(os.path.join(bpath, "build"), bpath)
except NameError:
pass

0 comments on commit a071f93

Please sign in to comment.