scripts/unit-test: Add clang-tidy support if configuration is provided

This enables clang tidy support for cmake and meson based builds. This
does not yet enforce that clang tidy passes for the projects which have
it enabled.

Tested:
    Ran against a clang tidy enabled sdeventplus. Does not reject the
    build for errors. Once fixed the output looks as expected.

Change-Id: If4a970f7319aaaa44fe28f87e6bbd8d45aecd71e
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/build-unit-test-docker.sh b/build-unit-test-docker.sh
index 6980705..0c4289f 100755
--- a/build-unit-test-docker.sh
+++ b/build-unit-test-docker.sh
@@ -189,7 +189,10 @@
     git \
     dbus \
     iputils-ping \
+    clang-6.0 \
     clang-format-6.0 \
+    clang-tidy-6.0 \
+    clang-tools-6.0 \
     iproute2 \
     libnl-3-dev \
     libnl-genl-3-dev \
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 50afb06..1406435 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -720,6 +720,14 @@
                 check_call_cmd(top_dir, 'meson', 'test', '-C', 'build',
                                '--wrap', 'valgrind')
 
+            # Run clang-tidy only if the project has a configuration
+            if os.path.isfile('.clang-tidy'):
+                check_call_cmd(top_dir, 'run-clang-tidy-6.0.py', '-p', 'build')
+            # Run the basic clang static analyzer otherwise
+            else:
+                os.environ['SCANBUILD'] = 'scan-build-6.0'
+                check_call_cmd(top_dir, 'ninja', '-C', 'build', 'scan-build')
+
             # Run tests through sanitizers
             # b_lundef is needed if clang++ is CXX since it resolves the asan
             # symbols at runtime only. We don't want to set it earlier in the
@@ -760,7 +768,7 @@
     elif os.path.isfile(CODE_SCAN_DIR + "/CMakeLists.txt"):
         top_dir = os.path.join(WORKSPACE, UNIT_TEST_PKG)
         os.chdir(top_dir)
-        check_call_cmd(top_dir, 'cmake', '.')
+        check_call_cmd(top_dir, 'cmake', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '.')
         check_call_cmd(top_dir, 'cmake', '--build', '.', '--', '-j',
                        str(multiprocessing.cpu_count()))
         if make_target_exists('test'):
@@ -768,6 +776,8 @@
         maybe_run_valgrind(top_dir)
         maybe_run_coverage(top_dir)
         run_cppcheck(top_dir)
+        if os.path.isfile('.clang-tidy'):
+            check_call_cmd(top_dir, 'run-clang-tidy-6.0.py', '-p', '.')
 
     else:
         print "Not a supported repo for CI Tests, exit"