llvm-build: Add --configure-target-def-file option.
 - Can be used to generate the substitution values we currently use for the various target related .def files.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@144345 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/llvm-build/llvmbuild/main.py b/utils/llvm-build/llvmbuild/main.py
index 51923eb..fa76aa5 100644
--- a/utils/llvm-build/llvmbuild/main.py
+++ b/utils/llvm-build/llvmbuild/main.py
@@ -2,6 +2,7 @@
 import sys
 
 import componentinfo
+import configutil
 
 from util import *
 
@@ -616,6 +617,9 @@
                      help=(
             "If given, an alternate path to search for LLVMBuild.txt files"),
                      action="store", default=None, metavar="PATH")
+    group.add_option("", "--build-root", dest="build_root", metavar="PATH",
+                      help="Path to the build directory (if needed) [%default]",
+                      action="store", default=None)
     parser.add_option_group(group)
 
     group = OptionGroup(parser, "Output Options")
@@ -637,6 +641,14 @@
                       dest="write_make_fragment", metavar="PATH",
                      help="Write the Makefile project information to PATH",
                      action="store", default=None)
+    group.add_option("", "--configure-target-def-file",
+                     dest="configure_target_def_files",
+                     help="""Configure the given file at SUBPATH (relative to
+the inferred or given source root, and with a '.in' suffix) by replacing certain
+substitution variables with lists of targets that support certain features (for
+example, targets with AsmPrinters) and write the result to the build root (as
+given by --build-root) at the same SUBPATH""",
+                     metavar="SUBPATH", action="append", default=None)
     parser.add_option_group(group)
 
     group = OptionGroup(parser, "Configuration Options")
@@ -701,5 +713,40 @@
     if opts.write_cmake_fragment:
         project_info.write_cmake_fragment(opts.write_cmake_fragment)
 
+    # Configure target definition files, if requested.
+    if opts.configure_target_def_files:
+        # Verify we were given a build root.
+        if not opts.build_root:
+            parser.error("must specify --build-root when using "
+                         "--configure-target-def-file")
+
+        # Create the substitution list.
+        available_targets = [ci for ci in project_info.component_infos
+                             if ci.type_name == 'TargetGroup']
+        substitutions = [
+            ("@LLVM_ENUM_TARGETS@",
+             ' '.join('LLVM_TARGET(%s)' % ci.name
+                      for ci in available_targets)),
+            ("@LLVM_ENUM_ASM_PRINTERS@",
+             ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
+                      for ci in available_targets
+                      if ci.has_asmprinter)),
+            ("@LLVM_ENUM_ASM_PARSERS@",
+             ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
+                      for ci in available_targets
+                      if ci.has_asmparser)),
+            ("@LLVM_ENUM_DISASSEMBLERS@",
+             ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
+                      for ci in available_targets
+                      if ci.has_disassembler))]
+
+        # Configure the given files.
+        for subpath in opts.configure_target_def_files:
+            inpath = os.path.join(source_root, subpath + '.in')
+            outpath = os.path.join(opts.build_root, subpath)
+            result = configutil.configure_file(inpath, outpath, substitutions)
+            if not result:
+                note("configured file %r hasn't changed" % outpath)
+
 if __name__=='__main__':
     main()