Daniel Dunbar | ad5e012 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 1 | import os |
Daniel Dunbar | 1cf14af | 2011-11-03 17:56:12 +0000 | [diff] [blame^] | 2 | import sys |
Daniel Dunbar | ad5e012 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 3 | |
Daniel Dunbar | df57825 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 4 | import componentinfo |
| 5 | |
Daniel Dunbar | 1cf14af | 2011-11-03 17:56:12 +0000 | [diff] [blame^] | 6 | from util import * |
| 7 | |
| 8 | ### |
| 9 | |
Daniel Dunbar | df57825 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 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): |
Daniel Dunbar | 1cf14af | 2011-11-03 17:56:12 +0000 | [diff] [blame^] | 42 | # Store our simple ivars. |
Daniel Dunbar | df57825 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 43 | self.source_root = source_root |
| 44 | self.component_infos = component_infos |
| 45 | |
Daniel Dunbar | 1cf14af | 2011-11-03 17:56:12 +0000 | [diff] [blame^] | 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 | # Topologically order the component information according to their |
| 58 | # component references. |
| 59 | def visit_component_info(ci, current_stack, current_set): |
| 60 | # Check for a cycles. |
| 61 | if ci in current_set: |
| 62 | # We found a cycle, report it and error out. |
| 63 | cycle_description = ' -> '.join( |
| 64 | '%r (%s)' % (ci.name, relation) |
| 65 | for relation,ci in current_stack) |
| 66 | fatal("found cycle to %r after following: %s -> %s" % ( |
| 67 | ci.name, cycle_description, ci.name)) |
| 68 | |
| 69 | # If we have already visited this item, we are done. |
| 70 | if ci not in components_to_visit: |
| 71 | return |
| 72 | |
| 73 | # Otherwise, mark the component info as visited and traverse. |
| 74 | components_to_visit.remove(ci) |
| 75 | |
| 76 | for relation,referent_name in ci.get_component_references(): |
| 77 | # Validate that the reference is ok. |
| 78 | referent = self.component_info_map.get(referent_name) |
| 79 | if referent is None: |
| 80 | fatal("component %r has invalid reference %r (via %r)" % ( |
| 81 | ci.name, referent_name, relation)) |
| 82 | |
| 83 | # Visit the reference. |
| 84 | current_stack.append((relation,ci)) |
| 85 | current_set.add(ci) |
| 86 | visit_component_info(referent, current_stack, current_set) |
| 87 | current_set.remove(ci) |
| 88 | current_stack.pop() |
| 89 | |
| 90 | # Finally, add the component info to the ordered list. |
| 91 | self.ordered_component_infos.append(ci) |
| 92 | |
| 93 | self.ordered_component_infos = [] |
| 94 | components_to_visit = set(component_infos) |
| 95 | while components_to_visit: |
| 96 | visit_component_info(iter(components_to_visit).next(), [], set()) |
| 97 | |
Daniel Dunbar | ad5e012 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 98 | def main(): |
| 99 | from optparse import OptionParser, OptionGroup |
| 100 | parser = OptionParser("usage: %prog [options]") |
| 101 | parser.add_option("", "--source-root", dest="source_root", metavar="PATH", |
| 102 | help="Path to the LLVM source (inferred if not given)", |
| 103 | action="store", default=None) |
Daniel Dunbar | df57825 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 104 | parser.add_option( |
| 105 | "", "--llvmbuild-source-root", dest="llvmbuild_source_root", |
| 106 | help="If given, an alternate path to search for LLVMBuild.txt files", |
| 107 | action="store", default=None, metavar="PATH") |
Daniel Dunbar | ad5e012 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 108 | (opts, args) = parser.parse_args() |
| 109 | |
| 110 | # Determine the LLVM source path, if not given. |
| 111 | source_root = opts.source_root |
| 112 | if source_root: |
| 113 | if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore', |
| 114 | 'Function.cpp')): |
| 115 | parser.error('invalid LLVM source root: %r' % source_root) |
| 116 | else: |
| 117 | llvmbuild_path = os.path.dirname(__file__) |
| 118 | llvm_build_path = os.path.dirname(llvmbuild_path) |
| 119 | utils_path = os.path.dirname(llvm_build_path) |
| 120 | source_root = os.path.dirname(utils_path) |
| 121 | if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore', |
| 122 | 'Function.cpp')): |
| 123 | parser.error('unable to infer LLVM source root, please specify') |
| 124 | |
Daniel Dunbar | df57825 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 125 | # Construct the LLVM project information. |
| 126 | llvmbuild_source_root = opts.llvmbuild_source_root or source_root |
| 127 | project_info = LLVMProjectInfo.load_from_path( |
| 128 | source_root, llvmbuild_source_root) |
| 129 | |
Daniel Dunbar | ad5e012 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 130 | if __name__=='__main__': |
| 131 | main() |