backend/ninja: Extend InlcudeDirs.rel_string_list for backend use

This adds the ability to remove build directories that don't exist.

This doesn't require a second call to reverse() because that was being
used to switch the order of the build and src directories, but
`rel_string_list` returns them in the expected order already.
This commit is contained in:
Dylan Baker
2025-10-28 14:12:03 -07:00
parent d1816fc725
commit a6ddf25634
2 changed files with 16 additions and 30 deletions

View File

@@ -3009,26 +3009,15 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
return (rel_obj, rel_src)
@lru_cache(maxsize=None)
def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system: bool) -> \
T.Tuple['ImmutableListProtocol[str]', 'ImmutableListProtocol[str]']:
# Avoid superfluous '/.' at the end of paths when d is '.'
if d not in ('', '.'):
expdir = os.path.normpath(os.path.join(basedir, d))
else:
expdir = basedir
srctreedir = os.path.normpath(os.path.join(self.build_to_src, expdir))
sargs = compiler.get_include_args(srctreedir, is_system)
# There may be include dirs where a build directory has not been
# created for some source dir. For example if someone does this:
#
# inc = include_directories('foo/bar/baz')
#
# But never subdir()s into the actual dir.
if os.path.isdir(os.path.join(self.environment.get_build_dir(), expdir)):
bargs = compiler.get_include_args(expdir, is_system)
else:
bargs = []
return (sargs, bargs)
def generate_inc_dir(self, compiler: 'Compiler', inc: build.IncludeDirs
) -> ImmutableListProtocol[str]:
# We should iterate include dirs in reversed orders because
# -Ipath will add to begin of array. And without reverse
# flags will be added in reversed order.
args: T.List[str] = []
for p in inc.rel_string_list(self.build_to_src, self.build_dir):
args.extend(compiler.get_include_args(p, inc.is_system))
return args
def _generate_single_compile(self, target: build.BuildTarget, compiler: Compiler) -> CompilerArgs:
commands = self._generate_single_compile_base_args(target, compiler)
@@ -3067,17 +3056,10 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
# external deps and must maintain the order in which they are specified.
# Hence, we must reverse the list so that the order is preserved.
for i in reversed(target.get_include_dirs()):
basedir = i.curdir
# We should iterate include dirs in reversed orders because
# -Ipath will add to begin of array. And without reverse
# flags will be added in reversed order.
for d in reversed(i.incdirs):
# Add source subdir first so that the build subdir overrides it
(compile_obj, includeargs) = self.generate_inc_dir(compiler, d, basedir, i.is_system)
commands += compile_obj
commands += includeargs
for d in i.extra_build_dirs:
commands += compiler.get_include_args(d, i.is_system)
commands += self.generate_inc_dir(compiler, i)
# Add per-target compile args, f.ex, `c_args : ['-DFOO']`. We set these
# near the end since these are supposed to override everything else.
commands += self.escape_extra_args(target.get_extra_args(compiler.get_language()))

View File

@@ -509,10 +509,12 @@ class IncludeDirs(HoldableObject):
strlist.append(os.path.join(builddir, self.curdir, idir))
return strlist
def rel_string_list(self, build_to_src: str) -> T.List[str]:
def rel_string_list(self, build_to_src: str, build_root: T.Optional[str] = None) -> T.List[str]:
"""Convert IncludeDirs object to a list of relative string paths.
:param build_to_src: The relative path from the build dir to source dir
:param build_root: The absolute build root. When provided, build
directories that have not been created will be ignored. Default: None.
:return: A list if strings (without compiler argument)
"""
strlist: T.List[str] = []
@@ -523,9 +525,11 @@ class IncludeDirs(HoldableObject):
expdir = bld_dir
else:
expdir = self.curdir
strlist.append(bld_dir)
if build_root is None or os.path.isdir(os.path.join(build_root, expdir)):
strlist.append(bld_dir)
if add_src:
strlist.append(os.path.normpath(os.path.join(build_to_src, expdir)))
return strlist