From 6c737c10c758468e46b3812371f5aee2e9767a92 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sat, 3 Jan 2026 00:51:01 +0100 Subject: [PATCH] Adds build step for protobuff --- esp32/scripts/compile_protos.py | 54 +++++++++++++++++++++++++++++++++ esp32/scripts/pre_build.py | 14 +++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 esp32/scripts/compile_protos.py diff --git a/esp32/scripts/compile_protos.py b/esp32/scripts/compile_protos.py new file mode 100644 index 0000000..b8d659f --- /dev/null +++ b/esp32/scripts/compile_protos.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +import subprocess +import os +import sys +from pathlib import Path + +def get_project_root(): + script_dir = Path(__file__).parent.absolute() + return script_dir.parent.parent + +def compile_nanopb(): + project_root = get_project_root() + proto_dir = project_root / "platform_shared" + output_dir = project_root / "esp32" / "src" / "platform_shared" + nanopb_gen = project_root / "submodules" / "nanopb" / "generator" / "nanopb_generator.py" + + output_dir.mkdir(parents=True, exist_ok=True) + + proto_files = [ + proto_dir / "websocket_message.proto", + proto_dir / "rest_message.proto" + ] + + print(f"Compiling protobuf files with nanopb...") + print(f" Proto dir: {proto_dir}") + print(f" Output dir: {output_dir}") + + cmd = [ + sys.executable, + str(nanopb_gen), + "-I", str(proto_dir), + "-D", str(output_dir), + ] + [str(f) for f in proto_files] + + print(f" Command: {' '.join(cmd)}") + + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode != 0: + print(f"Error compiling protos:") + print(result.stderr) + return False + + print(f" Successfully compiled {len(proto_files)} proto files") + return True + +def main(): + if not compile_nanopb(): + sys.exit(1) + print("Proto compilation complete!") + +if __name__ == "__main__": + main() + diff --git a/esp32/scripts/pre_build.py b/esp32/scripts/pre_build.py index 86f1085..ee3d830 100644 --- a/esp32/scripts/pre_build.py +++ b/esp32/scripts/pre_build.py @@ -1,7 +1,17 @@ from pathlib import Path +import subprocess +import sys Import("env") -filesystem_dir = env["PROJECT_DIR"] + "/esp32/data" +project_dir = Path(env["PROJECT_DIR"]) +filesystem_dir = project_dir / "esp32" / "data" -Path(filesystem_dir).mkdir(exist_ok=True) \ No newline at end of file +Path(filesystem_dir).mkdir(exist_ok=True) + +proto_script = project_dir / "esp32" / "scripts" / "compile_protos.py" +if proto_script.exists(): + print("Running proto compilation...") + result = subprocess.run([sys.executable, str(proto_script)], cwd=str(project_dir)) + if result.returncode != 0: + print("Warning: Proto compilation failed")