build-and-test: Make it possible to customize stage 2

Same possibilities as stage 1: subprojects, CMake definitions and build
flags.

Change-Id: Ib10be5d2f7687040979c79113e7eeb0af5903df1
diff --git a/scripts/llvm.py b/scripts/llvm.py
index 53ffd89..bdeae85 100644
--- a/scripts/llvm.py
+++ b/scripts/llvm.py
@@ -200,6 +200,9 @@
     stage1BuildFlags = args.stage1BuildFlags
 
     stage2BuildDir = args.stage2
+    stage2Subprojs = args.stage2Subprojs
+    stage2Defs = cmake_flags_from_args(args.stage2Defs)
+    stage2BuildFlags = args.stage2BuildFlags
 
     enableTestSuite = args.enableTestSuite
     sandboxDir = args.sandbox
@@ -229,6 +232,13 @@
         testedBuildDir = stage1BuildDir
 
         if stage2BuildDir is not None:
+            if stage2Subprojs:
+                toAdd = list(set(stage2Subprojs) - set(stage1Subprojs))
+                toRemove = list(set(stage1Subprojs) - set(stage2Subprojs))
+                sourceConfig.update(
+                    subproj_to_repo_map(toAdd, proj, llvmRepos, args.dry),
+                    toRemove)
+
             if not dryRun and not os.path.exists(stage2BuildDir):
                 os.makedirs(stage2BuildDir)
 
@@ -238,11 +248,11 @@
             # TODO: Make sure clang is actually built in this config (preferably
             # before reaching this point)
             buildConfig2.cmake(
-                [
+                stage2Defs + [
                     "-DCMAKE_C_COMPILER={}/bin/clang".format(stage1BuildDir),
                     "-DCMAKE_CXX_COMPILER={}/bin/clang++".format(stage1BuildDir)],
                 "Ninja")
-            buildConfig2.build()
+            buildConfig2.build(stage2BuildFlags)
             testedBuildDir = stage2BuildDir
 
         if enableTestSuite:
@@ -518,6 +528,36 @@
     dest='stage2',
     help="Path to the build directory for stage 2.")
 buildAndTest.add_argument(
+    '--stage2-subproject',
+    dest='stage2Subprojs',
+    metavar='SUBPROJ',
+    choices=valid_subprojects,
+    default=[],
+    action='append',
+    help="Subprojects to enable for stage 2 of the build. Can be passed "
+         "multiple times. Valid values for the subproject are: {}. "
+         "If this is a 2-stage build, the same subprojects will be used for "
+         "both stages unless other subprojects are explicitly requested for "
+         "stage 2.".format(" ".join(valid_subprojects)))
+buildAndTest.add_argument(
+    '--stage2-cmake-def',
+    dest='stage2Defs',
+    metavar='VAR=VALUE',
+    default=[],
+    action='append',
+    help="Additional CMake definitions for stage 2, e.g. "
+         "CMAKE_BUILD_TYPE=Release. Can be passed multiple times. "
+         "The -D is added automatically.")
+buildAndTest.add_argument(
+    '--stage2-build-flag',
+    dest='stage2BuildFlags',
+    metavar='FLAG',
+    default=[],
+    action='append',
+    help="Additional flags for the stage 2 build command (e.g. targets to "
+         "build). Can be passed multiple times. If your flag starts with "
+         "a '-', use '--stage2-build-flag=-FLAG' to pass it.")
+buildAndTest.add_argument(
     "--enable-test-suite",
     dest='enableTestSuite',
     action='store_true',