Skip to content

Commit

Permalink
Compute chip-specific config files in a generic way
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed May 30, 2024
1 parent bf85eaa commit c05228c
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions tools/build_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import logging
import pathlib
import argparse
import itertools
import contextlib
import subprocess
import multiprocessing
Expand All @@ -39,6 +38,14 @@
""",
flags=re.IGNORECASE | re.VERBOSE,
)

GRAPH_COMPONENT_REGEX = re.compile(r"^(\w+)|- (\w+)", flags=re.MULTILINE)
CHIP_SPECIFIC_COMPONENTS = {
"sl_system": "sl_system",
"sl_memory": "sl_memory",
# There are only a few components whose filename doesn't match the component name
"freertos": "freertos_kernel",
}
LOGGER = logging.getLogger(__name__)

yaml = YAML(typ="safe")
Expand Down Expand Up @@ -141,6 +148,43 @@ def git(*args: str) -> str:
return commit_id


def determine_chip_specific_config_filenames(
slcp_path: pathlib.Path, sdk: pathlib.Path, slc: str
) -> list[str]:
"""Determine the chip-specific config files to remove."""
proc = subprocess.run(
[slc, "graph", "-p", str(slcp_path.absolute()), "--sdk", str(sdk.absolute())],
cwd=str(slcp_path.parent),
check=True,
text=True,
capture_output=True,
)

all_components = {a or b for a, b in GRAPH_COMPONENT_REGEX.findall(proc.stdout)}

# Some configs don't seem to be present explicitly in the component graph
config_files = {"sl_memory_config.h"}

# List all of the SLCC files. There are neary 4,000.
slcc_paths = {slcc.stem.lower(): slcc for slcc in sdk.glob("**/*.slcc")}

for component_id in all_components:
if CHIP_SPECIFIC_REGEX.match(component_id):
pass
elif component_id in CHIP_SPECIFIC_COMPONENTS:
component_id = CHIP_SPECIFIC_COMPONENTS[component_id]
else:
continue

slcc = yaml.load(slcc_paths[component_id.lower()].read_text())

for config_file in slcc.get("config_file", []):
path = pathlib.PurePosixPath(config_file["path"]).name
config_files.add(path)

return config_files


def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
Expand Down Expand Up @@ -313,27 +357,6 @@ def main():
# Delete the original project file
(args.build_dir / base_project_slcp.name).unlink()

# Delete config files that must be regenerated by the SDK.
# XXX: This is fragile: you cannot force `slc` to always overwrite these files!
for filename in itertools.chain(
# RAIL config
(args.build_dir / "config").glob("sl_rail_*.h"),
[
# Z-Wave board and stack config
args.build_dir / "config/sl_memory_config.h",
args.build_dir / "config/sl_board_control_config.h",
args.build_dir / "config/FreeRTOSConfig.h",
# Hardware-specific startup config
args.build_dir / "config/sl_device_init_lfxo_config.h",
args.build_dir / "config/sl_device_init_hfxo_config.h",
args.build_dir / "config/sl_device_init_dcdc_config.h",
],
):
try:
filename.unlink()
except FileNotFoundError:
pass

# Write the new project SLCP file
with (args.build_dir / f"{base_project_name}.slcp").open("w") as f:
yaml.dump(output_project, f)
Expand Down Expand Up @@ -373,6 +396,17 @@ def main():
print(f"Project SDK version {base_project['sdk']['version']} not found")
sys.exit(1)

print("Building component graph and identifying board-specific files")
for name in determine_chip_specific_config_filenames(
slcp_path=base_project_slcp, sdk=sdk, slc=slc
):
try:
(args.build_dir / f"config/{name}").unlink()
except FileNotFoundError:
pass
else:
print(f"Deleted device-specific config: {name}")

# Find the toolchain required by the project
slps_path = (args.build_dir / base_project_name).with_suffix(".slps")
slps_xml = ElementTree.parse(slps_path)
Expand Down

0 comments on commit c05228c

Please sign in to comment.