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/scripts/llvm.py b/scripts/llvm.py
index d1bf29d..ff8ecb1 100644
--- a/scripts/llvm.py
+++ b/scripts/llvm.py
@@ -4,6 +4,7 @@
 from sys import argv
 from sys import exit
 
+from modules.llvm import build_llvm
 from modules.llvm import LLVMBuildConfig
 from modules.llvm import LLVMSubproject
 from modules.llvm import LLVMSourceConfig
@@ -116,6 +117,21 @@
         die("Failed to configure {} because:\n{}".format(args.build, str(exc)))
 
 
+def run_build(args):
+    """Run a build command in a given directory."""
+    build_dir = args.build
+
+    if args.dry:
+        consumer = CommandPrinter()
+    else:
+        consumer = CommandRunner()
+
+    try:
+        build_llvm(consumer, args.build, args.flags)
+    except RuntimeError as exc:
+        die("Failed to build {} because:\n{}".format(args.build, str(exc)))
+
+
 ##########################################################################
 # Command line parsing                                                   #
 ##########################################################################
@@ -215,6 +231,36 @@
     help="Print the CMake command instead of executing it.")
 configure.set_defaults(run_command=configure_build)
 
+# Subcommand for building a target
+build = subcommands.add_parser(
+    'build',
+    help="Run a build command in the given directory."
+    "The build command can be either a 'ninja' or a 'make' command, depending "
+    "on what the build directory contains. First, we look for a 'build.ninja' "
+    "file. If that is not found, we look for a 'Makefile'. If that is not "
+    "found either, the script fails.")
+build.add_argument(
+    '--build-dir',
+    dest='build',
+    required=True,
+    help="Path to the build directory. It must have already been configured.")
+build.add_argument(
+    '-n', '--dry-run',
+    dest='dry',
+    action='store_true',
+    default=False,
+    help="Print the build command instead of executing it.")
+build.add_argument(
+    '--build-flag',
+    dest='flags',
+    metavar='FLAG',
+    default=[],
+    action='append',
+    help="Additional flags for the build command (e.g. targets to build). "
+    "May be passed several times. If your flag starts with a '-', use "
+    "'--build-flag=-FLAG' to pass it.")
+build.set_defaults(run_command=run_build)
+
 args = options.parse_args()
 if args.subcommand == "projects" and args.add and not args.repos:
     projs.error(