mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 17:00:21 +00:00
[esp32] Precompile the shared core headers for src compiles
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@@ -488,3 +489,100 @@ def test_get_component_cmakelists_no_compile_features() -> None:
|
||||
content = get_component_cmakelists()
|
||||
|
||||
assert "target_compile_features" not in content
|
||||
|
||||
|
||||
def _make_pch_device(tmp_path: Path, name: str) -> Path:
|
||||
"""A device dir with the pch source headers and a stub compile_commands."""
|
||||
from esphome.build_gen.espidf import _PCH_HEADERS
|
||||
|
||||
dev = tmp_path / name
|
||||
for header in _PCH_HEADERS:
|
||||
path = dev / "src" / header
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("")
|
||||
build = dev / "build"
|
||||
build.mkdir(exist_ok=True)
|
||||
from esphome.build_helpers.pch import pch_header_text
|
||||
|
||||
(build / "esphome_pch.h").write_text(pch_header_text(_PCH_HEADERS))
|
||||
(build / "compile_commands.json").write_text(
|
||||
json.dumps(
|
||||
[
|
||||
{
|
||||
"directory": str(build),
|
||||
"command": (
|
||||
"g++ -DX=1 -include esphome_pch.h "
|
||||
"-o esp-idf/src/CMakeFiles/__idf_src.dir/a.cpp.obj "
|
||||
f"-c {dev}/src/a.cpp"
|
||||
),
|
||||
"file": f"{dev}/src/a.cpp",
|
||||
}
|
||||
]
|
||||
)
|
||||
)
|
||||
return dev
|
||||
|
||||
|
||||
def test_prepare_pch_writes_header_and_sum(tmp_path: Path) -> None:
|
||||
from esphome.build_gen.espidf import prepare_pch
|
||||
|
||||
dev = _make_pch_device(tmp_path, "dev_a")
|
||||
CORE.build_path = dev
|
||||
gch = dev / "build" / "esphome_pch.h.gch"
|
||||
|
||||
def fake_compile(cmd, **kwargs):
|
||||
# The compile must target the header, not the stub TU
|
||||
assert cmd[-5:-3] == ["c++-header", "-c"]
|
||||
gch.write_bytes(b"gch")
|
||||
return subprocess.CompletedProcess(cmd, 0, "", "")
|
||||
|
||||
with (
|
||||
patch.object(CORE, "name", "test"),
|
||||
patch("esphome.build_gen.espidf.subprocess.run", side_effect=fake_compile),
|
||||
):
|
||||
prepare_pch()
|
||||
assert (dev / "build" / "esphome_pch.h").read_text().startswith("#include")
|
||||
checksum = (dev / "build" / "esphome_pch.h.gch.sum").read_text().strip()
|
||||
assert len(checksum) == 64
|
||||
# Unchanged inputs: the second call must not recompile
|
||||
with (
|
||||
patch.object(CORE, "name", "test"),
|
||||
patch("esphome.build_gen.espidf.subprocess.run", side_effect=AssertionError),
|
||||
):
|
||||
prepare_pch()
|
||||
|
||||
|
||||
def test_pch_no_device_path_poison(tmp_path: Path) -> None:
|
||||
"""Regression: neither the injected -include nor the .sum may carry the
|
||||
per-device build path, or cross-device ccache sharing breaks."""
|
||||
from esphome.build_gen.espidf import get_component_cmakelists, prepare_pch
|
||||
|
||||
sums = []
|
||||
for name in ("dev_a", "dev_b"):
|
||||
dev = _make_pch_device(tmp_path, name)
|
||||
CORE.build_path = dev
|
||||
gch = dev / "build" / "esphome_pch.h.gch"
|
||||
|
||||
def fake_compile(cmd, _gch=gch, **kwargs):
|
||||
_gch.write_bytes(b"gch")
|
||||
return subprocess.CompletedProcess(cmd, 0, "", "")
|
||||
|
||||
with (
|
||||
patch.object(CORE, "name", name),
|
||||
patch("esphome.build_gen.espidf.subprocess.run", side_effect=fake_compile),
|
||||
):
|
||||
prepare_pch()
|
||||
content = get_component_cmakelists()
|
||||
assert str(dev) not in content
|
||||
sums.append((dev / "build" / "esphome_pch.h.gch.sum").read_text())
|
||||
assert sums[0] == sums[1]
|
||||
|
||||
|
||||
def test_component_cmakelists_pch_block(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from esphome.build_gen.espidf import get_component_cmakelists
|
||||
|
||||
content = get_component_cmakelists()
|
||||
assert '"$<$<COMPILE_LANGUAGE:CXX>:-include>"' in content
|
||||
assert '"$<$<COMPILE_LANGUAGE:CXX>:esphome_pch.h>"' in content
|
||||
monkeypatch.setenv("ESPHOME_PCH_ENABLE", "0")
|
||||
assert "-include" not in get_component_cmakelists()
|
||||
|
||||
@@ -327,7 +327,7 @@ def test_split_command_preserves_paths_and_unescapes_quotes() -> None:
|
||||
r"""Backslash paths survive while ``\"`` define-quoting is unescaped."""
|
||||
command = r"C:\esp\bin\riscv32-esp-elf-g++.exe -DVER=\"1.2.3\" -IC:/inc/a -c x.cpp"
|
||||
|
||||
tokens = idedata._split_command(command)
|
||||
tokens = idedata.split_command(command)
|
||||
|
||||
assert tokens[0] == r"C:\esp\bin\riscv32-esp-elf-g++.exe"
|
||||
assert '-DVER="1.2.3"' in tokens
|
||||
@@ -341,8 +341,8 @@ def test_split_command_empty_returns_empty() -> None:
|
||||
Guards against ``CommandLineToArgvW("")`` returning the current process name
|
||||
instead of an empty list.
|
||||
"""
|
||||
assert idedata._split_command("") == []
|
||||
assert idedata._split_command(" ") == []
|
||||
assert idedata.split_command("") == []
|
||||
assert idedata.split_command(" ") == []
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != "nt", reason="Windows argv tokenization")
|
||||
@@ -503,9 +503,9 @@ def test_load_or_build_idedata_rebuilds_non_dict_cache(tmp_path: Path) -> None:
|
||||
def test_is_launcher_matches_only_known_launchers() -> None:
|
||||
"""Compilers of any shape pass; only the closed launcher set matches."""
|
||||
for token in ("/t/g++-13", "gcc-8.4.0", "clang++-17", "armcc", "icx", "cc"):
|
||||
assert not idedata._is_launcher(token)
|
||||
assert not idedata.is_launcher(token)
|
||||
for token in ("/opt/homebrew/bin/ccache", "CCACHE.EXE", "distcc", "sccache"):
|
||||
assert idedata._is_launcher(token)
|
||||
assert idedata.is_launcher(token)
|
||||
|
||||
|
||||
def test_load_or_build_idedata_corrupted_cache_is_logged(
|
||||
|
||||
@@ -93,6 +93,13 @@ def test_get_configured_targets_ci_installs_all(monkeypatch: pytest.MonkeyPatch)
|
||||
assert toolchain._get_configured_targets() is None
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _no_ccache(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Deterministic run_compile: no host ccache probe, no pch work."""
|
||||
monkeypatch.setenv("IDF_CCACHE_ENABLE", "0")
|
||||
monkeypatch.setenv("ESPHOME_PCH_ENABLE", "0")
|
||||
|
||||
|
||||
def _setup_build(setup_core: Path) -> tuple[Path, Path]:
|
||||
"""Point CORE at a build dir; return (compile_commands, idedata cache) paths."""
|
||||
CORE.name = "test"
|
||||
|
||||
Reference in New Issue
Block a user