From 2e02ef6592fa6634eadbe388c2a5ee957ce20e59 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 9 Apr 2021 15:15:07 -0400 Subject: [PATCH] modules: Add methods dict everywhere This fix calling random internal methods from meson.build as long as they were not prefixed by underscore. --- mesonbuild/interpreter/interpreterobjects.py | 7 +----- mesonbuild/modules/__init__.py | 4 +-- mesonbuild/modules/cmake.py | 6 +++++ mesonbuild/modules/dlang.py | 3 +++ mesonbuild/modules/fs.py | 17 +++++++++++++ mesonbuild/modules/gnome.py | 14 +++++++++++ mesonbuild/modules/hotdoc.py | 4 +++ mesonbuild/modules/i18n.py | 6 +++++ mesonbuild/modules/keyval.py | 3 +++ mesonbuild/modules/modtest.py | 5 ++++ mesonbuild/modules/pkgconfig.py | 5 ++++ mesonbuild/modules/python.py | 3 +++ mesonbuild/modules/python3.py | 6 +++++ mesonbuild/modules/qt.py | 5 ++++ mesonbuild/modules/rpm.py | 26 +++++++++++--------- mesonbuild/modules/sourceset.py | 3 +++ mesonbuild/modules/unstable_icestorm.py | 3 +++ mesonbuild/modules/unstable_rust.py | 4 +++ mesonbuild/modules/unstable_simd.py | 3 +++ mesonbuild/modules/windows.py | 5 ++++ 20 files changed, 113 insertions(+), 19 deletions(-) diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index 3f76e4eec..c70b8b5a1 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -789,13 +789,8 @@ class ModuleObjectHolder(InterpreterObject, ObjectHolder['ModuleObject']): def method_call(self, method_name, args, kwargs): modobj = self.held_object method = modobj.methods.get(method_name) - if not method and not modobj.methods: - # FIXME: Port all modules to use the methods dict. - method = getattr(modobj, method_name, None) - if method_name.startswith('_'): - raise InvalidArguments(f'Method {method_name!r} is private.') if not method: - raise InvalidCode('Unknown method "%s" in object.' % method_name) + raise InvalidCode(f'Unknown method {method_name!r} in object.') if not getattr(method, 'no-args-flattening', False): args = flatten(args) state = ModuleState(self.interpreter) diff --git a/mesonbuild/modules/__init__.py b/mesonbuild/modules/__init__.py index 7a8312263..5bae3a0de 100644 --- a/mesonbuild/modules/__init__.py +++ b/mesonbuild/modules/__init__.py @@ -87,7 +87,7 @@ class ModuleObject: """Base class for all objects returned by modules """ def __init__(self) -> None: - self.methods = {} # type: T.Dict[str, T.Callable[[T.List[TYPE_nvar], TYPE_nkwargs], TYPE_var]] + self.methods = {} # type: T.Dict[str, T.Callable[[ModuleState, T.List[TYPE_nvar], TYPE_nkwargs], T.Union[ModuleReturnValue, TYPE_var]]] class MutableModuleObject(ModuleObject): pass @@ -112,7 +112,7 @@ def is_module_library(fname): class ModuleReturnValue: - def __init__(self, return_value: 'TYPE_var', new_objects: T.List['TYPE_var']) -> None: + def __init__(self, return_value: T.Optional['TYPE_var'], new_objects: T.List['TYPE_var']) -> None: self.return_value = return_value assert(isinstance(new_objects, list)) self.new_objects = new_objects diff --git a/mesonbuild/modules/cmake.py b/mesonbuild/modules/cmake.py index b4888431c..fac3b245d 100644 --- a/mesonbuild/modules/cmake.py +++ b/mesonbuild/modules/cmake.py @@ -212,6 +212,12 @@ class CmakeModule(ExtensionModule): def __init__(self, interpreter): super().__init__(interpreter) + self.methods.update({ + 'write_basic_package_version_file': self.write_basic_package_version_file, + 'configure_package_config_file': self.configure_package_config_file, + 'subproject': self.subproject, + 'subproject_options': self.subproject_options, + }) def detect_voidp_size(self, env): compilers = env.coredata.compilers.host diff --git a/mesonbuild/modules/dlang.py b/mesonbuild/modules/dlang.py index 1dcf5e278..f08803d8b 100644 --- a/mesonbuild/modules/dlang.py +++ b/mesonbuild/modules/dlang.py @@ -36,6 +36,9 @@ class DlangModule(ExtensionModule): def __init__(self, interpreter): super().__init__(interpreter) + self.methods.update({ + 'generate_dub_file': self.generate_dub_file, + }) def _init_dub(self): if DlangModule.class_dubbin is None: diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 58cf7e14c..caa21f7e0 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -36,6 +36,23 @@ class FSModule(ExtensionModule): def __init__(self, interpreter: 'Interpreter') -> None: super().__init__(interpreter) + self.methods.update({ + 'expanduser': self.expanduser, + 'is_absolute': self.is_absolute, + 'as_posix': self.as_posix, + 'exists': self.exists, + 'is_symlink': self.is_symlink, + 'is_file': self.is_file, + 'is_dir': self.is_dir, + 'hash': self.hash, + 'size': self.size, + 'is_samepath': self.is_samepath, + 'replace_suffix': self.replace_suffix, + 'parent': self.parent, + 'name': self.name, + 'stem': self.stem, + 'read': self.read, + }) def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path: """ diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py index 5a744fe2c..d2b64ddf4 100644 --- a/mesonbuild/modules/gnome.py +++ b/mesonbuild/modules/gnome.py @@ -57,6 +57,20 @@ class GnomeModule(ExtensionModule): self.install_gio_querymodules = [] self.install_gtk_update_icon_cache = False self.devenv = None + self.methods.update({ + 'post_install': self.post_install, + 'compile_resources': self.compile_resources, + 'generate_gir': self.generate_gir, + 'compile_schemas': self.compile_schemas, + 'yelp': self.yelp, + 'gtkdoc': self.gtkdoc, + 'gtkdoc_html_dir': self.gtkdoc_html_dir, + 'gdbus_codegen': self.gdbus_codegen, + 'mkenums': self.mkenums, + 'mkenums_simple': self.mkenums_simple, + 'genmarshal': self.genmarshal, + 'generate_vapi': self.generate_vapi, + }) @staticmethod def _get_native_glib_version(state): diff --git a/mesonbuild/modules/hotdoc.py b/mesonbuild/modules/hotdoc.py index ae8a2c6e9..90635e6b1 100644 --- a/mesonbuild/modules/hotdoc.py +++ b/mesonbuild/modules/hotdoc.py @@ -405,6 +405,10 @@ class HotDocModule(ExtensionModule): except Exception as e: raise MesonException('hotdoc {} required but not found. ({})'.format( MIN_HOTDOC_VERSION, e)) + self.methods.update({ + 'has_extensions': self.has_extensions, + 'generate_doc': self.generate_doc, + }) @noKwargs def has_extensions(self, state, args, kwargs): diff --git a/mesonbuild/modules/i18n.py b/mesonbuild/modules/i18n.py index b915745d1..98a8b0da3 100644 --- a/mesonbuild/modules/i18n.py +++ b/mesonbuild/modules/i18n.py @@ -57,6 +57,12 @@ PRESET_ARGS = { class I18nModule(ExtensionModule): + def __init__(self, interpreter): + super().__init__(interpreter) + self.methods.update({ + 'merge_file': self.merge_file, + 'gettext': self.gettext, + }) @staticmethod def nogettext_warning(): diff --git a/mesonbuild/modules/keyval.py b/mesonbuild/modules/keyval.py index 8e43491ab..d637ac0d2 100644 --- a/mesonbuild/modules/keyval.py +++ b/mesonbuild/modules/keyval.py @@ -25,6 +25,9 @@ class KeyvalModule(ExtensionModule): @FeatureNew('Keyval Module', '0.55.0') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.methods.update({ + 'load': self.load, + }) def _load_file(self, path_to_config): result = dict() diff --git a/mesonbuild/modules/modtest.py b/mesonbuild/modules/modtest.py index 7a4c19d69..dd2e2ff8d 100644 --- a/mesonbuild/modules/modtest.py +++ b/mesonbuild/modules/modtest.py @@ -16,6 +16,11 @@ from . import ExtensionModule from ..interpreterbase import noKwargs class TestModule(ExtensionModule): + def __init__(self, interpreter): + super().__init__(interpreter) + self.methods.update({ + 'print_hello': self.print_hello, + }) @noKwargs def print_hello(self, state, args, kwargs): diff --git a/mesonbuild/modules/pkgconfig.py b/mesonbuild/modules/pkgconfig.py index c9d53612d..c2f713c5a 100644 --- a/mesonbuild/modules/pkgconfig.py +++ b/mesonbuild/modules/pkgconfig.py @@ -280,6 +280,11 @@ class DependenciesHelper: self.cflags = _fn(self.cflags) class PkgConfigModule(ExtensionModule): + def __init__(self, interpreter): + super().__init__(interpreter) + self.methods.update({ + 'generate': self.generate, + }) def _get_lname(self, l, msg, pcfile, is_custom_target): if is_custom_target: diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 1efebf880..b7070dfa2 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -473,6 +473,9 @@ class PythonModule(ExtensionModule): @FeatureNew('Python Module', '0.46.0') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.methods.update({ + 'find_installation': self.find_installation, + }) # https://www.python.org/dev/peps/pep-0397/ def _get_win_pythonpath(self, name_or_path): diff --git a/mesonbuild/modules/python3.py b/mesonbuild/modules/python3.py index ac710492f..dc1f7c775 100644 --- a/mesonbuild/modules/python3.py +++ b/mesonbuild/modules/python3.py @@ -25,6 +25,12 @@ class Python3Module(ExtensionModule): @FeatureDeprecated('python3 module', '0.48.0') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.methods.update({ + 'extension_module': self.extension_module, + 'find_python': self.find_python, + 'language_version': self.language_version, + 'sysconfig_path': self.sysconfig_path, + }) @permittedKwargs(known_shmod_kwargs) def extension_module(self, state, args, kwargs): diff --git a/mesonbuild/modules/qt.py b/mesonbuild/modules/qt.py index 7455e8e3f..b03e75086 100644 --- a/mesonbuild/modules/qt.py +++ b/mesonbuild/modules/qt.py @@ -46,6 +46,11 @@ class QtBaseModule(ExtensionModule): self.uic: 'ExternalProgram' = NonExistingExternalProgram('uic') self.rcc: 'ExternalProgram' = NonExistingExternalProgram('rcc') self.lrelease: 'ExternalProgram' = NonExistingExternalProgram('lrelease') + self.methods.update({ + 'has_tools': self.has_tools, + 'preprocess': self.preprocess, + 'compile_translations': self.compile_translations, + }) def compilers_detect(self, qt_dep: 'QtBaseDependency') -> None: """Detect Qt (4 or 5) moc, uic, rcc in the specified bindir or in PATH""" diff --git a/mesonbuild/modules/rpm.py b/mesonbuild/modules/rpm.py index 7638a45d0..704e82b59 100644 --- a/mesonbuild/modules/rpm.py +++ b/mesonbuild/modules/rpm.py @@ -26,18 +26,22 @@ from ..interpreterbase import noKwargs import os class RPMModule(ExtensionModule): + def __init__(self, interpreter): + super().__init__(interpreter) + self.methods.update({ + 'generate_spec_template': self.generate_spec_template, + }) @noKwargs - def generate_spec_template(self, coredata, args, kwargs): - self.coredata = coredata - required_compilers = self.__get_required_compilers() - proj = coredata.project_name.replace(' ', '_').replace('\t', '_') + def generate_spec_template(self, state, args, kwargs): + required_compilers = self.__get_required_compilers(state) + proj = state.project_name.replace(' ', '_').replace('\t', '_') so_installed = False devel_subpkg = False files = set() files_devel = set() to_delete = set() - for target in coredata.targets.values(): + for target in state.targets.values(): if isinstance(target, build.Executable) and target.need_install: files.add('%%{_bindir}/%s' % target.get_filename()) elif isinstance(target, build.SharedLibrary) and target.need_install: @@ -56,13 +60,13 @@ class RPMModule(ExtensionModule): files_devel.add('%%{_datadir}/gir-1.0/%s' % target.get_filename()[0]) elif isinstance(target, TypelibTarget) and target.should_install(): files.add('%%{_libdir}/girepository-1.0/%s' % target.get_filename()[0]) - for header in coredata.headers: + for header in state.headers: if header.get_install_subdir(): files_devel.add('%%{_includedir}/%s/' % header.get_install_subdir()) else: for hdr_src in header.get_sources(): files_devel.add('%%{_includedir}/%s' % hdr_src) - for man in coredata.man: + for man in state.man: for man_file in man.get_sources(): if man.locale: files.add('%%{_mandir}/%s/man%u/%s.*' % (man.locale, int(man_file.split('.')[-1]), man_file)) @@ -71,7 +75,7 @@ class RPMModule(ExtensionModule): if files_devel: devel_subpkg = True - filename = os.path.join(coredata.environment.get_build_dir(), + filename = os.path.join(state.environment.get_build_dir(), '%s.spec' % proj) with open(filename, 'w+') as fn: fn.write('Name: %s\n' % proj) @@ -85,7 +89,7 @@ class RPMModule(ExtensionModule): fn.write('BuildRequires: meson\n') for compiler in required_compilers: fn.write('BuildRequires: %s\n' % compiler) - for dep in coredata.environment.coredata.deps.host: + for dep in state.environment.coredata.deps.host: fn.write('BuildRequires: pkgconfig(%s)\n' % dep[0]) # ext_libs and ext_progs have been removed from coredata so the following code # no longer works. It is kept as a reminder of the idea should anyone wish @@ -150,9 +154,9 @@ class RPMModule(ExtensionModule): fn.write('\n') mlog.log('RPM spec template written to %s.spec.\n' % proj) - def __get_required_compilers(self): + def __get_required_compilers(self, state): required_compilers = set() - for compiler in self.coredata.environment.coredata.compilers.host.values(): + for compiler in state.environment.coredata.compilers.host.values(): # Elbrus has one 'lcc' package for every compiler if isinstance(compiler, compilers.GnuCCompiler): required_compilers.add('gcc') diff --git a/mesonbuild/modules/sourceset.py b/mesonbuild/modules/sourceset.py index 134095930..e413e32ec 100644 --- a/mesonbuild/modules/sourceset.py +++ b/mesonbuild/modules/sourceset.py @@ -189,6 +189,9 @@ class SourceSetModule(ExtensionModule): @FeatureNew('SourceSet module', '0.51.0') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.methods.update({ + 'source_set': self.source_set, + }) @noKwargs @noPosargs diff --git a/mesonbuild/modules/unstable_icestorm.py b/mesonbuild/modules/unstable_icestorm.py index 874687039..be8388562 100644 --- a/mesonbuild/modules/unstable_icestorm.py +++ b/mesonbuild/modules/unstable_icestorm.py @@ -24,6 +24,9 @@ class IceStormModule(ExtensionModule): def __init__(self, interpreter): super().__init__(interpreter) self.yosys_bin = None + self.methods.update({ + 'project': self.project, + }) def detect_binaries(self, interpreter): self.yosys_bin = interpreter.find_program_impl(['yosys']) diff --git a/mesonbuild/modules/unstable_rust.py b/mesonbuild/modules/unstable_rust.py index 0f64df83e..30dea9fc8 100644 --- a/mesonbuild/modules/unstable_rust.py +++ b/mesonbuild/modules/unstable_rust.py @@ -38,6 +38,10 @@ class RustModule(ExtensionModule): def __init__(self, interpreter: 'Interpreter') -> None: super().__init__(interpreter) self._bindgen_bin: T.Optional['ExternalProgram'] = None + self.methods.update({ + 'test': self.test, + 'bindgen': self.bindgen, + }) @permittedKwargs(permitted_test_kwargs | {'dependencies'} ^ {'protocol'}) @typed_pos_args('rust.test', str, BuildTargetHolder) diff --git a/mesonbuild/modules/unstable_simd.py b/mesonbuild/modules/unstable_simd.py index 5f8edfdfe..df5faa1ae 100644 --- a/mesonbuild/modules/unstable_simd.py +++ b/mesonbuild/modules/unstable_simd.py @@ -35,6 +35,9 @@ class SimdModule(ExtensionModule): 'avx2', 'neon', ) + self.methods.update({ + 'check': self.check, + }) def check(self, state, args, kwargs): result = [] diff --git a/mesonbuild/modules/windows.py b/mesonbuild/modules/windows.py index c4fdc1966..441fb9f18 100644 --- a/mesonbuild/modules/windows.py +++ b/mesonbuild/modules/windows.py @@ -30,6 +30,11 @@ class ResourceCompilerType(enum.Enum): rc = 2 class WindowsModule(ExtensionModule): + def __init__(self, interpreter): + super().__init__(interpreter) + self.methods.update({ + 'compile_resources': self.compile_resources, + }) def detect_compiler(self, compilers): for l in ('c', 'cpp'):