[Mesa-dev] [PATCH v2] meson: generate translations for driconf

Marc Dietrich marvin24 at gmx.de
Sat Jan 27 14:36:51 UTC 2018


Hi Dylan,

Am Donnerstag, 25. Januar 2018, 20:32:23 CET schrieb Dylan Baker:
> Currently meson implements the same logic as SCons for translations,
> namely it doesn't do them. This patch changes meson to use logic more
> like autotools, and generate translations. To do this we have to go
> behind meson's back a bit, and wrap the whole thing up in a single
> python script.
> 
> Meson has a module for gettext translations, but it assumes that one
> will have a pot file, and then .mo translations will be generated and
> checked into the source repo (or generated by hand using custom ninja
> targets before building), mesa assumes that the targets will be
> regenerated on each invocation of make or ninja. I think this can be
> fixed upstream, but in the mean time this adds support for using
> translations.

I have some patch sitting in my local tree which also addresses this problem. 
It is a bit shorter and doesn't require an external script. I initially tried 
to solve this by adding some custom targets mostly in order to learn some 
meson. Unfortunately, I didn't came far. I also tried the i18n module, but as 
you said, there are still some features missing.

Nevertheless, here is my solution using run_commands instead of external 
script. The advantage maybe better maintainability:

diff --git a/src/util/xmlpool/meson.build b/src/util/xmlpool/meson.build
index 97693fac8c..91f2b025f6 100644
--- a/src/util/xmlpool/meson.build
+++ b/src/util/xmlpool/meson.build
@@ -18,11 +18,36 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
 # SOFTWARE.
 
+langs = ['ca', 'es', 'de', 'nl', 'sv', 'fr']
+deps = []
+pot = 'xmlpool.pot'
+out = meson.current_build_dir()
+in = meson.current_source_dir()
+
+xmlpool_pot = custom_target(
+  pot,
+  build_by_default : true,
+  input : 't_options.h',
+  output : pot,
+  command : ['xgettext', '-LC', '--from-code=utf-8', '-o', '@OUTPUT@', 
'@INPUT@'],
+)
+
+foreach l : langs
+  po = l+'.po'
+  mo = '@0@/LC_MESSAGES/options.mo'.format(l)
+  message('Merge new strings @0@ into @1@'.format(po, pot))
+  run_command('msgmerge', '-o', join_paths(out, po), join_paths(in, po), pot)
+  message('Updating (@0@) @1@ from @2 at .'.format(l, mo, po))
+  run_command('mkdir', '-p', join_paths(out, l, 'LC_MESSAGES'))
+  run_command('msgfmt', '-o', join_paths(out, mo), po)
+  deps += po
+endforeach
+
 xmlpool_options_h = custom_target(
   'xmlpool_options.h',
   input : ['gen_xmlpool.py', 't_options.h'],
   output : 'options.h',
-  command : [prog_python2, '@INPUT@', meson.current_source_dir()],
+  command : [prog_python2, '@INPUT@', out, langs],
   capture : true,
-  depend_files : files('ca.po', 'es.po', 'de.po', 'nl.po', 'sv.po', 'fr.po'),
+  depend_files : files(deps),
 )

---

Marc



> 
> Fixes: 3218056e0eb3 ("meson: Build i965 and dri stack")
> Signed-off-by: Dylan Baker <dylan.c.baker at intel.com>
> ---
> 
> I could have sworn that I've sent this out before, but I can't find it now.
> 
>  src/util/xmlpool/Makefile.am         |  3 +-
>  src/util/xmlpool/gen_translations.py | 91
> ++++++++++++++++++++++++++++++++++++ src/util/xmlpool/meson.build         |
> 31 ++++++++----
>  3 files changed, 116 insertions(+), 9 deletions(-)
>  create mode 100644 src/util/xmlpool/gen_translations.py
> 
> diff --git a/src/util/xmlpool/Makefile.am b/src/util/xmlpool/Makefile.am
> index 0ef7a5462a1..e3ff38449b0 100644
> --- a/src/util/xmlpool/Makefile.am
> +++ b/src/util/xmlpool/Makefile.am
> @@ -59,7 +59,8 @@ EXTRA_DIST = \
>  	$(POS) \
>  	$(MOS) \
>  	SConscript \
> -	meson.build
> +	meson.build \
> +	gen_translations.py
> 
>  BUILT_SOURCES = options.h
>  CLEANFILES = \
> diff --git a/src/util/xmlpool/gen_translations.py
> b/src/util/xmlpool/gen_translations.py new file mode 100644
> index 00000000000..5d08590227d
> --- /dev/null
> +++ b/src/util/xmlpool/gen_translations.py
> @@ -0,0 +1,91 @@
> +# encoding=utf-8
> +# Copyright © 2018 Intel Corporation
> +
> +# Permission is hereby granted, free of charge, to any person obtaining a
> copy +# of this software and associated documentation files (the
> "Software"), to deal +# in the Software without restriction, including
> without limitation the rights +# to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell +# copies of the Software, and to
> permit persons to whom the Software is +# furnished to do so, subject to
> the following conditions:
> +
> +# The above copyright notice and this permission notice shall be included
> in +# all copies or substantial portions of the Software.
> +
> +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
> OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
> TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE.
> +
> +"""Wraps gen_xmlpool.py and generates .mo files for use in meson.
> +
> +Currently meson and mesa have a mismatch in the way that they assume that
> +translations should be generated. Meson assumes that translations are
> generated +and checked into the tree, mesa assumes that they are generated
> at compile +time. This script bridges that difference.
> +"""
> +
> +from __future__ import print_function
> +import argparse
> +import os
> +import subprocess
> +import sys
> +
> +
> +def arg_parser():
> +    parser = argparse.ArgumentParser()
> +    parser.add_argument('inputfile', help='The template file
> (t_options.h)') +    parser.add_argument('outputfile', help='The file to
> write.')
> +    parser.add_argument('gen_xmlpool', help='The gen_xmlpool.py script')
> +    parser.add_argument('sourcedir', help='The source directory')
> +    parser.add_argument('builddir', help='The build directory')
> +    parser.add_argument('python', help='The python binary to call
> gen_xmlpool.py with') +    parser.add_argument('langs', nargs='+',
> help='langs to generate.') +    return parser.parse_args()
> +
> +
> +def gen_mo(lang, sourcedir, builddir):
> +    """Generate an mo file from each po file using msgfmt."""
> +    # TODO: Python actually provides a tool that serves the same purpose as
> +    # msgfmt. It might make sense to fall back to that tool if msgfmt
> isn't +    # available (or just use it since our po files aren't that
> complicated) for +    # other oses (like macOS and Windows).
> +
> +    outdir = os.path.join(builddir, lang, 'LC_MESSAGES')
> +    if not os.path.exists(outdir):
> +        os.makedirs(outdir)
> +
> +    po_file = os.path.join(sourcedir, '{}.po'.format(lang))
> +    mo_file = os.path.join(outdir, 'options.mo')
> +
> +    try:
> +        subprocess.check_call(['msgfmt', '-o', mo_file, po_file])
> +    except subprocess.CalledProcessError as e:
> +        print(e.output, file=sys.stderr)
> +        sys.exit(e.returncode)
> +
> +
> +def main():
> +    args = arg_parser()
> +
> +    for lang in args.langs:
> +        gen_mo(lang, args.sourcedir, args.builddir)
> +
> +    try:
> +        # We need to use subprocess since gen_xmlpool.py prints, and isn't
> safe +        # to import since it doens't use the if __name__ ==
> '__main__' idiom +        out = subprocess.check_output(
> +            [args.python, args.gen_xmlpool, args.inputfile, args.builddir]
> + +            args.langs)
> +    except subprocess.CalledProcessError as e:
> +        print(e.output, file=sys.stderr)
> +        sys.exit(e.returncode)
> +
> +    with open(args.outputfile, 'wb') as f:
> +        f.write(out)
> +
> +
> +if __name__ == '__main__':
> +    main()
> diff --git a/src/util/xmlpool/meson.build b/src/util/xmlpool/meson.build
> index 97693fac8c4..93f9c44322b 100644
> --- a/src/util/xmlpool/meson.build
> +++ b/src/util/xmlpool/meson.build
> @@ -18,11 +18,26 @@
>  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE # SOFTWARE.
> 
> -xmlpool_options_h = custom_target(
> -  'xmlpool_options.h',
> -  input : ['gen_xmlpool.py', 't_options.h'],
> -  output : 'options.h',
> -  command : [prog_python2, '@INPUT@', meson.current_source_dir()],
> -  capture : true,
> -  depend_files : files('ca.po', 'es.po', 'de.po', 'nl.po', 'sv.po',
> 'fr.po'), -)
> +prog_msgfmt = find_program('msgfmt', required : false)
> +if prog_msgfmt.found()
> +  xmlpool_options_h = custom_target(
> +    'xmlpool_options.h',
> +    input : ['gen_translations.py', 't_options.h', 'gen_xmlpool.py'],
> +    output : ['options.h'],
> +    command : [
> +      prog_python2, '@INPUT0@', '@INPUT1@', '@OUTPUT@', '@INPUT2@',
> +      meson.current_source_dir(), meson.current_build_dir(), prog_python2,
> +      'ca', 'de', 'sv', 'es', 'nl', 'fr',
> +    ],
> +  )
> +else
> +  xmlpool_options_h = custom_target(
> +    'xmlpool_options.h',
> +    input : ['gen_xmlpool.py', 't_options.h'],
> +    output : 'options.h',
> +    command : [
> +      prog_python2, '@INPUT0@', '@INPUT1@', meson.current_source_dir(),
> +    ],
> +    capture : true,
> +  )
> +endif

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part.
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180127/8e6edb6f/attachment.sig>


More information about the mesa-dev mailing list