Skip to content

Commit d9d6a4d

Browse files
committed
Tools - makecorever.py explicit output argument
Don't hide where the file is actually placed Using pathlib instead of raw strings to simplify path ops Plus, add stdout preview when output is missing Clean-up git-describe <-> version availility checks
1 parent 30780cb commit d9d6a4d

File tree

3 files changed

+128
-60
lines changed

3 files changed

+128
-60
lines changed

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ compiler.elf2hex.extra_flags=
121121
## needs git
122122
recipe.hooks.sketch.prebuild.pattern="{runtime.tools.python3.path}/python3" -I "{runtime.tools.signing}" --mode header --publickey "{build.source.path}/public.key" --out "{build.path}/core/Updater_Signing.h"
123123
# This is quite a working hack. This form of prebuild hook, while intuitive, is not explicitly documented.
124-
recipe.hooks.prebuild.1.pattern="{runtime.tools.python3.path}/python3" -I "{runtime.tools.makecorever}" --build_path "{build.path}" --platform_path "{runtime.platform.path}" --version "{version}"
124+
recipe.hooks.prebuild.1.pattern="{runtime.tools.python3.path}/python3" -I "{runtime.tools.makecorever}" --git-root "{runtime.platform.path}" --version "{version}" "{build.path}/core/core_version.h"
125125

126126
# Handle processing sketch global options
127127
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" -I "{runtime.tools.mkbuildoptglobals}" "{runtime.ide.path}" {runtime.ide.version} "{build.path}" "{build.opt.fqfn}" "{globals.h.source.fqfn}" "{commonhfile.fqfn}" {mkbuildoptglobals.extra_flags}

tools/makecorever.py

Lines changed: 122 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,60 @@
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919

2020
import argparse
21-
import os
21+
import pathlib
2222
import subprocess
23+
import sys
2324

25+
from typing import Optional, TextIO
2426

25-
def generate(path, platform_path, version="unspecified", release = False):
26-
def git(*args):
27-
cmd = ["git", "-C", platform_path]
28-
cmd.extend(args)
29-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True, stderr=subprocess.DEVNULL)
30-
return proc.stdout.readlines()[0].strip()
3127

32-
text = ""
28+
PWD = pathlib.Path(__file__).parent.absolute()
29+
30+
VERSION_UNSPECIFIED = "unspecified"
31+
VERSION_DEFAULT = ("0", "0", "0")
32+
33+
34+
def escape_version(v: str) -> str:
35+
return v.replace("-", "_").replace(".", "_")
36+
37+
38+
def check_git(*args: str, cwd: Optional[str]):
39+
cmd = ["git"]
40+
if cwd:
41+
cmd.extend(["-C", cwd])
42+
cmd.extend(args)
43+
44+
with subprocess.Popen(
45+
cmd,
46+
stdout=subprocess.PIPE,
47+
universal_newlines=True,
48+
stderr=subprocess.DEVNULL,
49+
) as proc:
50+
if proc.stdout:
51+
return proc.stdout.readlines()[0].strip()
52+
53+
return ""
54+
3355

34-
git_ver = "00000000"
56+
def generate(
57+
out: TextIO,
58+
*,
59+
git_root: pathlib.Path,
60+
hash_length: int = 8,
61+
release: bool,
62+
version: str,
63+
):
64+
git_root = git_root.absolute()
65+
git_cwd = git_root.as_posix()
66+
67+
def git(*args):
68+
return check_git(*args, cwd=git_cwd)
69+
70+
git_ver = "0" * hash_length
3571
try:
36-
git_ver = git("rev-parse", "--short=8", "HEAD")
72+
git_ver = git("rev-parse", f"--short={hash_length}", "HEAD")
3773
except Exception:
38-
pass
39-
text = "#define ARDUINO_ESP8266_GIT_VER 0x{}\n".format(git_ver)
74+
raise
4075

4176
# version is
4277
# - using Arduino-CLI:
@@ -50,70 +85,101 @@ def git(*args):
5085
# in any case, get a better version when git is around
5186
git_desc = git("describe", "--tags")
5287
except Exception:
88+
raise
89+
90+
if version == VERSION_UNSPECIFIED:
91+
version = git_desc
92+
93+
version_triple = list(VERSION_DEFAULT)
94+
95+
try:
96+
version_triple = version.split(".", 2)
97+
except ValueError:
5398
pass
5499

55-
text += "#define ARDUINO_ESP8266_GIT_DESC {}\n".format(git_desc)
56-
text += "#define ARDUINO_ESP8266_VERSION {}\n".format(version)
57-
text += "\n"
100+
major, minor, patch = version_triple
58101

59-
version_split = version.split(".")
60-
# major: if present, skip "unix-" in "unix-3"
61-
text += "#define ARDUINO_ESP8266_MAJOR {}\n".format(version_split[0].split("-")[-1])
62-
text += "#define ARDUINO_ESP8266_MINOR {}\n".format(version_split[1])
63-
# revision can be ".n" or ".n-dev" or ".n-42-g00d1e5"
64-
revision = version_split[2].split("-")
65-
text += "#define ARDUINO_ESP8266_REVISION {}\n".format(revision[0])
66-
text += "\n"
102+
major = major.split("-")[-1]
103+
revision = patch.split("-")[0]
67104

68-
# release or dev ?
105+
text = rf"""// ! ! ! DO NOT EDIT, AUTOMATICALLY GENERATED ! ! !
106+
#define ARDUINO_ESP8266_GIT_VER 0x{git_ver}
107+
#define ARDUINO_ESP8266_GIT_DESC {git_desc}
108+
#define ARDUINO_ESP8266_VERSION {version}
109+
110+
#define ARDUINO_ESP8266_MAJOR {major}
111+
#define ARDUINO_ESP8266_MINOR {minor}
112+
#define ARDUINO_ESP8266_REVISION {revision}
113+
"""
69114
if release:
70-
text += "#define ARDUINO_ESP8266_RELEASE \"{}\"\n".format(git_desc)
71-
text += "#define ARDUINO_ESP8266_RELEASE_{}\n".format(git_desc.replace("-","_").replace(".","_"))
115+
if version != VERSION_UNSPECIFIED:
116+
release_version = version
117+
else:
118+
release_version = git_desc
119+
120+
text += rf"""
121+
#define ARDUINO_ESP8266_RELEASE \"{release_version}\"
122+
#define ARDUINO_ESP8266_RELEASE_{escape_version(release_version)}
123+
"""
72124
else:
73-
text += "#define ARDUINO_ESP8266_DEV 1 // development version\n"
74-
75-
try:
76-
with open(path, "r") as inp:
77-
old_text = inp.read()
78-
if old_text == text:
79-
return
80-
except Exception:
81-
pass
125+
text += """
126+
#define ARDUINO_ESP8266_DEV 1 // development version
127+
"""
82128

83-
with open(path, "w") as out:
84-
out.write(text)
129+
out.write(text)
85130

86131

87132
if __name__ == "__main__":
88133
parser = argparse.ArgumentParser(description="Generate core_version.h")
89134

90135
parser.add_argument(
91-
"-b", "--build_path", action="store", required=True, help="build.path variable"
136+
"--git-root",
137+
action="store",
138+
help="ESP8266 Core Git root. In platform.txt, this is {platform.path}",
139+
type=pathlib.Path,
140+
default=PWD / "..",
141+
)
142+
parser.add_argument(
143+
"--hash-length",
144+
default=8,
145+
type=int,
146+
help="Used in git rev-parse --short=...",
92147
)
93148
parser.add_argument(
94-
"-p",
95-
"--platform_path",
149+
"--version",
96150
action="store",
97-
required=True,
98-
help="platform.path variable",
151+
default=VERSION_UNSPECIFIED,
152+
help="ESP8266 Core version string. In platform.txt, this is {version}",
99153
)
100154
parser.add_argument(
101-
"-v", "--version", action="store", required=True, help="version variable"
155+
"--release",
156+
action="store_true",
157+
default=False,
158+
help="In addition to numeric version, also provide ARDUINO_ESP8266_RELEASE{,_...} definitions",
159+
)
160+
parser.add_argument(
161+
"output",
162+
nargs="?",
163+
type=str,
164+
default="",
102165
)
103-
parser.add_argument("-i", "--include_dir", default="core")
104-
parser.add_argument("-r", "--release", action="store_true", default=False)
105166

106167
args = parser.parse_args()
107168

108-
include_dir = os.path.join(args.build_path, args.include_dir)
109-
try:
110-
os.makedirs(include_dir)
111-
except Exception:
112-
pass
169+
def select_output(s: str) -> TextIO:
170+
if not s:
171+
return sys.stdout
113172

114-
generate(
115-
os.path.join(include_dir, "core_version.h"),
116-
args.platform_path,
117-
version=args.version,
118-
release=args.release
119-
)
173+
out = pathlib.Path(s)
174+
out.parent.mkdir(parents=True, exist_ok=True)
175+
176+
return out.open("r", encoding="utf-8")
177+
178+
with select_output(args.output) as out:
179+
generate(
180+
out,
181+
git_root=args.git_root,
182+
hash_length=args.hash_length,
183+
release=args.release,
184+
version=args.version,
185+
)

tools/platformio-build.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,20 @@ def platform_txt_version(default):
409409

410410

411411
if isdir(join(FRAMEWORK_DIR, ".git")):
412-
cmd = '"$PYTHONEXE" "{script}" -b "$BUILD_DIR" -p "{framework_dir}" -v {version}'
412+
out = join("$BUILD_DIR", "core", "core_version.h")
413+
414+
cmd = '"$PYTHONEXE" "{script}" --git-root "{framework_dir}" --version {version} "$TARGET"'
413415
fmt = {
414416
"script": join(FRAMEWORK_DIR, "tools", "makecorever.py"),
415417
"framework_dir": FRAMEWORK_DIR,
416-
"version": platform_txt_version("unspecified")
418+
"version": platform_txt_version("unspecified"),
417419
}
418420

419421
env.Prepend(CPPPATH=[
420422
join("$BUILD_DIR", "core")
421423
])
422424
core_version = env.Command(
423-
join("$BUILD_DIR", "core", "core_version.h"),
425+
out,
424426
join(FRAMEWORK_DIR, ".git"),
425427
env.VerboseAction(cmd.format(**fmt), "Generating $TARGET")
426428
)

0 commit comments

Comments
 (0)