tools/metrics.py: Allow pre_cmd to set up environment.

This is a necessary step to allow cleanly building the xtensa port during
CI, as it's undesirable to apply the esp-idf environment settings while
building other ports.

PRE_CMD_<letter> can be used to override the command, or add a command if
not otherwise specified.  Note that <letter> is case sensitive.

Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit is contained in:
Jeff Epler
2025-09-03 14:09:49 -05:00
committed by Damien George
parent f982158f42
commit 7778e78843

View File

@@ -43,18 +43,19 @@ Other commands:
"""
import collections, sys, re, subprocess, multiprocessing
import collections, os, sys, re, shlex, subprocess, multiprocessing
MAKE_FLAGS = ["-j{}".format(multiprocessing.cpu_count()), "CFLAGS_EXTRA=-DNDEBUG"]
class PortData:
def __init__(self, name, dir, output, make_flags=None):
def __init__(self, name, dir, output, make_flags=None, pre_cmd=None):
self.name = name
self.dir = dir
self.output = output
self.make_flags = make_flags
self.needs_mpy_cross = dir not in ("bare-arm", "minimal")
self.pre_cmd = pre_cmd
mpy_cross_output = "mpy-cross/build/mpy-cross"
@@ -67,7 +68,12 @@ port_data = {
"s": PortData("stm32", "stm32", "build-PYBV10/firmware.elf", "BOARD=PYBV10"),
"c": PortData("cc3200", "cc3200", "build/WIPY/release/application.axf", "BTARGET=application"),
"8": PortData("esp8266", "esp8266", "build-ESP8266_GENERIC/firmware.elf"),
"3": PortData("esp32", "esp32", "build-ESP32_GENERIC/micropython.elf"),
"3": PortData(
"esp32",
"esp32",
"build-ESP32_GENERIC/micropython.elf",
pre_cmd=". esp-idf/export.sh",
),
"x": PortData("mimxrt", "mimxrt", "build-TEENSY40/firmware.elf"),
"e": PortData("renesas-ra", "renesas-ra", "build-EK_RA6M2/firmware.elf"),
"r": PortData("nrf", "nrf", "build-PCA10040/firmware.elf"),
@@ -76,8 +82,15 @@ port_data = {
"v": PortData("qemu rv32", "qemu", "build-VIRT_RV32/firmware.elf", "BOARD=VIRT_RV32"),
}
for port_letter, port in port_data.items():
port.pre_cmd = os.environ.get(f"PRE_CMD_{port_letter}", port.pre_cmd)
def syscmd(*args):
def quoted(args):
return " ".join(shlex.quote(word) for word in args)
def syscmd(*args, pre_cmd=None):
sys.stdout.flush()
a2 = []
for a in args:
@@ -85,6 +98,10 @@ def syscmd(*args):
a2.append(a)
elif a:
a2.extend(a)
if pre_cmd is not None:
a2_quoted = quoted(a2)
a2 = ["bash", "-c", "{} && {}".format(pre_cmd, a2_quoted)]
print(a2)
subprocess.check_call(a2)
@@ -190,7 +207,14 @@ def do_clean(args):
syscmd("make", "-C", "mpy-cross", "clean")
for port in ports:
syscmd("make", "-C", "ports/{}".format(port.dir), port.make_flags, "clean")
syscmd(
"make",
"-C",
"ports/{}".format(port.dir),
port.make_flags,
"clean",
pre_cmd=port.pre_cmd,
)
def do_build(args):
@@ -204,7 +228,14 @@ def do_build(args):
print("BUILDING PORTS")
for port in ports:
syscmd("make", "-C", "ports/{}".format(port.dir), MAKE_FLAGS, port.make_flags)
syscmd(
"make",
"-C",
"ports/{}".format(port.dir),
MAKE_FLAGS,
port.make_flags,
pre_cmd=port.pre_cmd,
)
do_sizes(args)