Skip to content

Commit

Permalink
Add support for BuildTargetTypes to various fs module functions
Browse files Browse the repository at this point in the history
The new support was added to fs.name, fs.parent, fs.replace_suffix, and
fs.stem.
  • Loading branch information
tristan957 committed Feb 23, 2024
1 parent b57b84b commit 90b8ed0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 33 deletions.
9 changes: 9 additions & 0 deletions docs/markdown/snippets/additional_types_for_fs_module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Added support for `[[@build_tgt]]`, `[[@custom_tgt]]`, and `[[@custom_idx]]` to certain FS module functions

Support for `[[@build_tgt]]`, `[[@custom_tgt]]`, and `[[@custom_idx]]` was
added to the following FS module APIs:

- `fs.name`
- `fs.parent`
- `fs.replace_suffix`
- `fs.stem`
66 changes: 33 additions & 33 deletions mesonbuild/modules/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
return Path(arg.absolute_path(state.source_root, state.environment.get_build_dir()))
return Path(state.source_root) / Path(state.subdir) / Path(arg).expanduser()

@staticmethod
def _obj_to_path(feature_new_prefix: str, obj: T.Union[FileOrString, BuildTargetTypes], state: ModuleState) -> PurePath:
if isinstance(obj, str):
return PurePath(obj)

if isinstance(obj, File):
FeatureNew(f'{feature_new_prefix} with file', '0.59.0').use(state.subproject, location=state.current_node)
return PurePath(str(obj))

FeatureNew(f'{feature_new_prefix} with build_tgt, custom_tgt, and custom_idx', '1.4.0').use(state.subproject, location=state.current_node)
return PurePath(state.backend.get_target_filename(obj))

def _resolve_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
"""
resolves symlinks and makes absolute a directory relative to calling meson.build,
Expand All @@ -97,7 +109,7 @@ def expanduser(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[st
@typed_pos_args('fs.is_absolute', (str, File))
def is_absolute(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> bool:
if isinstance(args[0], File):
FeatureNew('fs.is_absolute_file', '0.59.0').use(state.subproject, location=state.current_node)
FeatureNew('fs.is_absolute with file', '0.59.0').use(state.subproject, location=state.current_node)
return PurePath(str(args[0])).is_absolute()

@noKwargs
Expand All @@ -119,7 +131,7 @@ def exists(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T
@typed_pos_args('fs.is_symlink', (str, File))
def is_symlink(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> bool:
if isinstance(args[0], File):
FeatureNew('fs.is_symlink_file', '0.59.0').use(state.subproject, location=state.current_node)
FeatureNew('fs.is_symlink with file', '0.59.0').use(state.subproject, location=state.current_node)
return self._absolute_dir(state, args[0]).is_symlink()

@noKwargs
Expand All @@ -136,7 +148,7 @@ def is_dir(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T
@typed_pos_args('fs.hash', (str, File), str)
def hash(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> str:
if isinstance(args[0], File):
FeatureNew('fs.hash_file', '0.59.0').use(state.subproject, location=state.current_node)
FeatureNew('fs.hash with file', '0.59.0').use(state.subproject, location=state.current_node)
file = self._resolve_dir(state, args[0])
if not file.is_file():
raise MesonException(f'{file} is not a file and therefore cannot be hashed')
Expand All @@ -152,7 +164,7 @@ def hash(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs:
@typed_pos_args('fs.size', (str, File))
def size(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> int:
if isinstance(args[0], File):
FeatureNew('fs.size_file', '0.59.0').use(state.subproject, location=state.current_node)
FeatureNew('fs.size with file', '0.59.0').use(state.subproject, location=state.current_node)
file = self._resolve_dir(state, args[0])
if not file.is_file():
raise MesonException(f'{file} is not a file and therefore cannot be sized')
Expand All @@ -165,7 +177,7 @@ def size(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Di
@typed_pos_args('fs.is_samepath', (str, File), (str, File))
def is_samepath(self, state: 'ModuleState', args: T.Tuple['FileOrString', 'FileOrString'], kwargs: T.Dict[str, T.Any]) -> bool:
if isinstance(args[0], File) or isinstance(args[1], File):
FeatureNew('fs.is_samepath_file', '0.59.0').use(state.subproject, location=state.current_node)
FeatureNew('fs.is_samepath with file', '0.59.0').use(state.subproject, location=state.current_node)
file1 = self._resolve_dir(state, args[0])
file2 = self._resolve_dir(state, args[1])
if not file1.exists():
Expand All @@ -178,41 +190,29 @@ def is_samepath(self, state: 'ModuleState', args: T.Tuple['FileOrString', 'FileO
return False

@noKwargs
@typed_pos_args('fs.replace_suffix', (str, File), str)
def replace_suffix(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> str:
if isinstance(args[0], File):
FeatureNew('fs.replace_suffix_file', '0.59.0').use(state.subproject, location=state.current_node)
original = PurePath(str(args[0]))
new = original.with_suffix(args[1])
return str(new)
@typed_pos_args('fs.replace_suffix', (str, File, CustomTarget, CustomTargetIndex, BuildTarget), str)
def replace_suffix(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTargetTypes], str], kwargs: T.Dict[str, T.Any]) -> str:
path = self._obj_to_path('fs.replace_suffix', args[0], state)
return str(path.with_suffix(args[1]))

@noKwargs
@typed_pos_args('fs.parent', (str, File))
def parent(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> str:
if isinstance(args[0], File):
FeatureNew('fs.parent_file', '0.59.0').use(state.subproject, location=state.current_node)
original = PurePath(str(args[0]))
new = original.parent
return str(new)
@typed_pos_args('fs.parent', (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
def parent(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTargetTypes]], kwargs: T.Dict[str, T.Any]) -> str:
path = self._obj_to_path('fs.parent', args[0], state)
return str(path.parent)

@noKwargs
@typed_pos_args('fs.name', (str, File))
def name(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> str:
if isinstance(args[0], File):
FeatureNew('fs.name_file', '0.59.0').use(state.subproject, location=state.current_node)
original = PurePath(str(args[0]))
new = original.name
return str(new)
@typed_pos_args('fs.name', (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
def name(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTargetTypes]], kwargs: T.Dict[str, T.Any]) -> str:
path = self._obj_to_path('fs.name', args[0], state)
return str(path.name)

@noKwargs
@typed_pos_args('fs.stem', (str, File))
@typed_pos_args('fs.stem', (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
@FeatureNew('fs.stem', '0.54.0')
def stem(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> str:
if isinstance(args[0], File):
FeatureNew('fs.stem_file', '0.59.0').use(state.subproject, location=state.current_node)
original = PurePath(str(args[0]))
new = original.stem
return str(new)
def stem(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTargetTypes]], kwargs: T.Dict[str, T.Any]) -> str:
path = self._obj_to_path('fs.stem', args[0], state)
return str(path.stem)

@FeatureNew('fs.read', '0.57.0')
@typed_pos_args('fs.read', (str, File))
Expand Down
5 changes: 5 additions & 0 deletions test cases/common/220 fs module/btgt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int
main(void)
{
return 0;
}
Empty file.
19 changes: 19 additions & 0 deletions test cases/common/220 fs module/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ is_windows = build_machine.system() == 'windows'
fs = import('fs')

f = files('meson.build')
btgt = executable('btgt', 'btgt.c')
ctgt = fs.copyfile('ctgt.txt')

assert(fs.exists('meson.build'), 'Existing file reported as missing.')
assert(not fs.exists('nonexisting'), 'Nonexisting file was found.')
Expand Down Expand Up @@ -88,6 +90,13 @@ new_check = is_windows ? 'j:\\foo\\bar.ini' : '/foo/bar.ini'
new = fs.replace_suffix(original, '.ini')
assert(new == new_check, 'absolute path replace_suffix failed')

new = fs.replace_suffix(btgt, '.ini')
assert(new == 'btgt.ini', 'replace_suffix failed for build target')
new = fs.replace_suffix(ctgt, '.ini')
assert(new == 'ctgt.ini', 'replace_suffix failed for custom target')
new = fs.replace_suffix(ctgt[0], '.ini')
assert(new == 'ctgt.ini', 'replace_suffix failed for custom target index')

# -- hash

md5 = fs.hash('subdir/subdirfile.txt', 'md5')
Expand Down Expand Up @@ -135,11 +144,21 @@ assert(fs.parent(f[1]) == 'subdir/..', 'failed to get dirname')
else
assert(fs.parent(f[1]) == 'subdir\..', 'failed to get dirname')
endif
assert(fs.parent(btgt) == '.', 'failed to get dirname for build target')
assert(fs.parent(ctgt) == '.', 'failed to get dirname for custom target')
assert(fs.parent(ctgt[0]) == '.', 'failed to get dirname for custom target index')

assert(fs.name('foo/bar') == 'bar', 'failed to get basename')
assert(fs.name(f[1]) == 'meson.build', 'failed to get basename')
assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix')
assert(fs.name(btgt) == 'btgt', 'failed to get basename of build target')
assert(fs.name(ctgt) == 'ctgt.txt', 'failed to get basename of custom target')
assert(fs.name(ctgt[0]) == 'ctgt.txt', 'failed to get basename of custom target index')
assert(fs.stem('foo/bar/baz.dll') == 'baz', 'failed to get stem with suffix')
assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compound suffix')
assert(fs.stem(btgt) == 'btgt', 'failed to get stem of build target')
assert(fs.stem(ctgt) == 'ctgt', 'failed to get stem of custom target')
assert(fs.stem(ctgt[0]) == 'ctgt', 'failed to get stem of custom target index')

# relative_to
if build_machine.system() == 'windows'
Expand Down

0 comments on commit 90b8ed0

Please sign in to comment.