Skip to content

Commit 295fed3

Browse files
committed
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.
1 parent 03d5094 commit 295fed3

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

mesonbuild/backend/ninjabackend.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,26 +2988,15 @@ def generate_llvm_ir_compile(self, target, src: FileOrString) -> T.Tuple[str, st
29882988
return (rel_obj, rel_src)
29892989

29902990
@lru_cache(maxsize=None)
2991-
def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system: bool) -> \
2992-
T.Tuple['ImmutableListProtocol[str]', 'ImmutableListProtocol[str]']:
2993-
# Avoid superfluous '/.' at the end of paths when d is '.'
2994-
if d not in ('', '.'):
2995-
expdir = os.path.normpath(os.path.join(basedir, d))
2996-
else:
2997-
expdir = basedir
2998-
srctreedir = os.path.normpath(os.path.join(self.build_to_src, expdir))
2999-
sargs = compiler.get_include_args(srctreedir, is_system)
3000-
# There may be include dirs where a build directory has not been
3001-
# created for some source dir. For example if someone does this:
3002-
#
3003-
# inc = include_directories('foo/bar/baz')
3004-
#
3005-
# But never subdir()s into the actual dir.
3006-
if os.path.isdir(os.path.join(self.environment.get_build_dir(), expdir)):
3007-
bargs = compiler.get_include_args(expdir, is_system)
3008-
else:
3009-
bargs = []
3010-
return (sargs, bargs)
2991+
def generate_inc_dir(self, compiler: 'Compiler', inc: build.IncludeDirs
2992+
) -> ImmutableListProtocol[str]:
2993+
# We should iterate include dirs in reversed orders because
2994+
# -Ipath will add to begin of array. And without reverse
2995+
# flags will be added in reversed order.
2996+
args: T.List[str] = []
2997+
for p in inc.rel_string_list(self.build_to_src, self.build_dir):
2998+
args.extend(compiler.get_include_args(p, inc.is_system))
2999+
return args
30113000

30123001
def _generate_single_compile(self, target: build.BuildTarget, compiler: Compiler) -> CompilerArgs:
30133002
commands = self._generate_single_compile_base_args(target, compiler)
@@ -3046,17 +3035,10 @@ def _generate_single_compile_target_args(self, target: build.BuildTarget, compil
30463035
# external deps and must maintain the order in which they are specified.
30473036
# Hence, we must reverse the list so that the order is preserved.
30483037
for i in reversed(target.get_include_dirs()):
3049-
basedir = i.curdir
30503038
# We should iterate include dirs in reversed orders because
30513039
# -Ipath will add to begin of array. And without reverse
30523040
# flags will be added in reversed order.
3053-
for d in reversed(i.incdirs):
3054-
# Add source subdir first so that the build subdir overrides it
3055-
(compile_obj, includeargs) = self.generate_inc_dir(compiler, d, basedir, i.is_system)
3056-
commands += compile_obj
3057-
commands += includeargs
3058-
for d in i.extra_build_dirs:
3059-
commands += compiler.get_include_args(d, i.is_system)
3041+
commands += self.generate_inc_dir(compiler, i)
30603042
# Add per-target compile args, f.ex, `c_args : ['-DFOO']`. We set these
30613043
# near the end since these are supposed to override everything else.
30623044
commands += self.escape_extra_args(target.get_extra_args(compiler.get_language()))

mesonbuild/build.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,12 @@ def abs_string_list(self, sourcedir: str, builddir: str) -> T.List[str]:
510510
strlist.append(os.path.join(builddir, self.curdir, idir))
511511
return strlist
512512

513-
def rel_string_list(self, build_to_src: str) -> T.List[str]:
513+
def rel_string_list(self, build_to_src: str, build_root: T.Optional[str] = None) -> T.List[str]:
514514
"""Convert IncludeDirs object to a list of relative string paths.
515515
516516
:param build_to_src: The relative path from the build dir to source dir
517+
:param build_root: The absolute build root. When provided, build
518+
directories that have not been created will be ignored. Default: None.
517519
:return: A list if strings (without compiler argument)
518520
"""
519521
strlist: T.List[str] = []
@@ -524,9 +526,11 @@ def rel_string_list(self, build_to_src: str) -> T.List[str]:
524526
expdir = bld_dir
525527
else:
526528
expdir = self.curdir
527-
strlist.append(bld_dir)
529+
if build_root is None or os.path.isdir(os.path.join(build_root, expdir)):
530+
strlist.append(bld_dir)
528531
if add_src:
529532
strlist.append(os.path.normpath(os.path.join(build_to_src, expdir)))
533+
530534
return strlist
531535

532536

0 commit comments

Comments
 (0)