| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2016 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from os import path |
| 7 |
from pathlib import Path |
| 8 |
import shlex |
| 9 |
import typing as T |
| 10 |
|
| 11 |
from . import ExtensionModule, ModuleReturnValue, ModuleInfo |
| 12 |
from .. import build |
| 13 |
from .. import mesonlib |
| 14 |
from ..options import OptionKey |
| 15 |
from .. import mlog |
| 16 |
from ..interpreter.primitives import OptionString |
| 17 |
from ..interpreter.type_checking import CT_BUILD_BY_DEFAULT, CT_INPUT_KW, INSTALL_TAG_KW, OUTPUT_KW, INSTALL_DIR_KW, INSTALL_KW, NoneType, in_set_validator |
| 18 |
from ..interpreterbase import FeatureNew |
| 19 |
from ..interpreterbase.exceptions import InvalidArguments |
| 20 |
from ..interpreterbase.decorators import ContainerTypeInfo, KwargInfo, noPosargs, typed_kwargs, typed_pos_args |
| 21 |
from ..programs import ExternalProgram |
| 22 |
from ..scripts.gettext import read_linguas |
| 23 |
|
| 24 |
if T.TYPE_CHECKING: |
| 25 |
from typing_extensions import Literal, TypedDict |
| 26 |
|
| 27 |
from . import ModuleState |
| 28 |
from ..build import CommandTypes, Target |
| 29 |
from ..interpreter import Interpreter |
| 30 |
from ..interpreter.interpreter import CustomTargetSources |
| 31 |
from ..interpreter.kwargs import CustomTargetInputs |
| 32 |
from ..interpreterbase import TYPE_var |
| 33 |
from ..programs import Program |
| 34 |
|
| 35 |
class MergeFile(TypedDict): |
| 36 |
|
| 37 |
input: T.List[CustomTargetInputs] |
| 38 |
output: str |
| 39 |
build_by_default: bool |
| 40 |
install: bool |
| 41 |
install_dir: T.Optional[str] |
| 42 |
install_tag: T.Optional[str] |
| 43 |
args: T.List[str] |
| 44 |
data_dirs: T.List[str] |
| 45 |
po_dir: str |
| 46 |
type: Literal['xml', 'desktop'] |
| 47 |
|
| 48 |
class Gettext(TypedDict): |
| 49 |
|
| 50 |
args: T.List[str] |
| 51 |
msgfmt_args: T.List[str] |
| 52 |
data_dirs: T.List[str] |
| 53 |
install: bool |
| 54 |
install_dir: T.Optional[str] |
| 55 |
languages: T.List[str] |
| 56 |
preset: T.Optional[str] |
| 57 |
|
| 58 |
class ItsJoinFile(TypedDict): |
| 59 |
|
| 60 |
input: T.List[CustomTargetInputs] |
| 61 |
output: str |
| 62 |
build_by_default: bool |
| 63 |
install: bool |
| 64 |
install_dir: T.Optional[str] |
| 65 |
install_tag: T.Optional[str] |
| 66 |
its_files: T.List[str] |
| 67 |
mo_targets: T.List[build.BuildTargetTypes] |
| 68 |
|
| 69 |
class XgettextProgramT(TypedDict): |
| 70 |
|
| 71 |
args: T.List[str] |
| 72 |
recursive: bool |
| 73 |
install: bool |
| 74 |
install_dir: T.Optional[str] |
| 75 |
install_tag: T.Optional[str] |
| 76 |
|
| 77 |
SourcesType = T.Union[str, mesonlib.File, build.BuildTargetTypes, build.BothLibraries] |
| 78 |
|
| 79 |
|
| 80 |
_ARGS: KwargInfo[T.List[str]] = KwargInfo( |
| 81 |
'args', |
| 82 |
ContainerTypeInfo(list, str), |
| 83 |
default=[], |
| 84 |
listify=True, |
| 85 |
) |
| 86 |
|
| 87 |
_MSGFMT_ARGS: KwargInfo[T.List[str]] = KwargInfo( |
| 88 |
'msgfmt_args', |
| 89 |
ContainerTypeInfo(list, str), |
| 90 |
default=[], |
| 91 |
listify=True, |
| 92 |
since='1.12.0', |
| 93 |
) |
| 94 |
|
| 95 |
_DATA_DIRS: KwargInfo[T.List[str]] = KwargInfo( |
| 96 |
'data_dirs', |
| 97 |
ContainerTypeInfo(list, str), |
| 98 |
default=[], |
| 99 |
listify=True |
| 100 |
) |
| 101 |
|
| 102 |
PRESET_ARGS = { |
| 103 |
'glib': [ |
| 104 |
'--from-code=UTF-8', |
| 105 |
'--add-comments', |
| 106 |
|
| 107 |
# https://developer.gnome.org/glib/stable/glib-I18N.html |
| 108 |
'--keyword=_', |
| 109 |
'--keyword=N_', |
| 110 |
'--keyword=C_:1c,2', |
| 111 |
'--keyword=NC_:1c,2', |
| 112 |
'--keyword=g_dcgettext:2', |
| 113 |
'--keyword=g_dngettext:2,3', |
| 114 |
'--keyword=g_dpgettext2:2c,3', |
| 115 |
|
| 116 |
'--flag=N_:1:pass-c-format', |
| 117 |
'--flag=C_:2:pass-c-format', |
| 118 |
'--flag=NC_:2:pass-c-format', |
| 119 |
'--flag=g_dngettext:2:pass-c-format', |
| 120 |
'--flag=g_strdup_printf:1:c-format', |
| 121 |
'--flag=g_string_printf:2:c-format', |
| 122 |
'--flag=g_string_append_printf:2:c-format', |
| 123 |
'--flag=g_error_new:3:c-format', |
| 124 |
'--flag=g_set_error:4:c-format', |
| 125 |
'--flag=g_markup_printf_escaped:1:c-format', |
| 126 |
'--flag=g_log:3:c-format', |
| 127 |
'--flag=g_print:1:c-format', |
| 128 |
'--flag=g_printerr:1:c-format', |
| 129 |
'--flag=g_printf:1:c-format', |
| 130 |
'--flag=g_fprintf:2:c-format', |
| 131 |
'--flag=g_sprintf:2:c-format', |
| 132 |
'--flag=g_snprintf:3:c-format', |
| 133 |
] |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
class XgettextProgram: |
| 138 |
|
| 139 |
pot_files: T.Dict[str, build.CustomTarget] = {} |
| 140 |
|
| 141 |
def __init__(self, xgettext: ExternalProgram, interpreter: Interpreter): |
| 142 |
self.xgettext = xgettext |
| 143 |
self.interpreter = interpreter |
| 144 |
|
| 145 |
def extract(self, |
| 146 |
state: ModuleState, |
| 147 |
name: str, |
| 148 |
sources: T.List[SourcesType], |
| 149 |
args: T.List[str], |
| 150 |
recursive: bool, |
| 151 |
install: bool, |
| 152 |
install_dir: T.Optional[str], |
| 153 |
install_tag: T.Optional[str]) -> build.CustomTarget: |
| 154 |
|
| 155 |
if not name.endswith('.pot'): |
| 156 |
name += '.pot' |
| 157 |
|
| 158 |
source_files = self._get_source_files(sources) |
| 159 |
|
| 160 |
command = self.xgettext.command + args |
| 161 |
command.append(f'--directory={self.interpreter.environment.get_source_dir()}') |
| 162 |
command.append(f'--directory={self.interpreter.environment.get_build_dir()}') |
| 163 |
command.append('--output=@OUTPUT@') |
| 164 |
|
| 165 |
depends = list(self._get_depends(sources)) if recursive else [] |
| 166 |
rsp_file = self._get_rsp_file(name, source_files, depends, command) |
| 167 |
inputs: T.List[T.Union[mesonlib.File, build.CustomTarget]] |
| 168 |
if rsp_file: |
| 169 |
inputs = [rsp_file] |
| 170 |
depend_files = list(source_files) |
| 171 |
command.append('--files-from=@INPUT@') |
| 172 |
else: |
| 173 |
inputs = list(source_files) + depends |
| 174 |
depends = None |
| 175 |
depend_files = None |
| 176 |
command.append('@INPUT@') |
| 177 |
|
| 178 |
ct = build.CustomTarget( |
| 179 |
'', |
| 180 |
self.interpreter.subdir, |
| 181 |
self.interpreter.environment, |
| 182 |
command, |
| 183 |
inputs, |
| 184 |
[name], |
| 185 |
state.current_build_project, |
| 186 |
depend_files = depend_files, |
| 187 |
extra_depends = depends, |
| 188 |
install = install, |
| 189 |
install_dir = [install_dir] if install_dir else None, |
| 190 |
install_tag = [install_tag] if install_tag else None, |
| 191 |
description = 'Extracting translations to {}', |
| 192 |
) |
| 193 |
|
| 194 |
for source_id in self._get_source_id(sources): |
| 195 |
self.pot_files[source_id] = ct |
| 196 |
self.pot_files[ct.get_id()] = ct |
| 197 |
|
| 198 |
self.interpreter.add_target(ct.name, ct) |
| 199 |
return ct |
| 200 |
|
| 201 |
def _get_source_files(self, sources: T.Iterable[SourcesType]) -> T.Set[mesonlib.File]: |
| 202 |
source_files = set() |
| 203 |
for source in sources: |
| 204 |
if isinstance(source, mesonlib.File): |
| 205 |
source_files.add(source) |
| 206 |
elif isinstance(source, str): |
| 207 |
mesonlib.check_direntry_issues(source) |
| 208 |
source_files.add(mesonlib.File.from_source_file(self.interpreter.source_root, self.interpreter.subdir, source)) |
| 209 |
elif isinstance(source, build.BuildTarget): |
| 210 |
source_files.update(source.get_sources()) |
| 211 |
elif isinstance(source, build.BothLibraries): |
| 212 |
source_files.update(source.get('shared').get_sources()) |
| 213 |
elif isinstance(source, (build.CustomTarget, build.CustomTargetIndex)): |
| 214 |
source_files.update(mesonlib.File.from_built_file(source.get_subdir(), f) for f in source.get_outputs()) |
| 215 |
return source_files |
| 216 |
|
| 217 |
def _get_depends(self, sources: T.Iterable[SourcesType]) -> T.Set[build.CustomTarget]: |
| 218 |
depends = set() |
| 219 |
for source in sources: |
| 220 |
if isinstance(source, build.BuildTarget): |
| 221 |
dependencies = source.get_dependencies() |
| 222 |
for source_id in self._get_source_id(dependencies): |
| 223 |
if source_id in self.pot_files: |
| 224 |
depends.add(self.pot_files[source_id]) |
| 225 |
depends.update(self._get_depends(dependencies)) |
| 226 |
elif isinstance(source, build.CustomTarget): |
| 227 |
# Dependency on another extracted pot file |
| 228 |
source_id = source.get_id() |
| 229 |
if source_id in self.pot_files: |
| 230 |
depends.add(self.pot_files[source_id]) |
| 231 |
return depends |
| 232 |
|
| 233 |
def _get_rsp_file(self, |
| 234 |
name: str, |
| 235 |
source_files: T.Iterable[mesonlib.File], |
| 236 |
depends: T.Iterable[build.CustomTarget], |
| 237 |
arguments: T.List[str]) -> T.Optional[mesonlib.File]: |
| 238 |
source_list = '\n'.join(source.relative_name() for source in source_files) |
| 239 |
for dep in depends: |
| 240 |
source_list += '\n' + path.join(dep.subdir, dep.get_filename()) |
| 241 |
|
| 242 |
estimated_cmdline_length = len(source_list) + sum(len(arg) + 1 for arg in arguments) + 1 |
| 243 |
if estimated_cmdline_length < mesonlib.get_rsp_threshold(): |
| 244 |
return None |
| 245 |
|
| 246 |
rsp_file = Path(self.interpreter.environment.build_dir, self.interpreter.subdir, name+'.rsp') |
| 247 |
rsp_file.write_text(source_list, encoding='utf-8') |
| 248 |
|
| 249 |
return mesonlib.File.from_built_file(self.interpreter.subdir, rsp_file.name) |
| 250 |
|
| 251 |
@staticmethod |
| 252 |
def _get_source_id(sources: T.Iterable[SourcesType]) -> T.Iterable[str]: |
| 253 |
for source in sources: |
| 254 |
if isinstance(source, build.Target): |
| 255 |
yield source.get_id() |
| 256 |
elif isinstance(source, build.BothLibraries): |
| 257 |
yield source.get('static').get_id() |
| 258 |
yield source.get('shared').get_id() |
| 259 |
|
| 260 |
|
| 261 |
class I18nModule(ExtensionModule): |
| 262 |
|
| 263 |
INFO = ModuleInfo('i18n') |
| 264 |
|
| 265 |
def __init__(self, interpreter: 'Interpreter'): |
| 266 |
super().__init__(interpreter) |
| 267 |
self.methods.update({ |
| 268 |
'merge_file': self.merge_file, |
| 269 |
'gettext': self.gettext, |
| 270 |
'itstool_join': self.itstool_join, |
| 271 |
'xgettext': self.xgettext, |
| 272 |
}) |
| 273 |
self.tools: T.Dict[str, T.Optional[Program]] = { |
| 274 |
'itstool': None, |
| 275 |
'msgfmt': None, |
| 276 |
'msginit': None, |
| 277 |
'msgmerge': None, |
| 278 |
'xgettext': None, |
| 279 |
} |
| 280 |
|
| 281 |
@staticmethod |
| 282 |
def _get_data_dirs(state: 'ModuleState', dirs: T.Iterable[str]) -> T.List[str]: |
| 283 |
"""Returns source directories of relative paths""" |
| 284 |
src_dir = path.join(state.environment.get_source_dir(), state.subdir) |
| 285 |
return [path.join(src_dir, d) for d in dirs] |
| 286 |
|
| 287 |
@FeatureNew('i18n.merge_file', '0.37.0') |
| 288 |
@noPosargs |
| 289 |
@typed_kwargs( |
| 290 |
'i18n.merge_file', |
| 291 |
CT_BUILD_BY_DEFAULT, |
| 292 |
CT_INPUT_KW, |
| 293 |
KwargInfo('install_dir', (str, NoneType)), |
| 294 |
INSTALL_TAG_KW, |
| 295 |
OUTPUT_KW, |
| 296 |
INSTALL_KW, |
| 297 |
_ARGS.evolve(since='0.51.0'), |
| 298 |
_DATA_DIRS.evolve(since='0.41.0'), |
| 299 |
KwargInfo('po_dir', str, required=True), |
| 300 |
KwargInfo('type', str, default='xml', validator=in_set_validator({'xml', 'desktop'})), |
| 301 |
) |
| 302 |
def merge_file(self, state: 'ModuleState', args: T.List['TYPE_var'], kwargs: 'MergeFile') -> ModuleReturnValue: |
| 303 |
if kwargs['install'] and not kwargs['install_dir']: |
| 304 |
raise InvalidArguments('i18n.merge_file: "install_dir" keyword argument must be set when "install" is true.') |
| 305 |
|
| 306 |
if self.tools['msgfmt'] is None or not self.tools['msgfmt'].found(): |
| 307 |
self.tools['msgfmt'] = state.find_program('msgfmt', for_machine=mesonlib.MachineChoice.BUILD) |
| 308 |
if isinstance(self.tools['msgfmt'], ExternalProgram): |
| 309 |
try: |
| 310 |
have_version = self.tools['msgfmt'].get_version() |
| 311 |
except mesonlib.MesonException as e: |
| 312 |
raise mesonlib.MesonException('i18n.merge_file requires GNU msgfmt') from e |
| 313 |
want_version = '>=0.19' if kwargs['type'] == 'desktop' else '>=0.19.7' |
| 314 |
if not mesonlib.version_compare(have_version, want_version): |
| 315 |
msg = f'i18n.merge_file requires GNU msgfmt {want_version} to produce files of type: ' + kwargs['type'] + f' (got: {have_version})' |
| 316 |
raise mesonlib.MesonException(msg) |
| 317 |
podir = path.join(state.build_to_src, state.subdir, kwargs['po_dir']) |
| 318 |
|
| 319 |
ddirs = self._get_data_dirs(state, kwargs['data_dirs']) |
| 320 |
datadirs = '--datadirs=' + ':'.join(ddirs) if ddirs else None |
| 321 |
|
| 322 |
command: T.List[CommandTypes] = [] |
| 323 |
command.extend(state.environment.get_build_command()) |
| 324 |
command.extend([ |
| 325 |
'--internal', 'msgfmthelper', |
| 326 |
'--msgfmt=' + self.tools['msgfmt'].get_path(), |
| 327 |
]) |
| 328 |
if datadirs: |
| 329 |
command.append(datadirs) |
| 330 |
command.extend(['@INPUT@', '@OUTPUT@', kwargs['type'], podir]) |
| 331 |
if kwargs['args']: |
| 332 |
command.append('--') |
| 333 |
command.extend(kwargs['args']) |
| 334 |
|
| 335 |
build_by_default = kwargs['build_by_default'] |
| 336 |
if build_by_default is None: |
| 337 |
build_by_default = kwargs['install'] |
| 338 |
|
| 339 |
install_tag = [kwargs['install_tag']] if kwargs['install_tag'] is not None else None |
| 340 |
|
| 341 |
inputs: T.List[CustomTargetSources] = self.interpreter.source_strings_to_files(kwargs['input']) |
| 342 |
ct = build.CustomTarget( |
| 343 |
'', |
| 344 |
state.subdir, |
| 345 |
state.environment, |
| 346 |
command, |
| 347 |
inputs, |
| 348 |
[kwargs['output']], |
| 349 |
state.current_build_project, |
| 350 |
build_by_default=build_by_default, |
| 351 |
install=kwargs['install'], |
| 352 |
install_dir=[kwargs['install_dir']] if kwargs['install_dir'] is not None else None, |
| 353 |
install_tag=install_tag, |
| 354 |
description='Merging translations for {}', |
| 355 |
) |
| 356 |
|
| 357 |
return ModuleReturnValue(ct, [ct]) |
| 358 |
|
| 359 |
@typed_pos_args('i18n.gettext', str) |
| 360 |
@typed_kwargs( |
| 361 |
'i18n.gettext', |
| 362 |
_ARGS, |
| 363 |
_MSGFMT_ARGS, |
| 364 |
_DATA_DIRS.evolve(since='0.36.0'), |
| 365 |
INSTALL_KW.evolve(default=True), |
| 366 |
INSTALL_DIR_KW.evolve(since='0.50.0'), |
| 367 |
KwargInfo('languages', ContainerTypeInfo(list, str), default=[], listify=True), |
| 368 |
KwargInfo( |
| 369 |
'preset', |
| 370 |
(str, NoneType), |
| 371 |
validator=in_set_validator(set(PRESET_ARGS)), |
| 372 |
since='0.37.0', |
| 373 |
), |
| 374 |
) |
| 375 |
def gettext(self, state: 'ModuleState', args: T.Tuple[str], kwargs: 'Gettext') -> ModuleReturnValue: |
| 376 |
for tool, strict in [('msgfmt', True), ('msginit', False), ('msgmerge', False), ('xgettext', False)]: |
| 377 |
if self.tools[tool] is None: |
| 378 |
self.tools[tool] = state.find_program(tool, required=False, for_machine=mesonlib.MachineChoice.BUILD) |
| 379 |
# still not found? |
| 380 |
if not self.tools[tool].found(): |
| 381 |
if strict: |
| 382 |
mlog.warning('Gettext not found, all translation (po) targets will be ignored.', |
| 383 |
once=True, location=state.current_node) |
| 384 |
return ModuleReturnValue(None, []) |
| 385 |
else: |
| 386 |
mlog.warning(f'{tool!r} not found, maintainer targets will not work', |
| 387 |
once=True, fatal=False, location=state.current_node) |
| 388 |
packagename = args[0] |
| 389 |
pkg_arg = f'--pkgname={packagename}' |
| 390 |
|
| 391 |
languages = kwargs['languages'] |
| 392 |
lang_arg = '--langs=' + '@@'.join(languages) if languages else None |
| 393 |
|
| 394 |
_datadirs = ':'.join(self._get_data_dirs(state, kwargs['data_dirs'])) |
| 395 |
datadirs = f'--datadirs={_datadirs}' if _datadirs else None |
| 396 |
|
| 397 |
extra_args = kwargs['args'] |
| 398 |
targets: T.List['Target'] = [] |
| 399 |
gmotargets: T.List['build.CustomTarget'] = [] |
| 400 |
|
| 401 |
preset = kwargs['preset'] |
| 402 |
if preset: |
| 403 |
preset_args = PRESET_ARGS[preset] |
| 404 |
extra_args = list(mesonlib.OrderedSet(preset_args + extra_args)) |
| 405 |
|
| 406 |
extra_arg = '--extra-args=' + '@@'.join(extra_args) if extra_args else None |
| 407 |
|
| 408 |
source_root = path.join(state.source_root, state.root_subdir) |
| 409 |
subdir = path.relpath(state.subdir, start=state.root_subdir) if state.subdir else None |
| 410 |
|
| 411 |
potargs = state.environment.get_build_command() + ['--internal', 'gettext', 'pot', pkg_arg] |
| 412 |
potargs.append(f'--source-root={source_root}') |
| 413 |
if subdir: |
| 414 |
potargs.append(f'--subdir={subdir}') |
| 415 |
if datadirs: |
| 416 |
potargs.append(datadirs) |
| 417 |
if extra_arg: |
| 418 |
potargs.append(extra_arg) |
| 419 |
if self.tools['xgettext'].found(): |
| 420 |
potargs.append('--xgettext=' + self.tools['xgettext'].get_path()) |
| 421 |
pottarget = build.RunTarget(packagename + '-pot', potargs, [], state.subdir, |
| 422 |
state.environment, state.current_build_project, default_env=False) |
| 423 |
targets.append(pottarget) |
| 424 |
|
| 425 |
install = kwargs['install'] |
| 426 |
install_dir = kwargs['install_dir'] or state.environment.coredata.optstore.get_value_for(OptionKey('localedir')) |
| 427 |
assert isinstance(install_dir, str), 'for mypy' |
| 428 |
if not languages: |
| 429 |
languages = read_linguas(path.join(state.environment.source_dir, state.subdir)) |
| 430 |
for l in languages: |
| 431 |
po_file = mesonlib.File.from_source_file(state.environment.source_dir, state.subdir, l+'.po') |
| 432 |
mo_install_dir = path.join(install_dir, l, 'LC_MESSAGES') |
| 433 |
if isinstance(install_dir, OptionString): |
| 434 |
name = path.join(install_dir.optname, l, 'LC_MESSAGES') |
| 435 |
mo_install_dir = OptionString(mo_install_dir, name) |
| 436 |
|
| 437 |
gmobasecmd: list[str | Program] = [self.tools['msgfmt'], '-o', '@OUTPUT@', '@INPUT@'] |
| 438 |
|
| 439 |
gmotarget = build.CustomTarget( |
| 440 |
f'{packagename}-{l}.mo', |
| 441 |
path.join(state.subdir, l, 'LC_MESSAGES'), |
| 442 |
state.environment, |
| 443 |
[*gmobasecmd, *kwargs['msgfmt_args']], |
| 444 |
[po_file], |
| 445 |
[f'{packagename}.mo'], |
| 446 |
state.current_build_project, |
| 447 |
install=install, |
| 448 |
# We have multiple files all installed as packagename+'.mo' in different install subdirs. |
| 449 |
# What we really wanted to do, probably, is have a rename: kwarg, but that's not available |
| 450 |
# to custom_targets. Crude hack: set the build target's subdir manually. |
| 451 |
# Bonus: the build tree has something usable as an uninstalled bindtextdomain() target dir. |
| 452 |
install_dir=[mo_install_dir], |
| 453 |
install_tag=['i18n'], |
| 454 |
description='Building translation {}', |
| 455 |
) |
| 456 |
targets.append(gmotarget) |
| 457 |
gmotargets.append(gmotarget) |
| 458 |
|
| 459 |
allgmotarget = build.AliasTarget(packagename + '-gmo', gmotargets, state.subdir, |
| 460 |
state.environment, state.current_build_project) |
| 461 |
targets.append(allgmotarget) |
| 462 |
|
| 463 |
updatepoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'update_po', pkg_arg] |
| 464 |
updatepoargs.append(f'--source-root={source_root}') |
| 465 |
if subdir: |
| 466 |
updatepoargs.append(f'--subdir={subdir}') |
| 467 |
if lang_arg: |
| 468 |
updatepoargs.append(lang_arg) |
| 469 |
if datadirs: |
| 470 |
updatepoargs.append(datadirs) |
| 471 |
if extra_arg: |
| 472 |
updatepoargs.append(extra_arg) |
| 473 |
for tool in ['msginit', 'msgmerge']: |
| 474 |
if self.tools[tool].found(): |
| 475 |
updatepoargs.append(f'--{tool}=' + self.tools[tool].get_path()) |
| 476 |
updatepotarget = build.RunTarget(packagename + '-update-po', updatepoargs, [], state.subdir, |
| 477 |
state.environment, state.current_build_project, default_env=False) |
| 478 |
targets.append(updatepotarget) |
| 479 |
|
| 480 |
return ModuleReturnValue([gmotargets, pottarget, updatepotarget], targets) |
| 481 |
|
| 482 |
@FeatureNew('i18n.itstool_join', '0.62.0') |
| 483 |
@noPosargs |
| 484 |
@typed_kwargs( |
| 485 |
'i18n.itstool_join', |
| 486 |
CT_BUILD_BY_DEFAULT, |
| 487 |
CT_INPUT_KW, |
| 488 |
KwargInfo('install_dir', (str, NoneType)), |
| 489 |
INSTALL_TAG_KW, |
| 490 |
OUTPUT_KW, |
| 491 |
INSTALL_KW, |
| 492 |
_ARGS.evolve(), |
| 493 |
KwargInfo('its_files', ContainerTypeInfo(list, str)), |
| 494 |
KwargInfo('mo_targets', ContainerTypeInfo(list, build.CustomTarget), required=True), |
| 495 |
) |
| 496 |
def itstool_join(self, state: 'ModuleState', args: T.List['TYPE_var'], kwargs: 'ItsJoinFile') -> ModuleReturnValue: |
| 497 |
if kwargs['install'] and not kwargs['install_dir']: |
| 498 |
raise InvalidArguments('i18n.itstool_join: "install_dir" keyword argument must be set when "install" is true.') |
| 499 |
|
| 500 |
if self.tools['itstool'] is None: |
| 501 |
self.tools['itstool'] = state.find_program('itstool', for_machine=mesonlib.MachineChoice.BUILD) |
| 502 |
mo_targets = kwargs['mo_targets'] |
| 503 |
its_files = kwargs.get('its_files', []) |
| 504 |
|
| 505 |
mo_fnames = [] |
| 506 |
for target in mo_targets: |
| 507 |
mo_fnames.append(path.join(target.get_builddir(), target.get_outputs()[0])) |
| 508 |
|
| 509 |
command: T.List[CommandTypes] = [] |
| 510 |
command.extend(state.environment.get_build_command()) |
| 511 |
|
| 512 |
itstool_cmd = self.tools['itstool'].get_command() |
| 513 |
# TODO: python 3.8 can use shlex.join() |
| 514 |
command.extend([ |
| 515 |
'--internal', 'itstool', 'join', |
| 516 |
'-i', '@INPUT@', |
| 517 |
'-o', '@OUTPUT@', |
| 518 |
'--itstool=' + ' '.join(shlex.quote(c) for c in itstool_cmd), |
| 519 |
]) |
| 520 |
if its_files: |
| 521 |
for fname in its_files: |
| 522 |
if not path.isabs(fname): |
| 523 |
fname = path.join(state.environment.source_dir, state.subdir, fname) |
| 524 |
command.extend(['--its', fname]) |
| 525 |
command.extend(mo_fnames) |
| 526 |
|
| 527 |
build_by_default = kwargs['build_by_default'] |
| 528 |
if build_by_default is None: |
| 529 |
build_by_default = kwargs['install'] |
| 530 |
|
| 531 |
install_tag = [kwargs['install_tag']] if kwargs['install_tag'] is not None else None |
| 532 |
|
| 533 |
inputs: T.List[CustomTargetSources] = self.interpreter.source_strings_to_files(kwargs['input']) |
| 534 |
ct = build.CustomTarget( |
| 535 |
'', |
| 536 |
state.subdir, |
| 537 |
state.environment, |
| 538 |
command, |
| 539 |
inputs, |
| 540 |
[kwargs['output']], |
| 541 |
state.current_build_project, |
| 542 |
build_by_default=build_by_default, |
| 543 |
extra_depends=mo_targets, |
| 544 |
install=kwargs['install'], |
| 545 |
install_dir=[kwargs['install_dir']] if kwargs['install_dir'] is not None else None, |
| 546 |
install_tag=install_tag, |
| 547 |
description='Merging translations for {}', |
| 548 |
) |
| 549 |
|
| 550 |
return ModuleReturnValue(ct, [ct]) |
| 551 |
|
| 552 |
@FeatureNew('i18n.xgettext', '1.8.0') |
| 553 |
@typed_pos_args('i18n.xgettext', str, varargs=(str, mesonlib.File, build.BuildTarget, build.BothLibraries, build.CustomTarget, build.CustomTargetIndex), min_varargs=1) |
| 554 |
@typed_kwargs( |
| 555 |
'i18n.xgettext', |
| 556 |
_ARGS, |
| 557 |
KwargInfo('recursive', bool, default=False), |
| 558 |
INSTALL_KW, |
| 559 |
INSTALL_DIR_KW, |
| 560 |
INSTALL_TAG_KW, |
| 561 |
) |
| 562 |
def xgettext(self, state: ModuleState, args: T.Tuple[str, T.List[SourcesType]], kwargs: XgettextProgramT) -> build.CustomTarget: |
| 563 |
if any(isinstance(a, build.CustomTarget) for a in args[1]): |
| 564 |
FeatureNew.single_use('i18n.xgettext with custom_target is broken until 1.10', '1.10.0', self.interpreter.subproject, location=self.interpreter.current_node) |
| 565 |
if any(isinstance(a, build.CustomTargetIndex) for a in args[1]): |
| 566 |
FeatureNew.single_use('i18n.xgettext with custom_target index', '1.10.0', self.interpreter.subproject, location=self.interpreter.current_node) |
| 567 |
|
| 568 |
toolname = 'xgettext' |
| 569 |
if self.tools[toolname] is None or not self.tools[toolname].found(): |
| 570 |
self.tools[toolname] = state.find_program(toolname, required=True, for_machine=mesonlib.MachineChoice.BUILD) |
| 571 |
|
| 572 |
if kwargs['install'] and not kwargs['install_dir']: |
| 573 |
raise InvalidArguments('i18n.xgettext: "install_dir" keyword argument must be set when "install" is true.') |
| 574 |
|
| 575 |
xgettext_program = XgettextProgram(T.cast('ExternalProgram', self.tools[toolname]), self.interpreter) |
| 576 |
return xgettext_program.extract(state, *args, **kwargs) |
| 577 |
|
| 578 |
|
| 579 |
def initialize(interp: 'Interpreter') -> I18nModule: |
| 580 |
return I18nModule(interp) |