llvm-build: Sketch code to load LLVMBuild.txt files.
[oota-llvm.git] / utils / llvm-build / llvmbuild / main.py
1 import pprint
2 import os
3
4 import componentinfo
5
6 class LLVMProjectInfo(object):
7     @staticmethod
8     def load_infos_from_path(llvmbuild_source_root):
9         # FIXME: Implement a simple subpath file list cache, so we don't restat
10         # directories we have already traversed.
11
12         # First, discover all the LLVMBuild.txt files.
13         for dirpath,dirnames,filenames in os.walk(llvmbuild_source_root,
14                                                   followlinks = True):
15             # If there is no LLVMBuild.txt file in a directory, we don't recurse
16             # past it. This is a simple way to prune our search, although it
17             # makes it easy for users to add LLVMBuild.txt files in places they
18             # won't be seen.
19             if 'LLVMBuild.txt' not in filenames:
20                 del dirnames[:]
21                 continue
22
23             # Otherwise, load the LLVMBuild file in this directory.
24             assert dirpath.startswith(llvmbuild_source_root)
25             subpath = '/' + dirpath[len(llvmbuild_source_root)+1:]
26             llvmbuild_path = os.path.join(dirpath, 'LLVMBuild.txt')
27             for info in componentinfo.load_from_path(llvmbuild_path, subpath):
28                 yield info
29
30     @staticmethod
31     def load_from_path(source_root, llvmbuild_source_root):
32         infos = list(
33             LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
34
35         return LLVMProjectInfo(source_root, infos)
36
37     def __init__(self, source_root, component_infos):
38         self.source_root = source_root
39         self.component_infos = component_infos
40
41 def main():
42     from optparse import OptionParser, OptionGroup
43     parser = OptionParser("usage: %prog [options]")
44     parser.add_option("", "--source-root", dest="source_root", metavar="PATH",
45                       help="Path to the LLVM source (inferred if not given)",
46                       action="store", default=None)
47     parser.add_option(
48         "", "--llvmbuild-source-root", dest="llvmbuild_source_root",
49         help="If given, an alternate path to search for LLVMBuild.txt files",
50         action="store", default=None, metavar="PATH")
51     (opts, args) = parser.parse_args()
52
53     # Determine the LLVM source path, if not given.
54     source_root = opts.source_root
55     if source_root:
56         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
57                                            'Function.cpp')):
58             parser.error('invalid LLVM source root: %r' % source_root)
59     else:
60         llvmbuild_path = os.path.dirname(__file__)
61         llvm_build_path = os.path.dirname(llvmbuild_path)
62         utils_path = os.path.dirname(llvm_build_path)
63         source_root = os.path.dirname(utils_path)
64         if not os.path.exists(os.path.join(source_root, 'lib', 'VMCore',
65                                            'Function.cpp')):
66             parser.error('unable to infer LLVM source root, please specify')
67
68     # Construct the LLVM project information.
69     llvmbuild_source_root = opts.llvmbuild_source_root or source_root
70     project_info = LLVMProjectInfo.load_from_path(
71         source_root, llvmbuild_source_root)
72
73 if __name__=='__main__':
74     main()