diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2022-02-27 21:38:04 -0500 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2022-04-13 17:28:01 -0400 |
commit | 0e3ed2f6559ff97e4ba85a4d723597017630d150 (patch) | |
tree | 983b0d3876c59cfcce1dc16d375901c34269a6f9 /mesonbuild/interpreter | |
parent | b55349c2e9ac6f9e37e2fd6e7a8333f6893fbaa9 (diff) | |
download | meson-0e3ed2f6559ff97e4ba85a4d723597017630d150.zip meson-0e3ed2f6559ff97e4ba85a4d723597017630d150.tar.gz meson-0e3ed2f6559ff97e4ba85a4d723597017630d150.tar.bz2 |
dependencies: allow get_variable to expose files from subprojects
There are somewhat common, reasonable and legitimate use cases for a
dependency to provide data files installed to /usr which are used as
command inputs. When getting a dependency from a subproject, however,
the attempt to directly construct an input file from a subproject
results in a sandbox violation. This means not all dependencies can be
wrapped as a subproject.
One example is wayland-protocols XML files which get scanned and used to
produce C source files.
Teach Meson to recognize when a string path is the result of fetching a
dep.get_variable(), and special case this to be exempt from subproject
violations.
A requirement of this is that the file must be installed by
install_data() or install_subdir() because otherwise it is not actually
representative of what a pkg-config dependency would provide.
Diffstat (limited to 'mesonbuild/interpreter')
-rw-r--r-- | mesonbuild/interpreter/interpreter.py | 28 | ||||
-rw-r--r-- | mesonbuild/interpreter/interpreterobjects.py | 5 | ||||
-rw-r--r-- | mesonbuild/interpreter/primitives/__init__.py | 8 | ||||
-rw-r--r-- | mesonbuild/interpreter/primitives/string.py | 12 |
4 files changed, 49 insertions, 4 deletions
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index b31b7a8..5bac8ba 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -420,6 +420,7 @@ class Interpreter(InterpreterBase, HoldableObject): bool: P_OBJ.BooleanHolder, str: P_OBJ.StringHolder, P_OBJ.MesonVersionString: P_OBJ.MesonVersionStringHolder, + P_OBJ.DependencyVariableString: P_OBJ.DependencyVariableStringHolder, # Meson types mesonlib.File: OBJ.FileHolder, @@ -2716,7 +2717,12 @@ external dependencies (including libraries) must go to "dependencies".''') @typed_pos_args('join_paths', varargs=str, min_varargs=1) @noKwargs def func_join_paths(self, node: mparser.BaseNode, args: T.Tuple[T.List[str]], kwargs: 'TYPE_kwargs') -> str: - return os.path.join(*args[0]).replace('\\', '/') + parts = args[0] + ret = os.path.join(*parts).replace('\\', '/') + if isinstance(parts[0], P_OBJ.DependencyVariableString): + return P_OBJ.DependencyVariableString(ret) + else: + return ret def run(self) -> None: super().run() @@ -2759,6 +2765,26 @@ Try setting b_lundef to false instead.'''.format(self.coredata.options[OptionKey # declare_dependency). def validate_within_subproject(self, subdir, fname): srcdir = Path(self.environment.source_dir) + builddir = Path(self.environment.build_dir) + if isinstance(fname, P_OBJ.DependencyVariableString): + def validate_installable_file(fpath: Path) -> bool: + installablefiles: T.Set[Path] = set() + for d in self.build.data: + for s in d.sources: + installablefiles.add(Path(s.absolute_path(srcdir, builddir))) + installabledirs = [str(Path(srcdir, s.source_subdir)) for s in self.build.install_dirs] + if fpath in installablefiles: + return True + for d in installabledirs: + if str(fpath).startswith(d): + return True + return False + + norm = Path(fname) + # variables built from a dep.get_variable are allowed to refer to + # subproject files, as long as they are scheduled to be installed. + if norm.is_absolute() and '..' not in norm.parts and validate_installable_file(norm): + return norm = Path(srcdir, subdir, fname).resolve() if os.path.isdir(norm): inputtype = 'directory' diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index 39085e5..4aa5548 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -21,6 +21,7 @@ from ..interpreterbase import ( typed_pos_args, typed_kwargs, typed_operator, noArgsFlattening, noPosargs, noKwargs, unholder_return, flatten, resolve_second_level_holders, InterpreterException, InvalidArguments, InvalidCode) +from ..interpreter.primitives import DependencyVariableString from ..interpreter.type_checking import NoneType, ENV_SEPARATOR_KW from ..dependencies import Dependency, ExternalLibrary, InternalDependency from ..programs import ExternalProgram @@ -483,14 +484,14 @@ class DependencyHolder(ObjectHolder[Dependency]): default_varname = args[0] if default_varname is not None: FeatureNew('Positional argument to dependency.get_variable()', '0.58.0').use(self.subproject, self.current_node) - return self.held_object.get_variable( + return DependencyVariableString(self.held_object.get_variable( cmake=kwargs['cmake'] or default_varname, pkgconfig=kwargs['pkgconfig'] or default_varname, configtool=kwargs['configtool'] or default_varname, internal=kwargs['internal'] or default_varname, default_value=kwargs['default_value'], pkgconfig_define=kwargs['pkgconfig_define'], - ) + )) @FeatureNew('dependency.include_type', '0.52.0') @noPosargs diff --git a/mesonbuild/interpreter/primitives/__init__.py b/mesonbuild/interpreter/primitives/__init__.py index b4fe621..1874d0d 100644 --- a/mesonbuild/interpreter/primitives/__init__.py +++ b/mesonbuild/interpreter/primitives/__init__.py @@ -10,6 +10,8 @@ __all__ = [ 'StringHolder', 'MesonVersionString', 'MesonVersionStringHolder', + 'DependencyVariableString', + 'DependencyVariableStringHolder', ] from .array import ArrayHolder @@ -17,4 +19,8 @@ from .boolean import BooleanHolder from .dict import DictHolder from .integer import IntegerHolder from .range import RangeHolder -from .string import StringHolder, MesonVersionString, MesonVersionStringHolder +from .string import ( + StringHolder, + MesonVersionString, MesonVersionStringHolder, + DependencyVariableString, DependencyVariableStringHolder +) diff --git a/mesonbuild/interpreter/primitives/string.py b/mesonbuild/interpreter/primitives/string.py index 9129303..bf213ef 100644 --- a/mesonbuild/interpreter/primitives/string.py +++ b/mesonbuild/interpreter/primitives/string.py @@ -179,3 +179,15 @@ class MesonVersionStringHolder(StringHolder): def version_compare_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> bool: self.interpreter.tmp_meson_version = args[0] return version_compare(self.held_object, args[0]) + +# These special subclasses of string exist to cover the case where a dependency +# exports a string variable interchangeable with a system dependency. This +# matters because a dependency can only have string-type get_variable() return +# values. If at any time dependencies start supporting additional variable +# types, this class could be deprecated. +class DependencyVariableString(str): + pass + +class DependencyVariableStringHolder(StringHolder): + def op_div(self, other: str) -> DependencyVariableString: + return DependencyVariableString(super().op_div(other)) |