Skip to content

Commit

Permalink
File: Add full_path() method
Browse files Browse the repository at this point in the history
This is needed now that str.format() is not allowing it any more. It is
also more consistent with other objects that have that method as well,
such as build targets.

Fixes: #12406
  • Loading branch information
xclaesse authored and jpakkane committed Nov 24, 2023
1 parent 7f5f432 commit 85e4ee5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/markdown/snippets/file-full-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## File object now has `full_path()` method

Returns a full path pointing to the file. This is useful for printing the path
with e.g [[message]] function for debugging purpose.

**NOTE:** In most cases using the object itself will do the same job
as this and will also allow Meson to setup dependencies correctly.
12 changes: 11 additions & 1 deletion docs/yaml/objects/file.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
name: file
long_name: File
description: Opaque object that stores the path to an existing file
description: Object that stores the path to an existing file

methods:
- name: full_path
returns: str
since: 1.4.0
description: |
Returns a full path pointing to the file. This is useful for printing the
path with e.g [[message]] function for debugging purpose.
**NOTE:** In most cases using the object itself will do the same job
as this and will also allow Meson to setup dependencies correctly.
11 changes: 10 additions & 1 deletion mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,16 @@ class IncludeDirsHolder(ObjectHolder[build.IncludeDirs]):
pass

class FileHolder(ObjectHolder[mesonlib.File]):
pass
def __init__(self, file: mesonlib.File, interpreter: 'Interpreter'):
super().__init__(file, interpreter)
self.methods.update({'full_path': self.full_path_method,
})

@noPosargs
@noKwargs
@FeatureNew('file.full_path', '1.4.0')
def full_path_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
return self.held_object.absolute_path(self.env.source_dir, self.env.build_dir)

class HeadersHolder(ObjectHolder[build.Headers]):
pass
Expand Down
6 changes: 6 additions & 0 deletions test cases/common/74 file object/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ 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.
fs = import('fs')
assert(fs.as_posix(prog0[0].full_path()) == fs.as_posix(meson.current_source_dir() / 'prog.c'))

0 comments on commit 85e4ee5

Please sign in to comment.