The Meson Build System
http://mesonbuild.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
619 lines
30 KiB
619 lines
30 KiB
12 months ago
|
# SPDX-License-Identifier: Apache-2.0
|
||
5 years ago
|
# Copyright 2015 The Meson development team
|
||
12 months ago
|
# Copyright © 2021-2023 Intel Corporation
|
||
5 years ago
|
|
||
3 years ago
|
from __future__ import annotations
|
||
5 years ago
|
|
||
|
import os
|
||
5 years ago
|
import shutil
|
||
4 years ago
|
import typing as T
|
||
4 years ago
|
import xml.etree.ElementTree as ET
|
||
4 years ago
|
|
||
4 years ago
|
from . import ModuleReturnValue, ExtensionModule
|
||
5 years ago
|
from .. import build
|
||
3 years ago
|
from .. import coredata
|
||
4 years ago
|
from .. import mlog
|
||
1 year ago
|
from ..dependencies import find_external_dependency, Dependency, ExternalLibrary, InternalDependency
|
||
3 years ago
|
from ..mesonlib import MesonException, File, version_compare, Popen_safe
|
||
5 years ago
|
from ..interpreter import extract_required_kwarg
|
||
3 years ago
|
from ..interpreter.type_checking import INSTALL_DIR_KW, INSTALL_KW, NoneType
|
||
4 years ago
|
from ..interpreterbase import ContainerTypeInfo, FeatureDeprecated, KwargInfo, noPosargs, FeatureNew, typed_kwargs
|
||
3 years ago
|
from ..programs import NonExistingExternalProgram
|
||
5 years ago
|
|
||
4 years ago
|
if T.TYPE_CHECKING:
|
||
4 years ago
|
from . import ModuleState
|
||
4 years ago
|
from ..dependencies.qt import QtPkgConfigDependency, QmakeQtDependency
|
||
4 years ago
|
from ..interpreter import Interpreter
|
||
4 years ago
|
from ..interpreter import kwargs
|
||
3 years ago
|
from ..mesonlib import FileOrString
|
||
|
from ..programs import ExternalProgram
|
||
4 years ago
|
|
||
4 years ago
|
QtDependencyType = T.Union[QtPkgConfigDependency, QmakeQtDependency]
|
||
|
|
||
4 years ago
|
from typing_extensions import TypedDict
|
||
|
|
||
|
class ResourceCompilerKwArgs(TypedDict):
|
||
|
|
||
|
"""Keyword arguments for the Resource Compiler method."""
|
||
|
|
||
|
name: T.Optional[str]
|
||
3 years ago
|
sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
|
||
4 years ago
|
extra_args: T.List[str]
|
||
|
method: str
|
||
|
|
||
4 years ago
|
class UICompilerKwArgs(TypedDict):
|
||
|
|
||
|
"""Keyword arguments for the Ui Compiler method."""
|
||
|
|
||
3 years ago
|
sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
|
||
4 years ago
|
extra_args: T.List[str]
|
||
|
method: str
|
||
2 years ago
|
preserve_paths: bool
|
||
4 years ago
|
|
||
4 years ago
|
class MocCompilerKwArgs(TypedDict):
|
||
|
|
||
|
"""Keyword arguments for the Moc Compiler method."""
|
||
|
|
||
3 years ago
|
sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
|
||
|
headers: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
|
||
4 years ago
|
extra_args: T.List[str]
|
||
|
method: str
|
||
3 years ago
|
include_directories: T.List[T.Union[str, build.IncludeDirs]]
|
||
|
dependencies: T.List[T.Union[Dependency, ExternalLibrary]]
|
||
2 years ago
|
preserve_paths: bool
|
||
4 years ago
|
|
||
|
class PreprocessKwArgs(TypedDict):
|
||
|
|
||
3 years ago
|
sources: T.List[FileOrString]
|
||
3 years ago
|
moc_sources: T.List[T.Union[FileOrString, build.CustomTarget]]
|
||
|
moc_headers: T.List[T.Union[FileOrString, build.CustomTarget]]
|
||
3 years ago
|
qresources: T.List[FileOrString]
|
||
3 years ago
|
ui_files: T.List[T.Union[FileOrString, build.CustomTarget]]
|
||
4 years ago
|
moc_extra_arguments: T.List[str]
|
||
|
rcc_extra_arguments: T.List[str]
|
||
|
uic_extra_arguments: T.List[str]
|
||
3 years ago
|
include_directories: T.List[T.Union[str, build.IncludeDirs]]
|
||
|
dependencies: T.List[T.Union[Dependency, ExternalLibrary]]
|
||
4 years ago
|
method: str
|
||
2 years ago
|
preserve_paths: bool
|
||
4 years ago
|
|
||
4 years ago
|
class HasToolKwArgs(kwargs.ExtractRequired):
|
||
|
|
||
|
method: str
|
||
|
|
||
4 years ago
|
class CompileTranslationsKwArgs(TypedDict):
|
||
|
|
||
|
build_by_default: bool
|
||
|
install: bool
|
||
|
install_dir: T.Optional[str]
|
||
|
method: str
|
||
|
qresource: T.Optional[str]
|
||
|
rcc_extra_arguments: T.List[str]
|
||
3 years ago
|
ts_files: T.List[T.Union[str, File, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
|
||
4 years ago
|
|
||
5 years ago
|
class QtBaseModule(ExtensionModule):
|
||
4 years ago
|
_tools_detected = False
|
||
|
_rcc_supports_depfiles = False
|
||
3 years ago
|
_moc_supports_depfiles = False
|
||
5 years ago
|
|
||
4 years ago
|
def __init__(self, interpreter: 'Interpreter', qt_version: int = 5):
|
||
5 years ago
|
ExtensionModule.__init__(self, interpreter)
|
||
|
self.qt_version = qt_version
|
||
3 years ago
|
# It is important that this list does not change order as the order of
|
||
|
# the returned ExternalPrograms will change as well
|
||
2 years ago
|
self.tools: T.Dict[str, T.Union[ExternalProgram, build.Executable]] = {
|
||
3 years ago
|
'moc': NonExistingExternalProgram('moc'),
|
||
|
'uic': NonExistingExternalProgram('uic'),
|
||
|
'rcc': NonExistingExternalProgram('rcc'),
|
||
|
'lrelease': NonExistingExternalProgram('lrelease'),
|
||
|
}
|
||
4 years ago
|
self.methods.update({
|
||
|
'has_tools': self.has_tools,
|
||
|
'preprocess': self.preprocess,
|
||
|
'compile_translations': self.compile_translations,
|
||
4 years ago
|
'compile_resources': self.compile_resources,
|
||
4 years ago
|
'compile_ui': self.compile_ui,
|
||
4 years ago
|
'compile_moc': self.compile_moc,
|
||
4 years ago
|
})
|
||
4 years ago
|
|
||
4 years ago
|
def compilers_detect(self, state: 'ModuleState', qt_dep: 'QtDependencyType') -> None:
|
||
4 years ago
|
"""Detect Qt (4 or 5) moc, uic, rcc in the specified bindir or in PATH"""
|
||
|
wanted = f'== {qt_dep.version}'
|
||
|
|
||
|
def gen_bins() -> T.Generator[T.Tuple[str, str], None, None]:
|
||
3 years ago
|
for b in self.tools:
|
||
4 years ago
|
if qt_dep.bindir:
|
||
|
yield os.path.join(qt_dep.bindir, b), b
|
||
3 years ago
|
if qt_dep.libexecdir:
|
||
|
yield os.path.join(qt_dep.libexecdir, b), b
|
||
3 years ago
|
# prefer the (official) <tool><version> or (unofficial) <tool>-qt<version>
|
||
|
# of the tool to the plain one, as we
|
||
4 years ago
|
# don't know what the unsuffixed one points to without calling it.
|
||
3 years ago
|
yield f'{b}{qt_dep.qtver}', b
|
||
4 years ago
|
yield f'{b}-qt{qt_dep.qtver}', b
|
||
4 years ago
|
yield b, b
|
||
|
|
||
|
for b, name in gen_bins():
|
||
3 years ago
|
if self.tools[name].found():
|
||
4 years ago
|
continue
|
||
|
|
||
|
if name == 'lrelease':
|
||
|
arg = ['-version']
|
||
3 years ago
|
elif version_compare(qt_dep.version, '>= 5'):
|
||
4 years ago
|
arg = ['--version']
|
||
|
else:
|
||
|
arg = ['-v']
|
||
|
|
||
|
# Ensure that the version of qt and each tool are the same
|
||
2 years ago
|
def get_version(p: T.Union[ExternalProgram, build.Executable]) -> str:
|
||
3 years ago
|
_, out, err = Popen_safe(p.get_command() + arg)
|
||
2 years ago
|
if name == 'lrelease' or not qt_dep.version.startswith('4'):
|
||
4 years ago
|
care = out
|
||
|
else:
|
||
|
care = err
|
||
2 years ago
|
return care.rsplit(' ', maxsplit=1)[-1].replace(')', '').strip()
|
||
4 years ago
|
|
||
4 years ago
|
p = state.find_program(b, required=False,
|
||
|
version_func=get_version,
|
||
3 years ago
|
wanted=wanted)
|
||
4 years ago
|
if p.found():
|
||
3 years ago
|
self.tools[name] = p
|
||
5 years ago
|
|
||
4 years ago
|
def _detect_tools(self, state: 'ModuleState', method: str, required: bool = True) -> None:
|
||
4 years ago
|
if self._tools_detected:
|
||
5 years ago
|
return
|
||
4 years ago
|
self._tools_detected = True
|
||
4 years ago
|
mlog.log(f'Detecting Qt{self.qt_version} tools')
|
||
4 years ago
|
kwargs = {'required': required, 'modules': 'Core', 'method': method}
|
||
4 years ago
|
# Just pick one to make mypy happy
|
||
|
qt = T.cast('QtPkgConfigDependency', find_external_dependency(f'qt{self.qt_version}', state.environment, kwargs))
|
||
4 years ago
|
if qt.found():
|
||
|
# Get all tools and then make sure that they are the right version
|
||
4 years ago
|
self.compilers_detect(state, qt)
|
||
3 years ago
|
if version_compare(qt.version, '>=5.15.0'):
|
||
3 years ago
|
self._moc_supports_depfiles = True
|
||
|
else:
|
||
|
mlog.warning('moc dependencies will not work properly until you move to Qt >= 5.15', fatal=False)
|
||
3 years ago
|
if version_compare(qt.version, '>=5.14.0'):
|
||
4 years ago
|
self._rcc_supports_depfiles = True
|
||
4 years ago
|
else:
|
||
|
mlog.warning('rcc dependencies will not work properly until you move to Qt >= 5.14:',
|
||
3 years ago
|
mlog.bold('https://bugreports.qt.io/browse/QTBUG-45460'), fatal=False)
|
||
4 years ago
|
else:
|
||
4 years ago
|
suffix = f'-qt{self.qt_version}'
|
||
3 years ago
|
self.tools['moc'] = NonExistingExternalProgram(name='moc' + suffix)
|
||
|
self.tools['uic'] = NonExistingExternalProgram(name='uic' + suffix)
|
||
|
self.tools['rcc'] = NonExistingExternalProgram(name='rcc' + suffix)
|
||
|
self.tools['lrelease'] = NonExistingExternalProgram(name='lrelease' + suffix)
|
||
5 years ago
|
|
||
4 years ago
|
@staticmethod
|
||
3 years ago
|
def _qrc_nodes(state: 'ModuleState', rcc_file: 'FileOrString') -> T.Tuple[str, T.List[str]]:
|
||
4 years ago
|
abspath: str
|
||
|
if isinstance(rcc_file, str):
|
||
5 years ago
|
abspath = os.path.join(state.environment.source_dir, state.subdir, rcc_file)
|
||
4 years ago
|
else:
|
||
5 years ago
|
abspath = rcc_file.absolute_path(state.environment.source_dir, state.environment.build_dir)
|
||
2 years ago
|
rcc_dirname = os.path.dirname(abspath)
|
||
5 years ago
|
|
||
2 years ago
|
# FIXME: what error are we actually trying to check here? (probably parse errors?)
|
||
5 years ago
|
try:
|
||
|
tree = ET.parse(abspath)
|
||
|
root = tree.getroot()
|
||
4 years ago
|
result: T.List[str] = []
|
||
5 years ago
|
for child in root[0]:
|
||
|
if child.tag != 'file':
|
||
4 years ago
|
mlog.warning("malformed rcc file: ", os.path.join(state.subdir, str(rcc_file)))
|
||
5 years ago
|
break
|
||
2 years ago
|
elif child.text is None:
|
||
|
raise MesonException(f'<file> element without a path in {os.path.join(state.subdir, str(rcc_file))}')
|
||
5 years ago
|
else:
|
||
5 years ago
|
result.append(child.text)
|
||
|
|
||
|
return rcc_dirname, result
|
||
2 years ago
|
except MesonException:
|
||
|
raise
|
||
5 years ago
|
except Exception:
|
||
4 years ago
|
raise MesonException(f'Unable to parse resource file {abspath}')
|
||
5 years ago
|
|
||
3 years ago
|
def _parse_qrc_deps(self, state: 'ModuleState',
|
||
|
rcc_file_: T.Union['FileOrString', build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]) -> T.List[File]:
|
||
4 years ago
|
result: T.List[File] = []
|
||
3 years ago
|
inputs: T.Sequence['FileOrString'] = []
|
||
|
if isinstance(rcc_file_, (str, File)):
|
||
|
inputs = [rcc_file_]
|
||
|
else:
|
||
|
inputs = rcc_file_.get_outputs()
|
||
|
|
||
|
for rcc_file in inputs:
|
||
|
rcc_dirname, nodes = self._qrc_nodes(state, rcc_file)
|
||
|
for resource_path in nodes:
|
||
|
# We need to guess if the pointed resource is:
|
||
|
# a) in build directory -> implies a generated file
|
||
|
# b) in source directory
|
||
|
# c) somewhere else external dependency file to bundle
|
||
|
#
|
||
|
# Also from qrc documentation: relative path are always from qrc file
|
||
|
# So relative path must always be computed from qrc file !
|
||
|
if os.path.isabs(resource_path):
|
||
|
# a)
|
||
|
if resource_path.startswith(os.path.abspath(state.environment.build_dir)):
|
||
|
resource_relpath = os.path.relpath(resource_path, state.environment.build_dir)
|
||
|
result.append(File(is_built=True, subdir='', fname=resource_relpath))
|
||
|
# either b) or c)
|
||
|
else:
|
||
|
result.append(File(is_built=False, subdir=state.subdir, fname=resource_path))
|
||
4 years ago
|
else:
|
||
3 years ago
|
path_from_rcc = os.path.normpath(os.path.join(rcc_dirname, resource_path))
|
||
|
# a)
|
||
|
if path_from_rcc.startswith(state.environment.build_dir):
|
||
|
result.append(File(is_built=True, subdir=state.subdir, fname=resource_path))
|
||
|
# b)
|
||
|
else:
|
||
|
result.append(File(is_built=False, subdir=state.subdir, fname=path_from_rcc))
|
||
5 years ago
|
return result
|
||
5 years ago
|
|
||
4 years ago
|
@FeatureNew('qt.has_tools', '0.54.0')
|
||
5 years ago
|
@noPosargs
|
||
4 years ago
|
@typed_kwargs(
|
||
|
'qt.has_tools',
|
||
3 years ago
|
KwargInfo('required', (bool, coredata.UserFeatureOption), default=False),
|
||
4 years ago
|
KwargInfo('method', str, default='auto'),
|
||
|
)
|
||
|
def has_tools(self, state: 'ModuleState', args: T.Tuple, kwargs: 'HasToolKwArgs') -> bool:
|
||
5 years ago
|
method = kwargs.get('method', 'auto')
|
||
4 years ago
|
# We have to cast here because TypedDicts are invariant, even though
|
||
|
# ExtractRequiredKwArgs is a subset of HasToolKwArgs, type checkers
|
||
|
# will insist this is wrong
|
||
5 years ago
|
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
|
||
4 years ago
|
self._detect_tools(state, method, required=False)
|
||
3 years ago
|
for tool in self.tools.values():
|
||
5 years ago
|
if not tool.found():
|
||
|
if required:
|
||
|
raise MesonException('Qt tools not found')
|
||
|
return False
|
||
|
return True
|
||
|
|
||
4 years ago
|
@FeatureNew('qt.compile_resources', '0.59.0')
|
||
|
@noPosargs
|
||
|
@typed_kwargs(
|
||
|
'qt.compile_resources',
|
||
3 years ago
|
KwargInfo('name', (str, NoneType)),
|
||
3 years ago
|
KwargInfo(
|
||
|
'sources',
|
||
|
ContainerTypeInfo(list, (File, str, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList), allow_empty=False),
|
||
|
listify=True,
|
||
|
required=True,
|
||
|
),
|
||
4 years ago
|
KwargInfo('extra_args', ContainerTypeInfo(list, str), listify=True, default=[]),
|
||
4 years ago
|
KwargInfo('method', str, default='auto')
|
||
|
)
|
||
4 years ago
|
def compile_resources(self, state: 'ModuleState', args: T.Tuple, kwargs: 'ResourceCompilerKwArgs') -> ModuleReturnValue:
|
||
4 years ago
|
"""Compile Qt resources files.
|
||
|
|
||
|
Uses CustomTargets to generate .cpp files from .qrc files.
|
||
|
"""
|
||
3 years ago
|
if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in kwargs['sources']):
|
||
3 years ago
|
FeatureNew.single_use('qt.compile_resources: custom_target or generator for "sources" keyword argument',
|
||
|
'0.60.0', state.subproject, location=state.current_node)
|
||
3 years ago
|
out = self._compile_resources_impl(state, kwargs)
|
||
|
return ModuleReturnValue(out, [out])
|
||
|
|
||
|
def _compile_resources_impl(self, state: 'ModuleState', kwargs: 'ResourceCompilerKwArgs') -> T.List[build.CustomTarget]:
|
||
|
# Avoid the FeatureNew when dispatching from preprocess
|
||
4 years ago
|
self._detect_tools(state, kwargs['method'])
|
||
3 years ago
|
if not self.tools['rcc'].found():
|
||
4 years ago
|
err_msg = ("{0} sources specified and couldn't find {1}, "
|
||
|
"please check your qt{2} installation")
|
||
|
raise MesonException(err_msg.format('RCC', f'rcc-qt{self.qt_version}', self.qt_version))
|
||
|
|
||
|
# List of generated CustomTargets
|
||
|
targets: T.List[build.CustomTarget] = []
|
||
|
|
||
|
# depfile arguments
|
||
4 years ago
|
DEPFILE_ARGS: T.List[str] = ['--depfile', '@DEPFILE@'] if self._rcc_supports_depfiles else []
|
||
4 years ago
|
|
||
|
name = kwargs['name']
|
||
3 years ago
|
sources: T.List['FileOrString'] = []
|
||
|
for s in kwargs['sources']:
|
||
|
if isinstance(s, (str, File)):
|
||
|
sources.append(s)
|
||
|
else:
|
||
|
sources.extend(s.get_outputs())
|
||
4 years ago
|
extra_args = kwargs['extra_args']
|
||
4 years ago
|
|
||
|
# If a name was set generate a single .cpp file from all of the qrc
|
||
|
# files, otherwise generate one .cpp file per qrc file.
|
||
|
if name:
|
||
|
qrc_deps: T.List[File] = []
|
||
|
for s in sources:
|
||
|
qrc_deps.extend(self._parse_qrc_deps(state, s))
|
||
|
|
||
3 years ago
|
res_target = build.CustomTarget(
|
||
|
name,
|
||
|
state.subdir,
|
||
|
state.subproject,
|
||
3 years ago
|
state.environment,
|
||
3 years ago
|
self.tools['rcc'].get_command() + ['-name', name, '-o', '@OUTPUT@'] + extra_args + ['@INPUT@'] + DEPFILE_ARGS,
|
||
|
sources,
|
||
|
[f'{name}.cpp'],
|
||
|
depend_files=qrc_deps,
|
||
|
depfile=f'{name}.d',
|
||
1 year ago
|
description='Compiling Qt resources {}',
|
||
3 years ago
|
)
|
||
4 years ago
|
targets.append(res_target)
|
||
|
else:
|
||
|
for rcc_file in sources:
|
||
|
qrc_deps = self._parse_qrc_deps(state, rcc_file)
|
||
|
if isinstance(rcc_file, str):
|
||
|
basename = os.path.basename(rcc_file)
|
||
|
else:
|
||
|
basename = os.path.basename(rcc_file.fname)
|
||
|
name = f'qt{self.qt_version}-{basename.replace(".", "_")}'
|
||
3 years ago
|
res_target = build.CustomTarget(
|
||
|
name,
|
||
|
state.subdir,
|
||
|
state.subproject,
|
||
3 years ago
|
state.environment,
|
||
3 years ago
|
self.tools['rcc'].get_command() + ['-name', '@BASENAME@', '-o', '@OUTPUT@'] + extra_args + ['@INPUT@'] + DEPFILE_ARGS,
|
||
|
[rcc_file],
|
||
|
[f'{name}.cpp'],
|
||
|
depend_files=qrc_deps,
|
||
|
depfile=f'{name}.d',
|
||
1 year ago
|
description='Compiling Qt resources {}',
|
||
3 years ago
|
)
|
||
4 years ago
|
targets.append(res_target)
|
||
|
|
||
3 years ago
|
return targets
|
||
4 years ago
|
|
||
4 years ago
|
@FeatureNew('qt.compile_ui', '0.59.0')
|
||
|
@noPosargs
|
||
|
@typed_kwargs(
|
||
|
'qt.compile_ui',
|
||
3 years ago
|
KwargInfo(
|
||
|
'sources',
|
||
|
ContainerTypeInfo(list, (File, str, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList), allow_empty=False),
|
||
|
listify=True,
|
||
|
required=True,
|
||
|
),
|
||
4 years ago
|
KwargInfo('extra_args', ContainerTypeInfo(list, str), listify=True, default=[]),
|
||
2 years ago
|
KwargInfo('method', str, default='auto'),
|
||
|
KwargInfo('preserve_paths', bool, default=False, since='1.4.0'),
|
||
4 years ago
|
)
|
||
2 years ago
|
def compile_ui(self, state: ModuleState, args: T.Tuple, kwargs: UICompilerKwArgs) -> ModuleReturnValue:
|
||
4 years ago
|
"""Compile UI resources into cpp headers."""
|
||
3 years ago
|
if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in kwargs['sources']):
|
||
3 years ago
|
FeatureNew.single_use('qt.compile_ui: custom_target or generator for "sources" keyword argument',
|
||
|
'0.60.0', state.subproject, location=state.current_node)
|
||
3 years ago
|
out = self._compile_ui_impl(state, kwargs)
|
||
|
return ModuleReturnValue(out, [out])
|
||
|
|
||
2 years ago
|
def _compile_ui_impl(self, state: ModuleState, kwargs: UICompilerKwArgs) -> build.GeneratedList:
|
||
3 years ago
|
# Avoid the FeatureNew when dispatching from preprocess
|
||
4 years ago
|
self._detect_tools(state, kwargs['method'])
|
||
3 years ago
|
if not self.tools['uic'].found():
|
||
4 years ago
|
err_msg = ("{0} sources specified and couldn't find {1}, "
|
||
|
"please check your qt{2} installation")
|
||
|
raise MesonException(err_msg.format('UIC', f'uic-qt{self.qt_version}', self.qt_version))
|
||
|
|
||
2 years ago
|
preserve_path_from = os.path.join(state.source_root, state.subdir) if kwargs['preserve_paths'] else None
|
||
4 years ago
|
# TODO: This generator isn't added to the generator list in the Interpreter
|
||
4 years ago
|
gen = build.Generator(
|
||
3 years ago
|
self.tools['uic'],
|
||
4 years ago
|
kwargs['extra_args'] + ['-o', '@OUTPUT@', '@INPUT@'],
|
||
4 years ago
|
['ui_@BASENAME@.h'],
|
||
|
name=f'Qt{self.qt_version} ui')
|
||
2 years ago
|
return gen.process_files(kwargs['sources'], state, preserve_path_from)
|
||
4 years ago
|
|
||
4 years ago
|
@FeatureNew('qt.compile_moc', '0.59.0')
|
||
|
@noPosargs
|
||
|
@typed_kwargs(
|
||
|
'qt.compile_moc',
|
||
3 years ago
|
KwargInfo(
|
||
|
'sources',
|
||
|
ContainerTypeInfo(list, (File, str, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)),
|
||
|
listify=True,
|
||
|
default=[],
|
||
|
),
|
||
|
KwargInfo(
|
||
|
'headers',
|
||
|
ContainerTypeInfo(list, (File, str, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)),
|
||
|
listify=True,
|
||
|
default=[]
|
||
|
),
|
||
4 years ago
|
KwargInfo('extra_args', ContainerTypeInfo(list, str), listify=True, default=[]),
|
||
4 years ago
|
KwargInfo('method', str, default='auto'),
|
||
3 years ago
|
KwargInfo('include_directories', ContainerTypeInfo(list, (build.IncludeDirs, str)), listify=True, default=[]),
|
||
|
KwargInfo('dependencies', ContainerTypeInfo(list, (Dependency, ExternalLibrary)), listify=True, default=[]),
|
||
2 years ago
|
KwargInfo('preserve_paths', bool, default=False, since='1.4.0'),
|
||
4 years ago
|
)
|
||
2 years ago
|
def compile_moc(self, state: ModuleState, args: T.Tuple, kwargs: MocCompilerKwArgs) -> ModuleReturnValue:
|
||
3 years ago
|
if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in kwargs['headers']):
|
||
3 years ago
|
FeatureNew.single_use('qt.compile_moc: custom_target or generator for "headers" keyword argument',
|
||
|
'0.60.0', state.subproject, location=state.current_node)
|
||
3 years ago
|
if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in kwargs['sources']):
|
||
3 years ago
|
FeatureNew.single_use('qt.compile_moc: custom_target or generator for "sources" keyword argument',
|
||
|
'0.60.0', state.subproject, location=state.current_node)
|
||
3 years ago
|
out = self._compile_moc_impl(state, kwargs)
|
||
|
return ModuleReturnValue(out, [out])
|
||
|
|
||
2 years ago
|
def _compile_moc_impl(self, state: ModuleState, kwargs: MocCompilerKwArgs) -> T.List[build.GeneratedList]:
|
||
3 years ago
|
# Avoid the FeatureNew when dispatching from preprocess
|
||
4 years ago
|
self._detect_tools(state, kwargs['method'])
|
||
3 years ago
|
if not self.tools['moc'].found():
|
||
4 years ago
|
err_msg = ("{0} sources specified and couldn't find {1}, "
|
||
|
"please check your qt{2} installation")
|
||
|
raise MesonException(err_msg.format('MOC', f'uic-qt{self.qt_version}', self.qt_version))
|
||
|
|
||
|
if not (kwargs['headers'] or kwargs['sources']):
|
||
3 years ago
|
raise build.InvalidArguments('At least one of the "headers" or "sources" keyword arguments must be provided and not empty')
|
||
4 years ago
|
|
||
4 years ago
|
inc = state.get_include_args(include_dirs=kwargs['include_directories'])
|
||
4 years ago
|
compile_args: T.List[str] = []
|
||
4 years ago
|
for dep in kwargs['dependencies']:
|
||
1 year ago
|
compile_args.extend(a for a in dep.get_all_compile_args() if a.startswith(('-I', '-D')))
|
||
|
if isinstance(dep, InternalDependency):
|
||
|
for incl in dep.include_directories:
|
||
|
compile_args.extend(f'-I{i}' for i in incl.to_string_list(self.interpreter.source_root, self.interpreter.environment.build_dir))
|
||
4 years ago
|
|
||
|
output: T.List[build.GeneratedList] = []
|
||
|
|
||
3 years ago
|
# depfile arguments (defaults to <output-name>.d)
|
||
|
DEPFILE_ARGS: T.List[str] = ['--output-dep-file'] if self._moc_supports_depfiles else []
|
||
|
|
||
|
arguments = kwargs['extra_args'] + DEPFILE_ARGS + inc + compile_args + ['@INPUT@', '-o', '@OUTPUT@']
|
||
2 years ago
|
preserve_path_from = os.path.join(state.source_root, state.subdir) if kwargs['preserve_paths'] else None
|
||
4 years ago
|
if kwargs['headers']:
|
||
4 years ago
|
moc_gen = build.Generator(
|
||
3 years ago
|
self.tools['moc'], arguments, ['moc_@BASENAME@.cpp'],
|
||
3 years ago
|
depfile='moc_@BASENAME@.cpp.d',
|
||
4 years ago
|
name=f'Qt{self.qt_version} moc header')
|
||
2 years ago
|
output.append(moc_gen.process_files(kwargs['headers'], state, preserve_path_from))
|
||
4 years ago
|
if kwargs['sources']:
|
||
4 years ago
|
moc_gen = build.Generator(
|
||
3 years ago
|
self.tools['moc'], arguments, ['@BASENAME@.moc'],
|
||
1 year ago
|
depfile='@BASENAME@.moc.d',
|
||
4 years ago
|
name=f'Qt{self.qt_version} moc source')
|
||
2 years ago
|
output.append(moc_gen.process_files(kwargs['sources'], state, preserve_path_from))
|
||
4 years ago
|
|
||
3 years ago
|
return output
|
||
4 years ago
|
|
||
3 years ago
|
# We can't use typed_pos_args here, the signature is ambiguous
|
||
4 years ago
|
@typed_kwargs(
|
||
|
'qt.preprocess',
|
||
4 years ago
|
KwargInfo('sources', ContainerTypeInfo(list, (File, str)), listify=True, default=[], deprecated='0.59.0'),
|
||
4 years ago
|
KwargInfo('qresources', ContainerTypeInfo(list, (File, str)), listify=True, default=[]),
|
||
3 years ago
|
KwargInfo('ui_files', ContainerTypeInfo(list, (File, str, build.CustomTarget)), listify=True, default=[]),
|
||
|
KwargInfo('moc_sources', ContainerTypeInfo(list, (File, str, build.CustomTarget)), listify=True, default=[]),
|
||
|
KwargInfo('moc_headers', ContainerTypeInfo(list, (File, str, build.CustomTarget)), listify=True, default=[]),
|
||
4 years ago
|
KwargInfo('moc_extra_arguments', ContainerTypeInfo(list, str), listify=True, default=[], since='0.44.0'),
|
||
|
KwargInfo('rcc_extra_arguments', ContainerTypeInfo(list, str), listify=True, default=[], since='0.49.0'),
|
||
|
KwargInfo('uic_extra_arguments', ContainerTypeInfo(list, str), listify=True, default=[], since='0.49.0'),
|
||
4 years ago
|
KwargInfo('method', str, default='auto'),
|
||
3 years ago
|
KwargInfo('include_directories', ContainerTypeInfo(list, (build.IncludeDirs, str)), listify=True, default=[]),
|
||
|
KwargInfo('dependencies', ContainerTypeInfo(list, (Dependency, ExternalLibrary)), listify=True, default=[]),
|
||
2 years ago
|
KwargInfo('preserve_paths', bool, default=False, since='1.4.0'),
|
||
4 years ago
|
)
|
||
2 years ago
|
def preprocess(self, state: ModuleState, args: T.List[T.Union[str, File]], kwargs: PreprocessKwArgs) -> ModuleReturnValue:
|
||
4 years ago
|
_sources = args[1:]
|
||
|
if _sources:
|
||
3 years ago
|
FeatureDeprecated.single_use('qt.preprocess positional sources', '0.59', state.subproject, location=state.current_node)
|
||
3 years ago
|
# List is invariant, os we have to cast...
|
||
3 years ago
|
sources = T.cast('T.List[T.Union[str, File, build.GeneratedList, build.CustomTarget]]',
|
||
3 years ago
|
_sources + kwargs['sources'])
|
||
4 years ago
|
for s in sources:
|
||
|
if not isinstance(s, (str, File)):
|
||
|
raise build.InvalidArguments('Variadic arguments to qt.preprocess must be Strings or Files')
|
||
|
method = kwargs['method']
|
||
4 years ago
|
|
||
4 years ago
|
if kwargs['qresources']:
|
||
5 years ago
|
# custom output name set? -> one output file, multiple otherwise
|
||
2 years ago
|
rcc_kwargs: ResourceCompilerKwArgs = {'name': '', 'sources': kwargs['qresources'], 'extra_args': kwargs['rcc_extra_arguments'], 'method': method}
|
||
5 years ago
|
if args:
|
||
3 years ago
|
name = args[0]
|
||
|
if not isinstance(name, str):
|
||
4 years ago
|
raise build.InvalidArguments('First argument to qt.preprocess must be a string')
|
||
3 years ago
|
rcc_kwargs['name'] = name
|
||
|
sources.extend(self._compile_resources_impl(state, rcc_kwargs))
|
||
4 years ago
|
|
||
4 years ago
|
if kwargs['ui_files']:
|
||
2 years ago
|
ui_kwargs: UICompilerKwArgs = {
|
||
|
'sources': kwargs['ui_files'],
|
||
|
'extra_args': kwargs['uic_extra_arguments'],
|
||
|
'method': method,
|
||
|
'preserve_paths': kwargs['preserve_paths'],
|
||
|
}
|
||
3 years ago
|
sources.append(self._compile_ui_impl(state, ui_kwargs))
|
||
4 years ago
|
|
||
4 years ago
|
if kwargs['moc_headers'] or kwargs['moc_sources']:
|
||
2 years ago
|
moc_kwargs: MocCompilerKwArgs = {
|
||
4 years ago
|
'extra_args': kwargs['moc_extra_arguments'],
|
||
|
'sources': kwargs['moc_sources'],
|
||
|
'headers': kwargs['moc_headers'],
|
||
|
'include_directories': kwargs['include_directories'],
|
||
|
'dependencies': kwargs['dependencies'],
|
||
|
'method': method,
|
||
2 years ago
|
'preserve_paths': kwargs['preserve_paths'],
|
||
4 years ago
|
}
|
||
3 years ago
|
sources.extend(self._compile_moc_impl(state, moc_kwargs))
|
||
4 years ago
|
|
||
4 years ago
|
return ModuleReturnValue(sources, [sources])
|
||
5 years ago
|
|
||
|
@FeatureNew('qt.compile_translations', '0.44.0')
|
||
4 years ago
|
@noPosargs
|
||
4 years ago
|
@typed_kwargs(
|
||
|
'qt.compile_translations',
|
||
|
KwargInfo('build_by_default', bool, default=False),
|
||
3 years ago
|
INSTALL_KW,
|
||
|
INSTALL_DIR_KW,
|
||
4 years ago
|
KwargInfo('method', str, default='auto'),
|
||
3 years ago
|
KwargInfo('qresource', (str, NoneType), since='0.56.0'),
|
||
4 years ago
|
KwargInfo('rcc_extra_arguments', ContainerTypeInfo(list, str), listify=True, default=[], since='0.56.0'),
|
||
3 years ago
|
KwargInfo('ts_files', ContainerTypeInfo(list, (str, File, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)), listify=True, default=[]),
|
||
4 years ago
|
)
|
||
|
def compile_translations(self, state: 'ModuleState', args: T.Tuple, kwargs: 'CompileTranslationsKwArgs') -> ModuleReturnValue:
|
||
|
ts_files = kwargs['ts_files']
|
||
3 years ago
|
if any(isinstance(s, (build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)) for s in ts_files):
|
||
3 years ago
|
FeatureNew.single_use('qt.compile_translations: custom_target or generator for "ts_files" keyword argument',
|
||
|
'0.60.0', state.subproject, location=state.current_node)
|
||
3 years ago
|
if kwargs['install'] and not kwargs['install_dir']:
|
||
|
raise MesonException('qt.compile_translations: "install_dir" keyword argument must be set when "install" is true.')
|
||
4 years ago
|
qresource = kwargs['qresource']
|
||
5 years ago
|
if qresource:
|
||
|
if ts_files:
|
||
|
raise MesonException('qt.compile_translations: Cannot specify both ts_files and qresource')
|
||
|
if os.path.dirname(qresource) != '':
|
||
|
raise MesonException('qt.compile_translations: qresource file name must not contain a subdirectory.')
|
||
4 years ago
|
qresource_file = File.from_built_file(state.subdir, qresource)
|
||
|
infile_abs = os.path.join(state.environment.source_dir, qresource_file.relative_name())
|
||
|
outfile_abs = os.path.join(state.environment.build_dir, qresource_file.relative_name())
|
||
5 years ago
|
os.makedirs(os.path.dirname(outfile_abs), exist_ok=True)
|
||
|
shutil.copy2(infile_abs, outfile_abs)
|
||
|
self.interpreter.add_build_def_file(infile_abs)
|
||
|
|
||
4 years ago
|
_, nodes = self._qrc_nodes(state, qresource_file)
|
||
5 years ago
|
for c in nodes:
|
||
|
if c.endswith('.qm'):
|
||
4 years ago
|
ts_files.append(c.rstrip('.qm') + '.ts')
|
||
5 years ago
|
else:
|
||
4 years ago
|
raise MesonException(f'qt.compile_translations: qresource can only contain qm files, found {c}')
|
||
3 years ago
|
results = self.preprocess(state, [], {'qresources': qresource_file, 'rcc_extra_arguments': kwargs['rcc_extra_arguments']})
|
||
4 years ago
|
self._detect_tools(state, kwargs['method'])
|
||
|
translations: T.List[build.CustomTarget] = []
|
||
5 years ago
|
for ts in ts_files:
|
||
3 years ago
|
if not self.tools['lrelease'].found():
|
||
5 years ago
|
raise MesonException('qt.compile_translations: ' +
|
||
3 years ago
|
self.tools['lrelease'].name + ' not found')
|
||
5 years ago
|
if qresource:
|
||
3 years ago
|
# In this case we know that ts_files is always a List[str], as
|
||
|
# it's generated above and no ts_files are passed in. However,
|
||
|
# mypy can't figure that out so we use assert to assure it that
|
||
|
# what we're doing is safe
|
||
|
assert isinstance(ts, str), 'for mypy'
|
||
5 years ago
|
outdir = os.path.dirname(os.path.normpath(os.path.join(state.subdir, ts)))
|
||
|
ts = os.path.basename(ts)
|
||
|
else:
|
||
|
outdir = state.subdir
|
||
2 years ago
|
cmd: T.List[T.Union[ExternalProgram, build.Executable, str]] = [self.tools['lrelease'], '@INPUT@', '-qm', '@OUTPUT@']
|
||
3 years ago
|
lrelease_target = build.CustomTarget(
|
||
|
f'qt{self.qt_version}-compile-{ts}',
|
||
|
outdir,
|
||
|
state.subproject,
|
||
3 years ago
|
state.environment,
|
||
3 years ago
|
cmd,
|
||
|
[ts],
|
||
|
['@BASENAME@.qm'],
|
||
|
install=kwargs['install'],
|
||
3 years ago
|
install_dir=[kwargs['install_dir']],
|
||
3 years ago
|
install_tag=['i18n'],
|
||
|
build_by_default=kwargs['build_by_default'],
|
||
1 year ago
|
description='Compiling Qt translations {}',
|
||
3 years ago
|
)
|
||
5 years ago
|
translations.append(lrelease_target)
|
||
5 years ago
|
if qresource:
|
||
|
return ModuleReturnValue(results.return_value[0], [results.new_objects, translations])
|
||
|
else:
|
||
4 years ago
|
return ModuleReturnValue(translations, [translations])
|