diff options
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/coredata.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 9d5e7ce..7a9fefe 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -463,7 +463,7 @@ class CoreData: prefix = prefix[:-1] return prefix - def sanitize_dir_option_value(self, prefix, option, value): + def sanitize_dir_option_value(self, prefix: str, option: str, value: Any) -> Any: ''' If the option is an installation directory option and the value is an absolute path, check that it resides within prefix and return the value @@ -471,22 +471,31 @@ class CoreData: This way everyone can do f.ex, get_option('libdir') and be sure to get the library directory relative to prefix. + + .as_posix() keeps the posix-like file seperators Meson uses. ''' - if option.endswith('dir') and os.path.isabs(value) and \ + try: + value = PurePath(value) + except TypeError: + return value + if option.endswith('dir') and value.is_absolute() and \ option not in builtin_dir_noprefix_options: # Value must be a subdir of the prefix # commonpath will always return a path in the native format, so we # must use pathlib.PurePath to do the same conversion before # comparing. - if os.path.commonpath([value, prefix]) != str(PurePath(prefix)): - m = 'The value of the {!r} option is {!r} which must be a ' \ - 'subdir of the prefix {!r}.\nNote that if you pass a ' \ - 'relative path, it is assumed to be a subdir of prefix.' - raise MesonException(m.format(option, value, prefix)) - # Convert path to be relative to prefix - skip = len(prefix) + 1 - value = value[skip:] - return value + msg = ('The value of the {!r} option is {!r} which must be a ' + 'subdir of the prefix {!r}.\nNote that if you pass a ' + 'relative path, it is assumed to be a subdir of prefix.') + # os.path.commonpath doesn't understand case-insensitive filesystems, + # but PurePath().relative_to() does. + try: + value = value.relative_to(prefix) + except ValueError: + raise MesonException(msg.format(option, value, prefix)) + if '..' in str(value): + raise MesonException(msg.format(option, value, prefix)) + return value.as_posix() def init_builtins(self): # Create builtin options with default values |