Skip to content

Commit 03d5094

Browse files
committed
build: Add IncludeDirs.rel_string_list
This allows us to clean up many instances of the same pattern implemented elsewhere, by consolidating into one.
1 parent 798464b commit 03d5094

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

mesonbuild/build.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,26 @@ 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]:
514+
"""Convert IncludeDirs object to a list of relative string paths.
515+
516+
:param build_to_src: The relative path from the build dir to source dir
517+
:return: A list if strings (without compiler argument)
518+
"""
519+
strlist: T.List[str] = []
520+
for idirs, add_src in [(self.incdirs, True), (self.extra_build_dirs, False)]:
521+
for idir in idirs:
522+
bld_dir = os.path.normpath(os.path.join(self.curdir, idir))
523+
if idir not in {'', '.'}:
524+
expdir = bld_dir
525+
else:
526+
expdir = self.curdir
527+
strlist.append(bld_dir)
528+
if add_src:
529+
strlist.append(os.path.normpath(os.path.join(build_to_src, expdir)))
530+
return strlist
531+
532+
513533
@dataclass(eq=False)
514534
class ExtractedObjects(HoldableObject):
515535
'''

mesonbuild/compilers/d.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,8 @@ def get_feature_args(self, kwargs: DFeatures, build_to_src: str) -> T.List[str]:
513513
import_dir_arg = d_feature_args[self.id]['import_dir']
514514
if not import_dir_arg:
515515
raise EnvironmentException('D compiler %s does not support the "string import directories" feature.' % self.name_string())
516-
# TODO: ImportDirs.to_string_list(), but we need both the project source
517-
# root and project build root for that.
518516
for idir_obj in kwargs['import_dirs']:
519-
basedir = idir_obj.curdir
520-
for idir in idir_obj.incdirs:
521-
bldtreedir = os.path.join(basedir, idir)
522-
# Avoid superfluous '/.' at the end of paths when d is '.'
523-
if idir not in ('', '.'):
524-
expdir = bldtreedir
525-
else:
526-
expdir = basedir
527-
srctreedir = os.path.join(build_to_src, expdir)
528-
res.append(f'{import_dir_arg}{srctreedir}')
529-
res.append(f'{import_dir_arg}{bldtreedir}')
517+
res.extend(f'{import_dir_arg}{i}' for i in idir_obj.rel_string_list(build_to_src))
530518

531519
return res
532520

0 commit comments

Comments
 (0)