e04f0acb9679631a981fe3b4dfe5a66b90d18739
[oota-llvm.git] / utils / llvm-build / llvmbuild / main.py
1 import os
2 import sys
3
4 import componentinfo
5
6 from util import *
7
8 ###
9
10 class LLVMProjectInfo(object):
11     @staticmethod
12     def load_infos_from_path(llvmbuild_source_root):
13         # FIXME: Implement a simple subpath file list cache, so we don't restat
14         # directories we have already traversed.
15
16         # First, discover all the LLVMBuild.txt files.
17         for dirpath,dirnames,filenames in os.walk(llvmbuild_source_root,
18                                                   followlinks = True):
19             # If there is no LLVMBuild.txt file in a directory, we don't recurse
20             # past it. This is a simple way to prune our search, although it
21             # makes it easy for users to add LLVMBuild.txt files in places they
22             # won't be seen.
23             if 'LLVMBuild.txt' not in filenames:
24                 del dirnames[:]
25                 continue
26
27             # Otherwise, load the LLVMBuild file in this directory.
28             assert dirpath.startswith(llvmbuild_source_root)
29             subpath = '/' + dirpath[len(llvmbuild_source_root)+1:]
30             llvmbuild_path = os.path.join(dirpath, 'LLVMBuild.txt')
31             for info in componentinfo.load_from_path(llvmbuild_path, subpath):
32                 yield info
33
34     @staticmethod
35     def load_from_path(source_root, llvmbuild_source_root):
36         infos = list(
37             LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
38
39         return LLVMProjectInfo(source_root, infos)
40
41     def __init__(self, source_root, component_infos):
42         # Store our simple ivars.
43         self.source_root = source_root
44         self.component_infos = component_infos
45
46         # Create the component info map and validate that component names are
47         # unique.
48         self.component_info_map = {}
49         for ci in component_infos:
50             existing = self.component_info_map.get(ci.name)
51             if existing is not None:
52                 # We found a duplicate component name, report it and error out.
53                 fatal("found duplicate component %r (at %r and %r)" % (
54                         ci.name, ci.subpath, existing.subpath))
55             self.component_info_map[ci.name] = ci
56
57         # Add the root component, which does not go in any list.
58         if '$ROOT' in self.component_info_map:
59             fatal("project is not allowed to define $ROOT component")
60         self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
61             '/', '$ROOT', None)
62
63         # Topologically order the component information according to their
64         # component references.
65         def visit_component_info(ci, current_stack, current_set):
66             # Check for a cycles.
67             if ci in current_set:
68                 # We found a cycle, report it and error out.
69                 cycle_description = ' -> '.join(
70                     '%r (%s)' % (ci.name, relation)
71                     for relation,ci in current_stack)
72                 fatal("found cycle to %r after following: %s -> %s" % (
73                         ci.name, cycle_description, ci.name))
74
75             # If we have already visited this item, we are done.
76             if ci not in components_to_visit:
77                 return
78
79             # Otherwise, mark the component info as visited and traverse.
80             components_to_visit.remove(ci)
81
82             # Validate the parent reference, which we treat specially.
83             parent = self.component_info_map.get(ci.parent)
84             if parent is None:
85                 fatal("component %r has invalid reference %r (via %r)" % (
86                         ci.name, ci.parent, 'parent'))
87             ci.set_parent_instance(parent)
88
89             for relation,referent_name in ci.get_component_references():
90                 # Validate that the reference is ok.
91                 referent = self.component_info_map.get(referent_name)
92                 if referent is None:
93                     fatal("component %r has invalid reference %r (via %r)" % (
94                             ci.name, referent_name, relation))
95
96                 # Visit the reference.
97                 current_stack.append((relation,ci))
98                 current_set.add(ci)
99                 visit_component_info(referent, current_stack, current_set)
100                 current_set.remove(ci)
101                 current_stack.pop()
102
103             # Finally, add the component info to the ordered list.
104             self.ordered_component_infos.append(ci)
105
106         # FIXME: We aren't actually correctly checking for cycles along the
107         # parent edges. Haven't decided how I want to handle this -- I thought
108         # about only checking cycles by relation type. If we do that, it falls
109         # out easily. If we don't, we should special case the check.
110
111         self.ordered_component_infos = []
112         components_to_visit = set(component_infos)
113         while components_to_visit:
114             visit_component_info(iter(components_to_visit).next(), [], set())
115
116 def main():
117     from optparse import OptionParser, OptionGroup
118     parser = OptionParser("usage: %prog [options]")
119     parser.add_option("", "--source-root", dest="source_root", metavar="PATH",
120                       help="Path to the LLVM source (inferred if not given)",
121                       action="store", default=None)
122     parser.add_option(
123         "", "--llvmbuild-source-root", dest="llvmbuild_source_root",
124         help="If given, an alternate path to search for LLVMBuild.txt files",
125         action="store", default=None, metavar="PATH")
126     (opts, args) = parser.parse_args()
127
128     # Determine the LLVM source path, if not given.
129     source_root = opts.source_root
130     if source_root:
131         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
132                                            'Function.cpp')):
133             parser.error('invalid LLVM source root: %r' % source_root)
134     else:
135         llvmbuild_path = os.path.dirname(__file__)
136         llvm_build_path = os.path.dirname(llvmbuild_path)
137         utils_path = os.path.dirname(llvm_build_path)
138         source_root = os.path.dirname(utils_path)
139         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
140                                            'Function.cpp')):
141             parser.error('unable to infer LLVM source root, please specify')
142
143     # Construct the LLVM project information.
144     llvmbuild_source_root = opts.llvmbuild_source_root or source_root
145     project_info = LLVMProjectInfo.load_from_path(
146         source_root, llvmbuild_source_root)
147
148 if __name__=='__main__':
149     main()