[InstCombine] Signed saturation patterns
[llvm-complete.git] / utils / llvm-build / llvmbuild / main.py
blob99b82ad5e20c365119336df042f56f060a45cb99
1 from __future__ import absolute_import
2 import filecmp
3 import os
4 import sys
6 import llvmbuild.componentinfo as componentinfo
8 from llvmbuild.util import fatal, note
10 ###
12 def cmake_quote_string(value):
13 """
14 cmake_quote_string(value) -> str
16 Return a quoted form of the given value that is suitable for use in CMake
17 language files.
18 """
20 # Currently, we only handle escaping backslashes.
21 value = value.replace("\\", "\\\\")
23 return value
25 def cmake_quote_path(value):
26 """
27 cmake_quote_path(value) -> str
29 Return a quoted form of the given value that is suitable for use in CMake
30 language files.
31 """
33 # CMake has a bug in it's Makefile generator that doesn't properly quote
34 # strings it generates. So instead of using proper quoting, we just use "/"
35 # style paths. Currently, we only handle escaping backslashes.
36 value = value.replace("\\", "/")
38 return value
40 def make_install_dir(path):
41 """
42 make_install_dir(path) -> None
44 Create the given directory path for installation, including any parents.
45 """
47 # os.makedirs considers it an error to be called with an existent path.
48 if not os.path.exists(path):
49 os.makedirs(path)
51 ###
53 class LLVMProjectInfo(object):
54 @staticmethod
55 def load_infos_from_path(llvmbuild_source_root):
56 def recurse(subpath):
57 # Load the LLVMBuild file.
58 llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
59 'LLVMBuild.txt')
60 if not os.path.exists(llvmbuild_path):
61 fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
63 # Parse the components from it.
64 common,info_iter = componentinfo.load_from_path(llvmbuild_path,
65 subpath)
66 for info in info_iter:
67 yield info
69 # Recurse into the specified subdirectories.
70 for subdir in common.get_list("subdirectories"):
71 for item in recurse(os.path.join(subpath, subdir)):
72 yield item
74 return recurse("/")
76 @staticmethod
77 def load_from_path(source_root, llvmbuild_source_root):
78 infos = list(
79 LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
81 return LLVMProjectInfo(source_root, infos)
83 def __init__(self, source_root, component_infos):
84 # Store our simple ivars.
85 self.source_root = source_root
86 self.component_infos = list(component_infos)
87 self.component_info_map = None
88 self.ordered_component_infos = None
90 def validate_components(self):
91 """validate_components() -> None
93 Validate that the project components are well-defined. Among other
94 things, this checks that:
95 - Components have valid references.
96 - Components references do not form cycles.
98 We also construct the map from component names to info, and the
99 topological ordering of components.
102 # Create the component info map and validate that component names are
103 # unique.
104 self.component_info_map = {}
105 for ci in self.component_infos:
106 existing = self.component_info_map.get(ci.name)
107 if existing is not None:
108 # We found a duplicate component name, report it and error out.
109 fatal("found duplicate component %r (at %r and %r)" % (
110 ci.name, ci.subpath, existing.subpath))
111 self.component_info_map[ci.name] = ci
113 # Disallow 'all' as a component name, which is a special case.
114 if 'all' in self.component_info_map:
115 fatal("project is not allowed to define 'all' component")
117 # Add the root component.
118 if '$ROOT' in self.component_info_map:
119 fatal("project is not allowed to define $ROOT component")
120 self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
121 '/', '$ROOT', None)
122 self.component_infos.append(self.component_info_map['$ROOT'])
124 # Topologically order the component information according to their
125 # component references.
126 def visit_component_info(ci, current_stack, current_set):
127 # Check for a cycles.
128 if ci in current_set:
129 # We found a cycle, report it and error out.
130 cycle_description = ' -> '.join(
131 '%r (%s)' % (ci.name, relation)
132 for relation,ci in current_stack)
133 fatal("found cycle to %r after following: %s -> %s" % (
134 ci.name, cycle_description, ci.name))
136 # If we have already visited this item, we are done.
137 if ci not in components_to_visit:
138 return
140 # Otherwise, mark the component info as visited and traverse.
141 components_to_visit.remove(ci)
143 # Validate the parent reference, which we treat specially.
144 if ci.parent is not None:
145 parent = self.component_info_map.get(ci.parent)
146 if parent is None:
147 fatal("component %r has invalid reference %r (via %r)" % (
148 ci.name, ci.parent, 'parent'))
149 ci.set_parent_instance(parent)
151 for relation,referent_name in ci.get_component_references():
152 # Validate that the reference is ok.
153 referent = self.component_info_map.get(referent_name)
154 if referent is None:
155 fatal("component %r has invalid reference %r (via %r)" % (
156 ci.name, referent_name, relation))
158 # Visit the reference.
159 current_stack.append((relation,ci))
160 current_set.add(ci)
161 visit_component_info(referent, current_stack, current_set)
162 current_set.remove(ci)
163 current_stack.pop()
165 # Finally, add the component info to the ordered list.
166 self.ordered_component_infos.append(ci)
168 # FIXME: We aren't actually correctly checking for cycles along the
169 # parent edges. Haven't decided how I want to handle this -- I thought
170 # about only checking cycles by relation type. If we do that, it falls
171 # out easily. If we don't, we should special case the check.
173 self.ordered_component_infos = []
174 components_to_visit = sorted(
175 set(self.component_infos),
176 key = lambda c: c.name)
177 while components_to_visit:
178 visit_component_info(components_to_visit[0], [], set())
180 # Canonicalize children lists.
181 for c in self.ordered_component_infos:
182 c.children.sort(key = lambda c: c.name)
184 def print_tree(self):
185 def visit(node, depth = 0):
186 print('%s%-40s (%s)' % (' '*depth, node.name, node.type_name))
187 for c in node.children:
188 visit(c, depth + 1)
189 visit(self.component_info_map['$ROOT'])
191 def write_components(self, output_path):
192 # Organize all the components by the directory their LLVMBuild file
193 # should go in.
194 info_basedir = {}
195 for ci in self.component_infos:
196 # Ignore the $ROOT component.
197 if ci.parent is None:
198 continue
200 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
202 # Compute the list of subdirectories to scan.
203 subpath_subdirs = {}
204 for ci in self.component_infos:
205 # Ignore root components.
206 if ci.subpath == '/':
207 continue
209 # Otherwise, append this subpath to the parent list.
210 parent_path = os.path.dirname(ci.subpath)
211 subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
212 parent_path, set())
213 parent_list.add(os.path.basename(ci.subpath))
215 # Generate the build files.
216 for subpath, infos in info_basedir.items():
217 # Order the components by name to have a canonical ordering.
218 infos.sort(key = lambda ci: ci.name)
220 # Format the components into llvmbuild fragments.
221 fragments = []
223 # Add the common fragments.
224 subdirectories = subpath_subdirs.get(subpath)
225 if subdirectories:
226 fragment = """\
227 subdirectories = %s
228 """ % (" ".join(sorted(subdirectories)),)
229 fragments.append(("common", fragment))
231 # Add the component fragments.
232 num_common_fragments = len(fragments)
233 for ci in infos:
234 fragment = ci.get_llvmbuild_fragment()
235 if fragment is None:
236 continue
238 name = "component_%d" % (len(fragments) - num_common_fragments)
239 fragments.append((name, fragment))
241 if not fragments:
242 continue
244 assert subpath.startswith('/')
245 directory_path = os.path.join(output_path, subpath[1:])
247 # Create the directory if it does not already exist.
248 if not os.path.exists(directory_path):
249 os.makedirs(directory_path)
251 # In an effort to preserve comments (which aren't parsed), read in
252 # the original file and extract the comments. We only know how to
253 # associate comments that prefix a section name.
254 f = open(infos[0]._source_path)
255 comments_map = {}
256 comment_block = ""
257 for ln in f:
258 if ln.startswith(';'):
259 comment_block += ln
260 elif ln.startswith('[') and ln.endswith(']\n'):
261 comments_map[ln[1:-2]] = comment_block
262 else:
263 comment_block = ""
264 f.close()
266 # Create the LLVMBuild fil[e.
267 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
268 f = open(file_path, "w")
270 # Write the header.
271 header_fmt = ';===- %s %s-*- Conf -*--===;'
272 header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
273 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
274 header_string = header_fmt % (header_name, header_pad)
275 f.write("""\
278 ; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
279 ; See https://llvm.org/LICENSE.txt for license information.
280 ; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
282 ;===------------------------------------------------------------------------===;
284 ; This is an LLVMBuild description file for the components in this subdirectory.
286 ; For more information on the LLVMBuild system, please see:
288 ; http://llvm.org/docs/LLVMBuild.html
290 ;===------------------------------------------------------------------------===;
292 """ % header_string)
294 # Write out each fragment.each component fragment.
295 for name,fragment in fragments:
296 comment = comments_map.get(name)
297 if comment is not None:
298 f.write(comment)
299 f.write("[%s]\n" % name)
300 f.write(fragment)
301 if fragment is not fragments[-1][1]:
302 f.write('\n')
304 f.close()
306 def write_library_table(self, output_path, enabled_optional_components):
307 # Write out the mapping from component names to required libraries.
309 # We do this in topological order so that we know we can append the
310 # dependencies for added library groups.
311 entries = {}
312 for c in self.ordered_component_infos:
313 # Skip optional components which are not enabled.
314 if c.type_name == 'OptionalLibrary' \
315 and c.name not in enabled_optional_components:
316 continue
318 # Skip target groups which are not enabled.
319 tg = c.get_parent_target_group()
320 if tg and not tg.enabled:
321 continue
323 # Only certain components are in the table.
324 if c.type_name not in ('Library', 'OptionalLibrary', \
325 'LibraryGroup', 'TargetGroup'):
326 continue
328 # Compute the llvm-config "component name". For historical reasons,
329 # this is lowercased based on the library name.
330 llvmconfig_component_name = c.get_llvmconfig_component_name()
332 # Get the library name, or None for LibraryGroups.
333 if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
334 library_name = c.get_prefixed_library_name()
335 is_installed = c.installed
336 else:
337 library_name = None
338 is_installed = True
340 # Get the component names of all the required libraries.
341 required_llvmconfig_component_names = [
342 self.component_info_map[dep].get_llvmconfig_component_name()
343 for dep in c.required_libraries]
345 # Insert the entries for library groups we should add to.
346 for dep in c.add_to_library_groups:
347 entries[dep][2].append(llvmconfig_component_name)
349 # Add the entry.
350 entries[c.name] = (llvmconfig_component_name, library_name,
351 required_llvmconfig_component_names,
352 is_installed)
354 # Convert to a list of entries and sort by name.
355 entries = list(entries.values())
357 # Create an 'all' pseudo component. We keep the dependency list small by
358 # only listing entries that have no other dependents.
359 root_entries = set(e[0] for e in entries)
360 for _,_,deps,_ in entries:
361 root_entries -= set(deps)
362 entries.append(('all', None, sorted(root_entries), True))
364 entries.sort()
366 # Compute the maximum number of required libraries, plus one so there is
367 # always a sentinel.
368 max_required_libraries = max(len(deps)
369 for _,_,deps,_ in entries) + 1
371 # Write out the library table.
372 make_install_dir(os.path.dirname(output_path))
373 f = open(output_path+'.new', 'w')
374 f.write("""\
375 //===- llvm-build generated file --------------------------------*- C++ -*-===//
377 // Component Library Dependency Table
379 // Automatically generated file, do not edit!
381 //===----------------------------------------------------------------------===//
383 """)
384 f.write('struct AvailableComponent {\n')
385 f.write(' /// The name of the component.\n')
386 f.write(' const char *Name;\n')
387 f.write('\n')
388 f.write(' /// The name of the library for this component (or NULL).\n')
389 f.write(' const char *Library;\n')
390 f.write('\n')
391 f.write(' /// Whether the component is installed.\n')
392 f.write(' bool IsInstalled;\n')
393 f.write('\n')
394 f.write('\
395 /// The list of libraries required when linking this component.\n')
396 f.write(' const char *RequiredLibraries[%d];\n' % (
397 max_required_libraries))
398 f.write('} AvailableComponents[%d] = {\n' % len(entries))
399 for name,library_name,required_names,is_installed in entries:
400 if library_name is None:
401 library_name_as_cstr = 'nullptr'
402 else:
403 library_name_as_cstr = '"%s"' % library_name
404 if is_installed:
405 is_installed_as_cstr = 'true'
406 else:
407 is_installed_as_cstr = 'false'
408 f.write(' { "%s", %s, %s, { %s } },\n' % (
409 name, library_name_as_cstr, is_installed_as_cstr,
410 ', '.join('"%s"' % dep
411 for dep in required_names)))
412 f.write('};\n')
413 f.close()
415 if not os.path.isfile(output_path):
416 os.rename(output_path+'.new', output_path)
417 elif filecmp.cmp(output_path, output_path+'.new'):
418 os.remove(output_path+'.new')
419 else:
420 os.remove(output_path)
421 os.rename(output_path+'.new', output_path)
423 def get_required_libraries_for_component(self, ci, traverse_groups = False):
425 get_required_libraries_for_component(component_info) -> iter
427 Given a Library component info descriptor, return an iterator over all
428 of the directly required libraries for linking with this component. If
429 traverse_groups is True, then library and target groups will be
430 traversed to include their required libraries.
433 assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup')
435 for name in ci.required_libraries:
436 # Get the dependency info.
437 dep = self.component_info_map[name]
439 # If it is a library, yield it.
440 if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary':
441 yield dep
442 continue
444 # Otherwise if it is a group, yield or traverse depending on what
445 # was requested.
446 if dep.type_name in ('LibraryGroup', 'TargetGroup'):
447 if not traverse_groups:
448 yield dep
449 continue
451 for res in self.get_required_libraries_for_component(dep, True):
452 yield res
454 def get_fragment_dependencies(self):
456 get_fragment_dependencies() -> iter
458 Compute the list of files (as absolute paths) on which the output
459 fragments depend (i.e., files for which a modification should trigger a
460 rebuild of the fragment).
463 # Construct a list of all the dependencies of the Makefile fragment
464 # itself. These include all the LLVMBuild files themselves, as well as
465 # all of our own sources.
467 # Many components may come from the same file, so we make sure to unique
468 # these.
469 build_paths = set()
470 for ci in self.component_infos:
471 p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
472 if p not in build_paths:
473 yield p
474 build_paths.add(p)
476 # Gather the list of necessary sources by just finding all loaded
477 # modules that are inside the LLVM source tree.
478 for module in sys.modules.values():
479 # Find the module path.
480 if not hasattr(module, '__file__'):
481 continue
482 path = getattr(module, '__file__')
483 if not path:
484 continue
486 # Strip off any compiled suffix.
487 if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
488 path = path[:-1]
490 # If the path exists and is in the source tree, consider it a
491 # dependency.
492 if (path.startswith(self.source_root) and os.path.exists(path)):
493 yield path
495 def foreach_cmake_library(self, f,
496 enabled_optional_components,
497 skip_disabled,
498 skip_not_installed):
499 for ci in self.ordered_component_infos:
500 # Skip optional components which are not enabled.
501 if ci.type_name == 'OptionalLibrary' \
502 and ci.name not in enabled_optional_components:
503 continue
505 # We only write the information for libraries currently.
506 if ci.type_name not in ('Library', 'OptionalLibrary'):
507 continue
509 # Skip disabled targets.
510 if skip_disabled:
511 tg = ci.get_parent_target_group()
512 if tg and not tg.enabled:
513 continue
515 # Skip targets that will not be installed
516 if skip_not_installed and not ci.installed:
517 continue
519 f(ci)
522 def write_cmake_fragment(self, output_path, enabled_optional_components):
524 write_cmake_fragment(output_path) -> None
526 Generate a CMake fragment which includes all of the collated LLVMBuild
527 information in a format that is easily digestible by a CMake. The exact
528 contents of this are closely tied to how the CMake configuration
529 integrates LLVMBuild, see CMakeLists.txt in the top-level.
532 dependencies = list(self.get_fragment_dependencies())
534 # Write out the CMake fragment.
535 make_install_dir(os.path.dirname(output_path))
536 f = open(output_path, 'w')
538 # Write the header.
539 header_fmt = '\
540 #===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
541 header_name = os.path.basename(output_path)
542 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
543 header_string = header_fmt % (header_name, header_pad)
544 f.write("""\
547 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
548 # See https://llvm.org/LICENSE.txt for license information.
549 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
551 #===------------------------------------------------------------------------===#
553 # This file contains the LLVMBuild project information in a format easily
554 # consumed by the CMake based build system.
556 # This file is autogenerated by llvm-build, do not edit!
558 #===------------------------------------------------------------------------===#
560 """ % header_string)
562 # Write the dependency information in the best way we can.
563 f.write("""
564 # LLVMBuild CMake fragment dependencies.
566 # CMake has no builtin way to declare that the configuration depends on
567 # a particular file. However, a side effect of configure_file is to add
568 # said input file to CMake's internal dependency list. So, we use that
569 # and a dummy output file to communicate the dependency information to
570 # CMake.
572 # FIXME: File a CMake RFE to get a properly supported version of this
573 # feature.
574 """)
575 for dep in dependencies:
576 f.write("""\
577 configure_file(\"%s\"
578 ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % (
579 cmake_quote_path(dep),))
581 # Write the properties we use to encode the required library dependency
582 # information in a form CMake can easily use directly.
583 f.write("""
584 # Explicit library dependency information.
586 # The following property assignments effectively create a map from component
587 # names to required libraries, in a way that is easily accessed from CMake.
588 """)
589 self.foreach_cmake_library(
590 lambda ci:
591 f.write("""\
592 set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % (
593 ci.get_prefixed_library_name(), " ".join(sorted(
594 dep.get_prefixed_library_name()
595 for dep in self.get_required_libraries_for_component(ci)))))
597 enabled_optional_components,
598 skip_disabled = False,
599 skip_not_installed = False # Dependency info must be emitted for internals libs too
602 f.close()
604 def write_cmake_exports_fragment(self, output_path, enabled_optional_components):
606 write_cmake_exports_fragment(output_path) -> None
608 Generate a CMake fragment which includes LLVMBuild library
609 dependencies expressed similarly to how CMake would write
610 them via install(EXPORT).
613 dependencies = list(self.get_fragment_dependencies())
615 # Write out the CMake exports fragment.
616 make_install_dir(os.path.dirname(output_path))
617 f = open(output_path, 'w')
619 f.write("""\
620 # Explicit library dependency information.
622 # The following property assignments tell CMake about link
623 # dependencies of libraries imported from LLVM.
624 """)
625 self.foreach_cmake_library(
626 lambda ci:
627 f.write("""\
628 set_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
629 ci.get_prefixed_library_name(), " ".join(sorted(
630 dep.get_prefixed_library_name()
631 for dep in self.get_required_libraries_for_component(ci)))))
633 enabled_optional_components,
634 skip_disabled = True,
635 skip_not_installed = True # Do not export internal libraries like gtest
638 f.close()
640 def add_magic_target_components(parser, project, opts):
641 """add_magic_target_components(project, opts) -> None
643 Add the "magic" target based components to the project, which can only be
644 determined based on the target configuration options.
646 This currently is responsible for populating the required_libraries list of
647 the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
650 # Determine the available targets.
651 available_targets = dict((ci.name,ci)
652 for ci in project.component_infos
653 if ci.type_name == 'TargetGroup')
655 # Find the configured native target.
657 # We handle a few special cases of target names here for historical
658 # reasons, as these are the names configure currently comes up with.
659 native_target_name = { 'x86' : 'X86',
660 'x86_64' : 'X86',
661 'Unknown' : None }.get(opts.native_target,
662 opts.native_target)
663 if native_target_name is None:
664 native_target = None
665 else:
666 native_target = available_targets.get(native_target_name)
667 if native_target is None:
668 parser.error("invalid native target: %r (not in project)" % (
669 opts.native_target,))
670 if native_target.type_name != 'TargetGroup':
671 parser.error("invalid native target: %r (not a target)" % (
672 opts.native_target,))
674 # Find the list of targets to enable.
675 if opts.enable_targets is None:
676 enable_targets = available_targets.values()
677 else:
678 # We support both space separated and semi-colon separated lists.
679 if opts.enable_targets == '':
680 enable_target_names = []
681 elif ' ' in opts.enable_targets:
682 enable_target_names = opts.enable_targets.split()
683 else:
684 enable_target_names = opts.enable_targets.split(';')
686 enable_targets = []
687 for name in enable_target_names:
688 target = available_targets.get(name)
689 if target is None:
690 parser.error("invalid target to enable: %r (not in project)" % (
691 name,))
692 if target.type_name != 'TargetGroup':
693 parser.error("invalid target to enable: %r (not a target)" % (
694 name,))
695 enable_targets.append(target)
697 # Find the special library groups we are going to populate. We enforce that
698 # these appear in the project (instead of just adding them) so that they at
699 # least have an explicit representation in the project LLVMBuild files (and
700 # comments explaining how they are populated).
701 def find_special_group(name):
702 info = info_map.get(name)
703 if info is None:
704 fatal("expected project to contain special %r component" % (
705 name,))
707 if info.type_name != 'LibraryGroup':
708 fatal("special component %r should be a LibraryGroup" % (
709 name,))
711 if info.required_libraries:
712 fatal("special component %r must have empty %r list" % (
713 name, 'required_libraries'))
714 if info.add_to_library_groups:
715 fatal("special component %r must have empty %r list" % (
716 name, 'add_to_library_groups'))
718 info._is_special_group = True
719 return info
721 info_map = dict((ci.name, ci) for ci in project.component_infos)
722 all_targets = find_special_group('all-targets')
723 native_group = find_special_group('Native')
724 native_codegen_group = find_special_group('NativeCodeGen')
725 engine_group = find_special_group('Engine')
727 # Set the enabled bit in all the target groups, and append to the
728 # all-targets list.
729 for ci in enable_targets:
730 all_targets.required_libraries.append(ci.name)
731 ci.enabled = True
733 # If we have a native target, then that defines the native and
734 # native_codegen libraries.
735 if native_target and native_target.enabled:
736 native_group.required_libraries.append(native_target.name)
737 native_codegen_group.required_libraries.append(
738 '%sCodeGen' % native_target.name)
740 # If we have a native target with a JIT, use that for the engine. Otherwise,
741 # use the interpreter.
742 if native_target and native_target.enabled and native_target.has_jit:
743 engine_group.required_libraries.append('MCJIT')
744 engine_group.required_libraries.append(native_group.name)
745 else:
746 engine_group.required_libraries.append('Interpreter')
748 def main():
749 from optparse import OptionParser, OptionGroup
750 parser = OptionParser("usage: %prog [options]")
752 group = OptionGroup(parser, "Input Options")
753 group.add_option("", "--source-root", dest="source_root", metavar="PATH",
754 help="Path to the LLVM source (inferred if not given)",
755 action="store", default=None)
756 group.add_option("", "--llvmbuild-source-root",
757 dest="llvmbuild_source_root",
758 help=(
759 "If given, an alternate path to search for LLVMBuild.txt files"),
760 action="store", default=None, metavar="PATH")
761 parser.add_option_group(group)
763 group = OptionGroup(parser, "Output Options")
764 group.add_option("", "--print-tree", dest="print_tree",
765 help="Print out the project component tree [%default]",
766 action="store_true", default=False)
767 group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
768 help="Write out the LLVMBuild.txt files to PATH",
769 action="store", default=None, metavar="PATH")
770 group.add_option("", "--write-library-table",
771 dest="write_library_table", metavar="PATH",
772 help="Write the C++ library dependency table to PATH",
773 action="store", default=None)
774 group.add_option("", "--write-cmake-fragment",
775 dest="write_cmake_fragment", metavar="PATH",
776 help="Write the CMake project information to PATH",
777 action="store", default=None)
778 group.add_option("", "--write-cmake-exports-fragment",
779 dest="write_cmake_exports_fragment", metavar="PATH",
780 help="Write the CMake exports information to PATH",
781 action="store", default=None)
782 parser.add_option_group(group)
784 group = OptionGroup(parser, "Configuration Options")
785 group.add_option("", "--native-target",
786 dest="native_target", metavar="NAME",
787 help=("Treat the named target as the 'native' one, if "
788 "given [%default]"),
789 action="store", default=None)
790 group.add_option("", "--enable-targets",
791 dest="enable_targets", metavar="NAMES",
792 help=("Enable the given space or semi-colon separated "
793 "list of targets, or all targets if not present"),
794 action="store", default=None)
795 group.add_option("", "--enable-optional-components",
796 dest="optional_components", metavar="NAMES",
797 help=("Enable the given space or semi-colon separated "
798 "list of optional components"),
799 action="store", default="")
800 parser.add_option_group(group)
802 (opts, args) = parser.parse_args()
804 # Determine the LLVM source path, if not given.
805 source_root = opts.source_root
806 if source_root:
807 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
808 'Function.cpp')):
809 parser.error('invalid LLVM source root: %r' % source_root)
810 else:
811 llvmbuild_path = os.path.dirname(__file__)
812 llvm_build_path = os.path.dirname(llvmbuild_path)
813 utils_path = os.path.dirname(llvm_build_path)
814 source_root = os.path.dirname(utils_path)
815 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
816 'Function.cpp')):
817 parser.error('unable to infer LLVM source root, please specify')
819 # Construct the LLVM project information.
820 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
821 project_info = LLVMProjectInfo.load_from_path(
822 source_root, llvmbuild_source_root)
824 # Add the magic target based components.
825 add_magic_target_components(parser, project_info, opts)
827 # Validate the project component info.
828 project_info.validate_components()
830 # Print the component tree, if requested.
831 if opts.print_tree:
832 project_info.print_tree()
834 # Write out the components, if requested. This is useful for auto-upgrading
835 # the schema.
836 if opts.write_llvmbuild:
837 project_info.write_components(opts.write_llvmbuild)
839 # Write out the required library table, if requested.
840 if opts.write_library_table:
841 project_info.write_library_table(opts.write_library_table,
842 opts.optional_components)
844 # Write out the cmake fragment, if requested.
845 if opts.write_cmake_fragment:
846 project_info.write_cmake_fragment(opts.write_cmake_fragment,
847 opts.optional_components)
848 if opts.write_cmake_exports_fragment:
849 project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment,
850 opts.optional_components)
852 if __name__=='__main__':
853 main()