From 85142b5b99736953d90f47011c833cc56578ee5d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 26 Aug 2026 22:22:42 -0500 Subject: [PATCH] Pass -MF to the probe only alongside a dependency flag --- esphome/platformio/pch.py.script | 10 ++++++++-- tests/unit_tests/test_platformio_pch_script.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/esphome/platformio/pch.py.script b/esphome/platformio/pch.py.script index 2e0cdaa926..64f1729050 100644 --- a/esphome/platformio/pch.py.script +++ b/esphome/platformio/pch.py.script @@ -117,12 +117,18 @@ def _compile_gch(cxx, flags, header: Path, gch: Path, proj_dir: Path): raise OSError(f"compiler killed by signal {-result.returncode}") if result.returncode != 0: return result.stderr + # -MF is only legal alongside a dependency flag; pass it solely to + # redirect a depfile that -MD/-MMD in the flags would otherwise write + dep_redirect = ( + ["-MF", os.devnull] + if any(f in ("-MD", "-MMD", "-M", "-MM") for f in flags) + else [] + ) probe = subprocess.run( # noqa: PLW1510 [ cxx, *flags, - "-MF", - os.devnull, + *dep_redirect, "-Winvalid-pch", "-include", str(header), diff --git a/tests/unit_tests/test_platformio_pch_script.py b/tests/unit_tests/test_platformio_pch_script.py index 4469eecc41..ab6408f0ed 100644 --- a/tests/unit_tests/test_platformio_pch_script.py +++ b/tests/unit_tests/test_platformio_pch_script.py @@ -100,7 +100,9 @@ def _fake_cxx( body += f"echo {fail_msg or 'boom'} >&2\nexit 1\n" else: # Only the c++-header compile has a -o; the load probe has none - body += 'out=""; prev=""\nfor a in "$@"; do [ "$prev" = "-o" ] && out="$a"; prev="$a"; done\n' + body += 'out=""; prev=""; mf=0; dep=0\nfor a in "$@"; do [ "$prev" = "-o" ] && out="$a"; prev="$a"; [ "$a" = "-MF" ] && mf=1; case "$a" in -M|-MM|-MD|-MMD) dep=1;; esac; done\n' + # Real cc1plus rejects -MF without a dependency flag + body += 'if [ "$mf" = 1 ] && [ "$dep" = 0 ]; then echo "cc1plus: error: to generate dependencies you must specify either \x27-M\x27 or \x27-MM\x27" >&2; exit 1; fi\n' body += '[ -n "$out" ] && echo gch > "$out"\n' if reject_pch: body += 'case " $* " in *c++-header*) ;; *) echo "warning: esphome_pch.h.gch: had text segment at different address" >&2;; esac\n'