-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmerge-bin.py
More file actions
110 lines (91 loc) · 3.39 KB
/
Copy pathmerge-bin.py
File metadata and controls
110 lines (91 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python3
# Post-link: copy app .bin to out/<PIOENV>.bin and a timestamped copy; optional merge_bin target for merged flash image.
import os
import shutil
import subprocess
from datetime import datetime
Import("env", "projenv")
board_config = env.BoardConfig()
firmware_bin = "${BUILD_DIR}/${PROGNAME}.bin"
merged_bin = os.environ.get("MERGED_BIN_PATH", "${BUILD_DIR}/${PROGNAME}-merged.bin")
def _git_short_sha(project_dir):
try:
r = subprocess.run(
["git", "-C", project_dir, "rev-parse", "--short", "HEAD"],
capture_output=True,
text=True,
timeout=8,
)
sha = (r.stdout or "").strip()
if r.returncode != 0 or not sha:
return "nogit"
dirty = subprocess.run(
["git", "-C", project_dir, "diff", "--quiet"],
timeout=8,
capture_output=True,
)
return sha + ("-dirty" if dirty.returncode != 0 else "")
except (OSError, subprocess.TimeoutExpired):
return "nogit"
def _copy_one(src_path, dst_path):
shutil.copy2(src_path, dst_path)
print("Copied %s -> %s" % (src_path, dst_path))
def _copy_to_out(env, src_path, out_suffix):
if not src_path or not os.path.isfile(src_path):
return
pioenv = env["PIOENV"]
project_dir = env["PROJECT_DIR"]
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
sha = _git_short_sha(project_dir)
stable_name = pioenv + out_suffix + ".bin"
stamped_name = "%s%s-%s-%s.bin" % (pioenv, out_suffix, stamp, sha)
out_dir = os.path.join(project_dir, "out")
os.makedirs(out_dir, exist_ok=True)
_copy_one(src_path, os.path.join(out_dir, stable_name))
_copy_one(src_path, os.path.join(out_dir, stamped_name))
# When the project lives in a subfolder (e.g. HeltecV4/MeshCore), also copy to <workspace>/out
parent_out = os.path.normpath(os.path.join(project_dir, "..", "out"))
if parent_out != os.path.normpath(out_dir):
try:
os.makedirs(parent_out, exist_ok=True)
_copy_one(src_path, os.path.join(parent_out, stable_name))
_copy_one(src_path, os.path.join(parent_out, stamped_name))
except OSError as exc:
print("Note: could not mirror to parent out/: %s" % exc)
def copy_app_bin_to_out(target, source, env):
bin_path = os.path.join(env.subst("$BUILD_DIR"), env.subst("${PROGNAME}") + ".bin")
_copy_to_out(env, bin_path, "")
def merge_bin_action(source, target, env):
flash_images = [
*env.Flatten(env.get("FLASH_EXTRA_IMAGES", [])),
"$ESP32_APP_OFFSET",
source[0].get_abspath(),
]
merge_cmd = " ".join(
[
'"$PYTHONEXE"',
'"$OBJCOPY"',
"--chip",
board_config.get("build.mcu", "esp32"),
"merge_bin",
"-o",
merged_bin,
"--flash_mode",
board_config.get("build.flash_mode", "dio"),
"--flash_freq",
"${__get_board_f_flash(__env__)}",
"--flash_size",
board_config.get("upload.flash_size", "4MB"),
*flash_images,
]
)
env.Execute(merge_cmd)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", copy_app_bin_to_out)
env.AddCustomTarget(
name="mergebin",
dependencies=firmware_bin,
actions=merge_bin_action,
title="Merge binary",
description="Build combined image",
always_build=True,
)