Skip to content

Commit 4214e16

Browse files
authored
Tools - makecorever.py explicit output argument (esp8266#9248)
* 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 * typo * fix unhandled exc in git cmd * unused escape * RELEASE using same values
1 parent 30780cb commit 4214e16

File tree

5 files changed

+129
-68
lines changed

5 files changed

+129
-68
lines changed

cores/esp8266/Esp-version.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
#include <Arduino.h>
2222
#include <user_interface.h>
23-
#include <core_version.h>
2423
#include <lwipopts.h> // LWIP_HASH_STR (lwip2)
2524
#include <bearssl/bearssl_git.h> // BEARSSL_GIT short hash
2625

26+
#include <core_version.h>
27+
2728
#define STRHELPER(x) #x
2829
#define STR(x) STRHELPER(x) // stringifier
2930

cores/esp8266/Esp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ String EspClass::getCoreVersion()
254254
return String(core_release);
255255
}
256256
char buf[12];
257-
snprintf(buf, sizeof(buf), "%08x", core_version);
257+
snprintf(buf, sizeof(buf), PSTR("%08x"), core_version);
258258
return String(buf);
259259
}
260260

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: 120 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,59 @@
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
26+
27+
28+
PWD = pathlib.Path(__file__).parent.absolute()
29+
30+
VERSION_UNSPECIFIED = "unspecified"
31+
VERSION_DEFAULT = ("0", "0", "0")
2432

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()
3133

32-
text = ""
34+
def check_git(*args: str, cwd: Optional[str]):
35+
cmd = ["git"]
36+
if cwd:
37+
cmd.extend(["-C", cwd])
38+
cmd.extend(args)
3339

34-
git_ver = "00000000"
3540
try:
36-
git_ver = git("rev-parse", "--short=8", "HEAD")
37-
except Exception:
41+
with subprocess.Popen(
42+
cmd,
43+
stdout=subprocess.PIPE,
44+
universal_newlines=True,
45+
stderr=subprocess.DEVNULL,
46+
) as proc:
47+
if proc.stdout:
48+
lines = proc.stdout.readlines()
49+
return lines[0].strip()
50+
except IndexError:
3851
pass
39-
text = "#define ARDUINO_ESP8266_GIT_VER 0x{}\n".format(git_ver)
52+
except FileNotFoundError:
53+
pass
54+
55+
return ""
56+
57+
58+
def generate(
59+
out: TextIO,
60+
*,
61+
git_root: pathlib.Path,
62+
hash_length: int = 8,
63+
release: bool,
64+
version: str,
65+
):
66+
git_root = git_root.absolute()
67+
git_cwd = git_root.as_posix()
68+
69+
def git(*args):
70+
return check_git(*args, cwd=git_cwd)
71+
72+
git_ver = "0" * hash_length
73+
git_ver = git("rev-parse", f"--short={hash_length}", "HEAD") or git_ver
4074

4175
# version is
4276
# - using Arduino-CLI:
@@ -45,75 +79,99 @@ def git(*args):
4579
# - using git:
4680
# - 5.6.7 (from release script, official release)
4781
# - 5.6.7-42-g00d1e5 (from release script, test release)
48-
git_desc = version
49-
try:
50-
# in any case, get a better version when git is around
51-
git_desc = git("describe", "--tags")
52-
except Exception:
53-
pass
5482

55-
text += "#define ARDUINO_ESP8266_GIT_DESC {}\n".format(git_desc)
56-
text += "#define ARDUINO_ESP8266_VERSION {}\n".format(version)
57-
text += "\n"
83+
# in any case, get a better version when git is around
84+
git_desc = git("describe", "--tags") or version
85+
86+
if version == VERSION_UNSPECIFIED:
87+
version = git_desc
88+
89+
version_triple = list(VERSION_DEFAULT)
90+
91+
if version != VERSION_UNSPECIFIED:
92+
try:
93+
version_triple = version.split(".", 2)
94+
except ValueError:
95+
pass
5896

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"
97+
major, minor, patch = version_triple
6798

68-
# release or dev ?
99+
major = major.split("-")[-1]
100+
revision = patch.split("-")[0]
101+
102+
text = rf"""// ! ! ! DO NOT EDIT, AUTOMATICALLY GENERATED ! ! !
103+
#define ARDUINO_ESP8266_GIT_VER 0x{git_ver}
104+
#define ARDUINO_ESP8266_GIT_DESC {git_desc}
105+
#define ARDUINO_ESP8266_VERSION {version}
106+
107+
#define ARDUINO_ESP8266_MAJOR {major}
108+
#define ARDUINO_ESP8266_MINOR {minor}
109+
#define ARDUINO_ESP8266_REVISION {revision}
110+
"""
69111
if release:
70-
text += "#define ARDUINO_ESP8266_RELEASE \"{}\"\n".format(git_desc)
71-
text += "#define ARDUINO_ESP8266_RELEASE_{}\n".format(git_desc.replace("-","_").replace(".","_"))
112+
text += rf"""
113+
#define ARDUINO_ESP8266_RELEASE \"{major}.{minor}.{revision}\"
114+
#define ARDUINO_ESP8266_RELEASE_{major}_{minor}_{revision}
115+
"""
72116
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
117+
text += """
118+
#define ARDUINO_ESP8266_DEV 1 // development version
119+
"""
82120

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

86123

87124
if __name__ == "__main__":
88125
parser = argparse.ArgumentParser(description="Generate core_version.h")
89126

90127
parser.add_argument(
91-
"-b", "--build_path", action="store", required=True, help="build.path variable"
128+
"--git-root",
129+
action="store",
130+
help="ESP8266 Core Git root. In platform.txt, this is {platform.path}",
131+
type=pathlib.Path,
132+
default=PWD / "..",
133+
)
134+
parser.add_argument(
135+
"--hash-length",
136+
default=8,
137+
type=int,
138+
help="Used in git rev-parse --short=...",
92139
)
93140
parser.add_argument(
94-
"-p",
95-
"--platform_path",
141+
"--version",
96142
action="store",
97-
required=True,
98-
help="platform.path variable",
143+
default=VERSION_UNSPECIFIED,
144+
help="ESP8266 Core version string. In platform.txt, this is {version}",
99145
)
100146
parser.add_argument(
101-
"-v", "--version", action="store", required=True, help="version variable"
147+
"--release",
148+
action="store_true",
149+
default=False,
150+
help="In addition to numeric version, also provide ARDUINO_ESP8266_RELEASE{,_...} definitions",
151+
)
152+
parser.add_argument(
153+
"output",
154+
nargs="?",
155+
type=str,
156+
default="",
102157
)
103-
parser.add_argument("-i", "--include_dir", default="core")
104-
parser.add_argument("-r", "--release", action="store_true", default=False)
105158

106159
args = parser.parse_args()
107160

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

114-
generate(
115-
os.path.join(include_dir, "core_version.h"),
116-
args.platform_path,
117-
version=args.version,
118-
release=args.release
119-
)
165+
out = pathlib.Path(s)
166+
out.parent.mkdir(parents=True, exist_ok=True)
167+
168+
return out.open("w", encoding="utf-8")
169+
170+
with select_output(args.output) as out:
171+
generate(
172+
out,
173+
git_root=args.git_root,
174+
hash_length=args.hash_length,
175+
release=args.release,
176+
version=args.version,
177+
)

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)