Create a wrapper .cpp that includes the sketch .ino file and any additional .cpp files in the example directory. Returns path to the wrapper file.
(example_name: str, sketch_cache_dir: Path)
| 829 | |
| 830 | |
| 831 | def create_wrapper(example_name: str, sketch_cache_dir: Path) -> Path: |
| 832 | """ |
| 833 | Create a wrapper .cpp that includes the sketch .ino file and any |
| 834 | additional .cpp files in the example directory. |
| 835 | |
| 836 | Returns path to the wrapper file. |
| 837 | """ |
| 838 | example_dir = PROJECT_ROOT / "examples" / example_name |
| 839 | ino_file = example_dir / f"{example_name}.ino" |
| 840 | |
| 841 | if not ino_file.exists(): |
| 842 | raise FileNotFoundError(f"Example not found: {ino_file}") |
| 843 | |
| 844 | # Discover additional .cpp files in the example directory tree (sorted for determinism) |
| 845 | extra_cpps = sorted( |
| 846 | f |
| 847 | for f in example_dir.rglob("*.cpp") |
| 848 | if f.name != f"{example_name}.ino" and ".build" not in f.parts |
| 849 | ) |
| 850 | |
| 851 | wrapper_path = sketch_cache_dir / f"{example_name}_wrapper.cpp" |
| 852 | lines = [ |
| 853 | f"// Auto-generated wrapper for {example_name}.ino", |
| 854 | "// C++20 header unit import — ~2x faster than PCH for sketch compilation.", |
| 855 | '// The .ino\'s #include "FastLED.h" is a harmless no-op after this import.', |
| 856 | 'import "wasm_pch.h";', |
| 857 | f'#include "{ino_file.as_posix()}"', |
| 858 | ] |
| 859 | for cpp in extra_cpps: |
| 860 | lines.append(f'#include "{cpp.as_posix()}"') |
| 861 | wrapper_content = "\n".join(lines) + "\n" |
| 862 | |
| 863 | # Only write if content changed (preserve timestamp for incremental builds) |
| 864 | if wrapper_path.exists(): |
| 865 | existing = wrapper_path.read_text() |
| 866 | if existing == wrapper_content: |
| 867 | return wrapper_path |
| 868 | |
| 869 | wrapper_path.write_text(wrapper_content) |
| 870 | return wrapper_path |
| 871 | |
| 872 | |
| 873 | def build_sketch_pch( |