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