Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 1 | from __future__ import absolute_import |
Peter Collingbourne | 4fdb6ec | 2014-11-19 03:34:20 +0000 | [diff] [blame] | 2 | import filecmp |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 3 | import os |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 4 | import sys |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 5 | |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 6 | import llvmbuild.componentinfo as componentinfo |
Daniel Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 7 | |
Anders Waldenborg | 91527ef | 2014-04-23 19:17:42 +0000 | [diff] [blame] | 8 | from llvmbuild.util import fatal, note |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 9 | |
| 10 | ### |
| 11 | |
Daniel Dunbar | 9057a3d | 2011-11-05 04:07:43 +0000 | [diff] [blame] | 12 | def 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 Dunbar | 52f7122 | 2011-11-17 01:19:53 +0000 | [diff] [blame] | 25 | def 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 Dunbar | 0edba5c9 | 2011-11-05 04:07:49 +0000 | [diff] [blame] | 40 | def 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 Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 47 | # os.makedirs considers it an error to be called with an existent path. |
Daniel Dunbar | 0edba5c9 | 2011-11-05 04:07:49 +0000 | [diff] [blame] | 48 | if not os.path.exists(path): |
| 49 | os.makedirs(path) |
| 50 | |
Daniel Dunbar | b814ee4 | 2011-11-04 23:40:11 +0000 | [diff] [blame] | 51 | ### |
| 52 | |
Daniel Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 53 | class LLVMProjectInfo(object): |
| 54 | @staticmethod |
| 55 | def load_infos_from_path(llvmbuild_source_root): |
Daniel Dunbar | e29ffff | 2011-12-12 22:45:59 +0000 | [diff] [blame] | 56 | 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 Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | e29ffff | 2011-12-12 22:45:59 +0000 | [diff] [blame] | 63 | # Parse the components from it. |
| 64 | common,info_iter = componentinfo.load_from_path(llvmbuild_path, |
| 65 | subpath) |
| 66 | for info in info_iter: |
Daniel Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 67 | yield info |
| 68 | |
Daniel Dunbar | e29ffff | 2011-12-12 22:45:59 +0000 | [diff] [blame] | 69 | # 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 Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 76 | @staticmethod |
| 77 | def load_from_path(source_root, llvmbuild_source_root): |
| 78 | infos = list( |
| 79 | LLVMProjectInfo.load_infos_from_path(llvmbuild_source_root)) |
| 80 | |
| 81 | return LLVMProjectInfo(source_root, infos) |
| 82 | |
| 83 | def __init__(self, source_root, component_infos): |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 84 | # Store our simple ivars. |
Daniel Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 85 | self.source_root = source_root |
Daniel Dunbar | 79fa1e8 | 2011-11-10 00:49:58 +0000 | [diff] [blame] | 86 | 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 Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 101 | |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 102 | # Create the component info map and validate that component names are |
| 103 | # unique. |
| 104 | self.component_info_map = {} |
Daniel Dunbar | 79fa1e8 | 2011-11-10 00:49:58 +0000 | [diff] [blame] | 105 | for ci in self.component_infos: |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 106 | 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 Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 113 | # 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 Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 117 | # Add the root component. |
Daniel Dunbar | 4897255 | 2011-11-03 17:56:16 +0000 | [diff] [blame] | 118 | 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 Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 122 | self.component_infos.append(self.component_info_map['$ROOT']) |
Daniel Dunbar | 4897255 | 2011-11-03 17:56:16 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 124 | # 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 Dunbar | 4897255 | 2011-11-03 17:56:16 +0000 | [diff] [blame] | 143 | # Validate the parent reference, which we treat specially. |
Daniel Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 144 | 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 Dunbar | 4897255 | 2011-11-03 17:56:16 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 151 | 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 Dunbar | 4897255 | 2011-11-03 17:56:16 +0000 | [diff] [blame] | 168 | # 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 Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 173 | self.ordered_component_infos = [] |
NAKAMURA Takumi | a1d528b | 2012-12-20 10:35:18 +0000 | [diff] [blame] | 174 | components_to_visit = sorted( |
| 175 | set(self.component_infos), |
| 176 | key = lambda c: c.name) |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 177 | while components_to_visit: |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 178 | visit_component_info(components_to_visit[0], [], set()) |
Daniel Dunbar | 8844e3c | 2011-11-03 17:56:12 +0000 | [diff] [blame] | 179 | |
Daniel Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 180 | # 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 186 | print('%s%-40s (%s)' % (' '*depth, node.name, node.type_name)) |
Daniel Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 187 | for c in node.children: |
| 188 | visit(c, depth + 1) |
| 189 | visit(self.component_info_map['$ROOT']) |
| 190 | |
Daniel Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 191 | 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 Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 202 | # 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 Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 215 | # 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 Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 221 | fragments = [] |
| 222 | |
| 223 | # Add the common fragments. |
| 224 | subdirectories = subpath_subdirs.get(subpath) |
| 225 | if subdirectories: |
| 226 | fragment = """\ |
| 227 | subdirectories = %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 Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 241 | 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 Dunbar | ea07f34 | 2011-12-12 22:45:35 +0000 | [diff] [blame] | 251 | # 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 Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 261 | comments_map[ln[1:-2]] = comment_block |
Daniel Dunbar | ea07f34 | 2011-12-12 22:45:35 +0000 | [diff] [blame] | 262 | else: |
| 263 | comment_block = "" |
| 264 | f.close() |
| 265 | |
| 266 | # Create the LLVMBuild fil[e. |
Daniel Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 267 | file_path = os.path.join(directory_path, 'LLVMBuild.txt') |
| 268 | f = open(file_path, "w") |
Daniel Dunbar | 453146e | 2011-11-03 17:56:31 +0000 | [diff] [blame] | 269 | |
| 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 275 | f.write("""\ |
Daniel Dunbar | 453146e | 2011-11-03 17:56:31 +0000 | [diff] [blame] | 276 | %s |
| 277 | ; |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 278 | ; 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 Dunbar | 453146e | 2011-11-03 17:56:31 +0000 | [diff] [blame] | 281 | ; |
| 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 291 | |
| 292 | """ % header_string) |
Daniel Dunbar | 453146e | 2011-11-03 17:56:31 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 294 | # Write out each fragment.each component fragment. |
| 295 | for name,fragment in fragments: |
Daniel Dunbar | ea07f34 | 2011-12-12 22:45:35 +0000 | [diff] [blame] | 296 | comment = comments_map.get(name) |
| 297 | if comment is not None: |
| 298 | f.write(comment) |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 299 | f.write("[%s]\n" % name) |
Daniel Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 300 | f.write(fragment) |
Daniel Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 301 | if fragment is not fragments[-1][1]: |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 302 | f.write('\n') |
Daniel Dunbar | 8889bb0 | 2011-12-12 22:45:54 +0000 | [diff] [blame] | 303 | |
Daniel Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 304 | f.close() |
| 305 | |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 306 | def write_library_table(self, output_path, enabled_optional_components): |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 307 | # 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 Dunbar | f876da1 | 2012-05-15 18:44:12 +0000 | [diff] [blame] | 313 | # Skip optional components which are not enabled. |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 314 | if c.type_name == 'OptionalLibrary' \ |
| 315 | and c.name not in enabled_optional_components: |
| 316 | continue |
| 317 | |
Daniel Dunbar | f876da1 | 2012-05-15 18:44:12 +0000 | [diff] [blame] | 318 | # 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 Dunbar | 82219ad | 2011-11-10 00:49:51 +0000 | [diff] [blame] | 323 | # Only certain components are in the table. |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 324 | if c.type_name not in ('Library', 'OptionalLibrary', \ |
| 325 | 'LibraryGroup', 'TargetGroup'): |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 326 | 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 Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 331 | |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 332 | # Get the library name, or None for LibraryGroups. |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 333 | if c.type_name == 'Library' or c.type_name == 'OptionalLibrary': |
Daniel Dunbar | 06bb798 | 2011-12-15 23:35:08 +0000 | [diff] [blame] | 334 | library_name = c.get_prefixed_library_name() |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 335 | is_installed = c.installed |
Daniel Dunbar | 82219ad | 2011-11-10 00:49:51 +0000 | [diff] [blame] | 336 | else: |
| 337 | library_name = None |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 338 | is_installed = True |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 339 | |
| 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 Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 351 | required_llvmconfig_component_names, |
| 352 | is_installed) |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 353 | |
| 354 | # Convert to a list of entries and sort by name. |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 355 | entries = list(entries.values()) |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 356 | |
| 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 Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 360 | for _,_,deps,_ in entries: |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 361 | root_entries -= set(deps) |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 362 | entries.append(('all', None, root_entries, True)) |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 363 | |
| 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 Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 369 | for _,_,deps,_ in entries) + 1 |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 370 | |
| 371 | # Write out the library table. |
Daniel Dunbar | 0edba5c9 | 2011-11-05 04:07:49 +0000 | [diff] [blame] | 372 | make_install_dir(os.path.dirname(output_path)) |
Peter Collingbourne | 4fdb6ec | 2014-11-19 03:34:20 +0000 | [diff] [blame] | 373 | f = open(output_path+'.new', 'w') |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 374 | f.write("""\ |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 375 | //===- llvm-build generated file --------------------------------*- C++ -*-===// |
| 376 | // |
Nico Weber | 3a1b697 | 2018-04-20 17:21:10 +0000 | [diff] [blame] | 377 | // Component Library Dependency Table |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 378 | // |
| 379 | // Automatically generated file, do not edit! |
| 380 | // |
| 381 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 382 | |
| 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 Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 399 | for name,library_name,required_names,is_installed in entries: |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 400 | if library_name is None: |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 401 | library_name_as_cstr = 'nullptr' |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 402 | else: |
Ehsan Akhgari | 155ca8f | 2016-02-09 19:41:14 +0000 | [diff] [blame] | 403 | library_name_as_cstr = '"%s"' % library_name |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 404 | 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 Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 410 | ', '.join('"%s"' % dep |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 411 | for dep in required_names))) |
| 412 | f.write('};\n') |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 413 | f.close() |
| 414 | |
Peter Collingbourne | 4fdb6ec | 2014-11-19 03:34:20 +0000 | [diff] [blame] | 415 | 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 Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 423 | 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 Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 433 | assert ci.type_name in ('Library', 'OptionalLibrary', 'LibraryGroup', 'TargetGroup') |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 434 | |
| 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 Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 440 | if dep.type_name == 'Library' or dep.type_name == 'OptionalLibrary': |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 441 | 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 Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 454 | def get_fragment_dependencies(self): |
Daniel Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 455 | """ |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 456 | get_fragment_dependencies() -> iter |
Daniel Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 458 | 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 Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 461 | """ |
| 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 Dunbar | cda2a89 | 2011-12-06 23:13:42 +0000 | [diff] [blame] | 466 | # |
| 467 | # Many components may come from the same file, so we make sure to unique |
| 468 | # these. |
| 469 | build_paths = set() |
Daniel Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 470 | for ci in self.component_infos: |
Daniel Dunbar | cda2a89 | 2011-12-06 23:13:42 +0000 | [diff] [blame] | 471 | 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 Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 475 | |
| 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 Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 493 | yield path |
| 494 | |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 495 | def foreach_cmake_library(self, f, |
| 496 | enabled_optional_components, |
Dan Liew | 1e3dc52 | 2015-08-21 18:10:57 +0000 | [diff] [blame] | 497 | skip_disabled, |
| 498 | skip_not_installed): |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 499 | 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 Liew | 1e3dc52 | 2015-08-21 18:10:57 +0000 | [diff] [blame] | 515 | # Skip targets that will not be installed |
| 516 | if skip_not_installed and not ci.installed: |
| 517 | continue |
| 518 | |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 519 | f(ci) |
| 520 | |
| 521 | |
Michael Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 522 | def write_cmake_fragment(self, output_path, enabled_optional_components): |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 523 | """ |
| 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 Dunbar | 0edba5c9 | 2011-11-05 04:07:49 +0000 | [diff] [blame] | 535 | make_install_dir(os.path.dirname(output_path)) |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 536 | 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 544 | f.write("""\ |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 545 | %s |
| 546 | # |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 547 | # 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 Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 550 | # |
| 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 559 | |
| 560 | """ % header_string) |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 561 | |
| 562 | # Write the dependency information in the best way we can. |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 563 | f.write(""" |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 564 | # 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 573 | # feature. |
| 574 | """) |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 575 | for dep in dependencies: |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 576 | f.write("""\ |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 577 | configure_file(\"%s\" |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 578 | ${CMAKE_CURRENT_BINARY_DIR}/DummyConfigureOutput)\n""" % ( |
| 579 | cmake_quote_path(dep),)) |
Daniel Dunbar | 9057a3d | 2011-11-05 04:07:43 +0000 | [diff] [blame] | 580 | |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 581 | # Write the properties we use to encode the required library dependency |
| 582 | # information in a form CMake can easily use directly. |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 583 | f.write(""" |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 584 | # Explicit library dependency information. |
| 585 | # |
| 586 | # The following property assignments effectively create a map from component |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 587 | # names to required libraries, in a way that is easily accessed from CMake. |
| 588 | """) |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 589 | self.foreach_cmake_library( |
| 590 | lambda ci: |
| 591 | f.write("""\ |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 592 | set_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_%s %s)\n""" % ( |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 593 | ci.get_prefixed_library_name(), " ".join(sorted( |
| 594 | dep.get_prefixed_library_name() |
Daniel Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 595 | for dep in self.get_required_libraries_for_component(ci))))) |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 596 | , |
| 597 | enabled_optional_components, |
Dan Liew | 1e3dc52 | 2015-08-21 18:10:57 +0000 | [diff] [blame] | 598 | skip_disabled = False, |
| 599 | skip_not_installed = False # Dependency info must be emitted for internals libs too |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 600 | ) |
Daniel Dunbar | 4128db9 | 2011-11-29 00:06:50 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 602 | f.close() |
| 603 | |
Michael Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 604 | def write_cmake_exports_fragment(self, output_path, enabled_optional_components): |
NAKAMURA Takumi | 01e3c64f | 2014-02-09 16:37:02 +0000 | [diff] [blame] | 605 | """ |
| 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 Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 625 | self.foreach_cmake_library( |
| 626 | lambda ci: |
| 627 | f.write("""\ |
NAKAMURA Takumi | 01e3c64f | 2014-02-09 16:37:02 +0000 | [diff] [blame] | 628 | set_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 Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 632 | , |
| 633 | enabled_optional_components, |
Dan Liew | 1e3dc52 | 2015-08-21 18:10:57 +0000 | [diff] [blame] | 634 | skip_disabled = True, |
| 635 | skip_not_installed = True # Do not export internal libraries like gtest |
Dan Liew | a70fb6e | 2015-08-21 18:10:51 +0000 | [diff] [blame] | 636 | ) |
NAKAMURA Takumi | 01e3c64f | 2014-02-09 16:37:02 +0000 | [diff] [blame] | 637 | |
| 638 | f.close() |
| 639 | |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 640 | def 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 Dunbar | 807c6e4 | 2011-11-10 01:16:48 +0000 | [diff] [blame] | 647 | the "all-targets", "Native", "NativeCodeGen", and "Engine" components. |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 648 | """ |
| 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 Dunbar | 807c6e4 | 2011-11-10 01:16:48 +0000 | [diff] [blame] | 678 | # We support both space separated and semi-colon separated lists. |
Greg Fitzgerald | 986b407 | 2014-04-18 17:39:50 +0000 | [diff] [blame] | 679 | if opts.enable_targets == '': |
| 680 | enable_target_names = [] |
| 681 | elif ' ' in opts.enable_targets: |
Daniel Dunbar | 807c6e4 | 2011-11-10 01:16:48 +0000 | [diff] [blame] | 682 | enable_target_names = opts.enable_targets.split() |
| 683 | else: |
| 684 | enable_target_names = opts.enable_targets.split(';') |
| 685 | |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 686 | enable_targets = [] |
Daniel Dunbar | 807c6e4 | 2011-11-10 01:16:48 +0000 | [diff] [blame] | 687 | for name in enable_target_names: |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 688 | 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 Dunbar | 088f81b | 2011-12-12 22:45:41 +0000 | [diff] [blame] | 718 | info._is_special_group = True |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 719 | 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 Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 743 | engine_group.required_libraries.append('MCJIT') |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 744 | engine_group.required_libraries.append(native_group.name) |
| 745 | else: |
| 746 | engine_group.required_libraries.append('Interpreter') |
| 747 | |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 748 | def main(): |
| 749 | from optparse import OptionParser, OptionGroup |
| 750 | parser = OptionParser("usage: %prog [options]") |
Daniel Dunbar | c83a459 | 2011-11-10 00:49:42 +0000 | [diff] [blame] | 751 | |
| 752 | group = OptionGroup(parser, "Input Options") |
| 753 | group.add_option("", "--source-root", dest="source_root", metavar="PATH", |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 754 | help="Path to the LLVM source (inferred if not given)", |
| 755 | action="store", default=None) |
Daniel Dunbar | c83a459 | 2011-11-10 00:49:42 +0000 | [diff] [blame] | 756 | 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 Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 768 | help="Write out the LLVMBuild.txt files to PATH", |
| 769 | action="store", default=None, metavar="PATH") |
Daniel Dunbar | c83a459 | 2011-11-10 00:49:42 +0000 | [diff] [blame] | 770 | 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 Takumi | 01e3c64f | 2014-02-09 16:37:02 +0000 | [diff] [blame] | 778 | 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 Dunbar | c83a459 | 2011-11-10 00:49:42 +0000 | [diff] [blame] | 782 | parser.add_option_group(group) |
Daniel Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 783 | |
| 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 Dunbar | 807c6e4 | 2011-11-10 01:16:48 +0000 | [diff] [blame] | 792 | help=("Enable the given space or semi-colon separated " |
| 793 | "list of targets, or all targets if not present"), |
Daniel Dunbar | ab3b180 | 2011-11-03 22:46:19 +0000 | [diff] [blame] | 794 | action="store", default=None) |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 795 | 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 Dunbar | 25ff9f6 | 2013-08-14 23:15:39 +0000 | [diff] [blame] | 799 | action="store", default="") |
Daniel Dunbar | c83a459 | 2011-11-10 00:49:42 +0000 | [diff] [blame] | 800 | parser.add_option_group(group) |
| 801 | |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 802 | (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 Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 807 | if not os.path.exists(os.path.join(source_root, 'lib', 'IR', |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 808 | '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 Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 815 | if not os.path.exists(os.path.join(source_root, 'lib', 'IR', |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 816 | 'Function.cpp')): |
| 817 | parser.error('unable to infer LLVM source root, please specify') |
| 818 | |
Daniel Dunbar | dd3fb56 | 2011-11-03 17:56:06 +0000 | [diff] [blame] | 819 | # 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 Dunbar | 233c930 | 2011-11-10 00:50:07 +0000 | [diff] [blame] | 824 | # Add the magic target based components. |
| 825 | add_magic_target_components(parser, project_info, opts) |
| 826 | |
Daniel Dunbar | 79fa1e8 | 2011-11-10 00:49:58 +0000 | [diff] [blame] | 827 | # Validate the project component info. |
| 828 | project_info.validate_components() |
| 829 | |
Daniel Dunbar | f45369d | 2011-11-03 17:56:18 +0000 | [diff] [blame] | 830 | # Print the component tree, if requested. |
| 831 | if opts.print_tree: |
| 832 | project_info.print_tree() |
| 833 | |
Daniel Dunbar | dbbb258 | 2011-11-03 17:56:21 +0000 | [diff] [blame] | 834 | # 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 Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 839 | # Write out the required library table, if requested. |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 840 | if opts.write_library_table: |
Preston Gurd | e65f4e6 | 2012-05-07 19:38:40 +0000 | [diff] [blame] | 841 | project_info.write_library_table(opts.write_library_table, |
| 842 | opts.optional_components) |
Daniel Dunbar | 445e8f9 | 2011-11-03 17:56:28 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 844 | # Write out the cmake fragment, if requested. |
| 845 | if opts.write_cmake_fragment: |
Michael Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 846 | project_info.write_cmake_fragment(opts.write_cmake_fragment, |
| 847 | opts.optional_components) |
NAKAMURA Takumi | 01e3c64f | 2014-02-09 16:37:02 +0000 | [diff] [blame] | 848 | if opts.write_cmake_exports_fragment: |
Michael Kuperstein | f9c3480 | 2014-10-29 09:18:49 +0000 | [diff] [blame] | 849 | project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment, |
| 850 | opts.optional_components) |
Daniel Dunbar | e973385 | 2011-11-04 23:10:37 +0000 | [diff] [blame] | 851 | |
Daniel Dunbar | 01b0588 | 2011-11-03 17:56:03 +0000 | [diff] [blame] | 852 | if __name__=='__main__': |
| 853 | main() |