From ecb076ba002afb6a6678983dd02ac53d9b751f12 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Tue, 3 Mar 2020 10:50:15 -0500 Subject: [PATCH] qt5: Add has_tools() method --- docs/markdown/Qt5-module.md | 22 ++++++++++++++++++++++ docs/markdown/snippets/qt_has_tools.md | 10 ++++++++++ mesonbuild/modules/qt.py | 21 ++++++++++++++++++++- test cases/frameworks/4 qt/meson.build | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 docs/markdown/snippets/qt_has_tools.md diff --git a/docs/markdown/Qt5-module.md b/docs/markdown/Qt5-module.md index 3a5195485..f1c2f6c00 100644 --- a/docs/markdown/Qt5-module.md +++ b/docs/markdown/Qt5-module.md @@ -23,6 +23,28 @@ This method generates the necessary targets to build translation files with lrel - `install_dir` directory to install to (optional). - `build_by_default` when set to true, to have this target be built by default, that is, when invoking plain ninja; the default value is false (optional). +## has_tools + +This method returns `true` if all tools used by this module are found, `false` +otherwise. + +It should be used to compile optional Qt code: +```meson +qt5 = import('qt5') +if qt5.has_tools(required: get_option('qt_feature')) + moc_files = qt5.preprocess(...) + ... +endif +``` + +This method takes the following keyword arguments: +- `required`: by default, `required` is set to `false`. If `required` is set to + `true` or an enabled [`feature`](Build-options.md#features) and some tools are + missing Meson will abort. +- `method`: method used to find the Qt dependency (`auto` by default). + +*Since: 0.54.0* + ## Dependencies See [Qt dependencies](Dependencies.md#qt4-qt5) diff --git a/docs/markdown/snippets/qt_has_tools.md b/docs/markdown/snippets/qt_has_tools.md new file mode 100644 index 000000000..3569ecb96 --- /dev/null +++ b/docs/markdown/snippets/qt_has_tools.md @@ -0,0 +1,10 @@ +## Added `has_tools` method to qt module + +It should be used to compile optional Qt code: +```meson +qt5 = import('qt5') +if qt5.has_tools(required: get_option('qt_feature')) + moc_files = qt5.preprocess(...) + ... +endif +``` diff --git a/mesonbuild/modules/qt.py b/mesonbuild/modules/qt.py index 6887bc744..76edb7ef1 100644 --- a/mesonbuild/modules/qt.py +++ b/mesonbuild/modules/qt.py @@ -19,7 +19,8 @@ from ..mesonlib import MesonException, Popen_safe, extract_as_list, File from ..dependencies import Dependency, Qt4Dependency, Qt5Dependency import xml.etree.ElementTree as ET from . import ModuleReturnValue, get_include_args, ExtensionModule -from ..interpreterbase import permittedKwargs, FeatureNew, FeatureNewKwargs +from ..interpreterbase import noPosargs, permittedKwargs, FeatureNew, FeatureNewKwargs +from ..interpreter import extract_required_kwarg _QT_DEPS_LUT = { 4: Qt4Dependency, @@ -32,6 +33,7 @@ class QtBaseModule(ExtensionModule): def __init__(self, interpreter, qt_version=5): ExtensionModule.__init__(self, interpreter) + self.snippets.add('has_tools') self.qt_version = qt_version def _detect_tools(self, env, method): @@ -117,6 +119,23 @@ class QtBaseModule(ExtensionModule): except Exception: return [] + @noPosargs + @permittedKwargs({'method', 'required'}) + @FeatureNew('qt.has_tools', '0.54.0') + def has_tools(self, interpreter, state, args, kwargs): + method = kwargs.get('method', 'auto') + disabled, required, feature = extract_required_kwarg(kwargs, state.subproject, default=False) + if disabled: + mlog.log('qt.has_tools skipped: feature', mlog.bold(feature), 'disabled') + return False + self._detect_tools(state.environment, method) + for tool in (self.moc, self.uic, self.rcc, self.lrelease): + if not tool.found(): + if required: + raise MesonException('Qt tools not found') + return False + return True + @FeatureNewKwargs('qt.preprocess', '0.49.0', ['uic_extra_arguments']) @FeatureNewKwargs('qt.preprocess', '0.44.0', ['moc_extra_arguments']) @FeatureNewKwargs('qt.preprocess', '0.49.0', ['rcc_extra_arguments']) diff --git a/test cases/frameworks/4 qt/meson.build b/test cases/frameworks/4 qt/meson.build index 7934572db..8f18809bd 100644 --- a/test cases/frameworks/4 qt/meson.build +++ b/test cases/frameworks/4 qt/meson.build @@ -42,6 +42,7 @@ foreach qt : ['qt4', 'qt5'] qtdep = dependency(qt, modules : qt_modules, main : true, private_headers: true, required : required, method : get_option('method')) if qtdep.found() qtmodule = import(qt) + assert(qtmodule.has_tools()) # The following has two resource files because having two in one target # requires you to do it properly or you get linker symbol clashes.