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

Dylan Baker dylan at pnwbakers.com
Wed Jan 17 17:23:08 UTC 2018


ping

Quoting Dylan Baker (2018-01-09 11:36:36)
> 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.
> 
> Signed-off-by: Dylan Baker <dylan.c.baker at intel.com>
> ---
>  src/util/xmlpool/gen_translations.py | 91 +++++++++++++++++++++++++++++-
>  src/util/xmlpool/meson.build         | 31 +++++++---
>  2 files changed, 114 insertions(+), 8 deletions(-)
>  create mode 100644 src/util/xmlpool/gen_translations.py
> 
> diff --git a/src/util/xmlpool/gen_translations.py b/src/util/xmlpool/gen_translations.py
> new file mode 100644
> index 0000000..5d08590
> --- /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 97693fa..93f9c44 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
> 
> base-commit: 28c2d0d80b2ba15cc56651c0d3e6bc6eb31f9594
> -- 
> git-series 0.9.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180117/772a9a27/attachment-0001.sig>


More information about the mesa-dev mailing list