diff --git a/docs/markdown/snippets/include_directories_to_list.md b/docs/markdown/snippets/include_directories_to_list.md new file mode 100644 index 000000000..2b2d7dbf4 --- /dev/null +++ b/docs/markdown/snippets/include_directories_to_list.md @@ -0,0 +1,10 @@ +## include_directories object now have a to_list() method + +The [[@inc]] object returned by [[include_directories]] now has a `to_list()` method that +returns a list of strings of absolute paths. Unless the include path is a +system path, there will be a path for the source root and one for the build root. + +``` +inc = include_directories('include') +include_paths = inc.to_list() +``` diff --git a/docs/yaml/objects/inc.yaml b/docs/yaml/objects/inc.yaml index 9f5e6844d..cf88fece8 100644 --- a/docs/yaml/objects/inc.yaml +++ b/docs/yaml/objects/inc.yaml @@ -1,3 +1,13 @@ name: inc long_name: Include directories description: Opaque wrapper for storing include directories + +methods: +- name: to_list + returns: list[str] + since: 1.12.0 + description: | + Returns a list of strings of absolute paths to the directories + contained within the object. Unless the path is a system path, + there will be a path relative to the source root and one + relative to the build root. System paths are provided as-is. diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index 3ef37e784..441285ce6 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -797,9 +797,15 @@ class MachineHolder(ObjectHolder['MachineInfo']): return self.held_object.subsystem raise InterpreterException('Subsystem not defined or could not be autodetected.') - class IncludeDirsHolder(ObjectHolder[build.IncludeDirs]): - pass + @noPosargs + @noKwargs + @FeatureNew('inc.to_list', '1.12.0') + @InterpreterObject.method('to_list') + def to_list_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[str]: + if self.held_object.is_system: + return self.held_object.incdirs + return self.held_object.abs_string_list(self.env.source_dir, self.env.build_dir) class FileHolder(ObjectHolder[mesonlib.File]): def __init__(self, file: mesonlib.File, interpreter: 'Interpreter'): diff --git a/test cases/common/18 includedir/meson.build b/test cases/common/18 includedir/meson.build index 318058727..35eeea67b 100644 --- a/test cases/common/18 includedir/meson.build +++ b/test cases/common/18 includedir/meson.build @@ -30,3 +30,11 @@ testcase expect_error(errormsg) endtestcase # Test for issue #12217 include_directories(meson.current_source_dir() + 'xyz') + +# Test to see if an include_directory object will have the subdirectory in the path +source_dir = meson.current_source_dir() / 'src' +build_dir = meson.current_build_dir() / 'src' +expected = [source_dir / '.', build_dir / '.'] +# Use fs.as_posix() to ensure the results are the same across platforms. +fs = import('fs') +assert(fs.as_posix('@0@'.format(subdir_inc.to_list())) == '@0@'.format(expected)) diff --git a/test cases/common/18 includedir/src/meson.build b/test cases/common/18 includedir/src/meson.build index 30d2e0c87..b2b8e3976 100644 --- a/test cases/common/18 includedir/src/meson.build +++ b/test cases/common/18 includedir/src/meson.build @@ -3,3 +3,5 @@ test('inc test', exe) exe2 = executable('prog2', 'prog.c', 'func.c', include_directories : [['../include']]) test('inc test 2', exe2) + +subdir_inc = include_directories('.') diff --git a/test cases/common/218 include_dir dot/meson.build b/test cases/common/218 include_dir dot/meson.build index 71f7189c5..74807370a 100644 --- a/test cases/common/218 include_dir dot/meson.build +++ b/test cases/common/218 include_dir dot/meson.build @@ -5,4 +5,4 @@ project('Include Here', 'c') inc = include_directories('.') -subdir('src') \ No newline at end of file +subdir('src') diff --git a/test cases/common/218 include_dir dot/src/meson.build b/test cases/common/218 include_dir dot/src/meson.build index fcbefb0b9..d0c82542a 100644 --- a/test cases/common/218 include_dir dot/src/meson.build +++ b/test cases/common/218 include_dir dot/src/meson.build @@ -1,6 +1,15 @@ +# test to_list() for include_directories defined in the parent meson.build file +source_dir = meson.project_source_root() +build_dir = meson.project_build_root() +expected = [source_dir / '.', build_dir / '.'] +# Use fs.as_posix() to ensure the results are the same across platforms. +fs = import('fs') +assert(fs.as_posix('@0@'.format(inc.to_list())) == '@0@'.format(expected)) + +# test include directories t = executable( 'main', ['main.c', 'rone.c'], include_directories : inc, implicit_include_directories : false, -) \ No newline at end of file +) diff --git a/test cases/common/250 system include dir/meson.build b/test cases/common/250 system include dir/meson.build index 724a8e49c..e6883172e 100644 --- a/test cases/common/250 system include dir/meson.build +++ b/test cases/common/250 system include dir/meson.build @@ -9,5 +9,9 @@ if not ['gcc', 'clang', 'clang-cl'].contains(compiler_id) endif lib_include_directories = include_directories('lib', is_system: true) + +# test that a system library include_directories just contains a single path +assert('@0@'.format(lib_include_directories.to_list()) == '[\'lib\']') + add_project_arguments('-Wsign-conversion', language: 'cpp') -executable('system_include_dir_test', sources: 'main.cpp', include_directories: lib_include_directories) +executable('system_include_dir_b_test', sources: 'main.cpp', include_directories: lib_include_directories) diff --git a/test cases/common/74 file object/meson.build b/test cases/common/74 file object/meson.build index 05c24aef8..c3977e41a 100644 --- a/test cases/common/74 file object/meson.build +++ b/test cases/common/74 file object/meson.build @@ -7,8 +7,6 @@ test('fobj', executable('fobj', prog0, lib0)) subdir('subdir1') subdir('subdir2') -# Use fs.as_posix() because / operator replaces \ with / in the path, but -# full_path() method is not doing that. This is a pretty inconsistent across all -# Meson APIs. +# Use fs.as_posix() to ensure the results are the same across platforms. fs = import('fs') assert(fs.as_posix(prog0[0].full_path()) == fs.as_posix(meson.current_source_dir() / 'prog.c'))