xref: /aosp_15_r20/external/llvm/utils/llvm-build/llvmbuild/main.py (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Workerfrom __future__ import absolute_import
2*9880d681SAndroid Build Coastguard Workerimport filecmp
3*9880d681SAndroid Build Coastguard Workerimport os
4*9880d681SAndroid Build Coastguard Workerimport sys
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Workerimport llvmbuild.componentinfo as componentinfo
7*9880d681SAndroid Build Coastguard Workerimport llvmbuild.configutil as configutil
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Workerfrom llvmbuild.util import fatal, note
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Worker###
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard Workerdef cmake_quote_string(value):
14*9880d681SAndroid Build Coastguard Worker    """
15*9880d681SAndroid Build Coastguard Worker    cmake_quote_string(value) -> str
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker    Return a quoted form of the given value that is suitable for use in CMake
18*9880d681SAndroid Build Coastguard Worker    language files.
19*9880d681SAndroid Build Coastguard Worker    """
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker    # Currently, we only handle escaping backslashes.
22*9880d681SAndroid Build Coastguard Worker    value = value.replace("\\", "\\\\")
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker    return value
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Workerdef cmake_quote_path(value):
27*9880d681SAndroid Build Coastguard Worker    """
28*9880d681SAndroid Build Coastguard Worker    cmake_quote_path(value) -> str
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker    Return a quoted form of the given value that is suitable for use in CMake
31*9880d681SAndroid Build Coastguard Worker    language files.
32*9880d681SAndroid Build Coastguard Worker    """
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker    # CMake has a bug in it's Makefile generator that doesn't properly quote
35*9880d681SAndroid Build Coastguard Worker    # strings it generates. So instead of using proper quoting, we just use "/"
36*9880d681SAndroid Build Coastguard Worker    # style paths.  Currently, we only handle escaping backslashes.
37*9880d681SAndroid Build Coastguard Worker    value = value.replace("\\", "/")
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker    return value
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Workerdef mk_quote_string_for_target(value):
42*9880d681SAndroid Build Coastguard Worker    """
43*9880d681SAndroid Build Coastguard Worker    mk_quote_string_for_target(target_name) -> str
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker    Return a quoted form of the given target_name suitable for including in a
46*9880d681SAndroid Build Coastguard Worker    Makefile as a target name.
47*9880d681SAndroid Build Coastguard Worker    """
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker    # The only quoting we currently perform is for ':', to support msys users.
50*9880d681SAndroid Build Coastguard Worker    return value.replace(":", "\\:")
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Workerdef make_install_dir(path):
53*9880d681SAndroid Build Coastguard Worker    """
54*9880d681SAndroid Build Coastguard Worker    make_install_dir(path) -> None
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker    Create the given directory path for installation, including any parents.
57*9880d681SAndroid Build Coastguard Worker    """
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker    # os.makedirs considers it an error to be called with an existent path.
60*9880d681SAndroid Build Coastguard Worker    if not os.path.exists(path):
61*9880d681SAndroid Build Coastguard Worker        os.makedirs(path)
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker###
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Workerclass LLVMProjectInfo(object):
66*9880d681SAndroid Build Coastguard Worker    @staticmethod
67*9880d681SAndroid Build Coastguard Worker    def load_infos_from_path(llvmbuild_source_root):
68*9880d681SAndroid Build Coastguard Worker        def recurse(subpath):
69*9880d681SAndroid Build Coastguard Worker            # Load the LLVMBuild file.
70*9880d681SAndroid Build Coastguard Worker            llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
71*9880d681SAndroid Build Coastguard Worker                                          'LLVMBuild.txt')
72*9880d681SAndroid Build Coastguard Worker            if not os.path.exists(llvmbuild_path):
73*9880d681SAndroid Build Coastguard Worker                fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker            # Parse the components from it.
76*9880d681SAndroid Build Coastguard Worker            common,info_iter = componentinfo.load_from_path(llvmbuild_path,
77*9880d681SAndroid Build Coastguard Worker                                                            subpath)
78*9880d681SAndroid Build Coastguard Worker            for info in info_iter:
79*9880d681SAndroid Build Coastguard Worker                yield info
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker            # Recurse into the specified subdirectories.
82*9880d681SAndroid Build Coastguard Worker            for subdir in common.get_list("subdirectories"):
83*9880d681SAndroid Build Coastguard Worker                for item in recurse(os.path.join(subpath, subdir)):
84*9880d681SAndroid Build Coastguard Worker                    yield item
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker        return recurse("/")
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker    @staticmethod
89*9880d681SAndroid Build Coastguard Worker    def load_from_path(source_root, llvmbuild_source_root):
90*9880d681SAndroid Build Coastguard Worker        infos = list(
91*9880d681SAndroid Build Coastguard Worker            LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root))
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker        return LLVMProjectInfo(source_root, infos)
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker    def __init__(self, source_root, component_infos):
96*9880d681SAndroid Build Coastguard Worker        # Store our simple ivars.
97*9880d681SAndroid Build Coastguard Worker        self.source_root = source_root
98*9880d681SAndroid Build Coastguard Worker        self.component_infos = list(component_infos)
99*9880d681SAndroid Build Coastguard Worker        self.component_info_map = None
100*9880d681SAndroid Build Coastguard Worker        self.ordered_component_infos = None
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker    def validate_components(self):
103*9880d681SAndroid Build Coastguard Worker        """validate_components() -> None
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker        Validate that the project components are well-defined. Among other
106*9880d681SAndroid Build Coastguard Worker        things, this checks that:
107*9880d681SAndroid Build Coastguard Worker          - Components have valid references.
108*9880d681SAndroid Build Coastguard Worker          - Components references do not form cycles.
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker        We also construct the map from component names to info, and the
111*9880d681SAndroid Build Coastguard Worker        topological ordering of components.
112*9880d681SAndroid Build Coastguard Worker        """
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker        # Create the component info map and validate that component names are
115*9880d681SAndroid Build Coastguard Worker        # unique.
116*9880d681SAndroid Build Coastguard Worker        self.component_info_map = {}
117*9880d681SAndroid Build Coastguard Worker        for ci in self.component_infos:
118*9880d681SAndroid Build Coastguard Worker            existing = self.component_info_map.get(ci.name)
119*9880d681SAndroid Build Coastguard Worker            if existing is not None:
120*9880d681SAndroid Build Coastguard Worker                # We found a duplicate component name, report it and error out.
121*9880d681SAndroid Build Coastguard Worker                fatal("found duplicate component %r (at %r and %r)" % (
122*9880d681SAndroid Build Coastguard Worker                        ci.name, ci.subpath, existing.subpath))
123*9880d681SAndroid Build Coastguard Worker            self.component_info_map[ci.name] = ci
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker        # Disallow 'all' as a component name, which is a special case.
126*9880d681SAndroid Build Coastguard Worker        if 'all' in self.component_info_map:
127*9880d681SAndroid Build Coastguard Worker            fatal("project is not allowed to define 'all' component")
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker        # Add the root component.
130*9880d681SAndroid Build Coastguard Worker        if '$ROOT' in self.component_info_map:
131*9880d681SAndroid Build Coastguard Worker            fatal("project is not allowed to define $ROOT component")
132*9880d681SAndroid Build Coastguard Worker        self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
133*9880d681SAndroid Build Coastguard Worker            '/', '$ROOT', None)
134*9880d681SAndroid Build Coastguard Worker        self.component_infos.append(self.component_info_map['$ROOT'])
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker        # Topologically order the component information according to their
137*9880d681SAndroid Build Coastguard Worker        # component references.
138*9880d681SAndroid Build Coastguard Worker        def visit_component_info(ci, current_stack, current_set):
139*9880d681SAndroid Build Coastguard Worker            # Check for a cycles.
140*9880d681SAndroid Build Coastguard Worker            if ci in current_set:
141*9880d681SAndroid Build Coastguard Worker                # We found a cycle, report it and error out.
142*9880d681SAndroid Build Coastguard Worker                cycle_description = ' -> '.join(
143*9880d681SAndroid Build Coastguard Worker                    '%r (%s)' % (ci.name, relation)
144*9880d681SAndroid Build Coastguard Worker                    for relation,ci in current_stack)
145*9880d681SAndroid Build Coastguard Worker                fatal("found cycle to %r after following: %s -> %s" % (
146*9880d681SAndroid Build Coastguard Worker                        ci.name, cycle_description, ci.name))
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker            # If we have already visited this item, we are done.
149*9880d681SAndroid Build Coastguard Worker            if ci not in components_to_visit:
150*9880d681SAndroid Build Coastguard Worker                return
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker            # Otherwise, mark the component info as visited and traverse.
153*9880d681SAndroid Build Coastguard Worker            components_to_visit.remove(ci)
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker            # Validate the parent reference, which we treat specially.
156*9880d681SAndroid Build Coastguard Worker            if ci.parent is not None:
157*9880d681SAndroid Build Coastguard Worker                parent = self.component_info_map.get(ci.parent)
158*9880d681SAndroid Build Coastguard Worker                if parent is None:
159*9880d681SAndroid Build Coastguard Worker                    fatal("component %r has invalid reference %r (via %r)" % (
160*9880d681SAndroid Build Coastguard Worker                            ci.name, ci.parent, 'parent'))
161*9880d681SAndroid Build Coastguard Worker                ci.set_parent_instance(parent)
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker            for relation,referent_name in ci.get_component_references():
164*9880d681SAndroid Build Coastguard Worker                # Validate that the reference is ok.
165*9880d681SAndroid Build Coastguard Worker                referent = self.component_info_map.get(referent_name)
166*9880d681SAndroid Build Coastguard Worker                if referent is None:
167*9880d681SAndroid Build Coastguard Worker                    fatal("component %r has invalid reference %r (via %r)" % (
168*9880d681SAndroid Build Coastguard Worker                            ci.name, referent_name, relation))
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker                # Visit the reference.
171*9880d681SAndroid Build Coastguard Worker                current_stack.append((relation,ci))
172*9880d681SAndroid Build Coastguard Worker                current_set.add(ci)
173*9880d681SAndroid Build Coastguard Worker                visit_component_info(referent, current_stack, current_set)
174*9880d681SAndroid Build Coastguard Worker                current_set.remove(ci)
175*9880d681SAndroid Build Coastguard Worker                current_stack.pop()
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker            # Finally, add the component info to the ordered list.
178*9880d681SAndroid Build Coastguard Worker            self.ordered_component_infos.append(ci)
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker        # FIXME: We aren't actually correctly checking for cycles along the
181*9880d681SAndroid Build Coastguard Worker        # parent edges. Haven't decided how I want to handle this -- I thought
182*9880d681SAndroid Build Coastguard Worker        # about only checking cycles by relation type. If we do that, it falls
183*9880d681SAndroid Build Coastguard Worker        # out easily. If we don't, we should special case the check.
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker        self.ordered_component_infos = []
186*9880d681SAndroid Build Coastguard Worker        components_to_visit = sorted(
187*9880d681SAndroid Build Coastguard Worker            set(self.component_infos),
188*9880d681SAndroid Build Coastguard Worker            key = lambda c: c.name)
189*9880d681SAndroid Build Coastguard Worker        while components_to_visit:
190*9880d681SAndroid Build Coastguard Worker            visit_component_info(components_to_visit[0], [], set())
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker        # Canonicalize children lists.
193*9880d681SAndroid Build Coastguard Worker        for c in self.ordered_component_infos:
194*9880d681SAndroid Build Coastguard Worker            c.children.sort(key = lambda c: c.name)
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker    def print_tree(self):
197*9880d681SAndroid Build Coastguard Worker        def visit(node, depth = 0):
198*9880d681SAndroid Build Coastguard Worker            print('%s%-40s (%s)' % ('  '*depth, node.name, node.type_name))
199*9880d681SAndroid Build Coastguard Worker            for c in node.children:
200*9880d681SAndroid Build Coastguard Worker                visit(c, depth + 1)
201*9880d681SAndroid Build Coastguard Worker        visit(self.component_info_map['$ROOT'])
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker    def write_components(self, output_path):
204*9880d681SAndroid Build Coastguard Worker        # Organize all the components by the directory their LLVMBuild file
205*9880d681SAndroid Build Coastguard Worker        # should go in.
206*9880d681SAndroid Build Coastguard Worker        info_basedir = {}
207*9880d681SAndroid Build Coastguard Worker        for ci in self.component_infos:
208*9880d681SAndroid Build Coastguard Worker            # Ignore the $ROOT component.
209*9880d681SAndroid Build Coastguard Worker            if ci.parent is None:
210*9880d681SAndroid Build Coastguard Worker                continue
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker            info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker        # Compute the list of subdirectories to scan.
215*9880d681SAndroid Build Coastguard Worker        subpath_subdirs = {}
216*9880d681SAndroid Build Coastguard Worker        for ci in self.component_infos:
217*9880d681SAndroid Build Coastguard Worker            # Ignore root components.
218*9880d681SAndroid Build Coastguard Worker            if ci.subpath == '/':
219*9880d681SAndroid Build Coastguard Worker                continue
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker            # Otherwise, append this subpath to the parent list.
222*9880d681SAndroid Build Coastguard Worker            parent_path = os.path.dirname(ci.subpath)
223*9880d681SAndroid Build Coastguard Worker            subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
224*9880d681SAndroid Build Coastguard Worker                parent_path, set())
225*9880d681SAndroid Build Coastguard Worker            parent_list.add(os.path.basename(ci.subpath))
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker        # Generate the build files.
228*9880d681SAndroid Build Coastguard Worker        for subpath, infos in info_basedir.items():
229*9880d681SAndroid Build Coastguard Worker            # Order the components by name to have a canonical ordering.
230*9880d681SAndroid Build Coastguard Worker            infos.sort(key = lambda ci: ci.name)
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker            # Format the components into llvmbuild fragments.
233*9880d681SAndroid Build Coastguard Worker            fragments = []
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker            # Add the common fragments.
236*9880d681SAndroid Build Coastguard Worker            subdirectories = subpath_subdirs.get(subpath)
237*9880d681SAndroid Build Coastguard Worker            if subdirectories:
238*9880d681SAndroid Build Coastguard Worker                fragment = """\
239*9880d681SAndroid Build Coastguard Workersubdirectories = %s
240*9880d681SAndroid Build Coastguard Worker""" % (" ".join(sorted(subdirectories)),)
241*9880d681SAndroid Build Coastguard Worker                fragments.append(("common", fragment))
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker            # Add the component fragments.
244*9880d681SAndroid Build Coastguard Worker            num_common_fragments = len(fragments)
245*9880d681SAndroid Build Coastguard Worker            for ci in infos:
246*9880d681SAndroid Build Coastguard Worker                fragment = ci.get_llvmbuild_fragment()
247*9880d681SAndroid Build Coastguard Worker                if fragment is None:
248*9880d681SAndroid Build Coastguard Worker                    continue
249*9880d681SAndroid Build Coastguard Worker
250*9880d681SAndroid Build Coastguard Worker                name = "component_%d" % (len(fragments) - num_common_fragments)
251*9880d681SAndroid Build Coastguard Worker                fragments.append((name, fragment))
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker            if not fragments:
254*9880d681SAndroid Build Coastguard Worker                continue
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker            assert subpath.startswith('/')
257*9880d681SAndroid Build Coastguard Worker            directory_path = os.path.join(output_path, subpath[1:])
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard Worker            # Create the directory if it does not already exist.
260*9880d681SAndroid Build Coastguard Worker            if not os.path.exists(directory_path):
261*9880d681SAndroid Build Coastguard Worker                os.makedirs(directory_path)
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker            # In an effort to preserve comments (which aren't parsed), read in
264*9880d681SAndroid Build Coastguard Worker            # the original file and extract the comments. We only know how to
265*9880d681SAndroid Build Coastguard Worker            # associate comments that prefix a section name.
266*9880d681SAndroid Build Coastguard Worker            f = open(infos[0]._source_path)
267*9880d681SAndroid Build Coastguard Worker            comments_map = {}
268*9880d681SAndroid Build Coastguard Worker            comment_block = ""
269*9880d681SAndroid Build Coastguard Worker            for ln in f:
270*9880d681SAndroid Build Coastguard Worker                if ln.startswith(';'):
271*9880d681SAndroid Build Coastguard Worker                    comment_block += ln
272*9880d681SAndroid Build Coastguard Worker                elif ln.startswith('[') and ln.endswith(']\n'):
273*9880d681SAndroid Build Coastguard Worker                    comments_map[ln[1:-2]] = comment_block
274*9880d681SAndroid Build Coastguard Worker                else:
275*9880d681SAndroid Build Coastguard Worker                    comment_block = ""
276*9880d681SAndroid Build Coastguard Worker            f.close()
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard Worker            # Create the LLVMBuild fil[e.
279*9880d681SAndroid Build Coastguard Worker            file_path = os.path.join(directory_path, 'LLVMBuild.txt')
280*9880d681SAndroid Build Coastguard Worker            f = open(file_path, "w")
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker            # Write the header.
283*9880d681SAndroid Build Coastguard Worker            header_fmt = ';===- %s %s-*- Conf -*--===;'
284*9880d681SAndroid Build Coastguard Worker            header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
285*9880d681SAndroid Build Coastguard Worker            header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
286*9880d681SAndroid Build Coastguard Worker            header_string = header_fmt % (header_name, header_pad)
287*9880d681SAndroid Build Coastguard Worker            f.write("""\
288*9880d681SAndroid Build Coastguard Worker%s
289*9880d681SAndroid Build Coastguard Worker;
290*9880d681SAndroid Build Coastguard Worker;                     The LLVM Compiler Infrastructure
291*9880d681SAndroid Build Coastguard Worker;
292*9880d681SAndroid Build Coastguard Worker; This file is distributed under the University of Illinois Open Source
293*9880d681SAndroid Build Coastguard Worker; License. See LICENSE.TXT for details.
294*9880d681SAndroid Build Coastguard Worker;
295*9880d681SAndroid Build Coastguard Worker;===------------------------------------------------------------------------===;
296*9880d681SAndroid Build Coastguard Worker;
297*9880d681SAndroid Build Coastguard Worker; This is an LLVMBuild description file for the components in this subdirectory.
298*9880d681SAndroid Build Coastguard Worker;
299*9880d681SAndroid Build Coastguard Worker; For more information on the LLVMBuild system, please see:
300*9880d681SAndroid Build Coastguard Worker;
301*9880d681SAndroid Build Coastguard Worker;   http://llvm.org/docs/LLVMBuild.html
302*9880d681SAndroid Build Coastguard Worker;
303*9880d681SAndroid Build Coastguard Worker;===------------------------------------------------------------------------===;
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker""" % header_string)
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker            # Write out each fragment.each component fragment.
308*9880d681SAndroid Build Coastguard Worker            for name,fragment in fragments:
309*9880d681SAndroid Build Coastguard Worker                comment = comments_map.get(name)
310*9880d681SAndroid Build Coastguard Worker                if comment is not None:
311*9880d681SAndroid Build Coastguard Worker                    f.write(comment)
312*9880d681SAndroid Build Coastguard Worker                f.write("[%s]\n" % name)
313*9880d681SAndroid Build Coastguard Worker                f.write(fragment)
314*9880d681SAndroid Build Coastguard Worker                if fragment is not fragments[-1][1]:
315*9880d681SAndroid Build Coastguard Worker                    f.write('\n')
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard Worker            f.close()
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker    def write_library_table(self, output_path, enabled_optional_components):
320*9880d681SAndroid Build Coastguard Worker        # Write out the mapping from component names to required libraries.
321*9880d681SAndroid Build Coastguard Worker        #
322*9880d681SAndroid Build Coastguard Worker        # We do this in topological order so that we know we can append the
323*9880d681SAndroid Build Coastguard Worker        # dependencies for added library groups.
324*9880d681SAndroid Build Coastguard Worker        entries = {}
325*9880d681SAndroid Build Coastguard Worker        for c in self.ordered_component_infos:
326*9880d681SAndroid Build Coastguard Worker            # Skip optional components which are not enabled.
327*9880d681SAndroid Build Coastguard Worker            if c.type_name == 'OptionalLibrary' \
328*9880d681SAndroid Build Coastguard Worker                and c.name not in enabled_optional_components:
329*9880d681SAndroid Build Coastguard Worker                continue
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker            # Skip target groups which are not enabled.
332*9880d681SAndroid Build Coastguard Worker            tg = c.get_parent_target_group()
333*9880d681SAndroid Build Coastguard Worker            if tg and not tg.enabled:
334*9880d681SAndroid Build Coastguard Worker                continue
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker            # Only certain components are in the table.
337*9880d681SAndroid Build Coastguard Worker            if c.type_name not in ('Library', 'OptionalLibrary', \
338*9880d681SAndroid Build Coastguard Worker                                   'LibraryGroup', 'TargetGroup'):
339*9880d681SAndroid Build Coastguard Worker                continue
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker            # Compute the llvm-config "component name". For historical reasons,
342*9880d681SAndroid Build Coastguard Worker            # this is lowercased based on the library name.
343*9880d681SAndroid Build Coastguard Worker            llvmconfig_component_name = c.get_llvmconfig_component_name()
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker            # Get the library name, or None for LibraryGroups.
346*9880d681SAndroid Build Coastguard Worker            if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
347*9880d681SAndroid Build Coastguard Worker                library_name = c.get_prefixed_library_name()
348*9880d681SAndroid Build Coastguard Worker                is_installed = c.installed
349*9880d681SAndroid Build Coastguard Worker            else:
350*9880d681SAndroid Build Coastguard Worker                library_name = None
351*9880d681SAndroid Build Coastguard Worker                is_installed = True
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker            # Get the component names of all the required libraries.
354*9880d681SAndroid Build Coastguard Worker            required_llvmconfig_component_names = [
355*9880d681SAndroid Build Coastguard Worker                self.component_info_map[dep].get_llvmconfig_component_name()
356*9880d681SAndroid Build Coastguard Worker                for dep in c.required_libraries]
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard Worker            # Insert the entries for library groups we should add to.
359*9880d681SAndroid Build Coastguard Worker            for dep in c.add_to_library_groups:
360*9880d681SAndroid Build Coastguard Worker                entries[dep][2].append(llvmconfig_component_name)
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker            # Add the entry.
363*9880d681SAndroid Build Coastguard Worker            entries[c.name] = (llvmconfig_component_name, library_name,
364*9880d681SAndroid Build Coastguard Worker                               required_llvmconfig_component_names,
365*9880d681SAndroid Build Coastguard Worker                               is_installed)
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker        # Convert to a list of entries and sort by name.
368*9880d681SAndroid Build Coastguard Worker        entries = list(entries.values())
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker        # Create an 'all' pseudo component. We keep the dependency list small by
371*9880d681SAndroid Build Coastguard Worker        # only listing entries that have no other dependents.
372*9880d681SAndroid Build Coastguard Worker        root_entries = set(e[0] for e in entries)
373*9880d681SAndroid Build Coastguard Worker        for _,_,deps,_ in entries:
374*9880d681SAndroid Build Coastguard Worker            root_entries -= set(deps)
375*9880d681SAndroid Build Coastguard Worker        entries.append(('all', None, root_entries, True))
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Worker        entries.sort()
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard Worker        # Compute the maximum number of required libraries, plus one so there is
380*9880d681SAndroid Build Coastguard Worker        # always a sentinel.
381*9880d681SAndroid Build Coastguard Worker        max_required_libraries = max(len(deps)
382*9880d681SAndroid Build Coastguard Worker                                     for _,_,deps,_ in entries) + 1
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker        # Write out the library table.
385*9880d681SAndroid Build Coastguard Worker        make_install_dir(os.path.dirname(output_path))
386*9880d681SAndroid Build Coastguard Worker        f = open(output_path+'.new', 'w')
387*9880d681SAndroid Build Coastguard Worker        f.write("""\
388*9880d681SAndroid Build Coastguard Worker//===- llvm-build generated file --------------------------------*- C++ -*-===//
389*9880d681SAndroid Build Coastguard Worker//
390*9880d681SAndroid Build Coastguard Worker// Component Library Depenedency Table
391*9880d681SAndroid Build Coastguard Worker//
392*9880d681SAndroid Build Coastguard Worker// Automatically generated file, do not edit!
393*9880d681SAndroid Build Coastguard Worker//
394*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard Worker""")
397*9880d681SAndroid Build Coastguard Worker        f.write('struct AvailableComponent {\n')
398*9880d681SAndroid Build Coastguard Worker        f.write('  /// The name of the component.\n')
399*9880d681SAndroid Build Coastguard Worker        f.write('  const char *Name;\n')
400*9880d681SAndroid Build Coastguard Worker        f.write('\n')
401*9880d681SAndroid Build Coastguard Worker        f.write('  /// The name of the library for this component (or NULL).\n')
402*9880d681SAndroid Build Coastguard Worker        f.write('  const char *Library;\n')
403*9880d681SAndroid Build Coastguard Worker        f.write('\n')
404*9880d681SAndroid Build Coastguard Worker        f.write('  /// Whether the component is installed.\n')
405*9880d681SAndroid Build Coastguard Worker        f.write('  bool IsInstalled;\n')
406*9880d681SAndroid Build Coastguard Worker        f.write('\n')
407*9880d681SAndroid Build Coastguard Worker        f.write('\
408*9880d681SAndroid Build Coastguard Worker  /// The list of libraries required when linking this component.\n')
409*9880d681SAndroid Build Coastguard Worker        f.write('  const char *RequiredLibraries[%d];\n' % (
410*9880d681SAndroid Build Coastguard Worker            max_required_libraries))
411*9880d681SAndroid Build Coastguard Worker        f.write('} AvailableComponents[%d] = {\n' % len(entries))
412*9880d681SAndroid Build Coastguard Worker        for name,library_name,required_names,is_installed in entries:
413*9880d681SAndroid Build Coastguard Worker            if library_name is None:
414*9880d681SAndroid Build Coastguard Worker                library_name_as_cstr = 'nullptr'
415*9880d681SAndroid Build Coastguard Worker            else:
416*9880d681SAndroid Build Coastguard Worker                library_name_as_cstr = '"%s"' % library_name
417*9880d681SAndroid Build Coastguard Worker            if is_installed:
418*9880d681SAndroid Build Coastguard Worker                is_installed_as_cstr = 'true'
419*9880d681SAndroid Build Coastguard Worker            else:
420*9880d681SAndroid Build Coastguard Worker                is_installed_as_cstr = 'false'
421*9880d681SAndroid Build Coastguard Worker            f.write('  { "%s", %s, %s, { %s } },\n' % (
422*9880d681SAndroid Build Coastguard Worker                name, library_name_as_cstr, is_installed_as_cstr,
423*9880d681SAndroid Build Coastguard Worker                ', '.join('"%s"' % dep
424*9880d681SAndroid Build Coastguard Worker                          for dep in required_names)))
425*9880d681SAndroid Build Coastguard Worker        f.write('};\n')
426*9880d681SAndroid Build Coastguard Worker        f.close()
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker        if not os.path.isfile(output_path):
429*9880d681SAndroid Build Coastguard Worker            os.rename(output_path+'.new', output_path)
430*9880d681SAndroid Build Coastguard Worker        elif filecmp.cmp(output_path, output_path+'.new'):
431*9880d681SAndroid Build Coastguard Worker            os.remove(output_path+'.new')
432*9880d681SAndroid Build Coastguard Worker        else:
433*9880d681SAndroid Build Coastguard Worker            os.remove(output_path)
434*9880d681SAndroid Build Coastguard Worker            os.rename(output_path+'.new', output_path)
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker    def get_required_libraries_for_component(self, ci, traverse_groups = False):
437*9880d681SAndroid Build Coastguard Worker        """
438*9880d681SAndroid Build Coastguard Worker        get_required_libraries_for_component(component_info) -> iter
439*9880d681SAndroid Build Coastguard Worker
440*9880d681SAndroid Build Coastguard Worker        Given a Library component info descriptor, return an iterator over all
441*9880d681SAndroid Build Coastguard Worker        of the directly required libraries for linking with this component. If
442*9880d681SAndroid Build Coastguard Worker        traverse_groups is True, then library and target groups will be
443*9880d681SAndroid Build Coastguard Worker        traversed to include their required libraries.
444*9880d681SAndroid Build Coastguard Worker        """
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard Worker        assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup')
447*9880d681SAndroid Build Coastguard Worker
448*9880d681SAndroid Build Coastguard Worker        for name in ci.required_libraries:
449*9880d681SAndroid Build Coastguard Worker            # Get the dependency info.
450*9880d681SAndroid Build Coastguard Worker            dep = self.component_info_map[name]
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard Worker            # If it is a library, yield it.
453*9880d681SAndroid Build Coastguard Worker            if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary':
454*9880d681SAndroid Build Coastguard Worker                yield dep
455*9880d681SAndroid Build Coastguard Worker                continue
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker            # Otherwise if it is a group, yield or traverse depending on what
458*9880d681SAndroid Build Coastguard Worker            # was requested.
459*9880d681SAndroid Build Coastguard Worker            if dep.type_name in ('LibraryGroup', 'TargetGroup'):
460*9880d681SAndroid Build Coastguard Worker                if not traverse_groups:
461*9880d681SAndroid Build Coastguard Worker                    yield dep
462*9880d681SAndroid Build Coastguard Worker                    continue
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker                for res in self.get_required_libraries_for_component(dep, True):
465*9880d681SAndroid Build Coastguard Worker                    yield res
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker    def get_fragment_dependencies(self):
468*9880d681SAndroid Build Coastguard Worker        """
469*9880d681SAndroid Build Coastguard Worker        get_fragment_dependencies() -> iter
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker        Compute the list of files (as absolute paths) on which the output
472*9880d681SAndroid Build Coastguard Worker        fragments depend (i.e., files for which a modification should trigger a
473*9880d681SAndroid Build Coastguard Worker        rebuild of the fragment).
474*9880d681SAndroid Build Coastguard Worker        """
475*9880d681SAndroid Build Coastguard Worker
476*9880d681SAndroid Build Coastguard Worker        # Construct a list of all the dependencies of the Makefile fragment
477*9880d681SAndroid Build Coastguard Worker        # itself. These include all the LLVMBuild files themselves, as well as
478*9880d681SAndroid Build Coastguard Worker        # all of our own sources.
479*9880d681SAndroid Build Coastguard Worker        #
480*9880d681SAndroid Build Coastguard Worker        # Many components may come from the same file, so we make sure to unique
481*9880d681SAndroid Build Coastguard Worker        # these.
482*9880d681SAndroid Build Coastguard Worker        build_paths = set()
483*9880d681SAndroid Build Coastguard Worker        for ci in self.component_infos:
484*9880d681SAndroid Build Coastguard Worker            p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
485*9880d681SAndroid Build Coastguard Worker            if p not in build_paths:
486*9880d681SAndroid Build Coastguard Worker                yield p
487*9880d681SAndroid Build Coastguard Worker                build_paths.add(p)
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard Worker        # Gather the list of necessary sources by just finding all loaded
490*9880d681SAndroid Build Coastguard Worker        # modules that are inside the LLVM source tree.
491*9880d681SAndroid Build Coastguard Worker        for module in sys.modules.values():
492*9880d681SAndroid Build Coastguard Worker            # Find the module path.
493*9880d681SAndroid Build Coastguard Worker            if not hasattr(module, '__file__'):
494*9880d681SAndroid Build Coastguard Worker                continue
495*9880d681SAndroid Build Coastguard Worker            path = getattr(module, '__file__')
496*9880d681SAndroid Build Coastguard Worker            if not path:
497*9880d681SAndroid Build Coastguard Worker                continue
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker            # Strip off any compiled suffix.
500*9880d681SAndroid Build Coastguard Worker            if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
501*9880d681SAndroid Build Coastguard Worker                path = path[:-1]
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker            # If the path exists and is in the source tree, consider it a
504*9880d681SAndroid Build Coastguard Worker            # dependency.
505*9880d681SAndroid Build Coastguard Worker            if (path.startswith(self.source_root) and os.path.exists(path)):
506*9880d681SAndroid Build Coastguard Worker                yield path
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker    def foreach_cmake_library(self, f,
509*9880d681SAndroid Build Coastguard Worker                              enabled_optional_components,
510*9880d681SAndroid Build Coastguard Worker                              skip_disabled,
511*9880d681SAndroid Build Coastguard Worker                              skip_not_installed):
512*9880d681SAndroid Build Coastguard Worker        for ci in self.ordered_component_infos:
513*9880d681SAndroid Build Coastguard Worker            # Skip optional components which are not enabled.
514*9880d681SAndroid Build Coastguard Worker            if ci.type_name == 'OptionalLibrary' \
515*9880d681SAndroid Build Coastguard Worker                and ci.name not in enabled_optional_components:
516*9880d681SAndroid Build Coastguard Worker                continue
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker            # We only write the information for libraries currently.
519*9880d681SAndroid Build Coastguard Worker            if ci.type_name not in ('Library', 'OptionalLibrary'):
520*9880d681SAndroid Build Coastguard Worker                continue
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker            # Skip disabled targets.
523*9880d681SAndroid Build Coastguard Worker            if skip_disabled:
524*9880d681SAndroid Build Coastguard Worker                tg = ci.get_parent_target_group()
525*9880d681SAndroid Build Coastguard Worker                if tg and not tg.enabled:
526*9880d681SAndroid Build Coastguard Worker                    continue
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard Worker            # Skip targets that will not be installed
529*9880d681SAndroid Build Coastguard Worker            if skip_not_installed and not ci.installed:
530*9880d681SAndroid Build Coastguard Worker                continue
531*9880d681SAndroid Build Coastguard Worker
532*9880d681SAndroid Build Coastguard Worker            f(ci)
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker
535*9880d681SAndroid Build Coastguard Worker    def write_cmake_fragment(self, output_path, enabled_optional_components):
536*9880d681SAndroid Build Coastguard Worker        """
537*9880d681SAndroid Build Coastguard Worker        write_cmake_fragment(output_path) -> None
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker        Generate a CMake fragment which includes all of the collated LLVMBuild
540*9880d681SAndroid Build Coastguard Worker        information in a format that is easily digestible by a CMake. The exact
541*9880d681SAndroid Build Coastguard Worker        contents of this are closely tied to how the CMake configuration
542*9880d681SAndroid Build Coastguard Worker        integrates LLVMBuild, see CMakeLists.txt in the top-level.
543*9880d681SAndroid Build Coastguard Worker        """
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker        dependencies = list(self.get_fragment_dependencies())
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker        # Write out the CMake fragment.
548*9880d681SAndroid Build Coastguard Worker        make_install_dir(os.path.dirname(output_path))
549*9880d681SAndroid Build Coastguard Worker        f = open(output_path, 'w')
550*9880d681SAndroid Build Coastguard Worker
551*9880d681SAndroid Build Coastguard Worker        # Write the header.
552*9880d681SAndroid Build Coastguard Worker        header_fmt = '\
553*9880d681SAndroid Build Coastguard Worker#===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
554*9880d681SAndroid Build Coastguard Worker        header_name = os.path.basename(output_path)
555*9880d681SAndroid Build Coastguard Worker        header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
556*9880d681SAndroid Build Coastguard Worker        header_string = header_fmt % (header_name, header_pad)
557*9880d681SAndroid Build Coastguard Worker        f.write("""\
558*9880d681SAndroid Build Coastguard Worker%s
559*9880d681SAndroid Build Coastguard Worker#
560*9880d681SAndroid Build Coastguard Worker#                     The LLVM Compiler Infrastructure
561*9880d681SAndroid Build Coastguard Worker#
562*9880d681SAndroid Build Coastguard Worker# This file is distributed under the University of Illinois Open Source
563*9880d681SAndroid Build Coastguard Worker# License. See LICENSE.TXT for details.
564*9880d681SAndroid Build Coastguard Worker#
565*9880d681SAndroid Build Coastguard Worker#===------------------------------------------------------------------------===#
566*9880d681SAndroid Build Coastguard Worker#
567*9880d681SAndroid Build Coastguard Worker# This file contains the LLVMBuild project information in a format easily
568*9880d681SAndroid Build Coastguard Worker# consumed by the CMake based build system.
569*9880d681SAndroid Build Coastguard Worker#
570*9880d681SAndroid Build Coastguard Worker# This file is autogenerated by llvm-build, do not edit!
571*9880d681SAndroid Build Coastguard Worker#
572*9880d681SAndroid Build Coastguard Worker#===------------------------------------------------------------------------===#
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker""" % header_string)
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker        # Write the dependency information in the best way we can.
577*9880d681SAndroid Build Coastguard Worker        f.write("""
578*9880d681SAndroid Build Coastguard Worker# LLVMBuild CMake fragment dependencies.
579*9880d681SAndroid Build Coastguard Worker#
580*9880d681SAndroid Build Coastguard Worker# CMake has no builtin way to declare that the configuration depends on
581*9880d681SAndroid Build Coastguard Worker# a particular file. However, a side effect of configure_file is to add
582*9880d681SAndroid Build Coastguard Worker# said input file to CMake's internal dependency list. So, we use that
583*9880d681SAndroid Build Coastguard Worker# and a dummy output file to communicate the dependency information to
584*9880d681SAndroid Build Coastguard Worker# CMake.
585*9880d681SAndroid Build Coastguard Worker#
586*9880d681SAndroid Build Coastguard Worker# FIXME: File a CMake RFE to get a properly supported version of this
587*9880d681SAndroid Build Coastguard Worker# feature.
588*9880d681SAndroid Build Coastguard Worker""")
589*9880d681SAndroid Build Coastguard Worker        for dep in dependencies:
590*9880d681SAndroid Build Coastguard Worker            f.write("""\
591*9880d681SAndroid Build Coastguard Workerconfigure_file(\"%s\"
592*9880d681SAndroid Build Coastguard Worker               ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % (
593*9880d681SAndroid Build Coastguard Worker                cmake_quote_path(dep),))
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard Worker        # Write the properties we use to encode the required library dependency
596*9880d681SAndroid Build Coastguard Worker        # information in a form CMake can easily use directly.
597*9880d681SAndroid Build Coastguard Worker        f.write("""
598*9880d681SAndroid Build Coastguard Worker# Explicit library dependency information.
599*9880d681SAndroid Build Coastguard Worker#
600*9880d681SAndroid Build Coastguard Worker# The following property assignments effectively create a map from component
601*9880d681SAndroid Build Coastguard Worker# names to required libraries, in a way that is easily accessed from CMake.
602*9880d681SAndroid Build Coastguard Worker""")
603*9880d681SAndroid Build Coastguard Worker        self.foreach_cmake_library(
604*9880d681SAndroid Build Coastguard Worker            lambda ci:
605*9880d681SAndroid Build Coastguard Worker              f.write("""\
606*9880d681SAndroid Build Coastguard Workerset_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % (
607*9880d681SAndroid Build Coastguard Worker                ci.get_prefixed_library_name(), " ".join(sorted(
608*9880d681SAndroid Build Coastguard Worker                     dep.get_prefixed_library_name()
609*9880d681SAndroid Build Coastguard Worker                     for dep in self.get_required_libraries_for_component(ci)))))
610*9880d681SAndroid Build Coastguard Worker            ,
611*9880d681SAndroid Build Coastguard Worker            enabled_optional_components,
612*9880d681SAndroid Build Coastguard Worker            skip_disabled = False,
613*9880d681SAndroid Build Coastguard Worker            skip_not_installed = False # Dependency info must be emitted for internals libs too
614*9880d681SAndroid Build Coastguard Worker            )
615*9880d681SAndroid Build Coastguard Worker
616*9880d681SAndroid Build Coastguard Worker        f.close()
617*9880d681SAndroid Build Coastguard Worker
618*9880d681SAndroid Build Coastguard Worker    def write_cmake_exports_fragment(self, output_path, enabled_optional_components):
619*9880d681SAndroid Build Coastguard Worker        """
620*9880d681SAndroid Build Coastguard Worker        write_cmake_exports_fragment(output_path) -> None
621*9880d681SAndroid Build Coastguard Worker
622*9880d681SAndroid Build Coastguard Worker        Generate a CMake fragment which includes LLVMBuild library
623*9880d681SAndroid Build Coastguard Worker        dependencies expressed similarly to how CMake would write
624*9880d681SAndroid Build Coastguard Worker        them via install(EXPORT).
625*9880d681SAndroid Build Coastguard Worker        """
626*9880d681SAndroid Build Coastguard Worker
627*9880d681SAndroid Build Coastguard Worker        dependencies = list(self.get_fragment_dependencies())
628*9880d681SAndroid Build Coastguard Worker
629*9880d681SAndroid Build Coastguard Worker        # Write out the CMake exports fragment.
630*9880d681SAndroid Build Coastguard Worker        make_install_dir(os.path.dirname(output_path))
631*9880d681SAndroid Build Coastguard Worker        f = open(output_path, 'w')
632*9880d681SAndroid Build Coastguard Worker
633*9880d681SAndroid Build Coastguard Worker        f.write("""\
634*9880d681SAndroid Build Coastguard Worker# Explicit library dependency information.
635*9880d681SAndroid Build Coastguard Worker#
636*9880d681SAndroid Build Coastguard Worker# The following property assignments tell CMake about link
637*9880d681SAndroid Build Coastguard Worker# dependencies of libraries imported from LLVM.
638*9880d681SAndroid Build Coastguard Worker""")
639*9880d681SAndroid Build Coastguard Worker        self.foreach_cmake_library(
640*9880d681SAndroid Build Coastguard Worker            lambda ci:
641*9880d681SAndroid Build Coastguard Worker              f.write("""\
642*9880d681SAndroid Build Coastguard Workerset_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
643*9880d681SAndroid Build Coastguard Worker                ci.get_prefixed_library_name(), " ".join(sorted(
644*9880d681SAndroid Build Coastguard Worker                     dep.get_prefixed_library_name()
645*9880d681SAndroid Build Coastguard Worker                     for dep in self.get_required_libraries_for_component(ci)))))
646*9880d681SAndroid Build Coastguard Worker            ,
647*9880d681SAndroid Build Coastguard Worker            enabled_optional_components,
648*9880d681SAndroid Build Coastguard Worker            skip_disabled = True,
649*9880d681SAndroid Build Coastguard Worker            skip_not_installed = True # Do not export internal libraries like gtest
650*9880d681SAndroid Build Coastguard Worker            )
651*9880d681SAndroid Build Coastguard Worker
652*9880d681SAndroid Build Coastguard Worker        f.close()
653*9880d681SAndroid Build Coastguard Worker
654*9880d681SAndroid Build Coastguard Worker    def write_make_fragment(self, output_path, enabled_optional_components):
655*9880d681SAndroid Build Coastguard Worker        """
656*9880d681SAndroid Build Coastguard Worker        write_make_fragment(output_path) -> None
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker        Generate a Makefile fragment which includes all of the collated
659*9880d681SAndroid Build Coastguard Worker        LLVMBuild information in a format that is easily digestible by a
660*9880d681SAndroid Build Coastguard Worker        Makefile. The exact contents of this are closely tied to how the LLVM
661*9880d681SAndroid Build Coastguard Worker        Makefiles integrate LLVMBuild, see Makefile.rules in the top-level.
662*9880d681SAndroid Build Coastguard Worker        """
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker        dependencies = list(self.get_fragment_dependencies())
665*9880d681SAndroid Build Coastguard Worker
666*9880d681SAndroid Build Coastguard Worker        # Write out the Makefile fragment.
667*9880d681SAndroid Build Coastguard Worker        make_install_dir(os.path.dirname(output_path))
668*9880d681SAndroid Build Coastguard Worker        f = open(output_path, 'w')
669*9880d681SAndroid Build Coastguard Worker
670*9880d681SAndroid Build Coastguard Worker        # Write the header.
671*9880d681SAndroid Build Coastguard Worker        header_fmt = '\
672*9880d681SAndroid Build Coastguard Worker#===-- %s - LLVMBuild Configuration for LLVM %s-*- Makefile -*--===#'
673*9880d681SAndroid Build Coastguard Worker        header_name = os.path.basename(output_path)
674*9880d681SAndroid Build Coastguard Worker        header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
675*9880d681SAndroid Build Coastguard Worker        header_string = header_fmt % (header_name, header_pad)
676*9880d681SAndroid Build Coastguard Worker        f.write("""\
677*9880d681SAndroid Build Coastguard Worker%s
678*9880d681SAndroid Build Coastguard Worker#
679*9880d681SAndroid Build Coastguard Worker#                     The LLVM Compiler Infrastructure
680*9880d681SAndroid Build Coastguard Worker#
681*9880d681SAndroid Build Coastguard Worker# This file is distributed under the University of Illinois Open Source
682*9880d681SAndroid Build Coastguard Worker# License. See LICENSE.TXT for details.
683*9880d681SAndroid Build Coastguard Worker#
684*9880d681SAndroid Build Coastguard Worker#===------------------------------------------------------------------------===#
685*9880d681SAndroid Build Coastguard Worker#
686*9880d681SAndroid Build Coastguard Worker# This file contains the LLVMBuild project information in a format easily
687*9880d681SAndroid Build Coastguard Worker# consumed by the Makefile based build system.
688*9880d681SAndroid Build Coastguard Worker#
689*9880d681SAndroid Build Coastguard Worker# This file is autogenerated by llvm-build, do not edit!
690*9880d681SAndroid Build Coastguard Worker#
691*9880d681SAndroid Build Coastguard Worker#===------------------------------------------------------------------------===#
692*9880d681SAndroid Build Coastguard Worker
693*9880d681SAndroid Build Coastguard Worker""" % header_string)
694*9880d681SAndroid Build Coastguard Worker
695*9880d681SAndroid Build Coastguard Worker        # Write the dependencies for the fragment.
696*9880d681SAndroid Build Coastguard Worker        #
697*9880d681SAndroid Build Coastguard Worker        # FIXME: Technically, we need to properly quote for Make here.
698*9880d681SAndroid Build Coastguard Worker        f.write("""\
699*9880d681SAndroid Build Coastguard Worker# Clients must explicitly enable LLVMBUILD_INCLUDE_DEPENDENCIES to get
700*9880d681SAndroid Build Coastguard Worker# these dependencies. This is a compromise to help improve the
701*9880d681SAndroid Build Coastguard Worker# performance of recursive Make systems.
702*9880d681SAndroid Build Coastguard Worker""")
703*9880d681SAndroid Build Coastguard Worker        f.write('ifeq ($(LLVMBUILD_INCLUDE_DEPENDENCIES),1)\n')
704*9880d681SAndroid Build Coastguard Worker        f.write("# The dependencies for this Makefile fragment itself.\n")
705*9880d681SAndroid Build Coastguard Worker        f.write("%s: \\\n" % (mk_quote_string_for_target(output_path),))
706*9880d681SAndroid Build Coastguard Worker        for dep in dependencies:
707*9880d681SAndroid Build Coastguard Worker            f.write("\t%s \\\n" % (dep,))
708*9880d681SAndroid Build Coastguard Worker        f.write('\n')
709*9880d681SAndroid Build Coastguard Worker
710*9880d681SAndroid Build Coastguard Worker        # Generate dummy rules for each of the dependencies, so that things
711*9880d681SAndroid Build Coastguard Worker        # continue to work correctly if any of those files are moved or removed.
712*9880d681SAndroid Build Coastguard Worker        f.write("""\
713*9880d681SAndroid Build Coastguard Worker# The dummy targets to allow proper regeneration even when files are moved or
714*9880d681SAndroid Build Coastguard Worker# removed.
715*9880d681SAndroid Build Coastguard Worker""")
716*9880d681SAndroid Build Coastguard Worker        for dep in dependencies:
717*9880d681SAndroid Build Coastguard Worker            f.write("%s:\n" % (mk_quote_string_for_target(dep),))
718*9880d681SAndroid Build Coastguard Worker        f.write('endif\n')
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard Worker        f.write("""
721*9880d681SAndroid Build Coastguard Worker# List of libraries to be exported for use by applications.
722*9880d681SAndroid Build Coastguard Worker# See 'cmake/modules/Makefile'.
723*9880d681SAndroid Build Coastguard WorkerLLVM_LIBS_TO_EXPORT :=""")
724*9880d681SAndroid Build Coastguard Worker        self.foreach_cmake_library(
725*9880d681SAndroid Build Coastguard Worker            lambda ci:
726*9880d681SAndroid Build Coastguard Worker                f.write(' \\\n  %s' % ci.get_prefixed_library_name())
727*9880d681SAndroid Build Coastguard Worker            ,
728*9880d681SAndroid Build Coastguard Worker            enabled_optional_components,
729*9880d681SAndroid Build Coastguard Worker            skip_disabled = True,
730*9880d681SAndroid Build Coastguard Worker            skip_not_installed = True # Do not export internal libraries like gtest
731*9880d681SAndroid Build Coastguard Worker            )
732*9880d681SAndroid Build Coastguard Worker        f.write('\n')
733*9880d681SAndroid Build Coastguard Worker        f.close()
734*9880d681SAndroid Build Coastguard Worker
735*9880d681SAndroid Build Coastguard Workerdef add_magic_target_components(parser, project, opts):
736*9880d681SAndroid Build Coastguard Worker    """add_magic_target_components(project, opts) -> None
737*9880d681SAndroid Build Coastguard Worker
738*9880d681SAndroid Build Coastguard Worker    Add the "magic" target based components to the project, which can only be
739*9880d681SAndroid Build Coastguard Worker    determined based on the target configuration options.
740*9880d681SAndroid Build Coastguard Worker
741*9880d681SAndroid Build Coastguard Worker    This currently is responsible for populating the required_libraries list of
742*9880d681SAndroid Build Coastguard Worker    the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
743*9880d681SAndroid Build Coastguard Worker    """
744*9880d681SAndroid Build Coastguard Worker
745*9880d681SAndroid Build Coastguard Worker    # Determine the available targets.
746*9880d681SAndroid Build Coastguard Worker    available_targets = dict((ci.name,ci)
747*9880d681SAndroid Build Coastguard Worker                             for ci in project.component_infos
748*9880d681SAndroid Build Coastguard Worker                             if ci.type_name == 'TargetGroup')
749*9880d681SAndroid Build Coastguard Worker
750*9880d681SAndroid Build Coastguard Worker    # Find the configured native target.
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard Worker    # We handle a few special cases of target names here for historical
753*9880d681SAndroid Build Coastguard Worker    # reasons, as these are the names configure currently comes up with.
754*9880d681SAndroid Build Coastguard Worker    native_target_name = { 'x86' : 'X86',
755*9880d681SAndroid Build Coastguard Worker                           'x86_64' : 'X86',
756*9880d681SAndroid Build Coastguard Worker                           'Unknown' : None }.get(opts.native_target,
757*9880d681SAndroid Build Coastguard Worker                                                  opts.native_target)
758*9880d681SAndroid Build Coastguard Worker    if native_target_name is None:
759*9880d681SAndroid Build Coastguard Worker        native_target = None
760*9880d681SAndroid Build Coastguard Worker    else:
761*9880d681SAndroid Build Coastguard Worker        native_target = available_targets.get(native_target_name)
762*9880d681SAndroid Build Coastguard Worker        if native_target is None:
763*9880d681SAndroid Build Coastguard Worker            parser.error("invalid native target: %r (not in project)" % (
764*9880d681SAndroid Build Coastguard Worker                    opts.native_target,))
765*9880d681SAndroid Build Coastguard Worker        if native_target.type_name != 'TargetGroup':
766*9880d681SAndroid Build Coastguard Worker            parser.error("invalid native target: %r (not a target)" % (
767*9880d681SAndroid Build Coastguard Worker                    opts.native_target,))
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker    # Find the list of targets to enable.
770*9880d681SAndroid Build Coastguard Worker    if opts.enable_targets is None:
771*9880d681SAndroid Build Coastguard Worker        enable_targets = available_targets.values()
772*9880d681SAndroid Build Coastguard Worker    else:
773*9880d681SAndroid Build Coastguard Worker        # We support both space separated and semi-colon separated lists.
774*9880d681SAndroid Build Coastguard Worker        if opts.enable_targets == '':
775*9880d681SAndroid Build Coastguard Worker            enable_target_names = []
776*9880d681SAndroid Build Coastguard Worker        elif ' ' in opts.enable_targets:
777*9880d681SAndroid Build Coastguard Worker            enable_target_names = opts.enable_targets.split()
778*9880d681SAndroid Build Coastguard Worker        else:
779*9880d681SAndroid Build Coastguard Worker            enable_target_names = opts.enable_targets.split(';')
780*9880d681SAndroid Build Coastguard Worker
781*9880d681SAndroid Build Coastguard Worker        enable_targets = []
782*9880d681SAndroid Build Coastguard Worker        for name in enable_target_names:
783*9880d681SAndroid Build Coastguard Worker            target = available_targets.get(name)
784*9880d681SAndroid Build Coastguard Worker            if target is None:
785*9880d681SAndroid Build Coastguard Worker                parser.error("invalid target to enable: %r (not in project)" % (
786*9880d681SAndroid Build Coastguard Worker                        name,))
787*9880d681SAndroid Build Coastguard Worker            if target.type_name != 'TargetGroup':
788*9880d681SAndroid Build Coastguard Worker                parser.error("invalid target to enable: %r (not a target)" % (
789*9880d681SAndroid Build Coastguard Worker                        name,))
790*9880d681SAndroid Build Coastguard Worker            enable_targets.append(target)
791*9880d681SAndroid Build Coastguard Worker
792*9880d681SAndroid Build Coastguard Worker    # Find the special library groups we are going to populate. We enforce that
793*9880d681SAndroid Build Coastguard Worker    # these appear in the project (instead of just adding them) so that they at
794*9880d681SAndroid Build Coastguard Worker    # least have an explicit representation in the project LLVMBuild files (and
795*9880d681SAndroid Build Coastguard Worker    # comments explaining how they are populated).
796*9880d681SAndroid Build Coastguard Worker    def find_special_group(name):
797*9880d681SAndroid Build Coastguard Worker        info = info_map.get(name)
798*9880d681SAndroid Build Coastguard Worker        if info is None:
799*9880d681SAndroid Build Coastguard Worker            fatal("expected project to contain special %r component" % (
800*9880d681SAndroid Build Coastguard Worker                    name,))
801*9880d681SAndroid Build Coastguard Worker
802*9880d681SAndroid Build Coastguard Worker        if info.type_name != 'LibraryGroup':
803*9880d681SAndroid Build Coastguard Worker            fatal("special component %r should be a LibraryGroup" % (
804*9880d681SAndroid Build Coastguard Worker                    name,))
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker        if info.required_libraries:
807*9880d681SAndroid Build Coastguard Worker            fatal("special component %r must have empty %r list" % (
808*9880d681SAndroid Build Coastguard Worker                    name, 'required_libraries'))
809*9880d681SAndroid Build Coastguard Worker        if info.add_to_library_groups:
810*9880d681SAndroid Build Coastguard Worker            fatal("special component %r must have empty %r list" % (
811*9880d681SAndroid Build Coastguard Worker                    name, 'add_to_library_groups'))
812*9880d681SAndroid Build Coastguard Worker
813*9880d681SAndroid Build Coastguard Worker        info._is_special_group = True
814*9880d681SAndroid Build Coastguard Worker        return info
815*9880d681SAndroid Build Coastguard Worker
816*9880d681SAndroid Build Coastguard Worker    info_map = dict((ci.name, ci) for ci in project.component_infos)
817*9880d681SAndroid Build Coastguard Worker    all_targets = find_special_group('all-targets')
818*9880d681SAndroid Build Coastguard Worker    native_group = find_special_group('Native')
819*9880d681SAndroid Build Coastguard Worker    native_codegen_group = find_special_group('NativeCodeGen')
820*9880d681SAndroid Build Coastguard Worker    engine_group = find_special_group('Engine')
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard Worker    # Set the enabled bit in all the target groups, and append to the
823*9880d681SAndroid Build Coastguard Worker    # all-targets list.
824*9880d681SAndroid Build Coastguard Worker    for ci in enable_targets:
825*9880d681SAndroid Build Coastguard Worker        all_targets.required_libraries.append(ci.name)
826*9880d681SAndroid Build Coastguard Worker        ci.enabled = True
827*9880d681SAndroid Build Coastguard Worker
828*9880d681SAndroid Build Coastguard Worker    # If we have a native target, then that defines the native and
829*9880d681SAndroid Build Coastguard Worker    # native_codegen libraries.
830*9880d681SAndroid Build Coastguard Worker    if native_target and native_target.enabled:
831*9880d681SAndroid Build Coastguard Worker        native_group.required_libraries.append(native_target.name)
832*9880d681SAndroid Build Coastguard Worker        native_codegen_group.required_libraries.append(
833*9880d681SAndroid Build Coastguard Worker            '%sCodeGen' % native_target.name)
834*9880d681SAndroid Build Coastguard Worker
835*9880d681SAndroid Build Coastguard Worker    # If we have a native target with a JIT, use that for the engine. Otherwise,
836*9880d681SAndroid Build Coastguard Worker    # use the interpreter.
837*9880d681SAndroid Build Coastguard Worker    if native_target and native_target.enabled and native_target.has_jit:
838*9880d681SAndroid Build Coastguard Worker        engine_group.required_libraries.append('MCJIT')
839*9880d681SAndroid Build Coastguard Worker        engine_group.required_libraries.append(native_group.name)
840*9880d681SAndroid Build Coastguard Worker    else:
841*9880d681SAndroid Build Coastguard Worker        engine_group.required_libraries.append('Interpreter')
842*9880d681SAndroid Build Coastguard Worker
843*9880d681SAndroid Build Coastguard Workerdef main():
844*9880d681SAndroid Build Coastguard Worker    from optparse import OptionParser, OptionGroup
845*9880d681SAndroid Build Coastguard Worker    parser = OptionParser("usage: %prog [options]")
846*9880d681SAndroid Build Coastguard Worker
847*9880d681SAndroid Build Coastguard Worker    group = OptionGroup(parser, "Input Options")
848*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--source-root", dest="source_root", metavar="PATH",
849*9880d681SAndroid Build Coastguard Worker                      help="Path to the LLVM source (inferred if not given)",
850*9880d681SAndroid Build Coastguard Worker                      action="store", default=None)
851*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--llvmbuild-source-root",
852*9880d681SAndroid Build Coastguard Worker                     dest="llvmbuild_source_root",
853*9880d681SAndroid Build Coastguard Worker                     help=(
854*9880d681SAndroid Build Coastguard Worker            "If given, an alternate path to search for LLVMBuild.txt files"),
855*9880d681SAndroid Build Coastguard Worker                     action="store", default=None, metavar="PATH")
856*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--build-root", dest="build_root", metavar="PATH",
857*9880d681SAndroid Build Coastguard Worker                      help="Path to the build directory (if needed) [%default]",
858*9880d681SAndroid Build Coastguard Worker                      action="store", default=None)
859*9880d681SAndroid Build Coastguard Worker    parser.add_option_group(group)
860*9880d681SAndroid Build Coastguard Worker
861*9880d681SAndroid Build Coastguard Worker    group = OptionGroup(parser, "Output Options")
862*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--print-tree", dest="print_tree",
863*9880d681SAndroid Build Coastguard Worker                     help="Print out the project component tree [%default]",
864*9880d681SAndroid Build Coastguard Worker                     action="store_true", default=False)
865*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
866*9880d681SAndroid Build Coastguard Worker                      help="Write out the LLVMBuild.txt files to PATH",
867*9880d681SAndroid Build Coastguard Worker                      action="store", default=None, metavar="PATH")
868*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--write-library-table",
869*9880d681SAndroid Build Coastguard Worker                     dest="write_library_table", metavar="PATH",
870*9880d681SAndroid Build Coastguard Worker                     help="Write the C++ library dependency table to PATH",
871*9880d681SAndroid Build Coastguard Worker                     action="store", default=None)
872*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--write-cmake-fragment",
873*9880d681SAndroid Build Coastguard Worker                     dest="write_cmake_fragment", metavar="PATH",
874*9880d681SAndroid Build Coastguard Worker                     help="Write the CMake project information to PATH",
875*9880d681SAndroid Build Coastguard Worker                     action="store", default=None)
876*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--write-cmake-exports-fragment",
877*9880d681SAndroid Build Coastguard Worker                     dest="write_cmake_exports_fragment", metavar="PATH",
878*9880d681SAndroid Build Coastguard Worker                     help="Write the CMake exports information to PATH",
879*9880d681SAndroid Build Coastguard Worker                     action="store", default=None)
880*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--write-make-fragment",
881*9880d681SAndroid Build Coastguard Worker                      dest="write_make_fragment", metavar="PATH",
882*9880d681SAndroid Build Coastguard Worker                     help="Write the Makefile project information to PATH",
883*9880d681SAndroid Build Coastguard Worker                     action="store", default=None)
884*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--configure-target-def-file",
885*9880d681SAndroid Build Coastguard Worker                     dest="configure_target_def_files",
886*9880d681SAndroid Build Coastguard Worker                     help="""Configure the given file at SUBPATH (relative to
887*9880d681SAndroid Build Coastguard Workerthe inferred or given source root, and with a '.in' suffix) by replacing certain
888*9880d681SAndroid Build Coastguard Workersubstitution variables with lists of targets that support certain features (for
889*9880d681SAndroid Build Coastguard Workerexample, targets with AsmPrinters) and write the result to the build root (as
890*9880d681SAndroid Build Coastguard Workergiven by --build-root) at the same SUBPATH""",
891*9880d681SAndroid Build Coastguard Worker                     metavar="SUBPATH", action="append", default=None)
892*9880d681SAndroid Build Coastguard Worker    parser.add_option_group(group)
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker    group = OptionGroup(parser, "Configuration Options")
895*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--native-target",
896*9880d681SAndroid Build Coastguard Worker                      dest="native_target", metavar="NAME",
897*9880d681SAndroid Build Coastguard Worker                      help=("Treat the named target as the 'native' one, if "
898*9880d681SAndroid Build Coastguard Worker                            "given [%default]"),
899*9880d681SAndroid Build Coastguard Worker                      action="store", default=None)
900*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--enable-targets",
901*9880d681SAndroid Build Coastguard Worker                      dest="enable_targets", metavar="NAMES",
902*9880d681SAndroid Build Coastguard Worker                      help=("Enable the given space or semi-colon separated "
903*9880d681SAndroid Build Coastguard Worker                            "list of targets, or all targets if not present"),
904*9880d681SAndroid Build Coastguard Worker                      action="store", default=None)
905*9880d681SAndroid Build Coastguard Worker    group.add_option("", "--enable-optional-components",
906*9880d681SAndroid Build Coastguard Worker                      dest="optional_components", metavar="NAMES",
907*9880d681SAndroid Build Coastguard Worker                      help=("Enable the given space or semi-colon separated "
908*9880d681SAndroid Build Coastguard Worker                            "list of optional components"),
909*9880d681SAndroid Build Coastguard Worker                      action="store", default="")
910*9880d681SAndroid Build Coastguard Worker    parser.add_option_group(group)
911*9880d681SAndroid Build Coastguard Worker
912*9880d681SAndroid Build Coastguard Worker    (opts, args) = parser.parse_args()
913*9880d681SAndroid Build Coastguard Worker
914*9880d681SAndroid Build Coastguard Worker    # Determine the LLVM source path, if not given.
915*9880d681SAndroid Build Coastguard Worker    source_root = opts.source_root
916*9880d681SAndroid Build Coastguard Worker    if source_root:
917*9880d681SAndroid Build Coastguard Worker        if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
918*9880d681SAndroid Build Coastguard Worker                                           'Function.cpp')):
919*9880d681SAndroid Build Coastguard Worker            parser.error('invalid LLVM source root: %r' % source_root)
920*9880d681SAndroid Build Coastguard Worker    else:
921*9880d681SAndroid Build Coastguard Worker        llvmbuild_path = os.path.dirname(__file__)
922*9880d681SAndroid Build Coastguard Worker        llvm_build_path = os.path.dirname(llvmbuild_path)
923*9880d681SAndroid Build Coastguard Worker        utils_path = os.path.dirname(llvm_build_path)
924*9880d681SAndroid Build Coastguard Worker        source_root = os.path.dirname(utils_path)
925*9880d681SAndroid Build Coastguard Worker        if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
926*9880d681SAndroid Build Coastguard Worker                                           'Function.cpp')):
927*9880d681SAndroid Build Coastguard Worker            parser.error('unable to infer LLVM source root, please specify')
928*9880d681SAndroid Build Coastguard Worker
929*9880d681SAndroid Build Coastguard Worker    # Construct the LLVM project information.
930*9880d681SAndroid Build Coastguard Worker    llvmbuild_source_root = opts.llvmbuild_source_root or source_root
931*9880d681SAndroid Build Coastguard Worker    project_info = LLVMProjectInfo.load_from_path(
932*9880d681SAndroid Build Coastguard Worker        source_root, llvmbuild_source_root)
933*9880d681SAndroid Build Coastguard Worker
934*9880d681SAndroid Build Coastguard Worker    # Add the magic target based components.
935*9880d681SAndroid Build Coastguard Worker    add_magic_target_components(parser, project_info, opts)
936*9880d681SAndroid Build Coastguard Worker
937*9880d681SAndroid Build Coastguard Worker    # Validate the project component info.
938*9880d681SAndroid Build Coastguard Worker    project_info.validate_components()
939*9880d681SAndroid Build Coastguard Worker
940*9880d681SAndroid Build Coastguard Worker    # Print the component tree, if requested.
941*9880d681SAndroid Build Coastguard Worker    if opts.print_tree:
942*9880d681SAndroid Build Coastguard Worker        project_info.print_tree()
943*9880d681SAndroid Build Coastguard Worker
944*9880d681SAndroid Build Coastguard Worker    # Write out the components, if requested. This is useful for auto-upgrading
945*9880d681SAndroid Build Coastguard Worker    # the schema.
946*9880d681SAndroid Build Coastguard Worker    if opts.write_llvmbuild:
947*9880d681SAndroid Build Coastguard Worker        project_info.write_components(opts.write_llvmbuild)
948*9880d681SAndroid Build Coastguard Worker
949*9880d681SAndroid Build Coastguard Worker    # Write out the required library table, if requested.
950*9880d681SAndroid Build Coastguard Worker    if opts.write_library_table:
951*9880d681SAndroid Build Coastguard Worker        project_info.write_library_table(opts.write_library_table,
952*9880d681SAndroid Build Coastguard Worker                                         opts.optional_components)
953*9880d681SAndroid Build Coastguard Worker
954*9880d681SAndroid Build Coastguard Worker    # Write out the make fragment, if requested.
955*9880d681SAndroid Build Coastguard Worker    if opts.write_make_fragment:
956*9880d681SAndroid Build Coastguard Worker        project_info.write_make_fragment(opts.write_make_fragment,
957*9880d681SAndroid Build Coastguard Worker                                         opts.optional_components)
958*9880d681SAndroid Build Coastguard Worker
959*9880d681SAndroid Build Coastguard Worker    # Write out the cmake fragment, if requested.
960*9880d681SAndroid Build Coastguard Worker    if opts.write_cmake_fragment:
961*9880d681SAndroid Build Coastguard Worker        project_info.write_cmake_fragment(opts.write_cmake_fragment,
962*9880d681SAndroid Build Coastguard Worker                                          opts.optional_components)
963*9880d681SAndroid Build Coastguard Worker    if opts.write_cmake_exports_fragment:
964*9880d681SAndroid Build Coastguard Worker        project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment,
965*9880d681SAndroid Build Coastguard Worker                                                  opts.optional_components)
966*9880d681SAndroid Build Coastguard Worker
967*9880d681SAndroid Build Coastguard Worker    # Configure target definition files, if requested.
968*9880d681SAndroid Build Coastguard Worker    if opts.configure_target_def_files:
969*9880d681SAndroid Build Coastguard Worker        # Verify we were given a build root.
970*9880d681SAndroid Build Coastguard Worker        if not opts.build_root:
971*9880d681SAndroid Build Coastguard Worker            parser.error("must specify --build-root when using "
972*9880d681SAndroid Build Coastguard Worker                         "--configure-target-def-file")
973*9880d681SAndroid Build Coastguard Worker
974*9880d681SAndroid Build Coastguard Worker        # Create the substitution list.
975*9880d681SAndroid Build Coastguard Worker        available_targets = [ci for ci in project_info.component_infos
976*9880d681SAndroid Build Coastguard Worker                             if ci.type_name == 'TargetGroup']
977*9880d681SAndroid Build Coastguard Worker        substitutions = [
978*9880d681SAndroid Build Coastguard Worker            ("@LLVM_ENUM_TARGETS@",
979*9880d681SAndroid Build Coastguard Worker             ' '.join('LLVM_TARGET(%s)' % ci.name
980*9880d681SAndroid Build Coastguard Worker                      for ci in available_targets)),
981*9880d681SAndroid Build Coastguard Worker            ("@LLVM_ENUM_ASM_PRINTERS@",
982*9880d681SAndroid Build Coastguard Worker             ' '.join('LLVM_ASM_PRINTER(%s)' % ci.name
983*9880d681SAndroid Build Coastguard Worker                      for ci in available_targets
984*9880d681SAndroid Build Coastguard Worker                      if ci.has_asmprinter)),
985*9880d681SAndroid Build Coastguard Worker            ("@LLVM_ENUM_ASM_PARSERS@",
986*9880d681SAndroid Build Coastguard Worker             ' '.join('LLVM_ASM_PARSER(%s)' % ci.name
987*9880d681SAndroid Build Coastguard Worker                      for ci in available_targets
988*9880d681SAndroid Build Coastguard Worker                      if ci.has_asmparser)),
989*9880d681SAndroid Build Coastguard Worker            ("@LLVM_ENUM_DISASSEMBLERS@",
990*9880d681SAndroid Build Coastguard Worker             ' '.join('LLVM_DISASSEMBLER(%s)' % ci.name
991*9880d681SAndroid Build Coastguard Worker                      for ci in available_targets
992*9880d681SAndroid Build Coastguard Worker                      if ci.has_disassembler))]
993*9880d681SAndroid Build Coastguard Worker
994*9880d681SAndroid Build Coastguard Worker        # Configure the given files.
995*9880d681SAndroid Build Coastguard Worker        for subpath in opts.configure_target_def_files:
996*9880d681SAndroid Build Coastguard Worker            inpath = os.path.join(source_root, subpath + '.in')
997*9880d681SAndroid Build Coastguard Worker            outpath = os.path.join(opts.build_root, subpath)
998*9880d681SAndroid Build Coastguard Worker            result = configutil.configure_file(inpath, outpath, substitutions)
999*9880d681SAndroid Build Coastguard Worker            if not result:
1000*9880d681SAndroid Build Coastguard Worker                note("configured file %r hasn't changed" % outpath)
1001*9880d681SAndroid Build Coastguard Worker
1002*9880d681SAndroid Build Coastguard Workerif __name__=='__main__':
1003*9880d681SAndroid Build Coastguard Worker    main()
1004