blob: 9c3126c48015627d37910f9b7ac75722c8d896f5 [file] [log] [blame]
Daniel Dunbarad5e0122011-11-03 17:56:03 +00001import os
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00002import sys
Daniel Dunbarad5e0122011-11-03 17:56:03 +00003
Daniel Dunbardf578252011-11-03 17:56:06 +00004import componentinfo
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +00005import configutil
Daniel Dunbardf578252011-11-03 17:56:06 +00006
Daniel Dunbar1cf14af2011-11-03 17:56:12 +00007from util import *
8
9###
10
Daniel Dunbar57574fa2011-11-05 04:07:43 +000011def cmake_quote_string(value):
12 """
13 cmake_quote_string(value) -> str
14
15 Return a quoted form of the given value that is suitable for use in CMake
16 language files.
17 """
18
19 # Currently, we only handle escaping backslashes.
20 value = value.replace("\\", "\\\\")
21
22 return value
23
Daniel Dunbard5889d82011-11-17 01:19:53 +000024def cmake_quote_path(value):
25 """
26 cmake_quote_path(value) -> str
27
28 Return a quoted form of the given value that is suitable for use in CMake
29 language files.
30 """
31
32 # CMake has a bug in it's Makefile generator that doesn't properly quote
33 # strings it generates. So instead of using proper quoting, we just use "/"
34 # style paths. Currently, we only handle escaping backslashes.
35 value = value.replace("\\", "/")
36
37 return value
38
Daniel Dunbar20fb32b2011-11-04 23:40:11 +000039def mk_quote_string_for_target(value):
40 """
41 mk_quote_string_for_target(target_name) -> str
42
43 Return a quoted form of the given target_name suitable for including in a
44 Makefile as a target name.
45 """
46
47 # The only quoting we currently perform is for ':', to support msys users.
48 return value.replace(":", "\\:")
49
Daniel Dunbar195c6f32011-11-05 04:07:49 +000050def make_install_dir(path):
51 """
52 make_install_dir(path) -> None
53
54 Create the given directory path for installation, including any parents.
55 """
56
57 # os.makedirs considers it an error to be called with an existant path.
58 if not os.path.exists(path):
59 os.makedirs(path)
60
Daniel Dunbar20fb32b2011-11-04 23:40:11 +000061###
62
Daniel Dunbardf578252011-11-03 17:56:06 +000063class LLVMProjectInfo(object):
64 @staticmethod
65 def load_infos_from_path(llvmbuild_source_root):
66 # FIXME: Implement a simple subpath file list cache, so we don't restat
67 # directories we have already traversed.
68
69 # First, discover all the LLVMBuild.txt files.
Daniel Dunbare10233b2011-11-03 19:45:52 +000070 #
71 # FIXME: We would like to use followlinks=True here, but that isn't
72 # compatible with Python 2.4. Instead, we will either have to special
73 # case projects we would expect to possibly be linked to, or implement
74 # our own walk that can follow links. For now, it doesn't matter since
75 # we haven't picked up the LLVMBuild system in any other LLVM projects.
76 for dirpath,dirnames,filenames in os.walk(llvmbuild_source_root):
Daniel Dunbardf578252011-11-03 17:56:06 +000077 # If there is no LLVMBuild.txt file in a directory, we don't recurse
78 # past it. This is a simple way to prune our search, although it
79 # makes it easy for users to add LLVMBuild.txt files in places they
80 # won't be seen.
81 if 'LLVMBuild.txt' not in filenames:
82 del dirnames[:]
83 continue
84
85 # Otherwise, load the LLVMBuild file in this directory.
86 assert dirpath.startswith(llvmbuild_source_root)
87 subpath = '/' + dirpath[len(llvmbuild_source_root)+1:]
88 llvmbuild_path = os.path.join(dirpath, 'LLVMBuild.txt')
89 for info in componentinfo.load_from_path(llvmbuild_path, subpath):
90 yield info
91
92 @staticmethod
93 def load_from_path(source_root, llvmbuild_source_root):
94 infos = list(
95 LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
96
97 return LLVMProjectInfo(source_root, infos)
98
99 def __init__(self, source_root, component_infos):
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000100 # Store our simple ivars.
Daniel Dunbardf578252011-11-03 17:56:06 +0000101 self.source_root = source_root
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000102 self.component_infos = list(component_infos)
103 self.component_info_map = None
104 self.ordered_component_infos = None
105
106 def validate_components(self):
107 """validate_components() -> None
108
109 Validate that the project components are well-defined. Among other
110 things, this checks that:
111 - Components have valid references.
112 - Components references do not form cycles.
113
114 We also construct the map from component names to info, and the
115 topological ordering of components.
116 """
Daniel Dunbardf578252011-11-03 17:56:06 +0000117
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000118 # Create the component info map and validate that component names are
119 # unique.
120 self.component_info_map = {}
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000121 for ci in self.component_infos:
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000122 existing = self.component_info_map.get(ci.name)
123 if existing is not None:
124 # We found a duplicate component name, report it and error out.
125 fatal("found duplicate component %r (at %r and %r)" % (
126 ci.name, ci.subpath, existing.subpath))
127 self.component_info_map[ci.name] = ci
128
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000129 # Disallow 'all' as a component name, which is a special case.
130 if 'all' in self.component_info_map:
131 fatal("project is not allowed to define 'all' component")
132
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000133 # Add the root component.
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000134 if '$ROOT' in self.component_info_map:
135 fatal("project is not allowed to define $ROOT component")
136 self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
137 '/', '$ROOT', None)
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000138 self.component_infos.append(self.component_info_map['$ROOT'])
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000139
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000140 # Topologically order the component information according to their
141 # component references.
142 def visit_component_info(ci, current_stack, current_set):
143 # Check for a cycles.
144 if ci in current_set:
145 # We found a cycle, report it and error out.
146 cycle_description = ' -> '.join(
147 '%r (%s)' % (ci.name, relation)
148 for relation,ci in current_stack)
149 fatal("found cycle to %r after following: %s -> %s" % (
150 ci.name, cycle_description, ci.name))
151
152 # If we have already visited this item, we are done.
153 if ci not in components_to_visit:
154 return
155
156 # Otherwise, mark the component info as visited and traverse.
157 components_to_visit.remove(ci)
158
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000159 # Validate the parent reference, which we treat specially.
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000160 if ci.parent is not None:
161 parent = self.component_info_map.get(ci.parent)
162 if parent is None:
163 fatal("component %r has invalid reference %r (via %r)" % (
164 ci.name, ci.parent, 'parent'))
165 ci.set_parent_instance(parent)
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000166
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000167 for relation,referent_name in ci.get_component_references():
168 # Validate that the reference is ok.
169 referent = self.component_info_map.get(referent_name)
170 if referent is None:
171 fatal("component %r has invalid reference %r (via %r)" % (
172 ci.name, referent_name, relation))
173
174 # Visit the reference.
175 current_stack.append((relation,ci))
176 current_set.add(ci)
177 visit_component_info(referent, current_stack, current_set)
178 current_set.remove(ci)
179 current_stack.pop()
180
181 # Finally, add the component info to the ordered list.
182 self.ordered_component_infos.append(ci)
183
Daniel Dunbar86c119a2011-11-03 17:56:16 +0000184 # FIXME: We aren't actually correctly checking for cycles along the
185 # parent edges. Haven't decided how I want to handle this -- I thought
186 # about only checking cycles by relation type. If we do that, it falls
187 # out easily. If we don't, we should special case the check.
188
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000189 self.ordered_component_infos = []
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000190 components_to_visit = set(self.component_infos)
Daniel Dunbar1cf14af2011-11-03 17:56:12 +0000191 while components_to_visit:
192 visit_component_info(iter(components_to_visit).next(), [], set())
193
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000194 # Canonicalize children lists.
195 for c in self.ordered_component_infos:
196 c.children.sort(key = lambda c: c.name)
197
198 def print_tree(self):
199 def visit(node, depth = 0):
200 print '%s%-40s (%s)' % (' '*depth, node.name, node.type_name)
201 for c in node.children:
202 visit(c, depth + 1)
203 visit(self.component_info_map['$ROOT'])
204
Daniel Dunbar43120df2011-11-03 17:56:21 +0000205 def write_components(self, output_path):
206 # Organize all the components by the directory their LLVMBuild file
207 # should go in.
208 info_basedir = {}
209 for ci in self.component_infos:
210 # Ignore the $ROOT component.
211 if ci.parent is None:
212 continue
213
214 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
215
216 # 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.
222 fragments = filter(None, [ci.get_llvmbuild_fragment()
223 for ci in infos])
224 if not fragments:
225 continue
226
227 assert subpath.startswith('/')
228 directory_path = os.path.join(output_path, subpath[1:])
229
230 # Create the directory if it does not already exist.
231 if not os.path.exists(directory_path):
232 os.makedirs(directory_path)
233
234 # Create the LLVMBuild file.
235 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
236 f = open(file_path, "w")
Daniel Dunbarfb6d79a2011-11-03 17:56:31 +0000237
238 # Write the header.
239 header_fmt = ';===- %s %s-*- Conf -*--===;'
240 header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
241 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
242 header_string = header_fmt % (header_name, header_pad)
243 print >>f, """\
244%s
245;
246; The LLVM Compiler Infrastructure
247;
248; This file is distributed under the University of Illinois Open Source
249; License. See LICENSE.TXT for details.
250;
251;===------------------------------------------------------------------------===;
252;
253; This is an LLVMBuild description file for the components in this subdirectory.
254;
255; For more information on the LLVMBuild system, please see:
256;
257; http://llvm.org/docs/LLVMBuild.html
258;
259;===------------------------------------------------------------------------===;
260""" % header_string
261
Daniel Dunbar43120df2011-11-03 17:56:21 +0000262 for i,fragment in enumerate(fragments):
263 print >>f, '[component_%d]' % i
264 f.write(fragment)
265 print >>f
266 f.close()
267
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000268 def write_library_table(self, output_path):
269 # Write out the mapping from component names to required libraries.
270 #
271 # We do this in topological order so that we know we can append the
272 # dependencies for added library groups.
273 entries = {}
274 for c in self.ordered_component_infos:
Daniel Dunbarc352caf2011-11-10 00:49:51 +0000275 # Only certain components are in the table.
276 if c.type_name not in ('Library', 'LibraryGroup', 'TargetGroup'):
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000277 continue
278
279 # Compute the llvm-config "component name". For historical reasons,
280 # this is lowercased based on the library name.
281 llvmconfig_component_name = c.get_llvmconfig_component_name()
282
283 # Get the library name, or None for LibraryGroups.
Daniel Dunbarc352caf2011-11-10 00:49:51 +0000284 if c.type_name == 'Library':
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000285 library_name = c.get_library_name()
Daniel Dunbarc352caf2011-11-10 00:49:51 +0000286 else:
287 library_name = None
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000288
289 # Get the component names of all the required libraries.
290 required_llvmconfig_component_names = [
291 self.component_info_map[dep].get_llvmconfig_component_name()
292 for dep in c.required_libraries]
293
294 # Insert the entries for library groups we should add to.
295 for dep in c.add_to_library_groups:
296 entries[dep][2].append(llvmconfig_component_name)
297
298 # Add the entry.
299 entries[c.name] = (llvmconfig_component_name, library_name,
300 required_llvmconfig_component_names)
301
302 # Convert to a list of entries and sort by name.
303 entries = entries.values()
304
305 # Create an 'all' pseudo component. We keep the dependency list small by
306 # only listing entries that have no other dependents.
307 root_entries = set(e[0] for e in entries)
308 for _,_,deps in entries:
309 root_entries -= set(deps)
310 entries.append(('all', None, root_entries))
311
312 entries.sort()
313
314 # Compute the maximum number of required libraries, plus one so there is
315 # always a sentinel.
316 max_required_libraries = max(len(deps)
317 for _,_,deps in entries) + 1
318
319 # Write out the library table.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000320 make_install_dir(os.path.dirname(output_path))
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000321 f = open(output_path, 'w')
322 print >>f, """\
323//===- llvm-build generated file --------------------------------*- C++ -*-===//
324//
325// Component Library Depenedency Table
326//
327// Automatically generated file, do not edit!
328//
329//===----------------------------------------------------------------------===//
330"""
331 print >>f, 'struct AvailableComponent {'
332 print >>f, ' /// The name of the component.'
333 print >>f, ' const char *Name;'
334 print >>f, ''
335 print >>f, ' /// The name of the library for this component (or NULL).'
336 print >>f, ' const char *Library;'
337 print >>f, ''
338 print >>f, '\
339 /// The list of libraries required when linking this component.'
340 print >>f, ' const char *RequiredLibraries[%d];' % (
341 max_required_libraries)
342 print >>f, '} AvailableComponents[%d] = {' % len(entries)
343 for name,library_name,required_names in entries:
344 if library_name is None:
345 library_name_as_cstr = '0'
346 else:
347 # If we had a project level component, we could derive the
348 # library prefix.
349 library_name_as_cstr = '"libLLVM%s.a"' % library_name
350 print >>f, ' { "%s", %s, { %s } },' % (
351 name, library_name_as_cstr,
352 ', '.join('"%s"' % dep
353 for dep in required_names))
354 print >>f, '};'
355 f.close()
356
Daniel Dunbar5086de62011-11-29 00:06:50 +0000357 def get_required_libraries_for_component(self, ci, traverse_groups = False):
358 """
359 get_required_libraries_for_component(component_info) -> iter
360
361 Given a Library component info descriptor, return an iterator over all
362 of the directly required libraries for linking with this component. If
363 traverse_groups is True, then library and target groups will be
364 traversed to include their required libraries.
365 """
366
367 assert ci.type_name in ('Library', 'LibraryGroup', 'TargetGroup')
368
369 for name in ci.required_libraries:
370 # Get the dependency info.
371 dep = self.component_info_map[name]
372
373 # If it is a library, yield it.
374 if dep.type_name == 'Library':
375 yield dep
376 continue
377
378 # Otherwise if it is a group, yield or traverse depending on what
379 # was requested.
380 if dep.type_name in ('LibraryGroup', 'TargetGroup'):
381 if not traverse_groups:
382 yield dep
383 continue
384
385 for res in self.get_required_libraries_for_component(dep, True):
386 yield res
387
Daniel Dunbar16889612011-11-04 23:10:37 +0000388 def get_fragment_dependencies(self):
Daniel Dunbar02271a72011-11-03 22:46:19 +0000389 """
Daniel Dunbar16889612011-11-04 23:10:37 +0000390 get_fragment_dependencies() -> iter
Daniel Dunbar02271a72011-11-03 22:46:19 +0000391
Daniel Dunbar16889612011-11-04 23:10:37 +0000392 Compute the list of files (as absolute paths) on which the output
393 fragments depend (i.e., files for which a modification should trigger a
394 rebuild of the fragment).
Daniel Dunbar02271a72011-11-03 22:46:19 +0000395 """
396
397 # Construct a list of all the dependencies of the Makefile fragment
398 # itself. These include all the LLVMBuild files themselves, as well as
399 # all of our own sources.
Daniel Dunbar309fc862011-12-06 23:13:42 +0000400 #
401 # Many components may come from the same file, so we make sure to unique
402 # these.
403 build_paths = set()
Daniel Dunbar02271a72011-11-03 22:46:19 +0000404 for ci in self.component_infos:
Daniel Dunbar309fc862011-12-06 23:13:42 +0000405 p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
406 if p not in build_paths:
407 yield p
408 build_paths.add(p)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000409
410 # Gather the list of necessary sources by just finding all loaded
411 # modules that are inside the LLVM source tree.
412 for module in sys.modules.values():
413 # Find the module path.
414 if not hasattr(module, '__file__'):
415 continue
416 path = getattr(module, '__file__')
417 if not path:
418 continue
419
420 # Strip off any compiled suffix.
421 if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
422 path = path[:-1]
423
424 # If the path exists and is in the source tree, consider it a
425 # dependency.
426 if (path.startswith(self.source_root) and os.path.exists(path)):
Daniel Dunbar16889612011-11-04 23:10:37 +0000427 yield path
428
429 def write_cmake_fragment(self, output_path):
430 """
431 write_cmake_fragment(output_path) -> None
432
433 Generate a CMake fragment which includes all of the collated LLVMBuild
434 information in a format that is easily digestible by a CMake. The exact
435 contents of this are closely tied to how the CMake configuration
436 integrates LLVMBuild, see CMakeLists.txt in the top-level.
437 """
438
439 dependencies = list(self.get_fragment_dependencies())
440
441 # Write out the CMake fragment.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000442 make_install_dir(os.path.dirname(output_path))
Daniel Dunbar16889612011-11-04 23:10:37 +0000443 f = open(output_path, 'w')
444
445 # Write the header.
446 header_fmt = '\
447#===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
448 header_name = os.path.basename(output_path)
449 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
450 header_string = header_fmt % (header_name, header_pad)
451 print >>f, """\
452%s
453#
454# The LLVM Compiler Infrastructure
455#
456# This file is distributed under the University of Illinois Open Source
457# License. See LICENSE.TXT for details.
458#
459#===------------------------------------------------------------------------===#
460#
461# This file contains the LLVMBuild project information in a format easily
462# consumed by the CMake based build system.
463#
464# This file is autogenerated by llvm-build, do not edit!
465#
466#===------------------------------------------------------------------------===#
467""" % header_string
468
469 # Write the dependency information in the best way we can.
470 print >>f, """
471# LLVMBuild CMake fragment dependencies.
472#
473# CMake has no builtin way to declare that the configuration depends on
474# a particular file. However, a side effect of configure_file is to add
475# said input file to CMake's internal dependency list. So, we use that
476# and a dummy output file to communicate the dependency information to
477# CMake.
478#
479# FIXME: File a CMake RFE to get a properly supported version of this
480# feature."""
481 for dep in dependencies:
482 print >>f, """\
483configure_file(\"%s\"
Daniel Dunbar57574fa2011-11-05 04:07:43 +0000484 ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)""" % (
Daniel Dunbard5889d82011-11-17 01:19:53 +0000485 cmake_quote_path(dep),)
Daniel Dunbar57574fa2011-11-05 04:07:43 +0000486
Daniel Dunbar5086de62011-11-29 00:06:50 +0000487 # Write the properties we use to encode the required library dependency
488 # information in a form CMake can easily use directly.
489 print >>f, """
490# Explicit library dependency information.
491#
492# The following property assignments effectively create a map from component
493# names to required libraries, in a way that is easily accessed from CMake."""
494 for ci in self.ordered_component_infos:
495 # We only write the information for libraries currently.
496 if ci.type_name != 'Library':
497 continue
498
499 print >>f, """\
500set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)""" % (
501 ci.get_prefixed_library_name(), " ".join(sorted(
502 dep.get_prefixed_library_name()
503 for dep in self.get_required_libraries_for_component(ci))))
504
Daniel Dunbar16889612011-11-04 23:10:37 +0000505 f.close()
506
507 def write_make_fragment(self, output_path):
508 """
509 write_make_fragment(output_path) -> None
510
511 Generate a Makefile fragment which includes all of the collated
512 LLVMBuild information in a format that is easily digestible by a
513 Makefile. The exact contents of this are closely tied to how the LLVM
514 Makefiles integrate LLVMBuild, see Makefile.rules in the top-level.
515 """
516
517 dependencies = list(self.get_fragment_dependencies())
Daniel Dunbar02271a72011-11-03 22:46:19 +0000518
519 # Write out the Makefile fragment.
Daniel Dunbar195c6f32011-11-05 04:07:49 +0000520 make_install_dir(os.path.dirname(output_path))
Daniel Dunbar02271a72011-11-03 22:46:19 +0000521 f = open(output_path, 'w')
522
523 # Write the header.
524 header_fmt = '\
525#===-- %s - LLVMBuild Configuration for LLVM %s-*- Makefile -*--===#'
526 header_name = os.path.basename(output_path)
527 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
528 header_string = header_fmt % (header_name, header_pad)
529 print >>f, """\
530%s
531#
532# The LLVM Compiler Infrastructure
533#
534# This file is distributed under the University of Illinois Open Source
535# License. See LICENSE.TXT for details.
536#
537#===------------------------------------------------------------------------===#
538#
539# This file contains the LLVMBuild project information in a format easily
540# consumed by the Makefile based build system.
541#
542# This file is autogenerated by llvm-build, do not edit!
543#
544#===------------------------------------------------------------------------===#
545""" % header_string
546
547 # Write the dependencies for the fragment.
548 #
549 # FIXME: Technically, we need to properly quote for Make here.
550 print >>f, """\
551# Clients must explicitly enable LLVMBUILD_INCLUDE_DEPENDENCIES to get
552# these dependencies. This is a compromise to help improve the
553# performance of recursive Make systems."""
554 print >>f, 'ifeq ($(LLVMBUILD_INCLUDE_DEPENDENCIES),1)'
555 print >>f, "# The dependencies for this Makefile fragment itself."
Daniel Dunbar20fb32b2011-11-04 23:40:11 +0000556 print >>f, "%s: \\" % (mk_quote_string_for_target(output_path),)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000557 for dep in dependencies:
558 print >>f, "\t%s \\" % (dep,)
559 print >>f
560
561 # Generate dummy rules for each of the dependencies, so that things
562 # continue to work correctly if any of those files are moved or removed.
563 print >>f, """\
564# The dummy targets to allow proper regeneration even when files are moved or
565# removed."""
566 for dep in dependencies:
Daniel Dunbar20fb32b2011-11-04 23:40:11 +0000567 print >>f, "%s:" % (mk_quote_string_for_target(dep),)
Daniel Dunbar02271a72011-11-03 22:46:19 +0000568 print >>f, 'endif'
569
570 f.close()
571
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000572def add_magic_target_components(parser, project, opts):
573 """add_magic_target_components(project, opts) -> None
574
575 Add the "magic" target based components to the project, which can only be
576 determined based on the target configuration options.
577
578 This currently is responsible for populating the required_libraries list of
Daniel Dunbar83337302011-11-10 01:16:48 +0000579 the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000580 """
581
582 # Determine the available targets.
583 available_targets = dict((ci.name,ci)
584 for ci in project.component_infos
585 if ci.type_name == 'TargetGroup')
586
587 # Find the configured native target.
588
589 # We handle a few special cases of target names here for historical
590 # reasons, as these are the names configure currently comes up with.
591 native_target_name = { 'x86' : 'X86',
592 'x86_64' : 'X86',
593 'Unknown' : None }.get(opts.native_target,
594 opts.native_target)
595 if native_target_name is None:
596 native_target = None
597 else:
598 native_target = available_targets.get(native_target_name)
599 if native_target is None:
600 parser.error("invalid native target: %r (not in project)" % (
601 opts.native_target,))
602 if native_target.type_name != 'TargetGroup':
603 parser.error("invalid native target: %r (not a target)" % (
604 opts.native_target,))
605
606 # Find the list of targets to enable.
607 if opts.enable_targets is None:
608 enable_targets = available_targets.values()
609 else:
Daniel Dunbar83337302011-11-10 01:16:48 +0000610 # We support both space separated and semi-colon separated lists.
611 if ' ' in opts.enable_targets:
612 enable_target_names = opts.enable_targets.split()
613 else:
614 enable_target_names = opts.enable_targets.split(';')
615
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000616 enable_targets = []
Daniel Dunbar83337302011-11-10 01:16:48 +0000617 for name in enable_target_names:
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000618 target = available_targets.get(name)
619 if target is None:
620 parser.error("invalid target to enable: %r (not in project)" % (
621 name,))
622 if target.type_name != 'TargetGroup':
623 parser.error("invalid target to enable: %r (not a target)" % (
624 name,))
625 enable_targets.append(target)
626
627 # Find the special library groups we are going to populate. We enforce that
628 # these appear in the project (instead of just adding them) so that they at
629 # least have an explicit representation in the project LLVMBuild files (and
630 # comments explaining how they are populated).
631 def find_special_group(name):
632 info = info_map.get(name)
633 if info is None:
634 fatal("expected project to contain special %r component" % (
635 name,))
636
637 if info.type_name != 'LibraryGroup':
638 fatal("special component %r should be a LibraryGroup" % (
639 name,))
640
641 if info.required_libraries:
642 fatal("special component %r must have empty %r list" % (
643 name, 'required_libraries'))
644 if info.add_to_library_groups:
645 fatal("special component %r must have empty %r list" % (
646 name, 'add_to_library_groups'))
647
648 return info
649
650 info_map = dict((ci.name, ci) for ci in project.component_infos)
651 all_targets = find_special_group('all-targets')
652 native_group = find_special_group('Native')
653 native_codegen_group = find_special_group('NativeCodeGen')
654 engine_group = find_special_group('Engine')
655
656 # Set the enabled bit in all the target groups, and append to the
657 # all-targets list.
658 for ci in enable_targets:
659 all_targets.required_libraries.append(ci.name)
660 ci.enabled = True
661
662 # If we have a native target, then that defines the native and
663 # native_codegen libraries.
664 if native_target and native_target.enabled:
665 native_group.required_libraries.append(native_target.name)
666 native_codegen_group.required_libraries.append(
667 '%sCodeGen' % native_target.name)
668
669 # If we have a native target with a JIT, use that for the engine. Otherwise,
670 # use the interpreter.
671 if native_target and native_target.enabled and native_target.has_jit:
672 engine_group.required_libraries.append('JIT')
673 engine_group.required_libraries.append(native_group.name)
674 else:
675 engine_group.required_libraries.append('Interpreter')
676
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000677def main():
678 from optparse import OptionParser, OptionGroup
679 parser = OptionParser("usage: %prog [options]")
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000680
681 group = OptionGroup(parser, "Input Options")
682 group.add_option("", "--source-root", dest="source_root", metavar="PATH",
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000683 help="Path to the LLVM source (inferred if not given)",
684 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000685 group.add_option("", "--llvmbuild-source-root",
686 dest="llvmbuild_source_root",
687 help=(
688 "If given, an alternate path to search for LLVMBuild.txt files"),
689 action="store", default=None, metavar="PATH")
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000690 group.add_option("", "--build-root", dest="build_root", metavar="PATH",
691 help="Path to the build directory (if needed) [%default]",
692 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000693 parser.add_option_group(group)
694
695 group = OptionGroup(parser, "Output Options")
696 group.add_option("", "--print-tree", dest="print_tree",
697 help="Print out the project component tree [%default]",
698 action="store_true", default=False)
699 group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
Daniel Dunbar43120df2011-11-03 17:56:21 +0000700 help="Write out the LLVMBuild.txt files to PATH",
701 action="store", default=None, metavar="PATH")
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000702 group.add_option("", "--write-library-table",
703 dest="write_library_table", metavar="PATH",
704 help="Write the C++ library dependency table to PATH",
705 action="store", default=None)
706 group.add_option("", "--write-cmake-fragment",
707 dest="write_cmake_fragment", metavar="PATH",
708 help="Write the CMake project information to PATH",
709 action="store", default=None)
710 group.add_option("", "--write-make-fragment",
Daniel Dunbar02271a72011-11-03 22:46:19 +0000711 dest="write_make_fragment", metavar="PATH",
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000712 help="Write the Makefile project information to PATH",
713 action="store", default=None)
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000714 group.add_option("", "--configure-target-def-file",
715 dest="configure_target_def_files",
716 help="""Configure the given file at SUBPATH (relative to
717the inferred or given source root, and with a '.in' suffix) by replacing certain
718substitution variables with lists of targets that support certain features (for
719example, targets with AsmPrinters) and write the result to the build root (as
720given by --build-root) at the same SUBPATH""",
721 metavar="SUBPATH", action="append", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000722 parser.add_option_group(group)
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000723
724 group = OptionGroup(parser, "Configuration Options")
725 group.add_option("", "--native-target",
726 dest="native_target", metavar="NAME",
727 help=("Treat the named target as the 'native' one, if "
728 "given [%default]"),
729 action="store", default=None)
730 group.add_option("", "--enable-targets",
731 dest="enable_targets", metavar="NAMES",
Daniel Dunbar83337302011-11-10 01:16:48 +0000732 help=("Enable the given space or semi-colon separated "
733 "list of targets, or all targets if not present"),
Daniel Dunbar02271a72011-11-03 22:46:19 +0000734 action="store", default=None)
Daniel Dunbar1e5b2432011-11-10 00:49:42 +0000735 parser.add_option_group(group)
736
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000737 (opts, args) = parser.parse_args()
738
739 # Determine the LLVM source path, if not given.
740 source_root = opts.source_root
741 if source_root:
742 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
743 'Function.cpp')):
744 parser.error('invalid LLVM source root: %r' % source_root)
745 else:
746 llvmbuild_path = os.path.dirname(__file__)
747 llvm_build_path = os.path.dirname(llvmbuild_path)
748 utils_path = os.path.dirname(llvm_build_path)
749 source_root = os.path.dirname(utils_path)
750 if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
751 'Function.cpp')):
752 parser.error('unable to infer LLVM source root, please specify')
753
Daniel Dunbardf578252011-11-03 17:56:06 +0000754 # Construct the LLVM project information.
755 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
756 project_info = LLVMProjectInfo.load_from_path(
757 source_root, llvmbuild_source_root)
758
Daniel Dunbaraffc6cf2011-11-10 00:50:07 +0000759 # Add the magic target based components.
760 add_magic_target_components(parser, project_info, opts)
761
Daniel Dunbarb4eaee72011-11-10 00:49:58 +0000762 # Validate the project component info.
763 project_info.validate_components()
764
Daniel Dunbar00b4b4f2011-11-03 17:56:18 +0000765 # Print the component tree, if requested.
766 if opts.print_tree:
767 project_info.print_tree()
768
Daniel Dunbar43120df2011-11-03 17:56:21 +0000769 # Write out the components, if requested. This is useful for auto-upgrading
770 # the schema.
771 if opts.write_llvmbuild:
772 project_info.write_components(opts.write_llvmbuild)
773
Daniel Dunbar16889612011-11-04 23:10:37 +0000774 # Write out the required library table, if requested.
Daniel Dunbarefe2f642011-11-03 17:56:28 +0000775 if opts.write_library_table:
776 project_info.write_library_table(opts.write_library_table)
777
Daniel Dunbar16889612011-11-04 23:10:37 +0000778 # Write out the make fragment, if requested.
Daniel Dunbar02271a72011-11-03 22:46:19 +0000779 if opts.write_make_fragment:
780 project_info.write_make_fragment(opts.write_make_fragment)
781
Daniel Dunbar16889612011-11-04 23:10:37 +0000782 # Write out the cmake fragment, if requested.
783 if opts.write_cmake_fragment:
784 project_info.write_cmake_fragment(opts.write_cmake_fragment)
785
Daniel Dunbarb7f3bfc2011-11-11 00:24:00 +0000786 # Configure target definition files, if requested.
787 if opts.configure_target_def_files:
788 # Verify we were given a build root.
789 if not opts.build_root:
790 parser.error("must specify --build-root when using "
791 "--configure-target-def-file")
792
793 # Create the substitution list.
794 available_targets = [ci for ci in project_info.component_infos
795 if ci.type_name == 'TargetGroup']
796 substitutions = [
797 ("@LLVM_ENUM_TARGETS@",
798 ' '.join('LLVM_TARGET(%s)' % ci.name
799 for ci in available_targets)),
800 ("@LLVM_ENUM_ASM_PRINTERS@",
801 ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
802 for ci in available_targets
803 if ci.has_asmprinter)),
804 ("@LLVM_ENUM_ASM_PARSERS@",
805 ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
806 for ci in available_targets
807 if ci.has_asmparser)),
808 ("@LLVM_ENUM_DISASSEMBLERS@",
809 ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
810 for ci in available_targets
811 if ci.has_disassembler))]
812
813 # Configure the given files.
814 for subpath in opts.configure_target_def_files:
815 inpath = os.path.join(source_root, subpath + '.in')
816 outpath = os.path.join(opts.build_root, subpath)
817 result = configutil.configure_file(inpath, outpath, substitutions)
818 if not result:
819 note("configured file %r hasn't changed" % outpath)
820
Daniel Dunbarad5e0122011-11-03 17:56:03 +0000821if __name__=='__main__':
822 main()