blob: 9a9726424d4048aef3eeea5b289396ee8d45ad12 [file] [log] [blame]
Daniel Dunbar25ff9f62013-08-14 23:15:39 +00001from __future__ import absolute_import
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +00002import filecmp
Daniel Dunbar01b05882011-11-03 17:56:03 +00003import os
Daniel Dunbar8844e3c2011-11-03 17:56:12 +00004import sys
Daniel Dunbar01b05882011-11-03 17:56:03 +00005
Daniel Dunbar25ff9f62013-08-14 23:15:39 +00006import llvmbuild.componentinfo as componentinfo
Daniel Dunbardd3fb562011-11-03 17:56:06 +00007
Anders Waldenborg91527ef2014-04-23 19:17:42 +00008from llvmbuild.util import fatal, note
Daniel Dunbar8844e3c2011-11-03 17:56:12 +00009
10###
11
Daniel Dunbar9057a3d2011-11-05 04:07:43 +000012def cmake_quote_string(value):
13 """
14 cmake_quote_string(value) -> str
15
16 Return a quoted form of the given value that is suitable for use in CMake
17 language files.
18 """
19
20 # Currently, we only handle escaping backslashes.
21 value = value.replace("\\", "\\\\")
22
23 return value
24
Daniel Dunbar52f71222011-11-17 01:19:53 +000025def cmake_quote_path(value):
26 """
27 cmake_quote_path(value) -> str
28
29 Return a quoted form of the given value that is suitable for use in CMake
30 language files.
31 """
32
33 # CMake has a bug in it's Makefile generator that doesn't properly quote
34 # strings it generates. So instead of using proper quoting, we just use "/"
35 # style paths. Currently, we only handle escaping backslashes.
36 value = value.replace("\\", "/")
37
38 return value
39
Daniel Dunbar0edba5c92011-11-05 04:07:49 +000040def make_install_dir(path):
41 """
42 make_install_dir(path) -> None
43
44 Create the given directory path for installation, including any parents.
45 """
46
Benjamin Kramerbde91762012-06-02 10:20:22 +000047 # os.makedirs considers it an error to be called with an existent path.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +000048 if not os.path.exists(path):
49 os.makedirs(path)
50
Daniel Dunbarb814ee42011-11-04 23:40:11 +000051###
52
Daniel Dunbardd3fb562011-11-03 17:56:06 +000053class LLVMProjectInfo(object):
54 @staticmethod
55 def load_infos_from_path(llvmbuild_source_root):
Daniel Dunbare29ffff2011-12-12 22:45:59 +000056 def recurse(subpath):
57 # Load the LLVMBuild file.
58 llvmbuild_path = os.path.join(llvmbuild_source_root + subpath,
59 'LLVMBuild.txt')
60 if not os.path.exists(llvmbuild_path):
61 fatal("missing LLVMBuild.txt file at: %r" % (llvmbuild_path,))
Daniel Dunbardd3fb562011-11-03 17:56:06 +000062
Daniel Dunbare29ffff2011-12-12 22:45:59 +000063 # Parse the components from it.
64 common,info_iter = componentinfo.load_from_path(llvmbuild_path,
65 subpath)
66 for info in info_iter:
Daniel Dunbardd3fb562011-11-03 17:56:06 +000067 yield info
68
Daniel Dunbare29ffff2011-12-12 22:45:59 +000069 # Recurse into the specified subdirectories.
70 for subdir in common.get_list("subdirectories"):
71 for item in recurse(os.path.join(subpath, subdir)):
72 yield item
73
74 return recurse("/")
75
Daniel Dunbardd3fb562011-11-03 17:56:06 +000076 @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):
Daniel Dunbar8844e3c2011-11-03 17:56:12 +000084 # Store our simple ivars.
Daniel Dunbardd3fb562011-11-03 17:56:06 +000085 self.source_root = source_root
Daniel Dunbar79fa1e82011-11-10 00:49:58 +000086 self.component_infos = list(component_infos)
87 self.component_info_map = None
88 self.ordered_component_infos = None
89
90 def validate_components(self):
91 """validate_components() -> None
92
93 Validate that the project components are well-defined. Among other
94 things, this checks that:
95 - Components have valid references.
96 - Components references do not form cycles.
97
98 We also construct the map from component names to info, and the
99 topological ordering of components.
100 """
Daniel Dunbardd3fb562011-11-03 17:56:06 +0000101
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000102 # Create the component info map and validate that component names are
103 # unique.
104 self.component_info_map = {}
Daniel Dunbar79fa1e82011-11-10 00:49:58 +0000105 for ci in self.component_infos:
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000106 existing = self.component_info_map.get(ci.name)
107 if existing is not None:
108 # We found a duplicate component name, report it and error out.
109 fatal("found duplicate component %r (at %r and %r)" % (
110 ci.name, ci.subpath, existing.subpath))
111 self.component_info_map[ci.name] = ci
112
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000113 # Disallow 'all' as a component name, which is a special case.
114 if 'all' in self.component_info_map:
115 fatal("project is not allowed to define 'all' component")
116
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000117 # Add the root component.
Daniel Dunbar48972552011-11-03 17:56:16 +0000118 if '$ROOT' in self.component_info_map:
119 fatal("project is not allowed to define $ROOT component")
120 self.component_info_map['$ROOT'] = componentinfo.GroupComponentInfo(
121 '/', '$ROOT', None)
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000122 self.component_infos.append(self.component_info_map['$ROOT'])
Daniel Dunbar48972552011-11-03 17:56:16 +0000123
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000124 # Topologically order the component information according to their
125 # component references.
126 def visit_component_info(ci, current_stack, current_set):
127 # Check for a cycles.
128 if ci in current_set:
129 # We found a cycle, report it and error out.
130 cycle_description = ' -> '.join(
131 '%r (%s)' % (ci.name, relation)
132 for relation,ci in current_stack)
133 fatal("found cycle to %r after following: %s -> %s" % (
134 ci.name, cycle_description, ci.name))
135
136 # If we have already visited this item, we are done.
137 if ci not in components_to_visit:
138 return
139
140 # Otherwise, mark the component info as visited and traverse.
141 components_to_visit.remove(ci)
142
Daniel Dunbar48972552011-11-03 17:56:16 +0000143 # Validate the parent reference, which we treat specially.
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000144 if ci.parent is not None:
145 parent = self.component_info_map.get(ci.parent)
146 if parent is None:
147 fatal("component %r has invalid reference %r (via %r)" % (
148 ci.name, ci.parent, 'parent'))
149 ci.set_parent_instance(parent)
Daniel Dunbar48972552011-11-03 17:56:16 +0000150
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000151 for relation,referent_name in ci.get_component_references():
152 # Validate that the reference is ok.
153 referent = self.component_info_map.get(referent_name)
154 if referent is None:
155 fatal("component %r has invalid reference %r (via %r)" % (
156 ci.name, referent_name, relation))
157
158 # Visit the reference.
159 current_stack.append((relation,ci))
160 current_set.add(ci)
161 visit_component_info(referent, current_stack, current_set)
162 current_set.remove(ci)
163 current_stack.pop()
164
165 # Finally, add the component info to the ordered list.
166 self.ordered_component_infos.append(ci)
167
Daniel Dunbar48972552011-11-03 17:56:16 +0000168 # FIXME: We aren't actually correctly checking for cycles along the
169 # parent edges. Haven't decided how I want to handle this -- I thought
170 # about only checking cycles by relation type. If we do that, it falls
171 # out easily. If we don't, we should special case the check.
172
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000173 self.ordered_component_infos = []
NAKAMURA Takumia1d528b2012-12-20 10:35:18 +0000174 components_to_visit = sorted(
175 set(self.component_infos),
176 key = lambda c: c.name)
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000177 while components_to_visit:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000178 visit_component_info(components_to_visit[0], [], set())
Daniel Dunbar8844e3c2011-11-03 17:56:12 +0000179
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000180 # Canonicalize children lists.
181 for c in self.ordered_component_infos:
182 c.children.sort(key = lambda c: c.name)
183
184 def print_tree(self):
185 def visit(node, depth = 0):
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000186 print('%s%-40s (%s)' % (' '*depth, node.name, node.type_name))
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000187 for c in node.children:
188 visit(c, depth + 1)
189 visit(self.component_info_map['$ROOT'])
190
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000191 def write_components(self, output_path):
192 # Organize all the components by the directory their LLVMBuild file
193 # should go in.
194 info_basedir = {}
195 for ci in self.component_infos:
196 # Ignore the $ROOT component.
197 if ci.parent is None:
198 continue
199
200 info_basedir[ci.subpath] = info_basedir.get(ci.subpath, []) + [ci]
201
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000202 # Compute the list of subdirectories to scan.
203 subpath_subdirs = {}
204 for ci in self.component_infos:
205 # Ignore root components.
206 if ci.subpath == '/':
207 continue
208
209 # Otherwise, append this subpath to the parent list.
210 parent_path = os.path.dirname(ci.subpath)
211 subpath_subdirs[parent_path] = parent_list = subpath_subdirs.get(
212 parent_path, set())
213 parent_list.add(os.path.basename(ci.subpath))
214
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000215 # Generate the build files.
216 for subpath, infos in info_basedir.items():
217 # Order the components by name to have a canonical ordering.
218 infos.sort(key = lambda ci: ci.name)
219
220 # Format the components into llvmbuild fragments.
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000221 fragments = []
222
223 # Add the common fragments.
224 subdirectories = subpath_subdirs.get(subpath)
225 if subdirectories:
226 fragment = """\
227subdirectories = %s
228""" % (" ".join(sorted(subdirectories)),)
229 fragments.append(("common", fragment))
230
231 # Add the component fragments.
232 num_common_fragments = len(fragments)
233 for ci in infos:
234 fragment = ci.get_llvmbuild_fragment()
235 if fragment is None:
236 continue
237
238 name = "component_%d" % (len(fragments) - num_common_fragments)
239 fragments.append((name, fragment))
240
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000241 if not fragments:
242 continue
243
244 assert subpath.startswith('/')
245 directory_path = os.path.join(output_path, subpath[1:])
246
247 # Create the directory if it does not already exist.
248 if not os.path.exists(directory_path):
249 os.makedirs(directory_path)
250
Daniel Dunbarea07f342011-12-12 22:45:35 +0000251 # In an effort to preserve comments (which aren't parsed), read in
252 # the original file and extract the comments. We only know how to
253 # associate comments that prefix a section name.
254 f = open(infos[0]._source_path)
255 comments_map = {}
256 comment_block = ""
257 for ln in f:
258 if ln.startswith(';'):
259 comment_block += ln
260 elif ln.startswith('[') and ln.endswith(']\n'):
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000261 comments_map[ln[1:-2]] = comment_block
Daniel Dunbarea07f342011-12-12 22:45:35 +0000262 else:
263 comment_block = ""
264 f.close()
265
266 # Create the LLVMBuild fil[e.
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000267 file_path = os.path.join(directory_path, 'LLVMBuild.txt')
268 f = open(file_path, "w")
Daniel Dunbar453146e2011-11-03 17:56:31 +0000269
270 # Write the header.
271 header_fmt = ';===- %s %s-*- Conf -*--===;'
272 header_name = '.' + os.path.join(subpath, 'LLVMBuild.txt')
273 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
274 header_string = header_fmt % (header_name, header_pad)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000275 f.write("""\
Daniel Dunbar453146e2011-11-03 17:56:31 +0000276%s
277;
Chandler Carruth2946cd72019-01-19 08:50:56 +0000278; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
279; See https://llvm.org/LICENSE.txt for license information.
280; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar453146e2011-11-03 17:56:31 +0000281;
282;===------------------------------------------------------------------------===;
283;
284; This is an LLVMBuild description file for the components in this subdirectory.
285;
286; For more information on the LLVMBuild system, please see:
287;
288; http://llvm.org/docs/LLVMBuild.html
289;
290;===------------------------------------------------------------------------===;
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000291
292""" % header_string)
Daniel Dunbar453146e2011-11-03 17:56:31 +0000293
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000294 # Write out each fragment.each component fragment.
295 for name,fragment in fragments:
Daniel Dunbarea07f342011-12-12 22:45:35 +0000296 comment = comments_map.get(name)
297 if comment is not None:
298 f.write(comment)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000299 f.write("[%s]\n" % name)
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000300 f.write(fragment)
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000301 if fragment is not fragments[-1][1]:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000302 f.write('\n')
Daniel Dunbar8889bb02011-12-12 22:45:54 +0000303
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000304 f.close()
305
Preston Gurde65f4e62012-05-07 19:38:40 +0000306 def write_library_table(self, output_path, enabled_optional_components):
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000307 # Write out the mapping from component names to required libraries.
308 #
309 # We do this in topological order so that we know we can append the
310 # dependencies for added library groups.
311 entries = {}
312 for c in self.ordered_component_infos:
Daniel Dunbarf876da12012-05-15 18:44:12 +0000313 # Skip optional components which are not enabled.
Preston Gurde65f4e62012-05-07 19:38:40 +0000314 if c.type_name == 'OptionalLibrary' \
315 and c.name not in enabled_optional_components:
316 continue
317
Daniel Dunbarf876da12012-05-15 18:44:12 +0000318 # Skip target groups which are not enabled.
319 tg = c.get_parent_target_group()
320 if tg and not tg.enabled:
321 continue
322
Daniel Dunbar82219ad2011-11-10 00:49:51 +0000323 # Only certain components are in the table.
Preston Gurde65f4e62012-05-07 19:38:40 +0000324 if c.type_name not in ('Library', 'OptionalLibrary', \
325 'LibraryGroup', 'TargetGroup'):
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000326 continue
327
328 # Compute the llvm-config "component name". For historical reasons,
329 # this is lowercased based on the library name.
330 llvmconfig_component_name = c.get_llvmconfig_component_name()
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000331
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000332 # Get the library name, or None for LibraryGroups.
Preston Gurde65f4e62012-05-07 19:38:40 +0000333 if c.type_name == 'Library' or c.type_name == 'OptionalLibrary':
Daniel Dunbar06bb7982011-12-15 23:35:08 +0000334 library_name = c.get_prefixed_library_name()
Daniel Dunbarc364d682012-05-15 18:44:17 +0000335 is_installed = c.installed
Daniel Dunbar82219ad2011-11-10 00:49:51 +0000336 else:
337 library_name = None
Daniel Dunbarc364d682012-05-15 18:44:17 +0000338 is_installed = True
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000339
340 # Get the component names of all the required libraries.
341 required_llvmconfig_component_names = [
342 self.component_info_map[dep].get_llvmconfig_component_name()
343 for dep in c.required_libraries]
344
345 # Insert the entries for library groups we should add to.
346 for dep in c.add_to_library_groups:
347 entries[dep][2].append(llvmconfig_component_name)
348
349 # Add the entry.
350 entries[c.name] = (llvmconfig_component_name, library_name,
Daniel Dunbarc364d682012-05-15 18:44:17 +0000351 required_llvmconfig_component_names,
352 is_installed)
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000353
354 # Convert to a list of entries and sort by name.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000355 entries = list(entries.values())
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000356
357 # Create an 'all' pseudo component. We keep the dependency list small by
358 # only listing entries that have no other dependents.
359 root_entries = set(e[0] for e in entries)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000360 for _,_,deps,_ in entries:
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000361 root_entries -= set(deps)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000362 entries.append(('all', None, root_entries, True))
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000363
364 entries.sort()
365
366 # Compute the maximum number of required libraries, plus one so there is
367 # always a sentinel.
368 max_required_libraries = max(len(deps)
Daniel Dunbarc364d682012-05-15 18:44:17 +0000369 for _,_,deps,_ in entries) + 1
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000370
371 # Write out the library table.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +0000372 make_install_dir(os.path.dirname(output_path))
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +0000373 f = open(output_path+'.new', 'w')
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000374 f.write("""\
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000375//===- llvm-build generated file --------------------------------*- C++ -*-===//
376//
Nico Weber3a1b6972018-04-20 17:21:10 +0000377// Component Library Dependency Table
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000378//
379// Automatically generated file, do not edit!
380//
381//===----------------------------------------------------------------------===//
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000382
383""")
384 f.write('struct AvailableComponent {\n')
385 f.write(' /// The name of the component.\n')
386 f.write(' const char *Name;\n')
387 f.write('\n')
388 f.write(' /// The name of the library for this component (or NULL).\n')
389 f.write(' const char *Library;\n')
390 f.write('\n')
391 f.write(' /// Whether the component is installed.\n')
392 f.write(' bool IsInstalled;\n')
393 f.write('\n')
394 f.write('\
395 /// The list of libraries required when linking this component.\n')
396 f.write(' const char *RequiredLibraries[%d];\n' % (
397 max_required_libraries))
398 f.write('} AvailableComponents[%d] = {\n' % len(entries))
Daniel Dunbarc364d682012-05-15 18:44:17 +0000399 for name,library_name,required_names,is_installed in entries:
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000400 if library_name is None:
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000401 library_name_as_cstr = 'nullptr'
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000402 else:
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000403 library_name_as_cstr = '"%s"' % library_name
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000404 if is_installed:
405 is_installed_as_cstr = 'true'
406 else:
407 is_installed_as_cstr = 'false'
408 f.write(' { "%s", %s, %s, { %s } },\n' % (
409 name, library_name_as_cstr, is_installed_as_cstr,
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000410 ', '.join('"%s"' % dep
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000411 for dep in required_names)))
412 f.write('};\n')
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000413 f.close()
414
Peter Collingbourne4fdb6ec2014-11-19 03:34:20 +0000415 if not os.path.isfile(output_path):
416 os.rename(output_path+'.new', output_path)
417 elif filecmp.cmp(output_path, output_path+'.new'):
418 os.remove(output_path+'.new')
419 else:
420 os.remove(output_path)
421 os.rename(output_path+'.new', output_path)
422
Daniel Dunbar4128db92011-11-29 00:06:50 +0000423 def get_required_libraries_for_component(self, ci, traverse_groups = False):
424 """
425 get_required_libraries_for_component(component_info) -> iter
426
427 Given a Library component info descriptor, return an iterator over all
428 of the directly required libraries for linking with this component. If
429 traverse_groups is True, then library and target groups will be
430 traversed to include their required libraries.
431 """
432
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000433 assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup')
Daniel Dunbar4128db92011-11-29 00:06:50 +0000434
435 for name in ci.required_libraries:
436 # Get the dependency info.
437 dep = self.component_info_map[name]
438
439 # If it is a library, yield it.
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000440 if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary':
Daniel Dunbar4128db92011-11-29 00:06:50 +0000441 yield dep
442 continue
443
444 # Otherwise if it is a group, yield or traverse depending on what
445 # was requested.
446 if dep.type_name in ('LibraryGroup', 'TargetGroup'):
447 if not traverse_groups:
448 yield dep
449 continue
450
451 for res in self.get_required_libraries_for_component(dep, True):
452 yield res
453
Daniel Dunbare9733852011-11-04 23:10:37 +0000454 def get_fragment_dependencies(self):
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000455 """
Daniel Dunbare9733852011-11-04 23:10:37 +0000456 get_fragment_dependencies() -> iter
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000457
Daniel Dunbare9733852011-11-04 23:10:37 +0000458 Compute the list of files (as absolute paths) on which the output
459 fragments depend (i.e., files for which a modification should trigger a
460 rebuild of the fragment).
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000461 """
462
463 # Construct a list of all the dependencies of the Makefile fragment
464 # itself. These include all the LLVMBuild files themselves, as well as
465 # all of our own sources.
Daniel Dunbarcda2a892011-12-06 23:13:42 +0000466 #
467 # Many components may come from the same file, so we make sure to unique
468 # these.
469 build_paths = set()
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000470 for ci in self.component_infos:
Daniel Dunbarcda2a892011-12-06 23:13:42 +0000471 p = os.path.join(self.source_root, ci.subpath[1:], 'LLVMBuild.txt')
472 if p not in build_paths:
473 yield p
474 build_paths.add(p)
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000475
476 # Gather the list of necessary sources by just finding all loaded
477 # modules that are inside the LLVM source tree.
478 for module in sys.modules.values():
479 # Find the module path.
480 if not hasattr(module, '__file__'):
481 continue
482 path = getattr(module, '__file__')
483 if not path:
484 continue
485
486 # Strip off any compiled suffix.
487 if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
488 path = path[:-1]
489
490 # If the path exists and is in the source tree, consider it a
491 # dependency.
492 if (path.startswith(self.source_root) and os.path.exists(path)):
Daniel Dunbare9733852011-11-04 23:10:37 +0000493 yield path
494
Dan Liewa70fb6e2015-08-21 18:10:51 +0000495 def foreach_cmake_library(self, f,
496 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000497 skip_disabled,
498 skip_not_installed):
Dan Liewa70fb6e2015-08-21 18:10:51 +0000499 for ci in self.ordered_component_infos:
500 # Skip optional components which are not enabled.
501 if ci.type_name == 'OptionalLibrary' \
502 and ci.name not in enabled_optional_components:
503 continue
504
505 # We only write the information for libraries currently.
506 if ci.type_name not in ('Library', 'OptionalLibrary'):
507 continue
508
509 # Skip disabled targets.
510 if skip_disabled:
511 tg = ci.get_parent_target_group()
512 if tg and not tg.enabled:
513 continue
514
Dan Liew1e3dc522015-08-21 18:10:57 +0000515 # Skip targets that will not be installed
516 if skip_not_installed and not ci.installed:
517 continue
518
Dan Liewa70fb6e2015-08-21 18:10:51 +0000519 f(ci)
520
521
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000522 def write_cmake_fragment(self, output_path, enabled_optional_components):
Daniel Dunbare9733852011-11-04 23:10:37 +0000523 """
524 write_cmake_fragment(output_path) -> None
525
526 Generate a CMake fragment which includes all of the collated LLVMBuild
527 information in a format that is easily digestible by a CMake. The exact
528 contents of this are closely tied to how the CMake configuration
529 integrates LLVMBuild, see CMakeLists.txt in the top-level.
530 """
531
532 dependencies = list(self.get_fragment_dependencies())
533
534 # Write out the CMake fragment.
Daniel Dunbar0edba5c92011-11-05 04:07:49 +0000535 make_install_dir(os.path.dirname(output_path))
Daniel Dunbare9733852011-11-04 23:10:37 +0000536 f = open(output_path, 'w')
537
538 # Write the header.
539 header_fmt = '\
540#===-- %s - LLVMBuild Configuration for LLVM %s-*- CMake -*--===#'
541 header_name = os.path.basename(output_path)
542 header_pad = '-' * (80 - len(header_fmt % (header_name, '')))
543 header_string = header_fmt % (header_name, header_pad)
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000544 f.write("""\
Daniel Dunbare9733852011-11-04 23:10:37 +0000545%s
546#
Chandler Carruth2946cd72019-01-19 08:50:56 +0000547# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
548# See https://llvm.org/LICENSE.txt for license information.
549# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbare9733852011-11-04 23:10:37 +0000550#
551#===------------------------------------------------------------------------===#
552#
553# This file contains the LLVMBuild project information in a format easily
554# consumed by the CMake based build system.
555#
556# This file is autogenerated by llvm-build, do not edit!
557#
558#===------------------------------------------------------------------------===#
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000559
560""" % header_string)
Daniel Dunbare9733852011-11-04 23:10:37 +0000561
562 # Write the dependency information in the best way we can.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000563 f.write("""
Daniel Dunbare9733852011-11-04 23:10:37 +0000564# LLVMBuild CMake fragment dependencies.
565#
566# CMake has no builtin way to declare that the configuration depends on
567# a particular file. However, a side effect of configure_file is to add
568# said input file to CMake's internal dependency list. So, we use that
569# and a dummy output file to communicate the dependency information to
570# CMake.
571#
572# FIXME: File a CMake RFE to get a properly supported version of this
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000573# feature.
574""")
Daniel Dunbare9733852011-11-04 23:10:37 +0000575 for dep in dependencies:
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000576 f.write("""\
Daniel Dunbare9733852011-11-04 23:10:37 +0000577configure_file(\"%s\"
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000578 ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % (
579 cmake_quote_path(dep),))
Daniel Dunbar9057a3d2011-11-05 04:07:43 +0000580
Daniel Dunbar4128db92011-11-29 00:06:50 +0000581 # Write the properties we use to encode the required library dependency
582 # information in a form CMake can easily use directly.
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000583 f.write("""
Daniel Dunbar4128db92011-11-29 00:06:50 +0000584# Explicit library dependency information.
585#
586# The following property assignments effectively create a map from component
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000587# names to required libraries, in a way that is easily accessed from CMake.
588""")
Dan Liewa70fb6e2015-08-21 18:10:51 +0000589 self.foreach_cmake_library(
590 lambda ci:
591 f.write("""\
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000592set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % (
Daniel Dunbar4128db92011-11-29 00:06:50 +0000593 ci.get_prefixed_library_name(), " ".join(sorted(
594 dep.get_prefixed_library_name()
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000595 for dep in self.get_required_libraries_for_component(ci)))))
Dan Liewa70fb6e2015-08-21 18:10:51 +0000596 ,
597 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000598 skip_disabled = False,
599 skip_not_installed = False # Dependency info must be emitted for internals libs too
Dan Liewa70fb6e2015-08-21 18:10:51 +0000600 )
Daniel Dunbar4128db92011-11-29 00:06:50 +0000601
Daniel Dunbare9733852011-11-04 23:10:37 +0000602 f.close()
603
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000604 def write_cmake_exports_fragment(self, output_path, enabled_optional_components):
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000605 """
606 write_cmake_exports_fragment(output_path) -> None
607
608 Generate a CMake fragment which includes LLVMBuild library
609 dependencies expressed similarly to how CMake would write
610 them via install(EXPORT).
611 """
612
613 dependencies = list(self.get_fragment_dependencies())
614
615 # Write out the CMake exports fragment.
616 make_install_dir(os.path.dirname(output_path))
617 f = open(output_path, 'w')
618
619 f.write("""\
620# Explicit library dependency information.
621#
622# The following property assignments tell CMake about link
623# dependencies of libraries imported from LLVM.
624""")
Dan Liewa70fb6e2015-08-21 18:10:51 +0000625 self.foreach_cmake_library(
626 lambda ci:
627 f.write("""\
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000628set_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
629 ci.get_prefixed_library_name(), " ".join(sorted(
630 dep.get_prefixed_library_name()
631 for dep in self.get_required_libraries_for_component(ci)))))
Dan Liewa70fb6e2015-08-21 18:10:51 +0000632 ,
633 enabled_optional_components,
Dan Liew1e3dc522015-08-21 18:10:57 +0000634 skip_disabled = True,
635 skip_not_installed = True # Do not export internal libraries like gtest
Dan Liewa70fb6e2015-08-21 18:10:51 +0000636 )
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000637
638 f.close()
639
Daniel Dunbar233c9302011-11-10 00:50:07 +0000640def add_magic_target_components(parser, project, opts):
641 """add_magic_target_components(project, opts) -> None
642
643 Add the "magic" target based components to the project, which can only be
644 determined based on the target configuration options.
645
646 This currently is responsible for populating the required_libraries list of
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000647 the "all-targets", "Native", "NativeCodeGen", and "Engine" components.
Daniel Dunbar233c9302011-11-10 00:50:07 +0000648 """
649
650 # Determine the available targets.
651 available_targets = dict((ci.name,ci)
652 for ci in project.component_infos
653 if ci.type_name == 'TargetGroup')
654
655 # Find the configured native target.
656
657 # We handle a few special cases of target names here for historical
658 # reasons, as these are the names configure currently comes up with.
659 native_target_name = { 'x86' : 'X86',
660 'x86_64' : 'X86',
661 'Unknown' : None }.get(opts.native_target,
662 opts.native_target)
663 if native_target_name is None:
664 native_target = None
665 else:
666 native_target = available_targets.get(native_target_name)
667 if native_target is None:
668 parser.error("invalid native target: %r (not in project)" % (
669 opts.native_target,))
670 if native_target.type_name != 'TargetGroup':
671 parser.error("invalid native target: %r (not a target)" % (
672 opts.native_target,))
673
674 # Find the list of targets to enable.
675 if opts.enable_targets is None:
676 enable_targets = available_targets.values()
677 else:
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000678 # We support both space separated and semi-colon separated lists.
Greg Fitzgerald986b4072014-04-18 17:39:50 +0000679 if opts.enable_targets == '':
680 enable_target_names = []
681 elif ' ' in opts.enable_targets:
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000682 enable_target_names = opts.enable_targets.split()
683 else:
684 enable_target_names = opts.enable_targets.split(';')
685
Daniel Dunbar233c9302011-11-10 00:50:07 +0000686 enable_targets = []
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000687 for name in enable_target_names:
Daniel Dunbar233c9302011-11-10 00:50:07 +0000688 target = available_targets.get(name)
689 if target is None:
690 parser.error("invalid target to enable: %r (not in project)" % (
691 name,))
692 if target.type_name != 'TargetGroup':
693 parser.error("invalid target to enable: %r (not a target)" % (
694 name,))
695 enable_targets.append(target)
696
697 # Find the special library groups we are going to populate. We enforce that
698 # these appear in the project (instead of just adding them) so that they at
699 # least have an explicit representation in the project LLVMBuild files (and
700 # comments explaining how they are populated).
701 def find_special_group(name):
702 info = info_map.get(name)
703 if info is None:
704 fatal("expected project to contain special %r component" % (
705 name,))
706
707 if info.type_name != 'LibraryGroup':
708 fatal("special component %r should be a LibraryGroup" % (
709 name,))
710
711 if info.required_libraries:
712 fatal("special component %r must have empty %r list" % (
713 name, 'required_libraries'))
714 if info.add_to_library_groups:
715 fatal("special component %r must have empty %r list" % (
716 name, 'add_to_library_groups'))
717
Daniel Dunbar088f81b2011-12-12 22:45:41 +0000718 info._is_special_group = True
Daniel Dunbar233c9302011-11-10 00:50:07 +0000719 return info
720
721 info_map = dict((ci.name, ci) for ci in project.component_infos)
722 all_targets = find_special_group('all-targets')
723 native_group = find_special_group('Native')
724 native_codegen_group = find_special_group('NativeCodeGen')
725 engine_group = find_special_group('Engine')
726
727 # Set the enabled bit in all the target groups, and append to the
728 # all-targets list.
729 for ci in enable_targets:
730 all_targets.required_libraries.append(ci.name)
731 ci.enabled = True
732
733 # If we have a native target, then that defines the native and
734 # native_codegen libraries.
735 if native_target and native_target.enabled:
736 native_group.required_libraries.append(native_target.name)
737 native_codegen_group.required_libraries.append(
738 '%sCodeGen' % native_target.name)
739
740 # If we have a native target with a JIT, use that for the engine. Otherwise,
741 # use the interpreter.
742 if native_target and native_target.enabled and native_target.has_jit:
Eric Christopher79cc1e32014-09-02 22:28:02 +0000743 engine_group.required_libraries.append('MCJIT')
Daniel Dunbar233c9302011-11-10 00:50:07 +0000744 engine_group.required_libraries.append(native_group.name)
745 else:
746 engine_group.required_libraries.append('Interpreter')
747
Daniel Dunbar01b05882011-11-03 17:56:03 +0000748def main():
749 from optparse import OptionParser, OptionGroup
750 parser = OptionParser("usage: %prog [options]")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000751
752 group = OptionGroup(parser, "Input Options")
753 group.add_option("", "--source-root", dest="source_root", metavar="PATH",
Daniel Dunbar01b05882011-11-03 17:56:03 +0000754 help="Path to the LLVM source (inferred if not given)",
755 action="store", default=None)
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000756 group.add_option("", "--llvmbuild-source-root",
757 dest="llvmbuild_source_root",
758 help=(
759 "If given, an alternate path to search for LLVMBuild.txt files"),
760 action="store", default=None, metavar="PATH")
761 parser.add_option_group(group)
762
763 group = OptionGroup(parser, "Output Options")
764 group.add_option("", "--print-tree", dest="print_tree",
765 help="Print out the project component tree [%default]",
766 action="store_true", default=False)
767 group.add_option("", "--write-llvmbuild", dest="write_llvmbuild",
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000768 help="Write out the LLVMBuild.txt files to PATH",
769 action="store", default=None, metavar="PATH")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000770 group.add_option("", "--write-library-table",
771 dest="write_library_table", metavar="PATH",
772 help="Write the C++ library dependency table to PATH",
773 action="store", default=None)
774 group.add_option("", "--write-cmake-fragment",
775 dest="write_cmake_fragment", metavar="PATH",
776 help="Write the CMake project information to PATH",
777 action="store", default=None)
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000778 group.add_option("", "--write-cmake-exports-fragment",
779 dest="write_cmake_exports_fragment", metavar="PATH",
780 help="Write the CMake exports information to PATH",
781 action="store", default=None)
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000782 parser.add_option_group(group)
Daniel Dunbar233c9302011-11-10 00:50:07 +0000783
784 group = OptionGroup(parser, "Configuration Options")
785 group.add_option("", "--native-target",
786 dest="native_target", metavar="NAME",
787 help=("Treat the named target as the 'native' one, if "
788 "given [%default]"),
789 action="store", default=None)
790 group.add_option("", "--enable-targets",
791 dest="enable_targets", metavar="NAMES",
Daniel Dunbar807c6e42011-11-10 01:16:48 +0000792 help=("Enable the given space or semi-colon separated "
793 "list of targets, or all targets if not present"),
Daniel Dunbarab3b1802011-11-03 22:46:19 +0000794 action="store", default=None)
Preston Gurde65f4e62012-05-07 19:38:40 +0000795 group.add_option("", "--enable-optional-components",
796 dest="optional_components", metavar="NAMES",
797 help=("Enable the given space or semi-colon separated "
798 "list of optional components"),
Daniel Dunbar25ff9f62013-08-14 23:15:39 +0000799 action="store", default="")
Daniel Dunbarc83a4592011-11-10 00:49:42 +0000800 parser.add_option_group(group)
801
Daniel Dunbar01b05882011-11-03 17:56:03 +0000802 (opts, args) = parser.parse_args()
803
804 # Determine the LLVM source path, if not given.
805 source_root = opts.source_root
806 if source_root:
Chandler Carruthef860a22013-01-02 09:10:48 +0000807 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
Daniel Dunbar01b05882011-11-03 17:56:03 +0000808 'Function.cpp')):
809 parser.error('invalid LLVM source root: %r' % source_root)
810 else:
811 llvmbuild_path = os.path.dirname(__file__)
812 llvm_build_path = os.path.dirname(llvmbuild_path)
813 utils_path = os.path.dirname(llvm_build_path)
814 source_root = os.path.dirname(utils_path)
Chandler Carruthef860a22013-01-02 09:10:48 +0000815 if not os.path.exists(os.path.join(source_root, 'lib', 'IR',
Daniel Dunbar01b05882011-11-03 17:56:03 +0000816 'Function.cpp')):
817 parser.error('unable to infer LLVM source root, please specify')
818
Daniel Dunbardd3fb562011-11-03 17:56:06 +0000819 # Construct the LLVM project information.
820 llvmbuild_source_root = opts.llvmbuild_source_root or source_root
821 project_info = LLVMProjectInfo.load_from_path(
822 source_root, llvmbuild_source_root)
823
Daniel Dunbar233c9302011-11-10 00:50:07 +0000824 # Add the magic target based components.
825 add_magic_target_components(parser, project_info, opts)
826
Daniel Dunbar79fa1e82011-11-10 00:49:58 +0000827 # Validate the project component info.
828 project_info.validate_components()
829
Daniel Dunbarf45369d2011-11-03 17:56:18 +0000830 # Print the component tree, if requested.
831 if opts.print_tree:
832 project_info.print_tree()
833
Daniel Dunbardbbb2582011-11-03 17:56:21 +0000834 # Write out the components, if requested. This is useful for auto-upgrading
835 # the schema.
836 if opts.write_llvmbuild:
837 project_info.write_components(opts.write_llvmbuild)
838
Daniel Dunbare9733852011-11-04 23:10:37 +0000839 # Write out the required library table, if requested.
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000840 if opts.write_library_table:
Preston Gurde65f4e62012-05-07 19:38:40 +0000841 project_info.write_library_table(opts.write_library_table,
842 opts.optional_components)
Daniel Dunbar445e8f92011-11-03 17:56:28 +0000843
Daniel Dunbare9733852011-11-04 23:10:37 +0000844 # Write out the cmake fragment, if requested.
845 if opts.write_cmake_fragment:
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000846 project_info.write_cmake_fragment(opts.write_cmake_fragment,
847 opts.optional_components)
NAKAMURA Takumi01e3c64f2014-02-09 16:37:02 +0000848 if opts.write_cmake_exports_fragment:
Michael Kupersteinf9c34802014-10-29 09:18:49 +0000849 project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment,
850 opts.optional_components)
Daniel Dunbare9733852011-11-04 23:10:37 +0000851
Daniel Dunbar01b05882011-11-03 17:56:03 +0000852if __name__=='__main__':
853 main()