blob: 48b59bdac38cf84a8f7de7822f9365dd744c8c1a [file] [log] [blame]
Daniel Dunbara3217162011-12-12 22:45:35 +00001import StringIO
Daniel Dunbarad5e0122011-11-03 17:56:03 +00002import os
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00003import sys
Daniel Dunbarad5e0122011-11-03 17:56:03 +00004
Daniel Dunbardf578252011-11-03 17:56:06 +00005import componentinfo
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +00006import configutil
Daniel Dunbardf578252011-11-03 17:56:06 +00007
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00008from util import *
9
10###
11
Daniel Dunbar57574fa2011-11-05 04:07:43 +000012def cmake_quote_string(value):
13 """
14 cmake_quote_string(value) -> str
15
16 Return a quoted form of the given value that is suitable for use in CMake
17 language files.
18 """
19
20 # Currently, we only handle escaping backslashes.
21 value = value.replace("\\", "\\\\")
22
23 return value
24
Daniel Dunbard5889d82011-11-17 01:19:53 +000025def cmake_quote_path(value):
26 """
27 cmake_quote_path(value) -> str
28
29 Return a quoted form of the given value that is suitable for use in CMake
30 language files.
31 """
32
33 # CMake has a bug in it's Makefile generator that doesn't properly quote
34 # strings it generates. So instead of using proper quoting, we just use "/"
35 # style paths. Currently, we only handle escaping backslashes.
36 value = value.replace("\\", "/")
37
38 return value
39
Daniel Dunbar20fb32b2011-11-04 23:40:11 +000040def mk_quote_string_for_target(value):
41 """
42 mk_quote_string_for_target(target_name) -> str
43
44 Return a quoted form of the given target_name suitable for including in a
45 Makefile as a target name.
46 """
47
48 # The only quoting we currently perform is for ':', to support msys users.
49 return value.replace(":", "\\:")
50
Daniel Dunbar195c6f32011-11-05 04:07:49 +000051def make_install_dir(path):
52 """
53 make_install_dir(path) -> None
54
55 Create the given directory path for installation, including any parents.
56 """
57
58 # os.makedirs considers it an error to be called with an existant path.
59 if not os.path.exists(path):
60 os.makedirs(path)
61
Daniel Dunbar20fb32b2011-11-04 23:40:11 +000062###
63
Daniel Dunbardf578252011-11-03 17:56:06 +000064class LLVMProjectInfo(object):
65 @staticmethod
66 def load_infos_from_path(llvmbuild_source_root):
Daniel Dunbare5609ab2011-12-12 22:45:59 +000067 def recurse(subpath):
68 # Load the LLVMBuild file.
69 llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
70 'LLVMBuild.txt')
71 if not os.path.exists(llvmbuild_path):
72 fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
Daniel Dunbardf578252011-11-03 17:56:06 +000073
Daniel Dunbare5609ab2011-12-12 22:45:59 +000074 # Parse the components from it.
75 common,info_iter = componentinfo.load_from_path(llvmbuild_path,
76 subpath)
77 for info in info_iter:
Daniel Dunbardf578252011-11-03 17:56:06 +000078 yield info
79
Daniel Dunbare5609ab2011-12-12 22:45:59 +000080 # Recurse into the specified subdirectories.
81 for subdir in common.get_list("subdirectories"):
82 for item in recurse(os.path.join(subpath, subdir)):
83 yield item
84
85 return recurse("/")
86
Daniel Dunbardf578252011-11-03 17:56:06 +000087 @staticmethod
88 def load_from_path(source_root, llvmbuild_source_root):
89 infos = list(
90 LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
91
92 return LLVMProjectInfo(source_root, infos)
93
94 def __init__(self, source_root, component_infos):
Daniel Dunbar1cf14af2011-11-03 17:56:12 +000095 # Store our simple ivars.
Daniel Dunbardf578252011-11-03 17:56:06 +000096 self.source_root = source_root
Daniel Dunbarb4eaee72011-11-10 00:49:58 +000097 self.component_infos = list(component_infos)
98 self.component_info_map = None
99 self.ordered_component_infos = None
100
101 def validate_components(self):
102 """validate_components() -> None
103
104 Validate that the project components are well-defined. Among other
105 things, this checks that:
106 - Components have valid references.
107 - Components references do not form cycles.
108
109 We also construct the map from component names to info, and the
110 topological ordering of components.
111 """
Daniel Dunbardf578252011-11-03 17:56:06 +0000112
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000113 # Create the component info map and validate that component names are
114 # unique.
115 self.component_info_map = {}
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000116 for ci in self.component_infos:
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000117 existing = self.component_info_map.get(ci.name)
118 if existing is not None:
119 # We found a duplicate component name, report it and error out.
120 fatal("found duplicate component %r (at %r and %r)" % (
121 ci.name, ci.subpath, existing.subpath))
122 self.component_info_map[ci.name] = ci
123
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000124 # Disallow 'all' as a component name, which is a special case.
125 if 'all' in self.component_info_map:
126 fatal("project is not allowed to define 'all' component")
127
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000128 # Add the root component.
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000129 if '$ROOT' in self.component_info_map:
130 fatal("project is not allowed to define $ROOT component")
131 self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
132 '/', '$ROOT', None)
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000133 self.component_infos.append(self.component_info_map['$ROOT'])
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000134
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000135 # Topologically order the component information according to their
136 # component references.
137 def visit_component_info(ci, current_stack, current_set):
138 # Check for a cycles.
139 if ci in current_set:
140 # We found a cycle, report it and error out.
141 cycle_description = ' -> '.join(
142 '%r (%s)' % (ci.name, relation)
143 for relation,ci in current_stack)
144 fatal("found cycle to %r after following: %s -> %s" % (
145 ci.name, cycle_description, ci.name))
146
147 # If we have already visited this item, we are done.
148 if ci not in components_to_visit:
149 return
150
151 # Otherwise, mark the component info as visited and traverse.
152 components_to_visit.remove(ci)
153
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000154 # Validate the parent reference, which we treat specially.
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000155 if ci.parent is not None:
156 parent = self.component_info_map.get(ci.parent)
157 if parent is None:
158 fatal("component %r has invalid reference %r (via %r)" % (
159 ci.name, ci.parent, 'parent'))
160 ci.set_parent_instance(parent)
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000161
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000162 for relation,referent_name in ci.get_component_references():
163 # Validate that the reference is ok.
164 referent = self.component_info_map.get(referent_name)
165 if referent is None:
166 fatal("component %r has invalid reference %r (via %r)" % (
167 ci.name, referent_name, relation))
168
169 # Visit the reference.
170 current_stack.append((relation,ci))
171 current_set.add(ci)
172 visit_component_info(referent, current_stack, current_set)
173 current_set.remove(ci)
174 current_stack.pop()
175
176 # Finally, add the component info to the ordered list.
177 self.ordered_component_infos.append(ci)
178
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000179 # FIXME: We aren't actually correctly checking for cycles along the
180 # parent edges. Haven't decided how I want to handle this -- I thought
181 # about only checking cycles by relation type. If we do that, it falls
182 # out easily. If we don't, we should special case the check.
183
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000184 self.ordered_component_infos = []
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000185 components_to_visit = set(self.component_infos)
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000186 while components_to_visit:
187 visit_component_info(iter(components_to_visit).next(), [], set())
188
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000189 # Canonicalize children lists.
190 for c in self.ordered_component_infos:
191 c.children.sort(key = lambda c: c.name)
192
193 def print_tree(self):
194 def visit(node, depth = 0):
195 print '%s%-40s (%s)' % (' '*depth, node.name, node.type_name)
196 for c in node.children:
197 visit(c, depth + 1)
198 visit(self.component_info_map['$ROOT'])
199
Daniel Dunbar43120df2011-11-03 17:56:21 +0000200 def write_components(self, output_path):
201 # Organize all the components by the directory their LLVMBuild file
202 # should go in.
203 info_basedir = {}
204 for ci in self.component_infos:
205 # Ignore the $ROOT component.
206 if ci.parent is None:
207 continue
208
209 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
210
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000211 # Compute the list of subdirectories to scan.
212 subpath_subdirs = {}
213 for ci in self.component_infos:
214 # Ignore root components.
215 if ci.subpath == '/':
216 continue
217
218 # Otherwise, append this subpath to the parent list.
219 parent_path = os.path.dirname(ci.subpath)
220 subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
221 parent_path, set())
222 parent_list.add(os.path.basename(ci.subpath))
223
Daniel Dunbar43120df2011-11-03 17:56:21 +0000224 # Generate the build files.
225 for subpath, infos in info_basedir.items():
226 # Order the components by name to have a canonical ordering.
227 infos.sort(key = lambda ci: ci.name)
228
229 # Format the components into llvmbuild fragments.
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000230 fragments = []
231
232 # Add the common fragments.
233 subdirectories = subpath_subdirs.get(subpath)
234 if subdirectories:
235 fragment = """\
236subdirectories = %s
237""" % (" ".join(sorted(subdirectories)),)
238 fragments.append(("common", fragment))
239
240 # Add the component fragments.
241 num_common_fragments = len(fragments)
242 for ci in infos:
243 fragment = ci.get_llvmbuild_fragment()
244 if fragment is None:
245 continue
246
247 name = "component_%d" % (len(fragments) - num_common_fragments)
248 fragments.append((name, fragment))
249
Daniel Dunbar43120df2011-11-03 17:56:21 +0000250 if not fragments:
251 continue
252
253 assert subpath.startswith('/')
254 directory_path = os.path.join(output_path, subpath[1:])
255
256 # Create the directory if it does not already exist.
257 if not os.path.exists(directory_path):
258 os.makedirs(directory_path)
259
Daniel Dunbara3217162011-12-12 22:45:35 +0000260 # In an effort to preserve comments (which aren't parsed), read in
261 # the original file and extract the comments. We only know how to
262 # associate comments that prefix a section name.
263 f = open(infos[0]._source_path)
264 comments_map = {}
265 comment_block = ""
266 for ln in f:
267 if ln.startswith(';'):
268 comment_block += ln
269 elif ln.startswith('[') and ln.endswith(']\n'):
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000270 comments_map[ln[1:-2]] = comment_block
Daniel Dunbara3217162011-12-12 22:45:35 +0000271 else:
272 comment_block = ""
273 f.close()
274
275 # Create the LLVMBuild fil[e.
Daniel Dunbar43120df2011-11-03 17:56:21 +0000276 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
277 f = open(file_path, "w")
Daniel Dunbarfb6d79a2011-11-03 17:56:31 +0000278
279 # Write the header.
280 header_fmt = ';===- %s %s-*- Conf -*--===;'
281 header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
282 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
283 header_string = header_fmt % (header_name, header_pad)
284 print >>f, """\
285%s
286;
287; The LLVM Compiler Infrastructure
288;
289; This file is distributed under the University of Illinois Open Source
290; License. See LICENSE.TXT for details.
291;
292;===------------------------------------------------------------------------===;
293;
294; This is an LLVMBuild description file for the components in this subdirectory.
295;
296; For more information on the LLVMBuild system, please see:
297;
298; http://llvm.org/docs/LLVMBuild.html
299;
300;===------------------------------------------------------------------------===;
301""" % header_string
302
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000303 # Write out each fragment.each component fragment.
304 for name,fragment in fragments:
Daniel Dunbara3217162011-12-12 22:45:35 +0000305 comment = comments_map.get(name)
306 if comment is not None:
307 f.write(comment)
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000308 print >>f, "[%s]" % name
Daniel Dunbar43120df2011-11-03 17:56:21 +0000309 f.write(fragment)
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000310 if fragment is not fragments[-1][1]:
Daniel Dunbar4ab406d2011-12-12 19:48:00 +0000311 print >>f
Daniel Dunbarb0c594f2011-12-12 22:45:54 +0000312
Daniel Dunbar43120df2011-11-03 17:56:21 +0000313 f.close()
314
Preston Gurd75493542012-05-07 19:38:40 +0000315 def write_library_table(self, output_path, enabled_optional_components):
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000316 # Write out the mapping from component names to required libraries.
317 #
318 # We do this in topological order so that we know we can append the
319 # dependencies for added library groups.
320 entries = {}
321 for c in self.ordered_component_infos:
Daniel Dunbar177a1192012-05-15 18:44:12 +0000322 # Skip optional components which are not enabled.
Preston Gurd75493542012-05-07 19:38:40 +0000323 if c.type_name == 'OptionalLibrary' \
324 and c.name not in enabled_optional_components:
325 continue
326
Daniel Dunbar177a1192012-05-15 18:44:12 +0000327 # Skip target groups which are not enabled.
328 tg = c.get_parent_target_group()
329 if tg and not tg.enabled:
330 continue
331
Daniel Dunbarc352caf2011-11-10 00:49:51 +0000332 # Only certain components are in the table.
Preston Gurd75493542012-05-07 19:38:40 +0000333 if c.type_name not in ('Library', 'OptionalLibrary', \
334 'LibraryGroup', 'TargetGroup'):
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000335 continue
336
337 # Compute the llvm-config "component name". For historical reasons,
338 # this is lowercased based on the library name.
339 llvmconfig_component_name = c.get_llvmconfig_component_name()
340
341 # Get the library name, or None for LibraryGroups.
Preston Gurd75493542012-05-07 19:38:40 +0000342 if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
Daniel Dunbarbb53bbb2011-12-15 23:35:08 +0000343 library_name = c.get_prefixed_library_name()
Daniel Dunbarc352caf2011-11-10 00:49:51 +0000344 else:
345 library_name = None
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000346
347 # Get the component names of all the required libraries.
348 required_llvmconfig_component_names = [
349 self.component_info_map[dep].get_llvmconfig_component_name()
350 for dep in c.required_libraries]
351
352 # Insert the entries for library groups we should add to.
353 for dep in c.add_to_library_groups:
354 entries[dep][2].append(llvmconfig_component_name)
355
356 # Add the entry.
357 entries[c.name] = (llvmconfig_component_name, library_name,
358 required_llvmconfig_component_names)
359
360 # Convert to a list of entries and sort by name.
361 entries = entries.values()
362
363 # Create an 'all' pseudo component. We keep the dependency list small by
364 # only listing entries that have no other dependents.
365 root_entries = set(e[0] for e in entries)
366 for _,_,deps in entries:
367 root_entries -= set(deps)
368 entries.append(('all', None, root_entries))
369
370 entries.sort()
371
372 # Compute the maximum number of required libraries, plus one so there is
373 # always a sentinel.
374 max_required_libraries = max(len(deps)
375 for _,_,deps in entries) + 1
376
377 # Write out the library table.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000378 make_install_dir(os.path.dirname(output_path))
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000379 f = open(output_path, 'w')
380 print >>f, """\
381//===- llvm-build generated file --------------------------------*- C++ -*-===//
382//
383// Component Library Depenedency Table
384//
385// Automatically generated file, do not edit!
386//
387//===----------------------------------------------------------------------===//
388"""
389 print >>f, 'struct AvailableComponent {'
390 print >>f, ' /// The name of the component.'
391 print >>f, ' const char *Name;'
392 print >>f, ''
393 print >>f, ' /// The name of the library for this component (or NULL).'
394 print >>f, ' const char *Library;'
395 print >>f, ''
396 print >>f, '\
397 /// The list of libraries required when linking this component.'
398 print >>f, ' const char *RequiredLibraries[%d];' % (
399 max_required_libraries)
400 print >>f, '} AvailableComponents[%d] = {' % len(entries)
401 for name,library_name,required_names in entries:
402 if library_name is None:
403 library_name_as_cstr = '0'
404 else:
Daniel Dunbarbb53bbb2011-12-15 23:35:08 +0000405 library_name_as_cstr = '"lib%s.a"' % library_name
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000406 print >>f, ' { "%s", %s, { %s } },' % (
407 name, library_name_as_cstr,
408 ', '.join('"%s"' % dep
409 for dep in required_names))
410 print >>f, '};'
411 f.close()
412
Daniel Dunbar5086de62011-11-29 00:06:50 +0000413 def get_required_libraries_for_component(self, ci, traverse_groups = False):
414 """
415 get_required_libraries_for_component(component_info) -> iter
416
417 Given a Library component info descriptor, return an iterator over all
418 of the directly required libraries for linking with this component. If
419 traverse_groups is True, then library and target groups will be
420 traversed to include their required libraries.
421 """
422
423 assert ci.type_name in ('Library', 'LibraryGroup', 'TargetGroup')
424
425 for name in ci.required_libraries:
426 # Get the dependency info.
427 dep = self.component_info_map[name]
428
429 # If it is a library, yield it.
430 if dep.type_name == 'Library':
431 yield dep
432 continue
433
434 # Otherwise if it is a group, yield or traverse depending on what
435 # was requested.
436 if dep.type_name in ('LibraryGroup', 'TargetGroup'):
437 if not traverse_groups:
438 yield dep
439 continue
440
441 for res in self.get_required_libraries_for_component(dep, True):
442 yield res
443
Daniel Dunbar16889612011-11-04 23:10:37 +0000444 def get_fragment_dependencies(self):
Daniel Dunbar02271a72011-11-03 22:46:19 +0000445 """
Daniel Dunbar16889612011-11-04 23:10:37 +0000446 get_fragment_dependencies() -> iter
Daniel Dunbar02271a72011-11-03 22:46:19 +0000447
Daniel Dunbar16889612011-11-04 23:10:37 +0000448 Compute the list of files (as absolute paths) on which the output
449 fragments depend (i.e., files for which a modification should trigger a
450 rebuild of the fragment).
Daniel Dunbar02271a72011-11-03 22:46:19 +0000451 """
452
453 # Construct a list of all the dependencies of the Makefile fragment
454 # itself. These include all the LLVMBuild files themselves, as well as
455 # all of our own sources.
Daniel Dunbar309fc862011-12-06 23:13:42 +0000456 #
457 # Many components may come from the same file, so we make sure to unique
458 # these.
459 build_paths = set()
Daniel Dunbar02271a72011-11-03 22:46:19 +0000460 for ci in self.component_infos:
Daniel Dunbar309fc862011-12-06 23:13:42 +0000461 p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
462 if p not in build_paths:
463 yield p
464 build_paths.add(p)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000465
466 # Gather the list of necessary sources by just finding all loaded
467 # modules that are inside the LLVM source tree.
468 for module in sys.modules.values():
469 # Find the module path.
470 if not hasattr(module, '__file__'):
471 continue
472 path = getattr(module, '__file__')
473 if not path:
474 continue
475
476 # Strip off any compiled suffix.
477 if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
478 path = path[:-1]
479
480 # If the path exists and is in the source tree, consider it a
481 # dependency.
482 if (path.startswith(self.source_root) and os.path.exists(path)):
Daniel Dunbar16889612011-11-04 23:10:37 +0000483 yield path
484
485 def write_cmake_fragment(self, output_path):
486 """
487 write_cmake_fragment(output_path) -> None
488
489 Generate a CMake fragment which includes all of the collated LLVMBuild
490 information in a format that is easily digestible by a CMake. The exact
491 contents of this are closely tied to how the CMake configuration
492 integrates LLVMBuild, see CMakeLists.txt in the top-level.
493 """
494
495 dependencies = list(self.get_fragment_dependencies())
496
497 # Write out the CMake fragment.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000498 make_install_dir(os.path.dirname(output_path))
Daniel Dunbar16889612011-11-04 23:10:37 +0000499 f = open(output_path, 'w')
500
501 # Write the header.
502 header_fmt = '\
503#===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
504 header_name = os.path.basename(output_path)
505 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
506 header_string = header_fmt % (header_name, header_pad)
507 print >>f, """\
508%s
509#
510# The LLVM Compiler Infrastructure
511#
512# This file is distributed under the University of Illinois Open Source
513# License. See LICENSE.TXT for details.
514#
515#===------------------------------------------------------------------------===#
516#
517# This file contains the LLVMBuild project information in a format easily
518# consumed by the CMake based build system.
519#
520# This file is autogenerated by llvm-build, do not edit!
521#
522#===------------------------------------------------------------------------===#
523""" % header_string
524
525 # Write the dependency information in the best way we can.
526 print >>f, """
527# LLVMBuild CMake fragment dependencies.
528#
529# CMake has no builtin way to declare that the configuration depends on
530# a particular file. However, a side effect of configure_file is to add
531# said input file to CMake's internal dependency list. So, we use that
532# and a dummy output file to communicate the dependency information to
533# CMake.
534#
535# FIXME: File a CMake RFE to get a properly supported version of this
536# feature."""
537 for dep in dependencies:
538 print >>f, """\
539configure_file(\"%s\"
Daniel Dunbar57574fa2011-11-05 04:07:43 +0000540 ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)""" % (
Daniel Dunbard5889d82011-11-17 01:19:53 +0000541 cmake_quote_path(dep),)
Daniel Dunbar57574fa2011-11-05 04:07:43 +0000542
Daniel Dunbar5086de62011-11-29 00:06:50 +0000543 # Write the properties we use to encode the required library dependency
544 # information in a form CMake can easily use directly.
545 print >>f, """
546# Explicit library dependency information.
547#
548# The following property assignments effectively create a map from component
549# names to required libraries, in a way that is easily accessed from CMake."""
550 for ci in self.ordered_component_infos:
551 # We only write the information for libraries currently.
552 if ci.type_name != 'Library':
553 continue
554
555 print >>f, """\
556set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)""" % (
557 ci.get_prefixed_library_name(), " ".join(sorted(
558 dep.get_prefixed_library_name()
559 for dep in self.get_required_libraries_for_component(ci))))
560
Daniel Dunbar16889612011-11-04 23:10:37 +0000561 f.close()
562
563 def write_make_fragment(self, output_path):
564 """
565 write_make_fragment(output_path) -> None
566
567 Generate a Makefile fragment which includes all of the collated
568 LLVMBuild information in a format that is easily digestible by a
569 Makefile. The exact contents of this are closely tied to how the LLVM
570 Makefiles integrate LLVMBuild, see Makefile.rules in the top-level.
571 """
572
573 dependencies = list(self.get_fragment_dependencies())
Daniel Dunbar02271a72011-11-03 22:46:19 +0000574
575 # Write out the Makefile fragment.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000576 make_install_dir(os.path.dirname(output_path))
Daniel Dunbar02271a72011-11-03 22:46:19 +0000577 f = open(output_path, 'w')
578
579 # Write the header.
580 header_fmt = '\
581#===-- %s - LLVMBuild Configuration for LLVM %s-*- Makefile -*--===#'
582 header_name = os.path.basename(output_path)
583 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
584 header_string = header_fmt % (header_name, header_pad)
585 print >>f, """\
586%s
587#
588# The LLVM Compiler Infrastructure
589#
590# This file is distributed under the University of Illinois Open Source
591# License. See LICENSE.TXT for details.
592#
593#===------------------------------------------------------------------------===#
594#
595# This file contains the LLVMBuild project information in a format easily
596# consumed by the Makefile based build system.
597#
598# This file is autogenerated by llvm-build, do not edit!
599#
600#===------------------------------------------------------------------------===#
601""" % header_string
602
603 # Write the dependencies for the fragment.
604 #
605 # FIXME: Technically, we need to properly quote for Make here.
606 print >>f, """\
607# Clients must explicitly enable LLVMBUILD_INCLUDE_DEPENDENCIES to get
608# these dependencies. This is a compromise to help improve the
609# performance of recursive Make systems."""
610 print >>f, 'ifeq ($(LLVMBUILD_INCLUDE_DEPENDENCIES),1)'
611 print >>f, "# The dependencies for this Makefile fragment itself."
Daniel Dunbar20fb32b2011-11-04 23:40:11 +0000612 print >>f, "%s: \\" % (mk_quote_string_for_target(output_path),)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000613 for dep in dependencies:
614 print >>f, "\t%s \\" % (dep,)
615 print >>f
616
617 # Generate dummy rules for each of the dependencies, so that things
618 # continue to work correctly if any of those files are moved or removed.
619 print >>f, """\
620# The dummy targets to allow proper regeneration even when files are moved or
621# removed."""
622 for dep in dependencies:
Daniel Dunbar20fb32b2011-11-04 23:40:11 +0000623 print >>f, "%s:" % (mk_quote_string_for_target(dep),)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000624 print >>f, 'endif'
625
626 f.close()
627
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000628def add_magic_target_components(parser, project, opts):
629 """add_magic_target_components(project, opts) -> None
630
631 Add the "magic" target based components to the project, which can only be
632 determined based on the target configuration options.
633
634 This currently is responsible for populating the required_libraries list of
Daniel Dunbar83337302011-11-10 01:16:48 +0000635 the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000636 """
637
638 # Determine the available targets.
639 available_targets = dict((ci.name,ci)
640 for ci in project.component_infos
641 if ci.type_name == 'TargetGroup')
642
643 # Find the configured native target.
644
645 # We handle a few special cases of target names here for historical
646 # reasons, as these are the names configure currently comes up with.
647 native_target_name = { 'x86' : 'X86',
648 'x86_64' : 'X86',
649 'Unknown' : None }.get(opts.native_target,
650 opts.native_target)
651 if native_target_name is None:
652 native_target = None
653 else:
654 native_target = available_targets.get(native_target_name)
655 if native_target is None:
656 parser.error("invalid native target: %r (not in project)" % (
657 opts.native_target,))
658 if native_target.type_name != 'TargetGroup':
659 parser.error("invalid native target: %r (not a target)" % (
660 opts.native_target,))
661
662 # Find the list of targets to enable.
663 if opts.enable_targets is None:
664 enable_targets = available_targets.values()
665 else:
Daniel Dunbar83337302011-11-10 01:16:48 +0000666 # We support both space separated and semi-colon separated lists.
667 if ' ' in opts.enable_targets:
668 enable_target_names = opts.enable_targets.split()
669 else:
670 enable_target_names = opts.enable_targets.split(';')
671
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000672 enable_targets = []
Daniel Dunbar83337302011-11-10 01:16:48 +0000673 for name in enable_target_names:
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000674 target = available_targets.get(name)
675 if target is None:
676 parser.error("invalid target to enable: %r (not in project)" % (
677 name,))
678 if target.type_name != 'TargetGroup':
679 parser.error("invalid target to enable: %r (not a target)" % (
680 name,))
681 enable_targets.append(target)
682
683 # Find the special library groups we are going to populate. We enforce that
684 # these appear in the project (instead of just adding them) so that they at
685 # least have an explicit representation in the project LLVMBuild files (and
686 # comments explaining how they are populated).
687 def find_special_group(name):
688 info = info_map.get(name)
689 if info is None:
690 fatal("expected project to contain special %r component" % (
691 name,))
692
693 if info.type_name != 'LibraryGroup':
694 fatal("special component %r should be a LibraryGroup" % (
695 name,))
696
697 if info.required_libraries:
698 fatal("special component %r must have empty %r list" % (
699 name, 'required_libraries'))
700 if info.add_to_library_groups:
701 fatal("special component %r must have empty %r list" % (
702 name, 'add_to_library_groups'))
703
Daniel Dunbar54d8c7f2011-12-12 22:45:41 +0000704 info._is_special_group = True
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000705 return info
706
707 info_map = dict((ci.name, ci) for ci in project.component_infos)
708 all_targets = find_special_group('all-targets')
709 native_group = find_special_group('Native')
710 native_codegen_group = find_special_group('NativeCodeGen')
711 engine_group = find_special_group('Engine')
712
713 # Set the enabled bit in all the target groups, and append to the
714 # all-targets list.
715 for ci in enable_targets:
716 all_targets.required_libraries.append(ci.name)
717 ci.enabled = True
718
719 # If we have a native target, then that defines the native and
720 # native_codegen libraries.
721 if native_target and native_target.enabled:
722 native_group.required_libraries.append(native_target.name)
723 native_codegen_group.required_libraries.append(
724 '%sCodeGen' % native_target.name)
725
726 # If we have a native target with a JIT, use that for the engine. Otherwise,
727 # use the interpreter.
728 if native_target and native_target.enabled and native_target.has_jit:
729 engine_group.required_libraries.append('JIT')
730 engine_group.required_libraries.append(native_group.name)
731 else:
732 engine_group.required_libraries.append('Interpreter')
733
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000734def main():
735 from optparse import OptionParser, OptionGroup
736 parser = OptionParser("usage: %prog [options]")
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000737
738 group = OptionGroup(parser, "Input Options")
739 group.add_option("", "--source-root", dest="source_root", metavar="PATH",
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000740 help="Path to the LLVM source (inferred if not given)",
741 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000742 group.add_option("", "--llvmbuild-source-root",
743 dest="llvmbuild_source_root",
744 help=(
745 "If given, an alternate path to search for LLVMBuild.txt files"),
746 action="store", default=None, metavar="PATH")
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000747 group.add_option("", "--build-root", dest="build_root", metavar="PATH",
748 help="Path to the build directory (if needed) [%default]",
749 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000750 parser.add_option_group(group)
751
752 group = OptionGroup(parser, "Output Options")
753 group.add_option("", "--print-tree", dest="print_tree",
754 help="Print out the project component tree [%default]",
755 action="store_true", default=False)
756 group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
Daniel Dunbar43120df2011-11-03 17:56:21 +0000757 help="Write out the LLVMBuild.txt files to PATH",
758 action="store", default=None, metavar="PATH")
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000759 group.add_option("", "--write-library-table",
760 dest="write_library_table", metavar="PATH",
761 help="Write the C++ library dependency table to PATH",
762 action="store", default=None)
763 group.add_option("", "--write-cmake-fragment",
764 dest="write_cmake_fragment", metavar="PATH",
765 help="Write the CMake project information to PATH",
766 action="store", default=None)
767 group.add_option("", "--write-make-fragment",
Daniel Dunbar02271a72011-11-03 22:46:19 +0000768 dest="write_make_fragment", metavar="PATH",
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000769 help="Write the Makefile project information to PATH",
770 action="store", default=None)
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000771 group.add_option("", "--configure-target-def-file",
772 dest="configure_target_def_files",
773 help="""Configure the given file at SUBPATH (relative to
774the inferred or given source root, and with a '.in' suffix) by replacing certain
775substitution variables with lists of targets that support certain features (for
776example, targets with AsmPrinters) and write the result to the build root (as
777given by --build-root) at the same SUBPATH""",
778 metavar="SUBPATH", action="append", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000779 parser.add_option_group(group)
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000780
781 group = OptionGroup(parser, "Configuration Options")
782 group.add_option("", "--native-target",
783 dest="native_target", metavar="NAME",
784 help=("Treat the named target as the 'native' one, if "
785 "given [%default]"),
786 action="store", default=None)
787 group.add_option("", "--enable-targets",
788 dest="enable_targets", metavar="NAMES",
Daniel Dunbar83337302011-11-10 01:16:48 +0000789 help=("Enable the given space or semi-colon separated "
790 "list of targets, or all targets if not present"),
Daniel Dunbar02271a72011-11-03 22:46:19 +0000791 action="store", default=None)
Preston Gurd75493542012-05-07 19:38:40 +0000792 group.add_option("", "--enable-optional-components",
793 dest="optional_components", metavar="NAMES",
794 help=("Enable the given space or semi-colon separated "
795 "list of optional components"),
796 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000797 parser.add_option_group(group)
798
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000799 (opts, args) = parser.parse_args()
800
801 # Determine the LLVM source path, if not given.
802 source_root = opts.source_root
803 if source_root:
804 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
805 'Function.cpp')):
806 parser.error('invalid LLVM source root: %r' % source_root)
807 else:
808 llvmbuild_path = os.path.dirname(__file__)
809 llvm_build_path = os.path.dirname(llvmbuild_path)
810 utils_path = os.path.dirname(llvm_build_path)
811 source_root = os.path.dirname(utils_path)
812 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
813 'Function.cpp')):
814 parser.error('unable to infer LLVM source root, please specify')
815
Daniel Dunbardf578252011-11-03 17:56:06 +0000816 # Construct the LLVM project information.
817 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
818 project_info = LLVMProjectInfo.load_from_path(
819 source_root, llvmbuild_source_root)
820
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000821 # Add the magic target based components.
822 add_magic_target_components(parser, project_info, opts)
823
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000824 # Validate the project component info.
825 project_info.validate_components()
826
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000827 # Print the component tree, if requested.
828 if opts.print_tree:
829 project_info.print_tree()
830
Daniel Dunbar43120df2011-11-03 17:56:21 +0000831 # Write out the components, if requested. This is useful for auto-upgrading
832 # the schema.
833 if opts.write_llvmbuild:
834 project_info.write_components(opts.write_llvmbuild)
835
Daniel Dunbar16889612011-11-04 23:10:37 +0000836 # Write out the required library table, if requested.
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000837 if opts.write_library_table:
Preston Gurd75493542012-05-07 19:38:40 +0000838 project_info.write_library_table(opts.write_library_table,
839 opts.optional_components)
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000840
Daniel Dunbar16889612011-11-04 23:10:37 +0000841 # Write out the make fragment, if requested.
Daniel Dunbar02271a72011-11-03 22:46:19 +0000842 if opts.write_make_fragment:
843 project_info.write_make_fragment(opts.write_make_fragment)
844
Daniel Dunbar16889612011-11-04 23:10:37 +0000845 # Write out the cmake fragment, if requested.
846 if opts.write_cmake_fragment:
847 project_info.write_cmake_fragment(opts.write_cmake_fragment)
848
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000849 # Configure target definition files, if requested.
850 if opts.configure_target_def_files:
851 # Verify we were given a build root.
852 if not opts.build_root:
853 parser.error("must specify --build-root when using "
854 "--configure-target-def-file")
855
856 # Create the substitution list.
857 available_targets = [ci for ci in project_info.component_infos
858 if ci.type_name == 'TargetGroup']
859 substitutions = [
860 ("@LLVM_ENUM_TARGETS@",
861 ' '.join('LLVM_TARGET(%s)' % ci.name
862 for ci in available_targets)),
863 ("@LLVM_ENUM_ASM_PRINTERS@",
864 ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
865 for ci in available_targets
866 if ci.has_asmprinter)),
867 ("@LLVM_ENUM_ASM_PARSERS@",
868 ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
869 for ci in available_targets
870 if ci.has_asmparser)),
871 ("@LLVM_ENUM_DISASSEMBLERS@",
872 ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
873 for ci in available_targets
874 if ci.has_disassembler))]
875
876 # Configure the given files.
877 for subpath in opts.configure_target_def_files:
878 inpath = os.path.join(source_root, subpath + '.in')
879 outpath = os.path.join(opts.build_root, subpath)
880 result = configutil.configure_file(inpath, outpath, substitutions)
881 if not result:
882 note("configured file %r hasn't changed" % outpath)
883
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000884if __name__=='__main__':
885 main()