llvm-build: Tidy up options.
[oota-llvm.git] / utils / llvm-build / llvmbuild / main.py
1 import os
2 import sys
3
4 import componentinfo
5
6 from util import *
7
8 ###
9
10 def cmake_quote_string(value):
11     """
12     cmake_quote_string(value) -> str
13
14     Return a quoted form of the given value that is suitable for use in CMake
15     language files.
16     """
17
18     # Currently, we only handle escaping backslashes.
19     value = value.replace("\\", "\\\\")
20
21     return value
22
23 def mk_quote_string_for_target(value):
24     """
25     mk_quote_string_for_target(target_name) -> str
26
27     Return a quoted form of the given target_name suitable for including in a 
28     Makefile as a target name.
29     """
30
31     # The only quoting we currently perform is for ':', to support msys users.
32     return value.replace(":", "\\:")
33
34 def make_install_dir(path):
35     """
36     make_install_dir(path) -> None
37
38     Create the given directory path for installation, including any parents.
39     """
40
41     # os.makedirs considers it an error to be called with an existant path.
42     if not os.path.exists(path):
43         os.makedirs(path)
44
45 ###
46
47 class LLVMProjectInfo(object):
48     @staticmethod
49     def load_infos_from_path(llvmbuild_source_root):
50         # FIXME: Implement a simple subpath file list cache, so we don't restat
51         # directories we have already traversed.
52
53         # First, discover all the LLVMBuild.txt files.
54         #
55         # FIXME: We would like to use followlinks=True here, but that isn't
56         # compatible with Python 2.4. Instead, we will either have to special
57         # case projects we would expect to possibly be linked to, or implement
58         # our own walk that can follow links. For now, it doesn't matter since
59         # we haven't picked up the LLVMBuild system in any other LLVM projects.
60         for dirpath,dirnames,filenames in os.walk(llvmbuild_source_root):
61             # If there is no LLVMBuild.txt file in a directory, we don't recurse
62             # past it. This is a simple way to prune our search, although it
63             # makes it easy for users to add LLVMBuild.txt files in places they
64             # won't be seen.
65             if 'LLVMBuild.txt' not in filenames:
66                 del dirnames[:]
67                 continue
68
69             # Otherwise, load the LLVMBuild file in this directory.
70             assert dirpath.startswith(llvmbuild_source_root)
71             subpath = '/' + dirpath[len(llvmbuild_source_root)+1:]
72             llvmbuild_path = os.path.join(dirpath, 'LLVMBuild.txt')
73             for info in componentinfo.load_from_path(llvmbuild_path, subpath):
74                 yield info
75
76     @staticmethod
77     def load_from_path(source_root, llvmbuild_source_root):
78         infos = list(
79             LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
80
81         return LLVMProjectInfo(source_root, infos)
82
83     def __init__(self, source_root, component_infos):
84         # Store our simple ivars.
85         self.source_root = source_root
86         self.component_infos = component_infos
87
88         # Create the component info map and validate that component names are
89         # unique.
90         self.component_info_map = {}
91         for ci in component_infos:
92             existing = self.component_info_map.get(ci.name)
93             if existing is not None:
94                 # We found a duplicate component name, report it and error out.
95                 fatal("found duplicate component %r (at %r and %r)" % (
96                         ci.name, ci.subpath, existing.subpath))
97             self.component_info_map[ci.name] = ci
98
99         # Disallow 'all' as a component name, which is a special case.
100         if 'all' in self.component_info_map:
101             fatal("project is not allowed to define 'all' component")
102
103         # Add the root component.
104         if '$ROOT' in self.component_info_map:
105             fatal("project is not allowed to define $ROOT component")
106         self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
107             '/', '$ROOT', None)
108         self.component_infos.append(self.component_info_map['$ROOT'])
109
110         # Topologically order the component information according to their
111         # component references.
112         def visit_component_info(ci, current_stack, current_set):
113             # Check for a cycles.
114             if ci in current_set:
115                 # We found a cycle, report it and error out.
116                 cycle_description = ' -> '.join(
117                     '%r (%s)' % (ci.name, relation)
118                     for relation,ci in current_stack)
119                 fatal("found cycle to %r after following: %s -> %s" % (
120                         ci.name, cycle_description, ci.name))
121
122             # If we have already visited this item, we are done.
123             if ci not in components_to_visit:
124                 return
125
126             # Otherwise, mark the component info as visited and traverse.
127             components_to_visit.remove(ci)
128
129             # Validate the parent reference, which we treat specially.
130             if ci.parent is not None:
131                 parent = self.component_info_map.get(ci.parent)
132                 if parent is None:
133                     fatal("component %r has invalid reference %r (via %r)" % (
134                             ci.name, ci.parent, 'parent'))
135                 ci.set_parent_instance(parent)
136
137             for relation,referent_name in ci.get_component_references():
138                 # Validate that the reference is ok.
139                 referent = self.component_info_map.get(referent_name)
140                 if referent is None:
141                     fatal("component %r has invalid reference %r (via %r)" % (
142                             ci.name, referent_name, relation))
143
144                 # Visit the reference.
145                 current_stack.append((relation,ci))
146                 current_set.add(ci)
147                 visit_component_info(referent, current_stack, current_set)
148                 current_set.remove(ci)
149                 current_stack.pop()
150
151             # Finally, add the component info to the ordered list.
152             self.ordered_component_infos.append(ci)
153
154         # FIXME: We aren't actually correctly checking for cycles along the
155         # parent edges. Haven't decided how I want to handle this -- I thought
156         # about only checking cycles by relation type. If we do that, it falls
157         # out easily. If we don't, we should special case the check.
158
159         self.ordered_component_infos = []
160         components_to_visit = set(component_infos)
161         while components_to_visit:
162             visit_component_info(iter(components_to_visit).next(), [], set())
163
164         # Canonicalize children lists.
165         for c in self.ordered_component_infos:
166             c.children.sort(key = lambda c: c.name)
167
168     def print_tree(self):
169         def visit(node, depth = 0):
170             print '%s%-40s (%s)' % ('  '*depth, node.name, node.type_name)
171             for c in node.children:
172                 visit(c, depth + 1)
173         visit(self.component_info_map['$ROOT'])
174
175     def write_components(self, output_path):
176         # Organize all the components by the directory their LLVMBuild file
177         # should go in.
178         info_basedir = {}
179         for ci in self.component_infos:
180             # Ignore the $ROOT component.
181             if ci.parent is None:
182                 continue
183
184             info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
185
186         # Generate the build files.
187         for subpath, infos in info_basedir.items():
188             # Order the components by name to have a canonical ordering.
189             infos.sort(key = lambda ci: ci.name)
190
191             # Format the components into llvmbuild fragments.
192             fragments = filter(None, [ci.get_llvmbuild_fragment()
193                                       for ci in infos])
194             if not fragments:
195                 continue
196
197             assert subpath.startswith('/')
198             directory_path = os.path.join(output_path, subpath[1:])
199
200             # Create the directory if it does not already exist.
201             if not os.path.exists(directory_path):
202                 os.makedirs(directory_path)
203
204             # Create the LLVMBuild file.
205             file_path = os.path.join(directory_path, 'LLVMBuild.txt')
206             f = open(file_path, "w")
207
208             # Write the header.
209             header_fmt = ';===- %s %s-*- Conf -*--===;'
210             header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
211             header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
212             header_string = header_fmt % (header_name, header_pad)
213             print >>f, """\
214 %s
215 ;
216 ;                     The LLVM Compiler Infrastructure
217 ;
218 ; This file is distributed under the University of Illinois Open Source
219 ; License. See LICENSE.TXT for details.
220 ;
221 ;===------------------------------------------------------------------------===;
222 ;
223 ; This is an LLVMBuild description file for the components in this subdirectory.
224 ;
225 ; For more information on the LLVMBuild system, please see:
226 ;
227 ;   http://llvm.org/docs/LLVMBuild.html
228 ;
229 ;===------------------------------------------------------------------------===;
230 """ % header_string
231
232             for i,fragment in enumerate(fragments):
233                 print >>f, '[component_%d]' % i
234                 f.write(fragment)
235                 print >>f
236             f.close()
237
238     def write_library_table(self, output_path):
239         # Write out the mapping from component names to required libraries.
240         #
241         # We do this in topological order so that we know we can append the
242         # dependencies for added library groups.
243         entries = {}
244         for c in self.ordered_component_infos:
245             # Only Library and LibraryGroup components are in the table.
246             if c.type_name not in ('Library', 'LibraryGroup'):
247                 continue
248
249             # Compute the llvm-config "component name". For historical reasons,
250             # this is lowercased based on the library name.
251             llvmconfig_component_name = c.get_llvmconfig_component_name()
252             
253             # Get the library name, or None for LibraryGroups.
254             if c.type_name == 'LibraryGroup':
255                 library_name = None
256             else:
257                 library_name = c.get_library_name()
258
259             # Get the component names of all the required libraries.
260             required_llvmconfig_component_names = [
261                 self.component_info_map[dep].get_llvmconfig_component_name()
262                 for dep in c.required_libraries]
263
264             # Insert the entries for library groups we should add to.
265             for dep in c.add_to_library_groups:
266                 entries[dep][2].append(llvmconfig_component_name)
267
268             # Add the entry.
269             entries[c.name] = (llvmconfig_component_name, library_name,
270                                required_llvmconfig_component_names)
271
272         # Convert to a list of entries and sort by name.
273         entries = entries.values()
274
275         # Create an 'all' pseudo component. We keep the dependency list small by
276         # only listing entries that have no other dependents.
277         root_entries = set(e[0] for e in entries)
278         for _,_,deps in entries:
279             root_entries -= set(deps)
280         entries.append(('all', None, root_entries))
281
282         entries.sort()
283
284         # Compute the maximum number of required libraries, plus one so there is
285         # always a sentinel.
286         max_required_libraries = max(len(deps)
287                                      for _,_,deps in entries) + 1
288
289         # Write out the library table.
290         make_install_dir(os.path.dirname(output_path))
291         f = open(output_path, 'w')
292         print >>f, """\
293 //===- llvm-build generated file --------------------------------*- C++ -*-===//
294 //
295 // Component Library Depenedency Table
296 //
297 // Automatically generated file, do not edit!
298 //
299 //===----------------------------------------------------------------------===//
300 """
301         print >>f, 'struct AvailableComponent {'
302         print >>f, '  /// The name of the component.'
303         print >>f, '  const char *Name;'
304         print >>f, ''
305         print >>f, '  /// The name of the library for this component (or NULL).'
306         print >>f, '  const char *Library;'
307         print >>f, ''
308         print >>f, '\
309   /// The list of libraries required when linking this component.'
310         print >>f, '  const char *RequiredLibraries[%d];' % (
311             max_required_libraries)
312         print >>f, '} AvailableComponents[%d] = {' % len(entries)
313         for name,library_name,required_names in entries:
314             if library_name is None:
315                 library_name_as_cstr = '0'
316             else:
317                 # If we had a project level component, we could derive the
318                 # library prefix.
319                 library_name_as_cstr = '"libLLVM%s.a"' % library_name
320             print >>f, '  { "%s", %s, { %s } },' % (
321                 name, library_name_as_cstr,
322                 ', '.join('"%s"' % dep
323                           for dep in required_names))
324         print >>f, '};'
325         f.close()
326
327     def get_fragment_dependencies(self):
328         """
329         get_fragment_dependencies() -> iter
330
331         Compute the list of files (as absolute paths) on which the output
332         fragments depend (i.e., files for which a modification should trigger a
333         rebuild of the fragment).
334         """
335
336         # Construct a list of all the dependencies of the Makefile fragment
337         # itself. These include all the LLVMBuild files themselves, as well as
338         # all of our own sources.
339         for ci in self.component_infos:
340             yield os.path.join(self.source_root, ci.subpath[1:],
341                                'LLVMBuild.txt')
342
343         # Gather the list of necessary sources by just finding all loaded
344         # modules that are inside the LLVM source tree.
345         for module in sys.modules.values():
346             # Find the module path.
347             if not hasattr(module, '__file__'):
348                 continue
349             path = getattr(module, '__file__')
350             if not path:
351                 continue
352
353             # Strip off any compiled suffix.
354             if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
355                 path = path[:-1]
356
357             # If the path exists and is in the source tree, consider it a
358             # dependency.
359             if (path.startswith(self.source_root) and os.path.exists(path)):
360                 yield path
361
362     def write_cmake_fragment(self, output_path):
363         """
364         write_cmake_fragment(output_path) -> None
365
366         Generate a CMake fragment which includes all of the collated LLVMBuild
367         information in a format that is easily digestible by a CMake. The exact
368         contents of this are closely tied to how the CMake configuration
369         integrates LLVMBuild, see CMakeLists.txt in the top-level.
370         """
371
372         dependencies = list(self.get_fragment_dependencies())
373
374         # Write out the CMake fragment.
375         make_install_dir(os.path.dirname(output_path))
376         f = open(output_path, 'w')
377
378         # Write the header.
379         header_fmt = '\
380 #===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
381         header_name = os.path.basename(output_path)
382         header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
383         header_string = header_fmt % (header_name, header_pad)
384         print >>f, """\
385 %s
386 #
387 #                     The LLVM Compiler Infrastructure
388 #
389 # This file is distributed under the University of Illinois Open Source
390 # License. See LICENSE.TXT for details.
391 #
392 #===------------------------------------------------------------------------===#
393 #
394 # This file contains the LLVMBuild project information in a format easily
395 # consumed by the CMake based build system.
396 #
397 # This file is autogenerated by llvm-build, do not edit!
398 #
399 #===------------------------------------------------------------------------===#
400 """ % header_string
401
402         # Write the dependency information in the best way we can.
403         print >>f, """
404 # LLVMBuild CMake fragment dependencies.
405 #
406 # CMake has no builtin way to declare that the configuration depends on
407 # a particular file. However, a side effect of configure_file is to add
408 # said input file to CMake's internal dependency list. So, we use that
409 # and a dummy output file to communicate the dependency information to
410 # CMake.
411 #
412 # FIXME: File a CMake RFE to get a properly supported version of this
413 # feature."""
414         for dep in dependencies:
415             print >>f, """\
416 configure_file(\"%s\"
417                ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)""" % (
418                 cmake_quote_string(dep),)
419
420         f.close()
421
422     def write_make_fragment(self, output_path):
423         """
424         write_make_fragment(output_path) -> None
425
426         Generate a Makefile fragment which includes all of the collated
427         LLVMBuild information in a format that is easily digestible by a
428         Makefile. The exact contents of this are closely tied to how the LLVM
429         Makefiles integrate LLVMBuild, see Makefile.rules in the top-level.
430         """
431
432         dependencies = list(self.get_fragment_dependencies())
433
434         # Write out the Makefile fragment.
435         make_install_dir(os.path.dirname(output_path))
436         f = open(output_path, 'w')
437
438         # Write the header.
439         header_fmt = '\
440 #===-- %s - LLVMBuild Configuration for LLVM %s-*- Makefile -*--===#'
441         header_name = os.path.basename(output_path)
442         header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
443         header_string = header_fmt % (header_name, header_pad)
444         print >>f, """\
445 %s
446 #
447 #                     The LLVM Compiler Infrastructure
448 #
449 # This file is distributed under the University of Illinois Open Source
450 # License. See LICENSE.TXT for details.
451 #
452 #===------------------------------------------------------------------------===#
453 #
454 # This file contains the LLVMBuild project information in a format easily
455 # consumed by the Makefile based build system.
456 #
457 # This file is autogenerated by llvm-build, do not edit!
458 #
459 #===------------------------------------------------------------------------===#
460 """ % header_string
461
462         # Write the dependencies for the fragment.
463         #
464         # FIXME: Technically, we need to properly quote for Make here.
465         print >>f, """\
466 # Clients must explicitly enable LLVMBUILD_INCLUDE_DEPENDENCIES to get
467 # these dependencies. This is a compromise to help improve the
468 # performance of recursive Make systems.""" 
469         print >>f, 'ifeq ($(LLVMBUILD_INCLUDE_DEPENDENCIES),1)'
470         print >>f, "# The dependencies for this Makefile fragment itself."
471         print >>f, "%s: \\" % (mk_quote_string_for_target(output_path),)
472         for dep in dependencies:
473             print >>f, "\t%s \\" % (dep,)
474         print >>f
475
476         # Generate dummy rules for each of the dependencies, so that things
477         # continue to work correctly if any of those files are moved or removed.
478         print >>f, """\
479 # The dummy targets to allow proper regeneration even when files are moved or
480 # removed."""
481         for dep in dependencies:
482             print >>f, "%s:" % (mk_quote_string_for_target(dep),)
483         print >>f, 'endif'
484
485         f.close()
486
487 def main():
488     from optparse import OptionParser, OptionGroup
489     parser = OptionParser("usage: %prog [options]")
490
491     group = OptionGroup(parser, "Input Options")
492     group.add_option("", "--source-root", dest="source_root", metavar="PATH",
493                       help="Path to the LLVM source (inferred if not given)",
494                       action="store", default=None)
495     group.add_option("", "--llvmbuild-source-root",
496                      dest="llvmbuild_source_root",
497                      help=(
498             "If given, an alternate path to search for LLVMBuild.txt files"),
499                      action="store", default=None, metavar="PATH")
500     parser.add_option_group(group)
501
502     group = OptionGroup(parser, "Output Options")
503     group.add_option("", "--print-tree", dest="print_tree",
504                      help="Print out the project component tree [%default]",
505                      action="store_true", default=False)
506     group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
507                       help="Write out the LLVMBuild.txt files to PATH",
508                       action="store", default=None, metavar="PATH")
509     group.add_option("", "--write-library-table",
510                      dest="write_library_table", metavar="PATH",
511                      help="Write the C++ library dependency table to PATH",
512                      action="store", default=None)
513     group.add_option("", "--write-cmake-fragment",
514                      dest="write_cmake_fragment", metavar="PATH",
515                      help="Write the CMake project information to PATH",
516                      action="store", default=None)
517     group.add_option("", "--write-make-fragment",
518                       dest="write_make_fragment", metavar="PATH",
519                      help="Write the Makefile project information to PATH",
520                      action="store", default=None)
521     parser.add_option_group(group)
522                       action="store", default=None)
523     parser.add_option_group(group)
524
525     (opts, args) = parser.parse_args()
526
527     # Determine the LLVM source path, if not given.
528     source_root = opts.source_root
529     if source_root:
530         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
531                                            'Function.cpp')):
532             parser.error('invalid LLVM source root: %r' % source_root)
533     else:
534         llvmbuild_path = os.path.dirname(__file__)
535         llvm_build_path = os.path.dirname(llvmbuild_path)
536         utils_path = os.path.dirname(llvm_build_path)
537         source_root = os.path.dirname(utils_path)
538         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
539                                            'Function.cpp')):
540             parser.error('unable to infer LLVM source root, please specify')
541
542     # Construct the LLVM project information.
543     llvmbuild_source_root = opts.llvmbuild_source_root or source_root
544     project_info = LLVMProjectInfo.load_from_path(
545         source_root, llvmbuild_source_root)
546
547     # Print the component tree, if requested.
548     if opts.print_tree:
549         project_info.print_tree()
550
551     # Write out the components, if requested. This is useful for auto-upgrading
552     # the schema.
553     if opts.write_llvmbuild:
554         project_info.write_components(opts.write_llvmbuild)
555
556     # Write out the required library table, if requested.
557     if opts.write_library_table:
558         project_info.write_library_table(opts.write_library_table)
559
560     # Write out the make fragment, if requested.
561     if opts.write_make_fragment:
562         project_info.write_make_fragment(opts.write_make_fragment)
563
564     # Write out the cmake fragment, if requested.
565     if opts.write_cmake_fragment:
566         project_info.write_cmake_fragment(opts.write_cmake_fragment)
567
568 if __name__=='__main__':
569     main()