blob: 16d816820dbdc59c6acf5b52a59de7ac47e9be6a [file] [log] [blame]
Daniel Dunbarad5e0122011-11-03 17:56:03 +00001import os
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00002import sys
Daniel Dunbarad5e0122011-11-03 17:56:03 +00003
Daniel Dunbardf578252011-11-03 17:56:06 +00004import componentinfo
5
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00006from util import *
7
8###
9
Daniel Dunbardf578252011-11-03 17:56:06 +000010class 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):
Daniel Dunbar1cf14af2011-11-03 17:56:12 +000042 # Store our simple ivars.
Daniel Dunbardf578252011-11-03 17:56:06 +000043 self.source_root = source_root
44 self.component_infos = component_infos
45
Daniel Dunbar1cf14af2011-11-03 17:56:12 +000046 # 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
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +000057 # Add the root component.
Daniel Dunbar86c119a2011-11-03 17:56:16 +000058 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)
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +000062 self.component_infos.append(self.component_info_map['$ROOT'])
Daniel Dunbar86c119a2011-11-03 17:56:16 +000063
Daniel Dunbar1cf14af2011-11-03 17:56:12 +000064 # Topologically order the component information according to their
65 # component references.
66 def visit_component_info(ci, current_stack, current_set):
67 # Check for a cycles.
68 if ci in current_set:
69 # We found a cycle, report it and error out.
70 cycle_description = ' -> '.join(
71 '%r (%s)' % (ci.name, relation)
72 for relation,ci in current_stack)
73 fatal("found cycle to %r after following: %s -> %s" % (
74 ci.name, cycle_description, ci.name))
75
76 # If we have already visited this item, we are done.
77 if ci not in components_to_visit:
78 return
79
80 # Otherwise, mark the component info as visited and traverse.
81 components_to_visit.remove(ci)
82
Daniel Dunbar86c119a2011-11-03 17:56:16 +000083 # Validate the parent reference, which we treat specially.
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +000084 if ci.parent is not None:
85 parent = self.component_info_map.get(ci.parent)
86 if parent is None:
87 fatal("component %r has invalid reference %r (via %r)" % (
88 ci.name, ci.parent, 'parent'))
89 ci.set_parent_instance(parent)
Daniel Dunbar86c119a2011-11-03 17:56:16 +000090
Daniel Dunbar1cf14af2011-11-03 17:56:12 +000091 for relation,referent_name in ci.get_component_references():
92 # Validate that the reference is ok.
93 referent = self.component_info_map.get(referent_name)
94 if referent is None:
95 fatal("component %r has invalid reference %r (via %r)" % (
96 ci.name, referent_name, relation))
97
98 # Visit the reference.
99 current_stack.append((relation,ci))
100 current_set.add(ci)
101 visit_component_info(referent, current_stack, current_set)
102 current_set.remove(ci)
103 current_stack.pop()
104
105 # Finally, add the component info to the ordered list.
106 self.ordered_component_infos.append(ci)
107
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000108 # FIXME: We aren't actually correctly checking for cycles along the
109 # parent edges. Haven't decided how I want to handle this -- I thought
110 # about only checking cycles by relation type. If we do that, it falls
111 # out easily. If we don't, we should special case the check.
112
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000113 self.ordered_component_infos = []
114 components_to_visit = set(component_infos)
115 while components_to_visit:
116 visit_component_info(iter(components_to_visit).next(), [], set())
117
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000118 # Canonicalize children lists.
119 for c in self.ordered_component_infos:
120 c.children.sort(key = lambda c: c.name)
121
122 def print_tree(self):
123 def visit(node, depth = 0):
124 print '%s%-40s (%s)' % (' '*depth, node.name, node.type_name)
125 for c in node.children:
126 visit(c, depth + 1)
127 visit(self.component_info_map['$ROOT'])
128
Daniel Dunbar43120df2011-11-03 17:56:21 +0000129 def write_components(self, output_path):
130 # Organize all the components by the directory their LLVMBuild file
131 # should go in.
132 info_basedir = {}
133 for ci in self.component_infos:
134 # Ignore the $ROOT component.
135 if ci.parent is None:
136 continue
137
138 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
139
140 # Generate the build files.
141 for subpath, infos in info_basedir.items():
142 # Order the components by name to have a canonical ordering.
143 infos.sort(key = lambda ci: ci.name)
144
145 # Format the components into llvmbuild fragments.
146 fragments = filter(None, [ci.get_llvmbuild_fragment()
147 for ci in infos])
148 if not fragments:
149 continue
150
151 assert subpath.startswith('/')
152 directory_path = os.path.join(output_path, subpath[1:])
153
154 # Create the directory if it does not already exist.
155 if not os.path.exists(directory_path):
156 os.makedirs(directory_path)
157
158 # Create the LLVMBuild file.
159 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
160 f = open(file_path, "w")
161 for i,fragment in enumerate(fragments):
162 print >>f, '[component_%d]' % i
163 f.write(fragment)
164 print >>f
165 f.close()
166
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000167def main():
168 from optparse import OptionParser, OptionGroup
169 parser = OptionParser("usage: %prog [options]")
170 parser.add_option("", "--source-root", dest="source_root", metavar="PATH",
171 help="Path to the LLVM source (inferred if not given)",
172 action="store", default=None)
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000173 parser.add_option("", "--print-tree", dest="print_tree",
174 help="Print out the project component tree [%default]",
175 action="store_true", default=False)
Daniel Dunbar43120df2011-11-03 17:56:21 +0000176 parser.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
177 help="Write out the LLVMBuild.txt files to PATH",
178 action="store", default=None, metavar="PATH")
Daniel Dunbardf578252011-11-03 17:56:06 +0000179 parser.add_option(
180 "", "--llvmbuild-source-root", dest="llvmbuild_source_root",
181 help="If given, an alternate path to search for LLVMBuild.txt files",
182 action="store", default=None, metavar="PATH")
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000183 (opts, args) = parser.parse_args()
184
185 # Determine the LLVM source path, if not given.
186 source_root = opts.source_root
187 if source_root:
188 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
189 'Function.cpp')):
190 parser.error('invalid LLVM source root: %r' % source_root)
191 else:
192 llvmbuild_path = os.path.dirname(__file__)
193 llvm_build_path = os.path.dirname(llvmbuild_path)
194 utils_path = os.path.dirname(llvm_build_path)
195 source_root = os.path.dirname(utils_path)
196 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
197 'Function.cpp')):
198 parser.error('unable to infer LLVM source root, please specify')
199
Daniel Dunbardf578252011-11-03 17:56:06 +0000200 # Construct the LLVM project information.
201 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
202 project_info = LLVMProjectInfo.load_from_path(
203 source_root, llvmbuild_source_root)
204
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000205 # Print the component tree, if requested.
206 if opts.print_tree:
207 project_info.print_tree()
208
Daniel Dunbar43120df2011-11-03 17:56:21 +0000209 # Write out the components, if requested. This is useful for auto-upgrading
210 # the schema.
211 if opts.write_llvmbuild:
212 project_info.write_components(opts.write_llvmbuild)
213
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000214if __name__=='__main__':
215 main()