aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorTristan Partin <tristan@partin.io>2023-08-07 12:30:15 -0500
committerXavier Claessens <xclaesse@gmail.com>2023-08-17 17:05:49 -0400
commitf52bcaa27fc125ab9ae583af466ba99c164169f3 (patch)
tree34ecddd4e69581075035f405ff1fddcada6f252f /mesonbuild
parentcbf8e67f19e384e5f8eb7f65d3020551769de545 (diff)
downloadmeson-f52bcaa27fc125ab9ae583af466ba99c164169f3.zip
meson-f52bcaa27fc125ab9ae583af466ba99c164169f3.tar.gz
meson-f52bcaa27fc125ab9ae583af466ba99c164169f3.tar.bz2
Add fs.relative_to()
Returns a relative path from arg 2 to arg 1 similar to os.path.relpath().
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/modules/fs.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py
index 53174d2..a2dbdf2 100644
--- a/mesonbuild/modules/fs.py
+++ b/mesonbuild/modules/fs.py
@@ -20,18 +20,21 @@ import typing as T
from . import ExtensionModule, ModuleReturnValue, ModuleInfo
from .. import mlog
-from ..build import CustomTarget, InvalidArguments
+from ..build import BuildTarget, CustomTarget, CustomTargetIndex, InvalidArguments
from ..interpreter.type_checking import INSTALL_KW, INSTALL_MODE_KW, INSTALL_TAG_KW, NoneType
from ..interpreterbase import FeatureNew, KwargInfo, typed_kwargs, typed_pos_args, noKwargs
+from ..interpreterbase.baseobjects import TYPE_kwargs
from ..mesonlib import (
File,
MesonException,
has_path_sep,
path_is_in_root,
)
+from ..utils.universal import relpath
if T.TYPE_CHECKING:
from . import ModuleState
+ from ..build import BuildTargetTypes
from ..interpreter import Interpreter
from ..mesonlib import FileOrString, FileMode
@@ -75,6 +78,7 @@ class FSModule(ExtensionModule):
'stem': self.stem,
'read': self.read,
'copyfile': self.copyfile,
+ 'relative_to': self.relative_to,
})
def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
@@ -312,6 +316,23 @@ class FSModule(ExtensionModule):
return ModuleReturnValue(ct, [ct])
+ @FeatureNew('fs.relative_to', '1.3.0')
+ @typed_pos_args('fs.relative_to', (str, File, CustomTarget, CustomTargetIndex, BuildTarget), (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
+ @noKwargs
+ def relative_to(self, state: ModuleState, args: T.Tuple[T.Union[FileOrString, BuildTargetTypes], T.Union[FileOrString, BuildTargetTypes]], kwargs: TYPE_kwargs) -> str:
+ def to_path(arg: T.Union[FileOrString, CustomTarget, CustomTargetIndex, BuildTarget]) -> str:
+ if isinstance(arg, File):
+ return arg.absolute_path(state.environment.source_dir, state.environment.build_dir)
+ elif isinstance(arg, (CustomTarget, CustomTargetIndex, BuildTarget)):
+ return state.backend.get_target_filename_abs(arg)
+ else:
+ return os.path.join(state.environment.source_dir, state.subdir, arg)
+
+ t = to_path(args[0])
+ f = to_path(args[1])
+
+ return relpath(t, f)
+
def initialize(*args: T.Any, **kwargs: T.Any) -> FSModule:
return FSModule(*args, **kwargs)