Skip to content

Commit 683b63a

Browse files
FozzTexxdillera
authored andcommitted
Add debug_version.py to put commit ID into FN_VERSION_FULL during build without altering include/version.h
1 parent 7641f94 commit 683b63a

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ data/webui/device_specific/BUILD_ATARI/fnconfig.ini
5555
# ignore any releases downloaded via fujinet_firware_uploader
5656
fujinet_releases/*
5757

58+
*~
59+
*.orig
60+
*.rej

create-platformio-ini.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def create_local_ini(board_name, local_file_name):
1818
config.set('fujinet', 'build_board', board_name)
1919
with open(local_file_name, 'w') as configfile:
2020
config.write(configfile)
21+
configfile.write("[env]\n;build_flags += !python3 debug_version.py\n")
2122
print(f"{local_file_name} file created with build_board = {board_name}")
2223

2324
def merge_ini_files(base_file, local_file, output_file):
@@ -120,7 +121,7 @@ def main():
120121
parser.add_argument("-l", "--local-file", metavar="local_file", help="Use specified local_file instead of platformio.local.ini")
121122
parser.add_argument("-o", "--output-file", metavar="output_file", help="write to output_file instead of default platformio-generated.ini")
122123
parser.add_argument("-f", "--add-files", metavar="add_files", action="append", help="include additional ini file changes, can be specified multiple times")
123-
124+
124125
args = parser.parse_args()
125126
local_file = "platformio.local.ini"
126127
output_file = "platformio-generated.ini"
@@ -144,4 +145,4 @@ def main():
144145
print(f"Merged INI file created as '{output_file}'.")
145146

146147
if __name__ == "__main__":
147-
main()
148+
main()

debug_version.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Created by FozzTexx
4+
5+
import argparse
6+
import subprocess
7+
import os, sys
8+
import re
9+
import shlex
10+
from datetime import datetime
11+
from pathlib import Path
12+
13+
VERSION_H = "include/version.h"
14+
PLATFORMS = "build-platforms"
15+
MACRO_PATTERN = r"^\s*#define\s+(.*?)\s+(.*?)\s*(?://.*|/\*[\s\S]*?\*/)?$"
16+
17+
def build_argparser():
18+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
19+
parser.add_argument("--update_version_h", action="store_true",
20+
help="update include/version.h with current commit information")
21+
return parser
22+
23+
def run_command(cmd):
24+
return subprocess.check_output(cmd, universal_newlines=True).strip()
25+
26+
def get_repo_base():
27+
return run_command(["git", "rev-parse", "--show-toplevel"])
28+
29+
def is_fujinet_repo(base):
30+
if os.path.exists(os.path.join(base, VERSION_H)) \
31+
and os.path.isdir(os.path.join(base, PLATFORMS)):
32+
return True
33+
return False
34+
35+
def get_commit_version():
36+
return run_command(["git", "describe", "--always", "HEAD"])
37+
38+
def get_modified_files():
39+
files = run_command(["git", "diff", "--name-only"]).split("\n")
40+
files = [f for f in files if f]
41+
return files
42+
43+
def load_version_macros(path):
44+
txt = [line for line in open(path)]
45+
macros = {}
46+
for line in txt:
47+
m = re.match(MACRO_PATTERN, line)
48+
if m:
49+
name = m.group(1)
50+
if '(' in name:
51+
continue
52+
value = m.group(2)
53+
if not value:
54+
value = None
55+
macros[name] = value
56+
return macros
57+
58+
def main():
59+
base = get_repo_base()
60+
if not is_fujinet_repo(base):
61+
print("Not in FujiNet firmware repo")
62+
return 0
63+
64+
args = build_argparser().parse_args()
65+
# FIXME - don't allow update_version_h unless on the actual tag?
66+
67+
version_h_path = os.path.join(base, VERSION_H)
68+
version = get_commit_version()
69+
modified = get_modified_files()
70+
commit_id_long = run_command(["git", "rev-parse", "HEAD"])
71+
72+
mtime = int(run_command(["git", "show", "--no-patch", "--format=%ct", "HEAD"]))
73+
for path in modified:
74+
if os.path.exists(path):
75+
mt = os.path.getmtime(path)
76+
if not mtime or mt > mtime:
77+
mtime = mt
78+
79+
macros = {
80+
'FN_VERSION_BUILD': commit_id_long,
81+
}
82+
cur_macros = load_version_macros(version_h_path)
83+
ver_major = int(cur_macros['FN_VERSION_MAJOR'])
84+
ver_minor = int(cur_macros['FN_VERSION_MINOR'])
85+
86+
m = re.match(r"^v([0-9]+)[.]([0-9]+)[.]([0-9]+)-([0-9]+)-g(.*)", version)
87+
if m:
88+
ver_major = macros['FN_VERSION_MAJOR'] = int(m.group(1))
89+
ver_minor = macros['FN_VERSION_MINOR'] = int(m.group(2))
90+
version = f"v{m.group(1)}.{m.group(2)}-{m.group(5)}"
91+
else:
92+
m = re.match(r"^([a-z0-9]{8})$", version)
93+
if m:
94+
version = f"v{ver_major}.{ver_minor}-{version}"
95+
96+
if modified:
97+
version += "*"
98+
macros['FN_VERSION_FULL'] = version
99+
macros['FN_VERSION_DATE'] = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S")
100+
101+
new_macros = cur_macros.copy()
102+
new_macros.update(macros)
103+
104+
if new_macros != cur_macros:
105+
for key in macros:
106+
if isinstance(macros[key], str):
107+
macros[key] = f'"{macros[key]}"'
108+
109+
cur_macros.update(macros)
110+
macro_defs = []
111+
for key in cur_macros:
112+
value = cur_macros[key]
113+
mdef = f"-D{key}"
114+
if value is not None:
115+
mdef += f"={value}"
116+
macro_defs.append(shlex.quote(mdef))
117+
118+
# FIXME - if args.update_version_h then update, don't print
119+
macro_defs = " ".join(macro_defs)
120+
print(macro_defs)
121+
#Path(version_h_path).touch()
122+
123+
return
124+
125+
if __name__ == "__main__":
126+
exit(main() or 0)
127+
elif __name__ == "SCons.Script":
128+
print("Running as build script")

include/version.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
as needed.
88
*/
99

10+
#ifndef _VERSION_H
11+
#define _VERSION_H
12+
1013
#define FN_VERSION_MAJOR 1
1114
#define FN_VERSION_MINOR 4
1215

@@ -15,3 +18,5 @@
1518
#define FN_VERSION_DATE "2024-08-07 20:49:13"
1619

1720
#define FN_VERSION_FULL "v1.4"
21+
22+
#endif /* _VERSION_H */

0 commit comments

Comments
 (0)