Repository: mesonbuild/meson

Issue:
Detect changes to LINGUAS file on build
As mentioned in https://github.com/mesonbuild/meson/pull/1121#issuecomment-264168074


Discussion:
I meant by that comment to not merge yet but oh well. So I think the best solution to this is move the LINGAS parsing into `scripts/gettext.py` so it simply reads it every call.

Resolving patch:
diff --git a/mesonbuild/modules/i18n.py b/mesonbuild/modules/i18n.py
--- a/mesonbuild/modules/i18n.py
+++ b/mesonbuild/modules/i18n.py
@@ -60,22 +60,11 @@ def merge_file(self, state, args, kwargs):
                              '--template', '@INPUT@', '-d', podir, '-o', '@OUTPUT@']
         return build.CustomTarget(kwargs['output'] + '_merge', state.subdir, kwargs)
 
-    @staticmethod
-    def _read_linguas(state):
-        linguas = path.join(state.environment.get_source_dir(), state.subdir, 'LINGUAS')
-        try:
-            with open(linguas) as f:
-                return [line.strip() for line in f if not line.strip().startswith('#')]
-        except (FileNotFoundError, PermissionError):
-            return []
-
     def gettext(self, state, args, kwargs):
         if len(args) != 1:
             raise coredata.MesonException('Gettext requires one positional argument (package name).')
         packagename = args[0]
-        languages = mesonlib.stringlistify(kwargs.get('languages', self._read_linguas(state)))
-        if len(languages) == 0:
-            raise coredata.MesonException('List of languages empty.')
+        languages = mesonlib.stringlistify(kwargs.get('languages', []))
         datadirs = mesonlib.stringlistify(kwargs.get('data_dirs', []))
         extra_args = mesonlib.stringlistify(kwargs.get('args', []))
 
@@ -88,7 +77,7 @@ def gettext(self, state, args, kwargs):
             extra_args = set(preset_args + extra_args)
 
         pkg_arg = '--pkgname=' + packagename
-        lang_arg = '--langs=' + '@@'.join(languages)
+        lang_arg = '--langs=' + '@@'.join(languages) if languages else None
         datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
         extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None
 
@@ -99,10 +88,14 @@ def gettext(self, state, args, kwargs):
             potargs.append(extra_args)
         pottarget = build.RunTarget(packagename + '-pot', sys.executable, potargs, [], state.subdir)
 
-        gmoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'gen_gmo', lang_arg]
+        gmoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'gen_gmo']
+        if lang_arg:
+            gmoargs.append(lang_arg)
         gmotarget = build.RunTarget(packagename + '-gmo', sys.executable, gmoargs, [], state.subdir)
 
-        updatepoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'update_po', pkg_arg, lang_arg]
+        updatepoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'update_po', pkg_arg]
+        if lang_arg:
+            updatepoargs.append(lang_arg)
         if datadirs:
             updatepoargs.append(datadirs)
         if extra_args:
@@ -113,7 +106,9 @@ def gettext(self, state, args, kwargs):
                       '--internal', 'gettext', 'install',
                       '--subdir=' + state.subdir,
                       '--localedir=' + state.environment.coredata.get_builtin_option('localedir'),
-                      pkg_arg, lang_arg]
+                      pkg_arg]
+        if lang_arg:
+            installcmd.append(lang_arg)
         iscript = build.InstallScript(installcmd)
 
         return [pottarget, gmotarget, iscript, updatepotarget]
diff --git a/mesonbuild/scripts/gettext.py b/mesonbuild/scripts/gettext.py
--- a/mesonbuild/scripts/gettext.py
+++ b/mesonbuild/scripts/gettext.py
@@ -27,6 +27,22 @@
 parser.add_argument('--subdir', default='')
 parser.add_argument('--extra-args', default='')
 
+def read_linguas(src_sub):
+    # Syntax of this file is documented here:
+    # https://www.gnu.org/software/gettext/manual/html_node/po_002fLINGUAS.html
+    linguas = os.path.join(src_sub, 'LINGUAS')
+    try:
+        langs = []
+        with open(linguas) as f:
+            for line in f:
+                line = line.strip()
+                if line and not line.startswith('#'):
+                    langs += line.split()
+        return langs
+    except (FileNotFoundError, PermissionError):
+        print('Could not find file LINGUAS in {}'.format(src_sub))
+        return []
+
 def run_potgen(src_sub, pkgname, datadirs, args):
     listfile = os.path.join(src_sub, 'POTFILES')
     if not os.path.exists(listfile):
@@ -71,12 +87,15 @@ def do_install(src_sub, bld_sub, dest, pkgname, langs):
 def run(args):
     options = parser.parse_args(args)
     subcmd = options.command
-    langs = options.langs.split('@@')
+    langs = options.langs.split('@@') if options.langs else None
     extra_args = options.extra_args.split('@@')
     subdir = os.environ.get('MESON_SUBDIR', options.subdir)
     src_sub = os.path.join(os.environ['MESON_SOURCE_ROOT'], subdir)
     bld_sub = os.path.join(os.environ['MESON_BUILD_ROOT'], subdir)
 
+    if not langs:
+        langs = read_linguas(src_sub)
+
     if subcmd == 'pot':
         return run_potgen(src_sub, options.pkgname, options.datadirs, extra_args)
     elif subcmd == 'gen_gmo':


Test patch: