2 Descriptor objects for entities that are part of the LLVM project.
10 class ParseError(Exception):
13 class ComponentInfo(object):
15 Base class for component descriptions.
21 def parse_items(items, has_dependencies = True):
23 kwargs['name'] = items.get_string('name')
24 kwargs['parent'] = items.get_optional_string('parent')
26 kwargs['dependencies'] = items.get_list('dependencies')
29 def __init__(self, subpath, name, dependencies, parent):
30 if not subpath.startswith('/'):
31 raise ValueError,"invalid subpath: %r" % subpath
32 self.subpath = subpath
34 self.dependencies = list(dependencies)
36 # The name of the parent component to logically group this component
40 # The parent instance, once loaded.
41 self.parent_instance = None
44 def set_parent_instance(self, parent):
45 assert parent.name == self.parent, "Unexpected parent!"
46 self.parent_instance = parent
47 self.parent_instance.children.append(self)
49 def get_component_references(self):
50 """get_component_references() -> iter
52 Return an iterator over the named references to other components from
53 this object. Items are of the form (reference-type, component-name).
56 # Parent references are handled specially.
57 for r in self.dependencies:
58 yield ('dependency', r)
60 class GroupComponentInfo(ComponentInfo):
62 Group components have no semantics as far as the build system are concerned,
63 but exist to help organize other components into a logical tree structure.
69 def parse(subpath, items):
70 kwargs = ComponentInfo.parse_items(items, has_dependencies = False)
71 return GroupComponentInfo(subpath, **kwargs)
73 def __init__(self, subpath, name, parent):
74 ComponentInfo.__init__(self, subpath, name, [], parent)
76 class LibraryComponentInfo(ComponentInfo):
80 def parse(subpath, items):
81 kwargs = ComponentInfo.parse_items(items)
82 kwargs['library_name'] = items.get_optional_string('name')
83 kwargs['required_libraries'] = items.get_list('required_libraries')
84 kwargs['add_to_library_groups'] = items.get_list(
85 'add_to_library_groups')
86 return LibraryComponentInfo(subpath, **kwargs)
88 def __init__(self, subpath, name, dependencies, parent, library_name,
89 required_libraries, add_to_library_groups):
90 ComponentInfo.__init__(self, subpath, name, dependencies, parent)
92 # If given, the name to use for the library instead of deriving it from
94 self.library_name = library_name
96 # The names of the library components which are required when linking
97 # with this component.
98 self.required_libraries = list(required_libraries)
100 # The names of the library group components this component should be
101 # considered part of.
102 self.add_to_library_groups = list(add_to_library_groups)
104 def get_component_references(self):
105 for r in ComponentInfo.get_component_references(self):
107 for r in self.required_libraries:
108 yield ('required library', r)
109 for r in self.add_to_library_groups:
110 yield ('library group', r)
112 class LibraryGroupComponentInfo(ComponentInfo):
113 type_name = 'LibraryGroup'
116 def parse(subpath, items):
117 kwargs = ComponentInfo.parse_items(items, has_dependencies = False)
118 kwargs['required_libraries'] = items.get_list('required_libraries')
119 kwargs['add_to_library_groups'] = items.get_list(
120 'add_to_library_groups')
121 return LibraryGroupComponentInfo(subpath, **kwargs)
123 def __init__(self, subpath, name, parent, required_libraries = [],
124 add_to_library_groups = []):
125 ComponentInfo.__init__(self, subpath, name, [], parent)
127 # The names of the library components which are required when linking
128 # with this component.
129 self.required_libraries = list(required_libraries)
131 # The names of the library group components this component should be
132 # considered part of.
133 self.add_to_library_groups = list(add_to_library_groups)
135 def get_component_references(self):
136 for r in ComponentInfo.get_component_references(self):
138 for r in self.required_libraries:
139 yield ('required library', r)
140 for r in self.add_to_library_groups:
141 yield ('library group', r)
143 class ToolComponentInfo(ComponentInfo):
147 def parse(subpath, items):
148 kwargs = ComponentInfo.parse_items(items)
149 kwargs['required_libraries'] = items.get_list('required_libraries')
150 return ToolComponentInfo(subpath, **kwargs)
152 def __init__(self, subpath, name, dependencies, parent,
154 ComponentInfo.__init__(self, subpath, name, dependencies, parent)
156 # The names of the library components which are required to link this
158 self.required_libraries = list(required_libraries)
160 def get_component_references(self):
161 for r in ComponentInfo.get_component_references(self):
163 for r in self.required_libraries:
164 yield ('required library', r)
166 class BuildToolComponentInfo(ToolComponentInfo):
167 type_name = 'BuildTool'
170 def parse(subpath, items):
171 kwargs = ComponentInfo.parse_items(items)
172 kwargs['required_libraries'] = items.get_list('required_libraries')
173 return BuildToolComponentInfo(subpath, **kwargs)
177 class IniFormatParser(dict):
178 def get_list(self, key):
179 # Check if the value is defined.
180 value = self.get(key)
184 # Lists are just whitespace separated strings.
187 def get_optional_string(self, key):
188 value = self.get_list(key)
192 raise ParseError("multiple values for scalar key: %r" % key)
195 def get_string(self, key):
196 value = self.get_optional_string(key)
198 raise ParseError("missing value for required string: %r" % key)
201 _component_type_map = dict(
203 for t in (GroupComponentInfo,
204 LibraryComponentInfo, LibraryGroupComponentInfo,
205 ToolComponentInfo, BuildToolComponentInfo))
206 def load_from_path(path, subpath):
207 # Load the LLVMBuild.txt file as an .ini format file.
208 parser = ConfigParser.RawConfigParser()
211 # We load each section which starts with 'component' as a distinct component
212 # description (so multiple components can be described in one file).
213 for section in parser.sections():
214 if not section.startswith('component'):
215 # We don't expect arbitrary sections currently, warn the user.
216 warning("ignoring unknown section %r in %r" % (section, path))
219 # Determine the type of the component to instantiate.
220 if not parser.has_option(section, 'type'):
221 fatal("invalid component %r in %r: %s" % (
222 section, path, "no component type"))
224 type_name = parser.get(section, 'type')
225 type_class = _component_type_map.get(type_name)
226 if type_class is None:
227 fatal("invalid component %r in %r: %s" % (
228 section, path, "invalid component type: %r" % type_name))
230 # Instantiate the component based on the remaining values.
232 info = type_class.parse(subpath,
233 IniFormatParser(parser.items(section)))
235 print >>sys.stderr, "error: invalid component %r in %r: %s" % (
236 section, path, "unable to instantiate: %r" % type_name)
238 traceback.print_exc()
241 fatal("unable to load component %r in %r: %s" % (
242 section, path, e.message))