6a2da754c3b55f8e24e929b6f40de365f307a6ff
[oota-llvm.git] / utils / llvm-build / llvmbuild / main.py
1 from __future__ import absolute_import
2 import filecmp
3 import os
4 import sys
5
6 import llvmbuild.componentinfo as componentinfo
7 import llvmbuild.configutil as configutil
8
9 from llvmbuild.util import fatal, note
10
11 ###
12
13 def cmake_quote_string(value):
14     """
15     cmake_quote_string(value) -> str
16
17     Return a quoted form of the given value that is suitable for use in CMake
18     language files.
19     """
20
21     # Currently, we only handle escaping backslashes.
22     value = value.replace("\\", "\\\\")
23
24     return value
25
26 def cmake_quote_path(value):
27     """
28     cmake_quote_path(value) -> str
29
30     Return a quoted form of the given value that is suitable for use in CMake
31     language files.
32     """
33
34     # CMake has a bug in it's Makefile generator that doesn't properly quote
35     # strings it generates. So instead of using proper quoting, we just use "/"
36     # style paths.  Currently, we only handle escaping backslashes.
37     value = value.replace("\\", "/")
38
39     return value
40
41 def mk_quote_string_for_target(value):
42     """
43     mk_quote_string_for_target(target_name) -> str
44
45     Return a quoted form of the given target_name suitable for including in a
46     Makefile as a target name.
47     """
48
49     # The only quoting we currently perform is for ':', to support msys users.
50     return value.replace(":", "\\:")
51
52 def make_install_dir(path):
53     """
54     make_install_dir(path) -> None
55
56     Create the given directory path for installation, including any parents.
57     """
58
59     # os.makedirs considers it an error to be called with an existent path.
60     if not os.path.exists(path):
61         os.makedirs(path)
62
63 ###
64
65 class LLVMProjectInfo(object):
66     @staticmethod
67     def load_infos_from_path(llvmbuild_source_root):
68         def recurse(subpath):
69             # Load the LLVMBuild file.
70             llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
71                                           'LLVMBuild.txt')
72             if not os.path.exists(llvmbuild_path):
73                 fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
74
75             # Parse the components from it.
76             common,info_iter = componentinfo.load_from_path(llvmbuild_path,
77                                                             subpath)
78             for info in info_iter:
79                 yield info
80
81             # Recurse into the specified subdirectories.
82             for subdir in common.get_list("subdirectories"):
83                 for item in recurse(os.path.join(subpath, subdir)):
84                     yield item
85
86         return recurse("/")
87
88     @staticmethod
89     def load_from_path(source_root, llvmbuild_source_root):
90         infos = list(
91             LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
92
93         return LLVMProjectInfo(source_root, infos)
94
95     def __init__(self, source_root, component_infos):
96         # Store our simple ivars.
97         self.source_root = source_root
98         self.component_infos = list(component_infos)
99         self.component_info_map = None
100         self.ordered_component_infos = None
101
102     def validate_components(self):
103         """validate_components() -> None
104
105         Validate that the project components are well-defined. Among other
106         things, this checks that:
107           - Components have valid references.
108           - Components references do not form cycles.
109
110         We also construct the map from component names to info, and the
111         topological ordering of components.
112         """
113
114         # Create the component info map and validate that component names are
115         # unique.
116         self.component_info_map = {}
117         for ci in self.component_infos:
118             existing = self.component_info_map.get(ci.name)
119             if existing is not None:
120                 # We found a duplicate component name, report it and error out.
121                 fatal("found duplicate component %r (at %r and %r)" % (
122                         ci.name, ci.subpath, existing.subpath))
123             self.component_info_map[ci.name] = ci
124
125         # Disallow 'all' as a component name, which is a special case.
126         if 'all' in self.component_info_map:
127             fatal("project is not allowed to define 'all' component")
128
129         # Add the root component.
130         if '$ROOT' in self.component_info_map:
131             fatal("project is not allowed to define $ROOT component")
132         self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
133             '/', '$ROOT', None)
134         self.component_infos.append(self.component_info_map['$ROOT'])
135
136         # Topologically order the component information according to their
137         # component references.
138         def visit_component_info(ci, current_stack, current_set):
139             # Check for a cycles.
140             if ci in current_set:
141                 # We found a cycle, report it and error out.
142                 cycle_description = ' -> '.join(
143                     '%r (%s)' % (ci.name, relation)
144                     for relation,ci in current_stack)
145                 fatal("found cycle to %r after following: %s -> %s" % (
146                         ci.name, cycle_description, ci.name))
147
148             # If we have already visited this item, we are done.
149             if ci not in components_to_visit:
150                 return
151
152             # Otherwise, mark the component info as visited and traverse.
153             components_to_visit.remove(ci)
154
155             # Validate the parent reference, which we treat specially.
156             if ci.parent is not None:
157                 parent = self.component_info_map.get(ci.parent)
158                 if parent is None:
159                     fatal("component %r has invalid reference %r (via %r)" % (
160                             ci.name, ci.parent, 'parent'))
161                 ci.set_parent_instance(parent)
162
163             for relation,referent_name in ci.get_component_references():
164                 # Validate that the reference is ok.
165                 referent = self.component_info_map.get(referent_name)
166                 if referent is None:
167                     fatal("component %r has invalid reference %r (via %r)" % (
168                             ci.name, referent_name, relation))
169
170                 # Visit the reference.
171                 current_stack.append((relation,ci))
172                 current_set.add(ci)
173                 visit_component_info(referent, current_stack, current_set)
174                 current_set.remove(ci)
175                 current_stack.pop()
176
177             # Finally, add the component info to the ordered list.
178             self.ordered_component_infos.append(ci)
179
180         # FIXME: We aren't actually correctly checking for cycles along the
181         # parent edges. Haven't decided how I want to handle this -- I thought
182         # about only checking cycles by relation type. If we do that, it falls
183         # out easily. If we don't, we should special case the check.
184
185         self.ordered_component_infos = []
186         components_to_visit = sorted(
187             set(self.component_infos),
188             key = lambda c: c.name)
189         while components_to_visit:
190             visit_component_info(components_to_visit[0], [], set())
191
192         # Canonicalize children lists.
193         for c in self.ordered_component_infos:
194             c.children.sort(key = lambda c: c.name)
195
196     def print_tree(self):
197         def visit(node, depth = 0):
198             print('%s%-40s (%s)' % ('  '*depth, node.name, node.type_name))
199             for c in node.children:
200                 visit(c, depth + 1)
201         visit(self.component_info_map['$ROOT'])
202
203     def write_components(self, output_path):
204         # Organize all the components by the directory their LLVMBuild file
205         # should go in.
206         info_basedir = {}
207         for ci in self.component_infos:
208             # Ignore the $ROOT component.
209             if ci.parent is None:
210                 continue
211
212             info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
213
214         # Compute the list of subdirectories to scan.
215         subpath_subdirs = {}
216         for ci in self.component_infos:
217             # Ignore root components.
218             if ci.subpath == '/':
219                 continue
220
221             # Otherwise, append this subpath to the parent list.
222             parent_path = os.path.dirname(ci.subpath)
223             subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
224                 parent_path, set())
225             parent_list.add(os.path.basename(ci.subpath))
226
227         # Generate the build files.
228         for subpath, infos in info_basedir.items():
229             # Order the components by name to have a canonical ordering.
230             infos.sort(key = lambda ci: ci.name)
231
232             # Format the components into llvmbuild fragments.
233             fragments = []
234
235             # Add the common fragments.
236             subdirectories = subpath_subdirs.get(subpath)
237             if subdirectories:
238                 fragment = """\
239 subdirectories = %s
240 """ % (" ".join(sorted(subdirectories)),)
241                 fragments.append(("common", fragment))
242
243             # Add the component fragments.
244             num_common_fragments = len(fragments)
245             for ci in infos:
246                 fragment = ci.get_llvmbuild_fragment()
247                 if fragment is None:
248                     continue
249
250                 name = "component_%d" % (len(fragments) - num_common_fragments)
251                 fragments.append((name, fragment))
252
253             if not fragments:
254                 continue
255
256             assert subpath.startswith('/')
257             directory_path = os.path.join(output_path, subpath[1:])
258
259             # Create the directory if it does not already exist.
260             if not os.path.exists(directory_path):
261                 os.makedirs(directory_path)
262
263             # In an effort to preserve comments (which aren't parsed), read in
264             # the original file and extract the comments. We only know how to
265             # associate comments that prefix a section name.
266             f = open(infos[0]._source_path)
267             comments_map = {}
268             comment_block = ""
269             for ln in f:
270                 if ln.startswith(';'):
271                     comment_block += ln
272                 elif ln.startswith('[') and ln.endswith(']\n'):
273                     comments_map[ln[1:-2]] = comment_block
274                 else:
275                     comment_block = ""
276             f.close()
277
278             # Create the LLVMBuild fil[e.
279             file_path = os.path.join(directory_path, 'LLVMBuild.txt')
280             f = open(file_path, "w")
281
282             # Write the header.
283             header_fmt = ';===- %s %s-*- Conf -*--===;'
284             header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
285             header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
286             header_string = header_fmt % (header_name, header_pad)
287             f.write("""\
288 %s
289 ;
290 ;                     The LLVM Compiler Infrastructure
291 ;
292 ; This file is distributed under the University of Illinois Open Source
293 ; License. See LICENSE.TXT for details.
294 ;
295 ;===------------------------------------------------------------------------===;
296 ;
297 ; This is an LLVMBuild description file for the components in this subdirectory.
298 ;
299 ; For more information on the LLVMBuild system, please see:
300 ;
301 ;   http://llvm.org/docs/LLVMBuild.html
302 ;
303 ;===------------------------------------------------------------------------===;
304
305 """ % header_string)
306
307             # Write out each fragment.each component fragment.
308             for name,fragment in fragments:
309                 comment = comments_map.get(name)
310                 if comment is not None:
311                     f.write(comment)
312                 f.write("[%s]\n" % name)
313                 f.write(fragment)
314                 if fragment is not fragments[-1][1]:
315                     f.write('\n')
316
317             f.close()
318
319     def write_library_table(self, output_path, enabled_optional_components):
320         # Write out the mapping from component names to required libraries.
321         #
322         # We do this in topological order so that we know we can append the
323         # dependencies for added library groups.
324         entries = {}
325         for c in self.ordered_component_infos:
326             # Skip optional components which are not enabled.
327             if c.type_name == 'OptionalLibrary' \
328                 and c.name not in enabled_optional_components:
329                 continue
330
331             # Skip target groups which are not enabled.
332             tg = c.get_parent_target_group()
333             if tg and not tg.enabled:
334                 continue
335
336             # Only certain components are in the table.
337             if c.type_name not in ('Library', 'OptionalLibrary', \
338                                    'LibraryGroup', 'TargetGroup'):
339                 continue
340
341             # Compute the llvm-config "component name". For historical reasons,
342             # this is lowercased based on the library name.
343             llvmconfig_component_name = c.get_llvmconfig_component_name()
344
345             # Get the library name, or None for LibraryGroups.
346             if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
347                 library_name = c.get_prefixed_library_name()
348                 is_installed = c.installed
349             else:
350                 library_name = None
351                 is_installed = True
352
353             # Get the component names of all the required libraries.
354             required_llvmconfig_component_names = [
355                 self.component_info_map[dep].get_llvmconfig_component_name()
356                 for dep in c.required_libraries]
357
358             # Insert the entries for library groups we should add to.
359             for dep in c.add_to_library_groups:
360                 entries[dep][2].append(llvmconfig_component_name)
361
362             # Add the entry.
363             entries[c.name] = (llvmconfig_component_name, library_name,
364                                required_llvmconfig_component_names,
365                                is_installed)
366
367         # Convert to a list of entries and sort by name.
368         entries = list(entries.values())
369
370         # Create an 'all' pseudo component. We keep the dependency list small by
371         # only listing entries that have no other dependents.
372         root_entries = set(e[0] for e in entries)
373         for _,_,deps,_ in entries:
374             root_entries -= set(deps)
375         entries.append(('all', None, root_entries, True))
376
377         entries.sort()
378
379         # Compute the maximum number of required libraries, plus one so there is
380         # always a sentinel.
381         max_required_libraries = max(len(deps)
382                                      for _,_,deps,_ in entries) + 1
383
384         # Write out the library table.
385         make_install_dir(os.path.dirname(output_path))
386         f = open(output_path+'.new', 'w')
387         f.write("""\
388 //===- llvm-build generated file --------------------------------*- C++ -*-===//
389 //
390 // Component Library Depenedency Table
391 //
392 // Automatically generated file, do not edit!
393 //
394 //===----------------------------------------------------------------------===//
395
396 """)
397         f.write('struct AvailableComponent {\n')
398         f.write('  /// The name of the component.\n')
399         f.write('  const char *Name;\n')
400         f.write('\n')
401         f.write('  /// The name of the library for this component (or NULL).\n')
402         f.write('  const char *Library;\n')
403         f.write('\n')
404         f.write('  /// Whether the component is installed.\n')
405         f.write('  bool IsInstalled;\n')
406         f.write('\n')
407         f.write('\
408   /// The list of libraries required when linking this component.\n')
409         f.write('  const char *RequiredLibraries[%d];\n' % (
410             max_required_libraries))
411         f.write('} AvailableComponents[%d] = {\n' % len(entries))
412         for name,library_name,required_names,is_installed in entries:
413             if library_name is None:
414                 library_name_as_cstr = '0'
415             else:
416                 library_name_as_cstr = '"lib%s.a"' % library_name
417             f.write('  { "%s", %s, %d, { %s } },\n' % (
418                 name, library_name_as_cstr, is_installed,
419                 ', '.join('"%s"' % dep
420                           for dep in required_names)))
421         f.write('};\n')
422         f.close()
423
424         if not os.path.isfile(output_path):
425             os.rename(output_path+'.new', output_path)
426         elif filecmp.cmp(output_path, output_path+'.new'):
427             os.remove(output_path+'.new')
428         else:
429             os.remove(output_path)
430             os.rename(output_path+'.new', output_path)
431
432     def get_required_libraries_for_component(self, ci, traverse_groups = False):
433         """
434         get_required_libraries_for_component(component_info) -> iter
435
436         Given a Library component info descriptor, return an iterator over all
437         of the directly required libraries for linking with this component. If
438         traverse_groups is True, then library and target groups will be
439         traversed to include their required libraries.
440         """
441
442         assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup')
443
444         for name in ci.required_libraries:
445             # Get the dependency info.
446             dep = self.component_info_map[name]
447
448             # If it is a library, yield it.
449             if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary':
450                 yield dep
451                 continue
452
453             # Otherwise if it is a group, yield or traverse depending on what
454             # was requested.
455             if dep.type_name in ('LibraryGroup', 'TargetGroup'):
456                 if not traverse_groups:
457                     yield dep
458                     continue
459
460                 for res in self.get_required_libraries_for_component(dep, True):
461                     yield res
462
463     def get_fragment_dependencies(self):
464         """
465         get_fragment_dependencies() -> iter
466
467         Compute the list of files (as absolute paths) on which the output
468         fragments depend (i.e., files for which a modification should trigger a
469         rebuild of the fragment).
470         """
471
472         # Construct a list of all the dependencies of the Makefile fragment
473         # itself. These include all the LLVMBuild files themselves, as well as
474         # all of our own sources.
475         #
476         # Many components may come from the same file, so we make sure to unique
477         # these.
478         build_paths = set()
479         for ci in self.component_infos:
480             p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
481             if p not in build_paths:
482                 yield p
483                 build_paths.add(p)
484
485         # Gather the list of necessary sources by just finding all loaded
486         # modules that are inside the LLVM source tree.
487         for module in sys.modules.values():
488             # Find the module path.
489             if not hasattr(module, '__file__'):
490                 continue
491             path = getattr(module, '__file__')
492             if not path:
493                 continue
494
495             # Strip off any compiled suffix.
496             if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
497                 path = path[:-1]
498
499             # If the path exists and is in the source tree, consider it a
500             # dependency.
501             if (path.startswith(self.source_root) and os.path.exists(path)):
502                 yield path
503
504     def foreach_cmake_library(self, f,
505                               enabled_optional_components,
506                               skip_disabled):
507         for ci in self.ordered_component_infos:
508             # Skip optional components which are not enabled.
509             if ci.type_name == 'OptionalLibrary' \
510                 and ci.name not in enabled_optional_components:
511                 continue
512
513             # We only write the information for libraries currently.
514             if ci.type_name not in ('Library', 'OptionalLibrary'):
515                 continue
516
517             # Skip disabled targets.
518             if skip_disabled:
519                 tg = ci.get_parent_target_group()
520                 if tg and not tg.enabled:
521                     continue
522
523             f(ci)
524
525
526     def write_cmake_fragment(self, output_path, enabled_optional_components):
527         """
528         write_cmake_fragment(output_path) -> None
529
530         Generate a CMake fragment which includes all of the collated LLVMBuild
531         information in a format that is easily digestible by a CMake. The exact
532         contents of this are closely tied to how the CMake configuration
533         integrates LLVMBuild, see CMakeLists.txt in the top-level.
534         """
535
536         dependencies = list(self.get_fragment_dependencies())
537
538         # Write out the CMake fragment.
539         make_install_dir(os.path.dirname(output_path))
540         f = open(output_path, 'w')
541
542         # Write the header.
543         header_fmt = '\
544 #===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
545         header_name = os.path.basename(output_path)
546         header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
547         header_string = header_fmt % (header_name, header_pad)
548         f.write("""\
549 %s
550 #
551 #                     The LLVM Compiler Infrastructure
552 #
553 # This file is distributed under the University of Illinois Open Source
554 # License. See LICENSE.TXT for details.
555 #
556 #===------------------------------------------------------------------------===#
557 #
558 # This file contains the LLVMBuild project information in a format easily
559 # consumed by the CMake based build system.
560 #
561 # This file is autogenerated by llvm-build, do not edit!
562 #
563 #===------------------------------------------------------------------------===#
564
565 """ % header_string)
566
567         # Write the dependency information in the best way we can.
568         f.write("""
569 # LLVMBuild CMake fragment dependencies.
570 #
571 # CMake has no builtin way to declare that the configuration depends on
572 # a particular file. However, a side effect of configure_file is to add
573 # said input file to CMake's internal dependency list. So, we use that
574 # and a dummy output file to communicate the dependency information to
575 # CMake.
576 #
577 # FIXME: File a CMake RFE to get a properly supported version of this
578 # feature.
579 """)
580         for dep in dependencies:
581             f.write("""\
582 configure_file(\"%s\"
583                ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % (
584                 cmake_quote_path(dep),))
585
586         # Write the properties we use to encode the required library dependency
587         # information in a form CMake can easily use directly.
588         f.write("""
589 # Explicit library dependency information.
590 #
591 # The following property assignments effectively create a map from component
592 # names to required libraries, in a way that is easily accessed from CMake.
593 """)
594         self.foreach_cmake_library(
595             lambda ci:
596               f.write("""\
597 set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % (
598                 ci.get_prefixed_library_name(), " ".join(sorted(
599                      dep.get_prefixed_library_name()
600                      for dep in self.get_required_libraries_for_component(ci)))))
601             ,
602             enabled_optional_components,
603             skip_disabled = False
604             )
605
606         f.close()
607
608     def write_cmake_exports_fragment(self, output_path, enabled_optional_components):
609         """
610         write_cmake_exports_fragment(output_path) -> None
611
612         Generate a CMake fragment which includes LLVMBuild library
613         dependencies expressed similarly to how CMake would write
614         them via install(EXPORT).
615         """
616
617         dependencies = list(self.get_fragment_dependencies())
618
619         # Write out the CMake exports fragment.
620         make_install_dir(os.path.dirname(output_path))
621         f = open(output_path, 'w')
622
623         f.write("""\
624 # Explicit library dependency information.
625 #
626 # The following property assignments tell CMake about link
627 # dependencies of libraries imported from LLVM.
628 """)
629         self.foreach_cmake_library(
630             lambda ci:
631               f.write("""\
632 set_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
633                 ci.get_prefixed_library_name(), " ".join(sorted(
634                      dep.get_prefixed_library_name()
635                      for dep in self.get_required_libraries_for_component(ci)))))
636             ,
637             enabled_optional_components,
638             skip_disabled = True
639             )
640
641         f.close()
642
643     def write_make_fragment(self, output_path, enabled_optional_components):
644         """
645         write_make_fragment(output_path) -> None
646
647         Generate a Makefile fragment which includes all of the collated
648         LLVMBuild information in a format that is easily digestible by a
649         Makefile. The exact contents of this are closely tied to how the LLVM
650         Makefiles integrate LLVMBuild, see Makefile.rules in the top-level.
651         """
652
653         dependencies = list(self.get_fragment_dependencies())
654
655         # Write out the Makefile fragment.
656         make_install_dir(os.path.dirname(output_path))
657         f = open(output_path, 'w')
658
659         # Write the header.
660         header_fmt = '\
661 #===-- %s - LLVMBuild Configuration for LLVM %s-*- Makefile -*--===#'
662         header_name = os.path.basename(output_path)
663         header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
664         header_string = header_fmt % (header_name, header_pad)
665         f.write("""\
666 %s
667 #
668 #                     The LLVM Compiler Infrastructure
669 #
670 # This file is distributed under the University of Illinois Open Source
671 # License. See LICENSE.TXT for details.
672 #
673 #===------------------------------------------------------------------------===#
674 #
675 # This file contains the LLVMBuild project information in a format easily
676 # consumed by the Makefile based build system.
677 #
678 # This file is autogenerated by llvm-build, do not edit!
679 #
680 #===------------------------------------------------------------------------===#
681
682 """ % header_string)
683
684         # Write the dependencies for the fragment.
685         #
686         # FIXME: Technically, we need to properly quote for Make here.
687         f.write("""\
688 # Clients must explicitly enable LLVMBUILD_INCLUDE_DEPENDENCIES to get
689 # these dependencies. This is a compromise to help improve the
690 # performance of recursive Make systems.
691 """)
692         f.write('ifeq ($(LLVMBUILD_INCLUDE_DEPENDENCIES),1)\n')
693         f.write("# The dependencies for this Makefile fragment itself.\n")
694         f.write("%s: \\\n" % (mk_quote_string_for_target(output_path),))
695         for dep in dependencies:
696             f.write("\t%s \\\n" % (dep,))
697         f.write('\n')
698
699         # Generate dummy rules for each of the dependencies, so that things
700         # continue to work correctly if any of those files are moved or removed.
701         f.write("""\
702 # The dummy targets to allow proper regeneration even when files are moved or
703 # removed.
704 """)
705         for dep in dependencies:
706             f.write("%s:\n" % (mk_quote_string_for_target(dep),))
707         f.write('endif\n')
708
709         f.write("""
710 # List of libraries to be exported for use by applications.
711 # See 'cmake/modules/Makefile'.
712 LLVM_LIBS_TO_EXPORT :=""")
713         self.foreach_cmake_library(
714             lambda ci:
715                 f.write(' \\\n  %s' % ci.get_prefixed_library_name())
716             ,
717             enabled_optional_components,
718             skip_disabled = True
719             )
720         f.write('\n')
721
722         f.close()
723
724 def add_magic_target_components(parser, project, opts):
725     """add_magic_target_components(project, opts) -> None
726
727     Add the "magic" target based components to the project, which can only be
728     determined based on the target configuration options.
729
730     This currently is responsible for populating the required_libraries list of
731     the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
732     """
733
734     # Determine the available targets.
735     available_targets = dict((ci.name,ci)
736                              for ci in project.component_infos
737                              if ci.type_name == 'TargetGroup')
738
739     # Find the configured native target.
740
741     # We handle a few special cases of target names here for historical
742     # reasons, as these are the names configure currently comes up with.
743     native_target_name = { 'x86' : 'X86',
744                            'x86_64' : 'X86',
745                            'Unknown' : None }.get(opts.native_target,
746                                                   opts.native_target)
747     if native_target_name is None:
748         native_target = None
749     else:
750         native_target = available_targets.get(native_target_name)
751         if native_target is None:
752             parser.error("invalid native target: %r (not in project)" % (
753                     opts.native_target,))
754         if native_target.type_name != 'TargetGroup':
755             parser.error("invalid native target: %r (not a target)" % (
756                     opts.native_target,))
757
758     # Find the list of targets to enable.
759     if opts.enable_targets is None:
760         enable_targets = available_targets.values()
761     else:
762         # We support both space separated and semi-colon separated lists.
763         if opts.enable_targets == '':
764             enable_target_names = []
765         elif ' ' in opts.enable_targets:
766             enable_target_names = opts.enable_targets.split()
767         else:
768             enable_target_names = opts.enable_targets.split(';')
769
770         enable_targets = []
771         for name in enable_target_names:
772             target = available_targets.get(name)
773             if target is None:
774                 parser.error("invalid target to enable: %r (not in project)" % (
775                         name,))
776             if target.type_name != 'TargetGroup':
777                 parser.error("invalid target to enable: %r (not a target)" % (
778                         name,))
779             enable_targets.append(target)
780
781     # Find the special library groups we are going to populate. We enforce that
782     # these appear in the project (instead of just adding them) so that they at
783     # least have an explicit representation in the project LLVMBuild files (and
784     # comments explaining how they are populated).
785     def find_special_group(name):
786         info = info_map.get(name)
787         if info is None:
788             fatal("expected project to contain special %r component" % (
789                     name,))
790
791         if info.type_name != 'LibraryGroup':
792             fatal("special component %r should be a LibraryGroup" % (
793                     name,))
794
795         if info.required_libraries:
796             fatal("special component %r must have empty %r list" % (
797                     name, 'required_libraries'))
798         if info.add_to_library_groups:
799             fatal("special component %r must have empty %r list" % (
800                     name, 'add_to_library_groups'))
801
802         info._is_special_group = True
803         return info
804
805     info_map = dict((ci.name, ci) for ci in project.component_infos)
806     all_targets = find_special_group('all-targets')
807     native_group = find_special_group('Native')
808     native_codegen_group = find_special_group('NativeCodeGen')
809     engine_group = find_special_group('Engine')
810
811     # Set the enabled bit in all the target groups, and append to the
812     # all-targets list.
813     for ci in enable_targets:
814         all_targets.required_libraries.append(ci.name)
815         ci.enabled = True
816
817     # If we have a native target, then that defines the native and
818     # native_codegen libraries.
819     if native_target and native_target.enabled:
820         native_group.required_libraries.append(native_target.name)
821         native_codegen_group.required_libraries.append(
822             '%sCodeGen' % native_target.name)
823
824     # If we have a native target with a JIT, use that for the engine. Otherwise,
825     # use the interpreter.
826     if native_target and native_target.enabled and native_target.has_jit:
827         engine_group.required_libraries.append('MCJIT')
828         engine_group.required_libraries.append(native_group.name)
829     else:
830         engine_group.required_libraries.append('Interpreter')
831
832 def main():
833     from optparse import OptionParser, OptionGroup
834     parser = OptionParser("usage: %prog [options]")
835
836     group = OptionGroup(parser, "Input Options")
837     group.add_option("", "--source-root", dest="source_root", metavar="PATH",
838                       help="Path to the LLVM source (inferred if not given)",
839                       action="store", default=None)
840     group.add_option("", "--llvmbuild-source-root",
841                      dest="llvmbuild_source_root",
842                      help=(
843             "If given, an alternate path to search for LLVMBuild.txt files"),
844                      action="store", default=None, metavar="PATH")
845     group.add_option("", "--build-root", dest="build_root", metavar="PATH",
846                       help="Path to the build directory (if needed) [%default]",
847                       action="store", default=None)
848     parser.add_option_group(group)
849
850     group = OptionGroup(parser, "Output Options")
851     group.add_option("", "--print-tree", dest="print_tree",
852                      help="Print out the project component tree [%default]",
853                      action="store_true", default=False)
854     group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
855                       help="Write out the LLVMBuild.txt files to PATH",
856                       action="store", default=None, metavar="PATH")
857     group.add_option("", "--write-library-table",
858                      dest="write_library_table", metavar="PATH",
859                      help="Write the C++ library dependency table to PATH",
860                      action="store", default=None)
861     group.add_option("", "--write-cmake-fragment",
862                      dest="write_cmake_fragment", metavar="PATH",
863                      help="Write the CMake project information to PATH",
864                      action="store", default=None)
865     group.add_option("", "--write-cmake-exports-fragment",
866                      dest="write_cmake_exports_fragment", metavar="PATH",
867                      help="Write the CMake exports information to PATH",
868                      action="store", default=None)
869     group.add_option("", "--write-make-fragment",
870                       dest="write_make_fragment", metavar="PATH",
871                      help="Write the Makefile project information to PATH",
872                      action="store", default=None)
873     group.add_option("", "--configure-target-def-file",
874                      dest="configure_target_def_files",
875                      help="""Configure the given file at SUBPATH (relative to
876 the inferred or given source root, and with a '.in' suffix) by replacing certain
877 substitution variables with lists of targets that support certain features (for
878 example, targets with AsmPrinters) and write the result to the build root (as
879 given by --build-root) at the same SUBPATH""",
880                      metavar="SUBPATH", action="append", default=None)
881     parser.add_option_group(group)
882
883     group = OptionGroup(parser, "Configuration Options")
884     group.add_option("", "--native-target",
885                       dest="native_target", metavar="NAME",
886                       help=("Treat the named target as the 'native' one, if "
887                             "given [%default]"),
888                       action="store", default=None)
889     group.add_option("", "--enable-targets",
890                       dest="enable_targets", metavar="NAMES",
891                       help=("Enable the given space or semi-colon separated "
892                             "list of targets, or all targets if not present"),
893                       action="store", default=None)
894     group.add_option("", "--enable-optional-components",
895                       dest="optional_components", metavar="NAMES",
896                       help=("Enable the given space or semi-colon separated "
897                             "list of optional components"),
898                       action="store", default="")
899     parser.add_option_group(group)
900
901     (opts, args) = parser.parse_args()
902
903     # Determine the LLVM source path, if not given.
904     source_root = opts.source_root
905     if source_root:
906         if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
907                                            'Function.cpp')):
908             parser.error('invalid LLVM source root: %r' % source_root)
909     else:
910         llvmbuild_path = os.path.dirname(__file__)
911         llvm_build_path = os.path.dirname(llvmbuild_path)
912         utils_path = os.path.dirname(llvm_build_path)
913         source_root = os.path.dirname(utils_path)
914         if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
915                                            'Function.cpp')):
916             parser.error('unable to infer LLVM source root, please specify')
917
918     # Construct the LLVM project information.
919     llvmbuild_source_root = opts.llvmbuild_source_root or source_root
920     project_info = LLVMProjectInfo.load_from_path(
921         source_root, llvmbuild_source_root)
922
923     # Add the magic target based components.
924     add_magic_target_components(parser, project_info, opts)
925
926     # Validate the project component info.
927     project_info.validate_components()
928
929     # Print the component tree, if requested.
930     if opts.print_tree:
931         project_info.print_tree()
932
933     # Write out the components, if requested. This is useful for auto-upgrading
934     # the schema.
935     if opts.write_llvmbuild:
936         project_info.write_components(opts.write_llvmbuild)
937
938     # Write out the required library table, if requested.
939     if opts.write_library_table:
940         project_info.write_library_table(opts.write_library_table,
941                                          opts.optional_components)
942
943     # Write out the make fragment, if requested.
944     if opts.write_make_fragment:
945         project_info.write_make_fragment(opts.write_make_fragment,
946                                          opts.optional_components)
947
948     # Write out the cmake fragment, if requested.
949     if opts.write_cmake_fragment:
950         project_info.write_cmake_fragment(opts.write_cmake_fragment,
951                                           opts.optional_components)
952     if opts.write_cmake_exports_fragment:
953         project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment,
954                                                   opts.optional_components)
955
956     # Configure target definition files, if requested.
957     if opts.configure_target_def_files:
958         # Verify we were given a build root.
959         if not opts.build_root:
960             parser.error("must specify --build-root when using "
961                          "--configure-target-def-file")
962
963         # Create the substitution list.
964         available_targets = [ci for ci in project_info.component_infos
965                              if ci.type_name == 'TargetGroup']
966         substitutions = [
967             ("@LLVM_ENUM_TARGETS@",
968              ' '.join('LLVM_TARGET(%s)' % ci.name
969                       for ci in available_targets)),
970             ("@LLVM_ENUM_ASM_PRINTERS@",
971              ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
972                       for ci in available_targets
973                       if ci.has_asmprinter)),
974             ("@LLVM_ENUM_ASM_PARSERS@",
975              ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
976                       for ci in available_targets
977                       if ci.has_asmparser)),
978             ("@LLVM_ENUM_DISASSEMBLERS@",
979              ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
980                       for ci in available_targets
981                       if ci.has_disassembler))]
982
983         # Configure the given files.
984         for subpath in opts.configure_target_def_files:
985             inpath = os.path.join(source_root, subpath + '.in')
986             outpath = os.path.join(opts.build_root, subpath)
987             result = configutil.configure_file(inpath, outpath, substitutions)
988             if not result:
989                 note("configured file %r hasn't changed" % outpath)
990
991 if __name__=='__main__':
992     main()