Repository: mesonbuild/meson

Issue:
print colorful warnings for Vala warnings and errors
The Vala compiler outputs colorful warnings and errors by default (since version 0.27.1) but Meson shows them only in black and white.


Discussion:
That's because Ninja runs it with stdout and stderr redirected so the compiler thinks it is not connected to a terminal and switches to plain text mode. Other compilers have a switch to force color output even in this case. If Vala has one, then support for that needs to be added for this to work.
At a glance it has `--no-color` but nothing to force it so that would have to be added upstream.
A `--color=always` option was added to Vala with this commit:
https://git.gnome.org/browse/vala/commit/?id=e1b399b332dbf0875b6de8499f31877db6f9f3a1
It will appear in the 0.38 stable release.
The `Compiler` class has a `get_colorout_args(self, colortype)` method that returns an array. So the `ValaCompiler` class would need to override that method and return the `--color=always` option when `colortype` is `'always'`.

The problem is this should only happen when `valac --api-version` returns a value of 0.38 or greater. By using `--api-version` it allows for the current development version of `valac` to produce color warnings with meson. The problem is `environment.py` uses `valac --version`, rather than `valac --api-version`. `valac --version` returns something like `Vala 0.36.1.38-6532` for `valac` built from master. The question is whether it is worthwhile adding something to `environment.py` to also pull valac's API version in to Meson, as well as the release version.

Resolving patch:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1094,6 +1094,7 @@ def generate_vala_compile(self, target, outfile):
             valac_outputs.append(vala_c_file)
 
         args = self.generate_basic_compiler_args(target, valac)
+        args += valac.get_colorout_args(self.environment.coredata.base_options.get('b_colorout').value)
         # Tell Valac to output everything in our private directory. Sadly this
         # means it will also preserve the directory components of Vala sources
         # found inside the build tree (generated sources).
diff --git a/mesonbuild/compilers/vala.py b/mesonbuild/compilers/vala.py
--- a/mesonbuild/compilers/vala.py
+++ b/mesonbuild/compilers/vala.py
@@ -15,7 +15,7 @@
 import os.path
 
 from .. import mlog
-from ..mesonlib import EnvironmentException
+from ..mesonlib import EnvironmentException, version_compare
 
 from .compilers import Compiler
 
@@ -26,6 +26,7 @@ def __init__(self, exelist, version):
         self.version = version
         self.id = 'valac'
         self.is_cross = False
+        self.base_options = ['b_colorout']
 
     def name_string(self):
         return ' '.join(self.exelist)
@@ -54,6 +55,11 @@ def get_no_warn_args(self):
     def get_werror_args(self):
         return ['--fatal-warnings']
 
+    def get_colorout_args(self, colortype):
+        if version_compare(self.version, '>=0.37.1'):
+            return ['--color=' + colortype]
+        return []
+
     def sanity_check(self, work_dir, environment):
         code = 'class MesonSanityCheck : Object { }'
         args = self.get_cross_extra_flags(environment, link=False)


Test patch: