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 class GroupComponentInfo(ComponentInfo):
42 Group components have no semantics as far as the build system are concerned,
43 but exist to help organize other components into a logical tree structure.
49 def parse(subpath, items):
50 kwargs = ComponentInfo.parse_items(items, has_dependencies = False)
51 return GroupComponentInfo(subpath, **kwargs)
53 def __init__(self, subpath, name, parent):
54 ComponentInfo.__init__(self, subpath, name, [], parent)
56 class LibraryComponentInfo(ComponentInfo):
60 def parse(subpath, items):
61 kwargs = ComponentInfo.parse_items(items)
62 kwargs['library_name'] = items.get_optional_string('name')
63 kwargs['required_libraries'] = items.get_list('required_libraries')
64 kwargs['add_to_library_groups'] = items.get_list(
65 'add_to_library_groups')
66 return LibraryComponentInfo(subpath, **kwargs)
68 def __init__(self, subpath, name, dependencies, parent, library_name,
69 required_libraries, add_to_library_groups):
70 ComponentInfo.__init__(self, subpath, name, dependencies, parent)
72 # If given, the name to use for the library instead of deriving it from
74 self.library_name = library_name
76 # The names of the library components which are required when linking
77 # with this component.
78 self.required_libraries = list(required_libraries)
80 # The names of the library group components this component should be
82 self.add_to_library_groups = list(add_to_library_groups)
84 class LibraryGroupComponentInfo(ComponentInfo):
85 type_name = 'LibraryGroup'
88 def parse(subpath, items):
89 kwargs = ComponentInfo.parse_items(items, has_dependencies = False)
90 kwargs['required_libraries'] = items.get_list('required_libraries')
91 kwargs['add_to_library_groups'] = items.get_list(
92 'add_to_library_groups')
93 return LibraryGroupComponentInfo(subpath, **kwargs)
95 def __init__(self, subpath, name, parent, required_libraries = [],
96 add_to_library_groups = []):
97 ComponentInfo.__init__(self, subpath, name, [], parent)
99 # The names of the library components which are required when linking
100 # with this component.
101 self.required_libraries = list(required_libraries)
103 # The names of the library group components this component should be
104 # considered part of.
105 self.add_to_library_groups = list(add_to_library_groups)
107 class ToolComponentInfo(ComponentInfo):
111 def parse(subpath, items):
112 kwargs = ComponentInfo.parse_items(items)
113 kwargs['required_libraries'] = items.get_list('required_libraries')
114 return ToolComponentInfo(subpath, **kwargs)
116 def __init__(self, subpath, name, dependencies, parent,
118 ComponentInfo.__init__(self, subpath, name, dependencies, parent)
120 # The names of the library components which are required to link this
122 self.required_libraries = list(required_libraries)
124 class BuildToolComponentInfo(ToolComponentInfo):
125 type_name = 'BuildTool'
128 def parse(subpath, items):
129 kwargs = ComponentInfo.parse_items(items)
130 kwargs['required_libraries'] = items.get_list('required_libraries')
131 return BuildToolComponentInfo(subpath, **kwargs)
135 class IniFormatParser(dict):
136 def get_list(self, key):
137 # Check if the value is defined.
138 value = self.get(key)
142 # Lists are just whitespace separated strings.
145 def get_optional_string(self, key):
146 value = self.get_list(key)
150 raise ParseError("multiple values for scalar key: %r" % key)
153 def get_string(self, key):
154 value = self.get_optional_string(key)
156 raise ParseError("missing value for required string: %r" % key)
159 _component_type_map = dict(
161 for t in (GroupComponentInfo,
162 LibraryComponentInfo, LibraryGroupComponentInfo,
163 ToolComponentInfo, BuildToolComponentInfo))
164 def load_from_path(path, subpath):
165 # Load the LLVMBuild.txt file as an .ini format file.
166 parser = ConfigParser.RawConfigParser()
169 # We load each section which starts with 'component' as a distinct component
170 # description (so multiple components can be described in one file).
171 for section in parser.sections():
172 if not section.startswith('component'):
173 # We don't expect arbitrary sections currently, warn the user.
174 warning("ignoring unknown section %r in %r" % (section, path))
177 # Determine the type of the component to instantiate.
178 if not parser.has_option(section, 'type'):
179 fatal("invalid component %r in %r: %s" % (
180 section, path, "no component type"))
182 type_name = parser.get(section, 'type')
183 type_class = _component_type_map.get(type_name)
184 if type_class is None:
185 fatal("invalid component %r in %r: %s" % (
186 section, path, "invalid component type: %r" % type_name))
188 # Instantiate the component based on the remaining values.
190 info = type_class.parse(subpath,
191 IniFormatParser(parser.items(section)))
193 print >>sys.stderr, "error: invalid component %r in %r: %s" % (
194 section, path, "unable to instantiate: %r" % type_name)
196 traceback.print_exc()
199 fatal("unable to load component %r in %r: %s" % (
200 section, path, e.message))