-
Notifications
You must be signed in to change notification settings - Fork 7
/
make_flags.py
executable file
·116 lines (80 loc) · 2.59 KB
/
make_flags.py
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
111
112
113
114
115
116
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import pathlib
import re
import subprocess
import tempfile
defines = {
# "DEBUG_ESP_HTTP_UPDATE": 1,
# "DEBUG_ESP_WIFI": 1,
# "DEBUG_ESP_PORT": "Serial",
"PIO_FRAMEWORK_ESP_IDF_ENABLE_EXCEPTIONS": None,
"ARDUINOJSON_ENABLE_PROGMEM": 0,
"ARDUINOJSON_ENABLE_STD_STRING": 1,
"ARDUINOJSON_USE_LONG_LONG": 1,
}
parser = argparse.ArgumentParser()
parser.add_argument("-D", "--define", action="append")
args = parser.parse_args()
if os.getenv("CI"):
git_desc = os.getenv("GITHUB_REF")
else:
git_desc = (
subprocess.check_output(["git", "describe", "--dirty"]).decode("utf-8").strip()
)
VER_TPL = """
#include "config.h"
const char* cfg::msgs::FW_VERSION = "{git_desc}";
"""
def replace_content(fd, new_content):
content = fd.read()
if content != new_content:
fd.seek(0)
fd.truncate()
fd.write(new_content)
ver_content = VER_TPL.format(**locals())
with open("./src/version.cpp", "a+") as fd:
replace_content(fd, ver_content)
# Embed all files
if 0:
ddir = pathlib.Path("./data")
defines["COMPONENT_EMBED_TXTFILES"] = ":".join(str(f) for f in ddir.iterdir())
with open("./src/embedded_data.h", "a+") as fd:
edata_content = "#pragma once\n"
for f in ddir.iterdir():
name = re.sub(r"[/,\.\ ]", "_", str(f))
TPL = """extern const uint8_t {name}_{label}[] asm("_binary_{name}_{label}");\n"""
edata_content += TPL.format(name=name, label="start")
edata_content += TPL.format(name=name, label="end")
replace_content(fd, edata_content)
# Embed icons
if 1:
ddir = pathlib.Path("./icons")
xbm_content = ""
for f in sorted(ddir.glob("*.bmp")):
tf = f.with_suffix(".xbm")
# tf = pathlib.Path(tempfile.mktemp(suffix='.xbm'))
if not tf.exists():
subprocess.check_output(["convert", str(f), str(tf)])
with tf.open("r") as fd:
xbm = fd.read()
xbm_content += xbm.replace("char ", "const uint8_t icon_").replace(
"#define ", "#define icon_"
)
# tf.unlink()
with open("./src/icons_xbm.h", "a+") as fd:
replace_content(fd, xbm_content)
if args.define:
for d in args.define:
if "=" in d:
k, v = d.split("=", 1)
else:
k, v = d, None
defines[k] = v
def format_def(k, v):
if v is None:
return "-D" + k
return "-D{k}={v}".format(**locals())
print(" ".join(sorted((format_def(k, v) for k, v in defines.items()))))