scripts: west build: propagate verbosity to cmake generator

When run as "west -v build", make sure that the underlying build tool
is run in verbose mode as well (if the generator is known to support
it, which is the case for Unix Makefiles and Ninja based generators).

The per-generator hacks here are needed to support CMake 3.13. If we
move to CMake 3.14 or later, we can just run "cmake --build BUILD -v"
and be done with it.

Signed-off-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
diff --git a/scripts/west_commands/build.py b/scripts/west_commands/build.py
index c6b0537..afe2f3c 100644
--- a/scripts/west_commands/build.py
+++ b/scripts/west_commands/build.py
@@ -399,5 +399,28 @@
         if self.args.build_opt:
             extra_args.append('--')
             extra_args.extend(self.args.build_opt)
+        if self.args.verbose:
+            self._append_verbose_args(extra_args,
+                                      not bool(self.args.build_opt))
         run_build(self.build_dir, extra_args=extra_args,
                   dry_run=self.args.dry_run)
+
+    def _append_verbose_args(self, extra_args, add_dashes):
+        # These hacks are only needed for CMake versions earlier than
+        # 3.14. When Zephyr's minimum version is at least that, we can
+        # drop this nonsense and just run "cmake --build BUILD -v".
+        self._update_cache()
+        if not self.cmake_cache:
+            return
+        generator = self.cmake_cache.get('CMAKE_GENERATOR')
+        if not generator:
+            return
+        # Substring matching is for things like "Eclipse CDT4 - Ninja".
+        if 'Ninja' in generator:
+            if add_dashes:
+                extra_args.append('--')
+            extra_args.append('-v')
+        elif generator == 'Unix Makefiles':
+            if add_dashes:
+                extra_args.append('--')
+            extra_args.append('VERBOSE=1')