From 2d1448c0bf4b3e6ad5f488ebefa9a5106bc6d174 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Fri, 28 Nov 2025 15:53:35 +0000 Subject: [PATCH] Prioritise headers provided by dependencies over system headers Header directories collected from direct dependencies are placed in the header search path using `-isystem`, which allows them to be preempted by the compiler toolchain injecting its own `-I` options into the argument list. This causes build failures if the compiler toolchain's injected directories contain a header with the same file name as one provided by a dependency but a different API; in the worst case, it can cause compilation to silently succeed but trigger unexpected run-time behaviour. Add header directories for dependencies to the search path via `-I`, but make sure they appear in the argument list after the options added by `compiler_flags` so they can be overridden if necessary. Fixes #96. --- build_defs/cc.build_defs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build_defs/cc.build_defs b/build_defs/cc.build_defs index c7d64b8..bfdfaa6 100644 --- a/build_defs/cc.build_defs +++ b/build_defs/cc.build_defs @@ -828,6 +828,8 @@ def _library_cmds(c:bool=False, compiler_flags:list=[], pkg_config_libs:list=[], # "only run preprocess, compile and assemble steps"). common_flags = [] if any([cf in _ACTION_FLAGS for cf in compiler_flags]) else ["-c"] + # Prepend the current directory to the header search path. This takes precedence over any header directories that + # this target inherits from its dependencies. common_flags += ["-I", "."] dbg_flags = _build_flags(compiler_flags, pkg_config_libs, pkg_config_cflags, defines, c=c, dbg=True) opt_flags = _build_flags(compiler_flags, pkg_config_libs, pkg_config_cflags, defines, c=c) @@ -900,7 +902,7 @@ def _library_apply_labels(c:bool=False, compiler_flags:list=[], pkg_config_libs: # Only the headers for this library's direct dependencies should be collected - transitive dependencies aren't # needed at compile time, and just pollute the build environment. for l in get_labels(name, "cc:inc:", maxdepth=1): - flags += ["-isystem", l] + flags += ["-I", l] for l in get_labels(name, "cc:"): if l.startswith("pc:") and l[3:] not in pkg_config_libs: pkg_config_libs += [l[3:]]