blob: 0dfa3d18c50e163c3795f9d489b70ac52c78f3ff [file] [log] [blame]
Daniel Dunbar25ff9f62013-08-14 23:15:39 +00001from __future__ import absolute_import
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +00002import filecmp
Daniel Dunbar01b05882011-11-03 17:56:03 +00003import os
Daniel Dunbar8844e3c2011-11-03 17:56:12 +00004import sys
Daniel Dunbar01b05882011-11-03 17:56:03 +00005
Daniel Dunbar25ff9f62013-08-14 23:15:39 +00006import llvmbuild.componentinfo as componentinfo
7import llvmbuild.configutil as configutil
Daniel Dunbardd3fb562011-11-03 17:56:06 +00008
Anders Waldenborg91527ef2014-04-23 19:17:42 +00009from llvmbuild.util import fatal, note
Daniel Dunbar8844e3c2011-11-03 17:56:12 +000010
11###
12
Daniel Dunbar9057a3d2011-11-05 04:07:43 +000013def cmake_quote_string(value):
14 """
15 cmake_quote_string(value) -> str
16
17 Return a quoted form of the given value that is suitable for use in CMake
18 language files.
19 """
20
21 # Currently, we only handle escaping backslashes.
22 value = value.replace("\\", "\\\\")
23
24 return value
25
Daniel Dunbar52f71222011-11-17 01:19:53 +000026def cmake_quote_path(value):
27 """
28 cmake_quote_path(value) -> str
29
30 Return a quoted form of the given value that is suitable for use in CMake
31 language files.
32 """
33
34 # CMake has a bug in it's Makefile generator that doesn't properly quote
35 # strings it generates. So instead of using proper quoting, we just use "/"
36 # style paths. Currently, we only handle escaping backslashes.
37 value = value.replace("\\", "/")
38
39 return value
40
Daniel Dunbar0edba5c92011-11-05 04:07:49 +000041def make_install_dir(path):
42 """
43 make_install_dir(path) -> None
44
45 Create the given directory path for installation, including any parents.
46 """
47
Benjamin Kramerbde91762012-06-02 10:20:22 +000048 # os.makedirs considers it an error to be called with an existent path.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +000049 if not os.path.exists(path):
50 os.makedirs(path)
51
Daniel Dunbarb814ee42011-11-04 23:40:11 +000052###
53
Daniel Dunbardd3fb562011-11-03 17:56:06 +000054class LLVMProjectInfo(object):
55 @staticmethod
56 def load_infos_from_path(llvmbuild_source_root):
Daniel Dunbare29ffff2011-12-12 22:45:59 +000057 def recurse(subpath):
58 # Load the LLVMBuild file.
59 llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
60 'LLVMBuild.txt')
61 if not os.path.exists(llvmbuild_path):
62 fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
Daniel Dunbardd3fb562011-11-03 17:56:06 +000063
Daniel Dunbare29ffff2011-12-12 22:45:59 +000064 # Parse the components from it.
65 common,info_iter = componentinfo.load_from_path(llvmbuild_path,
66 subpath)
67 for info in info_iter:
Daniel Dunbardd3fb562011-11-03 17:56:06 +000068 yield info
69
Daniel Dunbare29ffff2011-12-12 22:45:59 +000070 # Recurse into the specified subdirectories.
71 for subdir in common.get_list("subdirectories"):
72 for item in recurse(os.path.join(subpath, subdir)):
73 yield item
74
75 return recurse("/")
76
Daniel Dunbardd3fb562011-11-03 17:56:06 +000077 @staticmethod
78 def load_from_path(source_root, llvmbuild_source_root):
79 infos = list(
80 LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
81
82 return LLVMProjectInfo(source_root, infos)
83
84 def __init__(self, source_root, component_infos):
Daniel Dunbar8844e3c2011-11-03 17:56:12 +000085 # Store our simple ivars.
Daniel Dunbardd3fb562011-11-03 17:56:06 +000086 self.source_root = source_root
Daniel Dunbar79fa1e82011-11-10 00:49:58 +000087 self.component_infos = list(component_infos)
88 self.component_info_map = None
89 self.ordered_component_infos = None
90
91 def validate_components(self):
92 """validate_components() -> None
93
94 Validate that the project components are well-defined. Among other
95 things, this checks that:
96 - Components have valid references.
97 - Components references do not form cycles.
98
99 We also construct the map from component names to info, and the
100 topological ordering of components.
101 """
Daniel Dunbardd3fb562011-11-03 17:56:06 +0000102
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000103 # Create the component info map and validate that component names are
104 # unique.
105 self.component_info_map = {}
Daniel Dunbar79fa1e82011-11-10 00:49:58 +0000106 for ci in self.component_infos:
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000107 existing = self.component_info_map.get(ci.name)
108 if existing is not None:
109 # We found a duplicate component name, report it and error out.
110 fatal("found duplicate component %r (at %r and %r)" % (
111 ci.name, ci.subpath, existing.subpath))
112 self.component_info_map[ci.name] = ci
113
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000114 # Disallow 'all' as a component name, which is a special case.
115 if 'all' in self.component_info_map:
116 fatal("project is not allowed to define 'all' component")
117
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000118 # Add the root component.
Daniel Dunbar48972552011-11-03 17:56:16 +0000119 if '$ROOT' in self.component_info_map:
120 fatal("project is not allowed to define $ROOT component")
121 self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
122 '/', '$ROOT', None)
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000123 self.component_infos.append(self.component_info_map['$ROOT'])
Daniel Dunbar48972552011-11-03 17:56:16 +0000124
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000125 # Topologically order the component information according to their
126 # component references.
127 def visit_component_info(ci, current_stack, current_set):
128 # Check for a cycles.
129 if ci in current_set:
130 # We found a cycle, report it and error out.
131 cycle_description = ' -> '.join(
132 '%r (%s)' % (ci.name, relation)
133 for relation,ci in current_stack)
134 fatal("found cycle to %r after following: %s -> %s" % (
135 ci.name, cycle_description, ci.name))
136
137 # If we have already visited this item, we are done.
138 if ci not in components_to_visit:
139 return
140
141 # Otherwise, mark the component info as visited and traverse.
142 components_to_visit.remove(ci)
143
Daniel Dunbar48972552011-11-03 17:56:16 +0000144 # Validate the parent reference, which we treat specially.
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000145 if ci.parent is not None:
146 parent = self.component_info_map.get(ci.parent)
147 if parent is None:
148 fatal("component %r has invalid reference %r (via %r)" % (
149 ci.name, ci.parent, 'parent'))
150 ci.set_parent_instance(parent)
Daniel Dunbar48972552011-11-03 17:56:16 +0000151
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000152 for relation,referent_name in ci.get_component_references():
153 # Validate that the reference is ok.
154 referent = self.component_info_map.get(referent_name)
155 if referent is None:
156 fatal("component %r has invalid reference %r (via %r)" % (
157 ci.name, referent_name, relation))
158
159 # Visit the reference.
160 current_stack.append((relation,ci))
161 current_set.add(ci)
162 visit_component_info(referent, current_stack, current_set)
163 current_set.remove(ci)
164 current_stack.pop()
165
166 # Finally, add the component info to the ordered list.
167 self.ordered_component_infos.append(ci)
168
Daniel Dunbar48972552011-11-03 17:56:16 +0000169 # FIXME: We aren't actually correctly checking for cycles along the
170 # parent edges. Haven't decided how I want to handle this -- I thought
171 # about only checking cycles by relation type. If we do that, it falls
172 # out easily. If we don't, we should special case the check.
173
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000174 self.ordered_component_infos = []
NAKAMURA Takumia1d528b2012-12-20 10:35:18 +0000175 components_to_visit = sorted(
176 set(self.component_infos),
177 key = lambda c: c.name)
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000178 while components_to_visit:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000179 visit_component_info(components_to_visit[0], [], set())
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000180
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000181 # Canonicalize children lists.
182 for c in self.ordered_component_infos:
183 c.children.sort(key = lambda c: c.name)
184
185 def print_tree(self):
186 def visit(node, depth = 0):
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000187 print('%s%-40s (%s)' % (' '*depth, node.name, node.type_name))
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000188 for c in node.children:
189 visit(c, depth + 1)
190 visit(self.component_info_map['$ROOT'])
191
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000192 def write_components(self, output_path):
193 # Organize all the components by the directory their LLVMBuild file
194 # should go in.
195 info_basedir = {}
196 for ci in self.component_infos:
197 # Ignore the $ROOT component.
198 if ci.parent is None:
199 continue
200
201 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
202
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000203 # Compute the list of subdirectories to scan.
204 subpath_subdirs = {}
205 for ci in self.component_infos:
206 # Ignore root components.
207 if ci.subpath == '/':
208 continue
209
210 # Otherwise, append this subpath to the parent list.
211 parent_path = os.path.dirname(ci.subpath)
212 subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
213 parent_path, set())
214 parent_list.add(os.path.basename(ci.subpath))
215
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000216 # Generate the build files.
217 for subpath, infos in info_basedir.items():
218 # Order the components by name to have a canonical ordering.
219 infos.sort(key = lambda ci: ci.name)
220
221 # Format the components into llvmbuild fragments.
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000222 fragments = []
223
224 # Add the common fragments.
225 subdirectories = subpath_subdirs.get(subpath)
226 if subdirectories:
227 fragment = """\
228subdirectories = %s
229""" % (" ".join(sorted(subdirectories)),)
230 fragments.append(("common", fragment))
231
232 # Add the component fragments.
233 num_common_fragments = len(fragments)
234 for ci in infos:
235 fragment = ci.get_llvmbuild_fragment()
236 if fragment is None:
237 continue
238
239 name = "component_%d" % (len(fragments) - num_common_fragments)
240 fragments.append((name, fragment))
241
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000242 if not fragments:
243 continue
244
245 assert subpath.startswith('/')
246 directory_path = os.path.join(output_path, subpath[1:])
247
248 # Create the directory if it does not already exist.
249 if not os.path.exists(directory_path):
250 os.makedirs(directory_path)
251
Daniel Dunbarea07f342011-12-12 22:45:35 +0000252 # In an effort to preserve comments (which aren't parsed), read in
253 # the original file and extract the comments. We only know how to
254 # associate comments that prefix a section name.
255 f = open(infos[0]._source_path)
256 comments_map = {}
257 comment_block = ""
258 for ln in f:
259 if ln.startswith(';'):
260 comment_block += ln
261 elif ln.startswith('[') and ln.endswith(']\n'):
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000262 comments_map[ln[1:-2]] = comment_block
Daniel Dunbarea07f342011-12-12 22:45:35 +0000263 else:
264 comment_block = ""
265 f.close()
266
267 # Create the LLVMBuild fil[e.
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000268 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
269 f = open(file_path, "w")
Daniel Dunbar453146e2011-11-03 17:56:31 +0000270
271 # Write the header.
272 header_fmt = ';===- %s %s-*- Conf -*--===;'
273 header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
274 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
275 header_string = header_fmt % (header_name, header_pad)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000276 f.write("""\
Daniel Dunbar453146e2011-11-03 17:56:31 +0000277%s
278;
279; The LLVM Compiler Infrastructure
280;
281; This file is distributed under the University of Illinois Open Source
282; License. See LICENSE.TXT for details.
283;
284;===------------------------------------------------------------------------===;
285;
286; This is an LLVMBuild description file for the components in this subdirectory.
287;
288; For more information on the LLVMBuild system, please see:
289;
290; http://llvm.org/docs/LLVMBuild.html
291;
292;===------------------------------------------------------------------------===;
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000293
294""" % header_string)
Daniel Dunbar453146e2011-11-03 17:56:31 +0000295
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000296 # Write out each fragment.each component fragment.
297 for name,fragment in fragments:
Daniel Dunbarea07f342011-12-12 22:45:35 +0000298 comment = comments_map.get(name)
299 if comment is not None:
300 f.write(comment)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000301 f.write("[%s]\n" % name)
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000302 f.write(fragment)
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000303 if fragment is not fragments[-1][1]:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000304 f.write('\n')
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000305
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000306 f.close()
307
Preston Gurde65f4e62012-05-07 19:38:40 +0000308 def write_library_table(self, output_path, enabled_optional_components):
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000309 # Write out the mapping from component names to required libraries.
310 #
311 # We do this in topological order so that we know we can append the
312 # dependencies for added library groups.
313 entries = {}
314 for c in self.ordered_component_infos:
Daniel Dunbarf876da12012-05-15 18:44:12 +0000315 # Skip optional components which are not enabled.
Preston Gurde65f4e62012-05-07 19:38:40 +0000316 if c.type_name == 'OptionalLibrary' \
317 and c.name not in enabled_optional_components:
318 continue
319
Daniel Dunbarf876da12012-05-15 18:44:12 +0000320 # Skip target groups which are not enabled.
321 tg = c.get_parent_target_group()
322 if tg and not tg.enabled:
323 continue
324
Daniel Dunbar82219ad2011-11-10 00:49:51 +0000325 # Only certain components are in the table.
Preston Gurde65f4e62012-05-07 19:38:40 +0000326 if c.type_name not in ('Library', 'OptionalLibrary', \
327 'LibraryGroup', 'TargetGroup'):
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000328 continue
329
330 # Compute the llvm-config "component name". For historical reasons,
331 # this is lowercased based on the library name.
332 llvmconfig_component_name = c.get_llvmconfig_component_name()
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000333
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000334 # Get the library name, or None for LibraryGroups.
Preston Gurde65f4e62012-05-07 19:38:40 +0000335 if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
Daniel Dunbar06bb7982011-12-15 23:35:08 +0000336 library_name = c.get_prefixed_library_name()
Daniel Dunbarc364d682012-05-15 18:44:17 +0000337 is_installed = c.installed
Daniel Dunbar82219ad2011-11-10 00:49:51 +0000338 else:
339 library_name = None
Daniel Dunbarc364d682012-05-15 18:44:17 +0000340 is_installed = True
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000341
342 # Get the component names of all the required libraries.
343 required_llvmconfig_component_names = [
344 self.component_info_map[dep].get_llvmconfig_component_name()
345 for dep in c.required_libraries]
346
347 # Insert the entries for library groups we should add to.
348 for dep in c.add_to_library_groups:
349 entries[dep][2].append(llvmconfig_component_name)
350
351 # Add the entry.
352 entries[c.name] = (llvmconfig_component_name, library_name,
Daniel Dunbarc364d682012-05-15 18:44:17 +0000353 required_llvmconfig_component_names,
354 is_installed)
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000355
356 # Convert to a list of entries and sort by name.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000357 entries = list(entries.values())
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000358
359 # Create an 'all' pseudo component. We keep the dependency list small by
360 # only listing entries that have no other dependents.
361 root_entries = set(e[0] for e in entries)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000362 for _,_,deps,_ in entries:
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000363 root_entries -= set(deps)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000364 entries.append(('all', None, root_entries, True))
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000365
366 entries.sort()
367
368 # Compute the maximum number of required libraries, plus one so there is
369 # always a sentinel.
370 max_required_libraries = max(len(deps)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000371 for _,_,deps,_ in entries) + 1
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000372
373 # Write out the library table.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +0000374 make_install_dir(os.path.dirname(output_path))
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +0000375 f = open(output_path+'.new', 'w')
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000376 f.write("""\
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000377//===- llvm-build generated file --------------------------------*- C++ -*-===//
378//
379// Component Library Depenedency Table
380//
381// Automatically generated file, do not edit!
382//
383//===----------------------------------------------------------------------===//
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000384
385""")
386 f.write('struct AvailableComponent {\n')
387 f.write(' /// The name of the component.\n')
388 f.write(' const char *Name;\n')
389 f.write('\n')
390 f.write(' /// The name of the library for this component (or NULL).\n')
391 f.write(' const char *Library;\n')
392 f.write('\n')
393 f.write(' /// Whether the component is installed.\n')
394 f.write(' bool IsInstalled;\n')
395 f.write('\n')
396 f.write('\
397 /// The list of libraries required when linking this component.\n')
398 f.write(' const char *RequiredLibraries[%d];\n' % (
399 max_required_libraries))
400 f.write('} AvailableComponents[%d] = {\n' % len(entries))
Daniel Dunbarc364d682012-05-15 18:44:17 +0000401 for name,library_name,required_names,is_installed in entries:
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000402 if library_name is None:
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000403 library_name_as_cstr = 'nullptr'
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000404 else:
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000405 library_name_as_cstr = '"%s"' % library_name
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000406 if is_installed:
407 is_installed_as_cstr = 'true'
408 else:
409 is_installed_as_cstr = 'false'
410 f.write(' { "%s", %s, %s, { %s } },\n' % (
411 name, library_name_as_cstr, is_installed_as_cstr,
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000412 ', '.join('"%s"' % dep
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000413 for dep in required_names)))
414 f.write('};\n')
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000415 f.close()
416
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +0000417 if not os.path.isfile(output_path):
418 os.rename(output_path+'.new', output_path)
419 elif filecmp.cmp(output_path, output_path+'.new'):
420 os.remove(output_path+'.new')
421 else:
422 os.remove(output_path)
423 os.rename(output_path+'.new', output_path)
424
Daniel Dunbar4128db92011-11-29 00:06:50 +0000425 def get_required_libraries_for_component(self, ci, traverse_groups = False):
426 """
427 get_required_libraries_for_component(component_info) -> iter
428
429 Given a Library component info descriptor, return an iterator over all
430 of the directly required libraries for linking with this component. If
431 traverse_groups is True, then library and target groups will be
432 traversed to include their required libraries.
433 """
434
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000435 assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup')
Daniel Dunbar4128db92011-11-29 00:06:50 +0000436
437 for name in ci.required_libraries:
438 # Get the dependency info.
439 dep = self.component_info_map[name]
440
441 # If it is a library, yield it.
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000442 if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary':
Daniel Dunbar4128db92011-11-29 00:06:50 +0000443 yield dep
444 continue
445
446 # Otherwise if it is a group, yield or traverse depending on what
447 # was requested.
448 if dep.type_name in ('LibraryGroup', 'TargetGroup'):
449 if not traverse_groups:
450 yield dep
451 continue
452
453 for res in self.get_required_libraries_for_component(dep, True):
454 yield res
455
Daniel Dunbare9733852011-11-04 23:10:37 +0000456 def get_fragment_dependencies(self):
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000457 """
Daniel Dunbare9733852011-11-04 23:10:37 +0000458 get_fragment_dependencies() -> iter
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000459
Daniel Dunbare9733852011-11-04 23:10:37 +0000460 Compute the list of files (as absolute paths) on which the output
461 fragments depend (i.e., files for which a modification should trigger a
462 rebuild of the fragment).
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000463 """
464
465 # Construct a list of all the dependencies of the Makefile fragment
466 # itself. These include all the LLVMBuild files themselves, as well as
467 # all of our own sources.
Daniel Dunbarcda2a892011-12-06 23:13:42 +0000468 #
469 # Many components may come from the same file, so we make sure to unique
470 # these.
471 build_paths = set()
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000472 for ci in self.component_infos:
Daniel Dunbarcda2a892011-12-06 23:13:42 +0000473 p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
474 if p not in build_paths:
475 yield p
476 build_paths.add(p)
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000477
478 # Gather the list of necessary sources by just finding all loaded
479 # modules that are inside the LLVM source tree.
480 for module in sys.modules.values():
481 # Find the module path.
482 if not hasattr(module, '__file__'):
483 continue
484 path = getattr(module, '__file__')
485 if not path:
486 continue
487
488 # Strip off any compiled suffix.
489 if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
490 path = path[:-1]
491
492 # If the path exists and is in the source tree, consider it a
493 # dependency.
494 if (path.startswith(self.source_root) and os.path.exists(path)):
Daniel Dunbare9733852011-11-04 23:10:37 +0000495 yield path
496
Dan Liewa70fb6e2015-08-21 18:10:51 +0000497 def foreach_cmake_library(self, f,
498 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000499 skip_disabled,
500 skip_not_installed):
Dan Liewa70fb6e2015-08-21 18:10:51 +0000501 for ci in self.ordered_component_infos:
502 # Skip optional components which are not enabled.
503 if ci.type_name == 'OptionalLibrary' \
504 and ci.name not in enabled_optional_components:
505 continue
506
507 # We only write the information for libraries currently.
508 if ci.type_name not in ('Library', 'OptionalLibrary'):
509 continue
510
511 # Skip disabled targets.
512 if skip_disabled:
513 tg = ci.get_parent_target_group()
514 if tg and not tg.enabled:
515 continue
516
Dan Liew1e3dc522015-08-21 18:10:57 +0000517 # Skip targets that will not be installed
518 if skip_not_installed and not ci.installed:
519 continue
520
Dan Liewa70fb6e2015-08-21 18:10:51 +0000521 f(ci)
522
523
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000524 def write_cmake_fragment(self, output_path, enabled_optional_components):
Daniel Dunbare9733852011-11-04 23:10:37 +0000525 """
526 write_cmake_fragment(output_path) -> None
527
528 Generate a CMake fragment which includes all of the collated LLVMBuild
529 information in a format that is easily digestible by a CMake. The exact
530 contents of this are closely tied to how the CMake configuration
531 integrates LLVMBuild, see CMakeLists.txt in the top-level.
532 """
533
534 dependencies = list(self.get_fragment_dependencies())
535
536 # Write out the CMake fragment.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +0000537 make_install_dir(os.path.dirname(output_path))
Daniel Dunbare9733852011-11-04 23:10:37 +0000538 f = open(output_path, 'w')
539
540 # Write the header.
541 header_fmt = '\
542#===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
543 header_name = os.path.basename(output_path)
544 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
545 header_string = header_fmt % (header_name, header_pad)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000546 f.write("""\
Daniel Dunbare9733852011-11-04 23:10:37 +0000547%s
548#
549# The LLVM Compiler Infrastructure
550#
551# This file is distributed under the University of Illinois Open Source
552# License. See LICENSE.TXT for details.
553#
554#===------------------------------------------------------------------------===#
555#
556# This file contains the LLVMBuild project information in a format easily
557# consumed by the CMake based build system.
558#
559# This file is autogenerated by llvm-build, do not edit!
560#
561#===------------------------------------------------------------------------===#
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000562
563""" % header_string)
Daniel Dunbare9733852011-11-04 23:10:37 +0000564
565 # Write the dependency information in the best way we can.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000566 f.write("""
Daniel Dunbare9733852011-11-04 23:10:37 +0000567# LLVMBuild CMake fragment dependencies.
568#
569# CMake has no builtin way to declare that the configuration depends on
570# a particular file. However, a side effect of configure_file is to add
571# said input file to CMake's internal dependency list. So, we use that
572# and a dummy output file to communicate the dependency information to
573# CMake.
574#
575# FIXME: File a CMake RFE to get a properly supported version of this
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000576# feature.
577""")
Daniel Dunbare9733852011-11-04 23:10:37 +0000578 for dep in dependencies:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000579 f.write("""\
Daniel Dunbare9733852011-11-04 23:10:37 +0000580configure_file(\"%s\"
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000581 ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % (
582 cmake_quote_path(dep),))
Daniel Dunbar9057a3d2011-11-05 04:07:43 +0000583
Daniel Dunbar4128db92011-11-29 00:06:50 +0000584 # Write the properties we use to encode the required library dependency
585 # information in a form CMake can easily use directly.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000586 f.write("""
Daniel Dunbar4128db92011-11-29 00:06:50 +0000587# Explicit library dependency information.
588#
589# The following property assignments effectively create a map from component
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000590# names to required libraries, in a way that is easily accessed from CMake.
591""")
Dan Liewa70fb6e2015-08-21 18:10:51 +0000592 self.foreach_cmake_library(
593 lambda ci:
594 f.write("""\
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000595set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % (
Daniel Dunbar4128db92011-11-29 00:06:50 +0000596 ci.get_prefixed_library_name(), " ".join(sorted(
597 dep.get_prefixed_library_name()
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000598 for dep in self.get_required_libraries_for_component(ci)))))
Dan Liewa70fb6e2015-08-21 18:10:51 +0000599 ,
600 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000601 skip_disabled = False,
602 skip_not_installed = False # Dependency info must be emitted for internals libs too
Dan Liewa70fb6e2015-08-21 18:10:51 +0000603 )
Daniel Dunbar4128db92011-11-29 00:06:50 +0000604
Daniel Dunbare9733852011-11-04 23:10:37 +0000605 f.close()
606
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000607 def write_cmake_exports_fragment(self, output_path, enabled_optional_components):
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000608 """
609 write_cmake_exports_fragment(output_path) -> None
610
611 Generate a CMake fragment which includes LLVMBuild library
612 dependencies expressed similarly to how CMake would write
613 them via install(EXPORT).
614 """
615
616 dependencies = list(self.get_fragment_dependencies())
617
618 # Write out the CMake exports fragment.
619 make_install_dir(os.path.dirname(output_path))
620 f = open(output_path, 'w')
621
622 f.write("""\
623# Explicit library dependency information.
624#
625# The following property assignments tell CMake about link
626# dependencies of libraries imported from LLVM.
627""")
Dan Liewa70fb6e2015-08-21 18:10:51 +0000628 self.foreach_cmake_library(
629 lambda ci:
630 f.write("""\
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000631set_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
632 ci.get_prefixed_library_name(), " ".join(sorted(
633 dep.get_prefixed_library_name()
634 for dep in self.get_required_libraries_for_component(ci)))))
Dan Liewa70fb6e2015-08-21 18:10:51 +0000635 ,
636 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000637 skip_disabled = True,
638 skip_not_installed = True # Do not export internal libraries like gtest
Dan Liewa70fb6e2015-08-21 18:10:51 +0000639 )
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000640
641 f.close()
642
Daniel Dunbar233c9302011-11-10 00:50:07 +0000643def add_magic_target_components(parser, project, opts):
644 """add_magic_target_components(project, opts) -> None
645
646 Add the "magic" target based components to the project, which can only be
647 determined based on the target configuration options.
648
649 This currently is responsible for populating the required_libraries list of
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000650 the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
Daniel Dunbar233c9302011-11-10 00:50:07 +0000651 """
652
653 # Determine the available targets.
654 available_targets = dict((ci.name,ci)
655 for ci in project.component_infos
656 if ci.type_name == 'TargetGroup')
657
658 # Find the configured native target.
659
660 # We handle a few special cases of target names here for historical
661 # reasons, as these are the names configure currently comes up with.
662 native_target_name = { 'x86' : 'X86',
663 'x86_64' : 'X86',
664 'Unknown' : None }.get(opts.native_target,
665 opts.native_target)
666 if native_target_name is None:
667 native_target = None
668 else:
669 native_target = available_targets.get(native_target_name)
670 if native_target is None:
671 parser.error("invalid native target: %r (not in project)" % (
672 opts.native_target,))
673 if native_target.type_name != 'TargetGroup':
674 parser.error("invalid native target: %r (not a target)" % (
675 opts.native_target,))
676
677 # Find the list of targets to enable.
678 if opts.enable_targets is None:
679 enable_targets = available_targets.values()
680 else:
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000681 # We support both space separated and semi-colon separated lists.
Greg Fitzgerald986b4072014-04-18 17:39:50 +0000682 if opts.enable_targets == '':
683 enable_target_names = []
684 elif ' ' in opts.enable_targets:
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000685 enable_target_names = opts.enable_targets.split()
686 else:
687 enable_target_names = opts.enable_targets.split(';')
688
Daniel Dunbar233c9302011-11-10 00:50:07 +0000689 enable_targets = []
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000690 for name in enable_target_names:
Daniel Dunbar233c9302011-11-10 00:50:07 +0000691 target = available_targets.get(name)
692 if target is None:
693 parser.error("invalid target to enable: %r (not in project)" % (
694 name,))
695 if target.type_name != 'TargetGroup':
696 parser.error("invalid target to enable: %r (not a target)" % (
697 name,))
698 enable_targets.append(target)
699
700 # Find the special library groups we are going to populate. We enforce that
701 # these appear in the project (instead of just adding them) so that they at
702 # least have an explicit representation in the project LLVMBuild files (and
703 # comments explaining how they are populated).
704 def find_special_group(name):
705 info = info_map.get(name)
706 if info is None:
707 fatal("expected project to contain special %r component" % (
708 name,))
709
710 if info.type_name != 'LibraryGroup':
711 fatal("special component %r should be a LibraryGroup" % (
712 name,))
713
714 if info.required_libraries:
715 fatal("special component %r must have empty %r list" % (
716 name, 'required_libraries'))
717 if info.add_to_library_groups:
718 fatal("special component %r must have empty %r list" % (
719 name, 'add_to_library_groups'))
720
Daniel Dunbar088f81b2011-12-12 22:45:41 +0000721 info._is_special_group = True
Daniel Dunbar233c9302011-11-10 00:50:07 +0000722 return info
723
724 info_map = dict((ci.name, ci) for ci in project.component_infos)
725 all_targets = find_special_group('all-targets')
726 native_group = find_special_group('Native')
727 native_codegen_group = find_special_group('NativeCodeGen')
728 engine_group = find_special_group('Engine')
729
730 # Set the enabled bit in all the target groups, and append to the
731 # all-targets list.
732 for ci in enable_targets:
733 all_targets.required_libraries.append(ci.name)
734 ci.enabled = True
735
736 # If we have a native target, then that defines the native and
737 # native_codegen libraries.
738 if native_target and native_target.enabled:
739 native_group.required_libraries.append(native_target.name)
740 native_codegen_group.required_libraries.append(
741 '%sCodeGen' % native_target.name)
742
743 # If we have a native target with a JIT, use that for the engine. Otherwise,
744 # use the interpreter.
745 if native_target and native_target.enabled and native_target.has_jit:
Eric Christopher79cc1e32014-09-02 22:28:02 +0000746 engine_group.required_libraries.append('MCJIT')
Daniel Dunbar233c9302011-11-10 00:50:07 +0000747 engine_group.required_libraries.append(native_group.name)
748 else:
749 engine_group.required_libraries.append('Interpreter')
750
Daniel Dunbar01b05882011-11-03 17:56:03 +0000751def main():
752 from optparse import OptionParser, OptionGroup
753 parser = OptionParser("usage: %prog [options]")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000754
755 group = OptionGroup(parser, "Input Options")
756 group.add_option("", "--source-root", dest="source_root", metavar="PATH",
Daniel Dunbar01b05882011-11-03 17:56:03 +0000757 help="Path to the LLVM source (inferred if not given)",
758 action="store", default=None)
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000759 group.add_option("", "--llvmbuild-source-root",
760 dest="llvmbuild_source_root",
761 help=(
762 "If given, an alternate path to search for LLVMBuild.txt files"),
763 action="store", default=None, metavar="PATH")
Daniel Dunbarf258ad82011-11-11 00:24:00 +0000764 group.add_option("", "--build-root", dest="build_root", metavar="PATH",
765 help="Path to the build directory (if needed) [%default]",
766 action="store", default=None)
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000767 parser.add_option_group(group)
768
769 group = OptionGroup(parser, "Output Options")
770 group.add_option("", "--print-tree", dest="print_tree",
771 help="Print out the project component tree [%default]",
772 action="store_true", default=False)
773 group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000774 help="Write out the LLVMBuild.txt files to PATH",
775 action="store", default=None, metavar="PATH")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000776 group.add_option("", "--write-library-table",
777 dest="write_library_table", metavar="PATH",
778 help="Write the C++ library dependency table to PATH",
779 action="store", default=None)
780 group.add_option("", "--write-cmake-fragment",
781 dest="write_cmake_fragment", metavar="PATH",
782 help="Write the CMake project information to PATH",
783 action="store", default=None)
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000784 group.add_option("", "--write-cmake-exports-fragment",
785 dest="write_cmake_exports_fragment", metavar="PATH",
786 help="Write the CMake exports information to PATH",
787 action="store", default=None)
Daniel Dunbarf258ad82011-11-11 00:24:00 +0000788 group.add_option("", "--configure-target-def-file",
789 dest="configure_target_def_files",
790 help="""Configure the given file at SUBPATH (relative to
791the inferred or given source root, and with a '.in' suffix) by replacing certain
792substitution variables with lists of targets that support certain features (for
793example, targets with AsmPrinters) and write the result to the build root (as
794given by --build-root) at the same SUBPATH""",
795 metavar="SUBPATH", action="append", default=None)
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000796 parser.add_option_group(group)
Daniel Dunbar233c9302011-11-10 00:50:07 +0000797
798 group = OptionGroup(parser, "Configuration Options")
799 group.add_option("", "--native-target",
800 dest="native_target", metavar="NAME",
801 help=("Treat the named target as the 'native' one, if "
802 "given [%default]"),
803 action="store", default=None)
804 group.add_option("", "--enable-targets",
805 dest="enable_targets", metavar="NAMES",
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000806 help=("Enable the given space or semi-colon separated "
807 "list of targets, or all targets if not present"),
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000808 action="store", default=None)
Preston Gurde65f4e62012-05-07 19:38:40 +0000809 group.add_option("", "--enable-optional-components",
810 dest="optional_components", metavar="NAMES",
811 help=("Enable the given space or semi-colon separated "
812 "list of optional components"),
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000813 action="store", default="")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000814 parser.add_option_group(group)
815
Daniel Dunbar01b05882011-11-03 17:56:03 +0000816 (opts, args) = parser.parse_args()
817
818 # Determine the LLVM source path, if not given.
819 source_root = opts.source_root
820 if source_root:
Chandler Carruthef860a22013-01-02 09:10:48 +0000821 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
Daniel Dunbar01b05882011-11-03 17:56:03 +0000822 'Function.cpp')):
823 parser.error('invalid LLVM source root: %r' % source_root)
824 else:
825 llvmbuild_path = os.path.dirname(__file__)
826 llvm_build_path = os.path.dirname(llvmbuild_path)
827 utils_path = os.path.dirname(llvm_build_path)
828 source_root = os.path.dirname(utils_path)
Chandler Carruthef860a22013-01-02 09:10:48 +0000829 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
Daniel Dunbar01b05882011-11-03 17:56:03 +0000830 'Function.cpp')):
831 parser.error('unable to infer LLVM source root, please specify')
832
Daniel Dunbardd3fb562011-11-03 17:56:06 +0000833 # Construct the LLVM project information.
834 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
835 project_info = LLVMProjectInfo.load_from_path(
836 source_root, llvmbuild_source_root)
837
Daniel Dunbar233c9302011-11-10 00:50:07 +0000838 # Add the magic target based components.
839 add_magic_target_components(parser, project_info, opts)
840
Daniel Dunbar79fa1e82011-11-10 00:49:58 +0000841 # Validate the project component info.
842 project_info.validate_components()
843
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000844 # Print the component tree, if requested.
845 if opts.print_tree:
846 project_info.print_tree()
847
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000848 # Write out the components, if requested. This is useful for auto-upgrading
849 # the schema.
850 if opts.write_llvmbuild:
851 project_info.write_components(opts.write_llvmbuild)
852
Daniel Dunbare9733852011-11-04 23:10:37 +0000853 # Write out the required library table, if requested.
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000854 if opts.write_library_table:
Preston Gurde65f4e62012-05-07 19:38:40 +0000855 project_info.write_library_table(opts.write_library_table,
856 opts.optional_components)
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000857
Daniel Dunbare9733852011-11-04 23:10:37 +0000858 # Write out the cmake fragment, if requested.
859 if opts.write_cmake_fragment:
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000860 project_info.write_cmake_fragment(opts.write_cmake_fragment,
861 opts.optional_components)
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000862 if opts.write_cmake_exports_fragment:
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000863 project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment,
864 opts.optional_components)
Daniel Dunbare9733852011-11-04 23:10:37 +0000865
Daniel Dunbarf258ad82011-11-11 00:24:00 +0000866 # Configure target definition files, if requested.
867 if opts.configure_target_def_files:
868 # Verify we were given a build root.
869 if not opts.build_root:
870 parser.error("must specify --build-root when using "
871 "--configure-target-def-file")
872
873 # Create the substitution list.
874 available_targets = [ci for ci in project_info.component_infos
875 if ci.type_name == 'TargetGroup']
876 substitutions = [
877 ("@LLVM_ENUM_TARGETS@",
878 ' '.join('LLVM_TARGET(%s)' % ci.name
879 for ci in available_targets)),
880 ("@LLVM_ENUM_ASM_PRINTERS@",
881 ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
882 for ci in available_targets
883 if ci.has_asmprinter)),
884 ("@LLVM_ENUM_ASM_PARSERS@",
885 ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
886 for ci in available_targets
887 if ci.has_asmparser)),
888 ("@LLVM_ENUM_DISASSEMBLERS@",
889 ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
890 for ci in available_targets
891 if ci.has_disassembler))]
892
893 # Configure the given files.
894 for subpath in opts.configure_target_def_files:
895 inpath = os.path.join(source_root, subpath + '.in')
896 outpath = os.path.join(opts.build_root, subpath)
897 result = configutil.configure_file(inpath, outpath, substitutions)
898 if not result:
899 note("configured file %r hasn't changed" % outpath)
900
Daniel Dunbar01b05882011-11-03 17:56:03 +0000901if __name__=='__main__':
902 main()