[Mesa-dev] [PATCH v2] meson: generate translations for driconf
Dylan Baker
dylan at pnwbakers.com
Thu Jan 25 19:32:23 UTC 2018
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.
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
--
2.16.0
More information about the mesa-dev
mailing list