Add to_list() method to include_directories objects

clean up

fix tests for windows paths mimicing what was done in other unit tests (e.g., test case 74)

Update mesonbuild/interpreter/interpreterobjects.py

Co-authored-by: Charles Brunet <charles.brunet@optelgroup.com>

Update docs/markdown/snippets/include_directories_to_list.md

Co-authored-by: Charles Brunet <charles.brunet@optelgroup.com>

Update mesonbuild/interpreter/interpreterobjects.py

Co-authored-by: Charles Brunet <charles.brunet@optelgroup.com>

Update docs/yaml/objects/inc.yaml

Co-authored-by: Charles Brunet <charles.brunet@optelgroup.com>

Fix comment language
This commit is contained in:
Gabe Black
2024-04-02 22:50:24 +00:00
committed by Paolo Bonzini
parent 4146c44723
commit a5562ce95a
9 changed files with 55 additions and 8 deletions

View File

@@ -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()
```

View File

@@ -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.

View File

@@ -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'):

View File

@@ -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))

View File

@@ -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('.')

View File

@@ -5,4 +5,4 @@ project('Include Here', 'c')
inc = include_directories('.')
subdir('src')
subdir('src')

View File

@@ -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,
)
)

View File

@@ -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)

View File

@@ -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'))