Add support for building LLVM

Add a llvm.py subcommand for building LLVM in a given build directory.
The build directory must have already been configured with CMake. The
build command that is run will be either a 'ninja' command or a 'make'
command, depending on whether the build directory contains a
'build.ninja' or a 'Makefile'. If it contains neither of those, an error
is generated.

I initially wanted to put the implementation for this in the
LLVMBuildConfig, but it doesn't really need anything from there. For now
I left it as a free-standing function. It doesn't have any knowledge of
LLVM whatsoever, so an argument could be made for moving it into the
"utils" package rather than the "llvm" package. In the future, we may
want to remove the LLVMBuildConfig class and see if there's a better way
to organize the code for configuring and building.

One disadvantage with using python here is that we lose the nice
progress bars that we could see when running 'ninja check' from the bash
scripts. I'm not sure how to fix that, suggestions welcome.

Change-Id: I48cf464a6412238a26eb5bcfb4723946983c86f2
diff --git a/tests/unittests/testbuildllvm.py b/tests/unittests/testbuildllvm.py
new file mode 100644
index 0000000..5a31eb3
--- /dev/null
+++ b/tests/unittests/testbuildllvm.py
@@ -0,0 +1,66 @@
+from modules.llvm import build_llvm
+
+import os
+
+from shutil import rmtree
+from tempfile import mkdtemp
+
+from unittest import TestCase
+from unittest.mock import MagicMock
+
+
+def create_empty_dir():
+    return mkdtemp()
+
+
+def create_dir_with_empty_file(filename):
+    dir = create_empty_dir()
+    open(os.path.join(dir, filename), "wt").close()
+    return dir
+
+
+class TestBuildLLVM(TestCase):
+
+    def tearDown(self):
+        rmtree(self.buildDir)
+
+    def test_invalid_build_dir(self):
+        self.buildDir = create_empty_dir()
+
+        with self.assertRaises(RuntimeError) as context:
+            build_llvm(None, self.buildDir)
+
+        self.assertRegex(
+            str(context.exception),
+            "(.*\n)*Couldn't identify build system to use for {}(.*\n)*".format(
+                self.buildDir))
+
+    def test_ninja_build_dir(self):
+        self.buildDir = create_dir_with_empty_file("build.ninja")
+        consumer = MagicMock()
+
+        build_llvm(consumer, self.buildDir)
+        command, directory = consumer.consume.call_args[0]
+
+        self.assertEqual(command, ["ninja"])
+        self.assertEqual(directory, self.buildDir)
+
+    def test_make_build_dir(self):
+        self.buildDir = create_dir_with_empty_file("Makefile")
+        consumer = MagicMock()
+
+        build_llvm(consumer, self.buildDir)
+        command, directory = consumer.consume.call_args[0]
+
+        self.assertEqual(command, ["make"])
+        self.assertEqual(directory, self.buildDir)
+
+    def test_flags(self):
+        self.buildDir = create_dir_with_empty_file("build.ninja")
+        consumer = MagicMock()
+
+        build_llvm(consumer, self.buildDir, ["-t", "targets"])
+        command, directory = consumer.consume.call_args[0]
+
+        self.assertEqual(command, ["ninja", "-t", "targets"])
+        self.assertEqual(directory, self.buildDir)