1# Copyright (c) 2012 Google Inc. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Xcode project file generator. 6 7This module is both an Xcode project file generator and a documentation of the 8Xcode project file format. Knowledge of the project file format was gained 9based on extensive experience with Xcode, and by making changes to projects in 10Xcode.app and observing the resultant changes in the associated project files. 11 12XCODE PROJECT FILES 13 14The generator targets the file format as written by Xcode 3.2 (specifically, 153.2.6), but past experience has taught that the format has not changed 16significantly in the past several years, and future versions of Xcode are able 17to read older project files. 18 19Xcode project files are "bundled": the project "file" from an end-user's 20perspective is actually a directory with an ".xcodeproj" extension. The 21project file from this module's perspective is actually a file inside this 22directory, always named "project.pbxproj". This file contains a complete 23description of the project and is all that is needed to use the xcodeproj. 24Other files contained in the xcodeproj directory are simply used to store 25per-user settings, such as the state of various UI elements in the Xcode 26application. 27 28The project.pbxproj file is a property list, stored in a format almost 29identical to the NeXTstep property list format. The file is able to carry 30Unicode data, and is encoded in UTF-8. The root element in the property list 31is a dictionary that contains several properties of minimal interest, and two 32properties of immense interest. The most important property is a dictionary 33named "objects". The entire structure of the project is represented by the 34children of this property. The objects dictionary is keyed by unique 96-bit 35values represented by 24 uppercase hexadecimal characters. Each value in the 36objects dictionary is itself a dictionary, describing an individual object. 37 38Each object in the dictionary is a member of a class, which is identified by 39the "isa" property of each object. A variety of classes are represented in a 40project file. Objects can refer to other objects by ID, using the 24-character 41hexadecimal object key. A project's objects form a tree, with a root object 42of class PBXProject at the root. As an example, the PBXProject object serves 43as parent to an XCConfigurationList object defining the build configurations 44used in the project, a PBXGroup object serving as a container for all files 45referenced in the project, and a list of target objects, each of which defines 46a target in the project. There are several different types of target object, 47such as PBXNativeTarget and PBXAggregateTarget. In this module, this 48relationship is expressed by having each target type derive from an abstract 49base named XCTarget. 50 51The project.pbxproj file's root dictionary also contains a property, sibling to 52the "objects" dictionary, named "rootObject". The value of rootObject is a 5324-character object key referring to the root PBXProject object in the 54objects dictionary. 55 56In Xcode, every file used as input to a target or produced as a final product 57of a target must appear somewhere in the hierarchy rooted at the PBXGroup 58object referenced by the PBXProject's mainGroup property. A PBXGroup is 59generally represented as a folder in the Xcode application. PBXGroups can 60contain other PBXGroups as well as PBXFileReferences, which are pointers to 61actual files. 62 63Each XCTarget contains a list of build phases, represented in this module by 64the abstract base XCBuildPhase. Examples of concrete XCBuildPhase derivations 65are PBXSourcesBuildPhase and PBXFrameworksBuildPhase, which correspond to the 66"Compile Sources" and "Link Binary With Libraries" phases displayed in the 67Xcode application. Files used as input to these phases (for example, source 68files in the former case and libraries and frameworks in the latter) are 69represented by PBXBuildFile objects, referenced by elements of "files" lists 70in XCTarget objects. Each PBXBuildFile object refers to a PBXBuildFile 71object as a "weak" reference: it does not "own" the PBXBuildFile, which is 72owned by the root object's mainGroup or a descendant group. In most cases, the 73layer of indirection between an XCBuildPhase and a PBXFileReference via a 74PBXBuildFile appears extraneous, but there's actually one reason for this: 75file-specific compiler flags are added to the PBXBuildFile object so as to 76allow a single file to be a member of multiple targets while having distinct 77compiler flags for each. These flags can be modified in the Xcode applciation 78in the "Build" tab of a File Info window. 79 80When a project is open in the Xcode application, Xcode will rewrite it. As 81such, this module is careful to adhere to the formatting used by Xcode, to 82avoid insignificant changes appearing in the file when it is used in the 83Xcode application. This will keep version control repositories happy, and 84makes it possible to compare a project file used in Xcode to one generated by 85this module to determine if any significant changes were made in the 86application. 87 88Xcode has its own way of assigning 24-character identifiers to each object, 89which is not duplicated here. Because the identifier only is only generated 90once, when an object is created, and is then left unchanged, there is no need 91to attempt to duplicate Xcode's behavior in this area. The generator is free 92to select any identifier, even at random, to refer to the objects it creates, 93and Xcode will retain those identifiers and use them when subsequently 94rewriting the project file. However, the generator would choose new random 95identifiers each time the project files are generated, leading to difficulties 96comparing "used" project files to "pristine" ones produced by this module, 97and causing the appearance of changes as every object identifier is changed 98when updated projects are checked in to a version control repository. To 99mitigate this problem, this module chooses identifiers in a more deterministic 100way, by hashing a description of each object as well as its parent and ancestor 101objects. This strategy should result in minimal "shift" in IDs as successive 102generations of project files are produced. 103 104THIS MODULE 105 106This module introduces several classes, all derived from the XCObject class. 107Nearly all of the "brains" are built into the XCObject class, which understands 108how to create and modify objects, maintain the proper tree structure, compute 109identifiers, and print objects. For the most part, classes derived from 110XCObject need only provide a _schema class object, a dictionary that 111expresses what properties objects of the class may contain. 112 113Given this structure, it's possible to build a minimal project file by creating 114objects of the appropriate types and making the proper connections: 115 116 config_list = XCConfigurationList() 117 group = PBXGroup() 118 project = PBXProject({'buildConfigurationList': config_list, 119 'mainGroup': group}) 120 121With the project object set up, it can be added to an XCProjectFile object. 122XCProjectFile is a pseudo-class in the sense that it is a concrete XCObject 123subclass that does not actually correspond to a class type found in a project 124file. Rather, it is used to represent the project file's root dictionary. 125Printing an XCProjectFile will print the entire project file, including the 126full "objects" dictionary. 127 128 project_file = XCProjectFile({'rootObject': project}) 129 project_file.ComputeIDs() 130 project_file.Print() 131 132Xcode project files are always encoded in UTF-8. This module will accept 133strings of either the str class or the unicode class. Strings of class str 134are assumed to already be encoded in UTF-8. Obviously, if you're just using 135ASCII, you won't encounter difficulties because ASCII is a UTF-8 subset. 136Strings of class unicode are handled properly and encoded in UTF-8 when 137a project file is output. 138""" 139 140import functools 141import gyp.common 142import posixpath 143import re 144import struct 145import sys 146 147# hashlib is supplied as of Python 2.5 as the replacement interface for sha 148# and other secure hashes. In 2.6, sha is deprecated. Import hashlib if 149# available, avoiding a deprecation warning under 2.6. Import sha otherwise, 150# preserving 2.4 compatibility. 151try: 152 import hashlib 153 _new_sha1 = hashlib.sha1 154except ImportError: 155 import sha 156 _new_sha1 = sha.new 157 158try: 159 # basestring was removed in python3. 160 basestring 161except NameError: 162 basestring = str 163 164try: 165 # cmp was removed in python3. 166 cmp 167except NameError: 168 def cmp(a, b): 169 return (a > b) - (a < b) 170 171# See XCObject._EncodeString. This pattern is used to determine when a string 172# can be printed unquoted. Strings that match this pattern may be printed 173# unquoted. Strings that do not match must be quoted and may be further 174# transformed to be properly encoded. Note that this expression matches the 175# characters listed with "+", for 1 or more occurrences: if a string is empty, 176# it must not match this pattern, because it needs to be encoded as "". 177_unquoted = re.compile('^[A-Za-z0-9$./_]+$') 178 179# Strings that match this pattern are quoted regardless of what _unquoted says. 180# Oddly, Xcode will quote any string with a run of three or more underscores. 181_quoted = re.compile('___') 182 183# This pattern should match any character that needs to be escaped by 184# XCObject._EncodeString. See that function. 185_escaped = re.compile('[\\\\"]|[\x00-\x1f]') 186 187 188# Used by SourceTreeAndPathFromPath 189_path_leading_variable = re.compile(r'^\$\((.*?)\)(/(.*))?$') 190 191def SourceTreeAndPathFromPath(input_path): 192 """Given input_path, returns a tuple with sourceTree and path values. 193 194 Examples: 195 input_path (source_tree, output_path) 196 '$(VAR)/path' ('VAR', 'path') 197 '$(VAR)' ('VAR', None) 198 'path' (None, 'path') 199 """ 200 201 source_group_match = _path_leading_variable.match(input_path) 202 if source_group_match: 203 source_tree = source_group_match.group(1) 204 output_path = source_group_match.group(3) # This may be None. 205 else: 206 source_tree = None 207 output_path = input_path 208 209 return (source_tree, output_path) 210 211def ConvertVariablesToShellSyntax(input_string): 212 return re.sub(r'\$\((.*?)\)', '${\\1}', input_string) 213 214class XCObject(object): 215 """The abstract base of all class types used in Xcode project files. 216 217 Class variables: 218 _schema: A dictionary defining the properties of this class. The keys to 219 _schema are string property keys as used in project files. Values 220 are a list of four or five elements: 221 [ is_list, property_type, is_strong, is_required, default ] 222 is_list: True if the property described is a list, as opposed 223 to a single element. 224 property_type: The type to use as the value of the property, 225 or if is_list is True, the type to use for each 226 element of the value's list. property_type must 227 be an XCObject subclass, or one of the built-in 228 types str, int, or dict. 229 is_strong: If property_type is an XCObject subclass, is_strong 230 is True to assert that this class "owns," or serves 231 as parent, to the property value (or, if is_list is 232 True, values). is_strong must be False if 233 property_type is not an XCObject subclass. 234 is_required: True if the property is required for the class. 235 Note that is_required being True does not preclude 236 an empty string ("", in the case of property_type 237 str) or list ([], in the case of is_list True) from 238 being set for the property. 239 default: Optional. If is_requried is True, default may be set 240 to provide a default value for objects that do not supply 241 their own value. If is_required is True and default 242 is not provided, users of the class must supply their own 243 value for the property. 244 Note that although the values of the array are expressed in 245 boolean terms, subclasses provide values as integers to conserve 246 horizontal space. 247 _should_print_single_line: False in XCObject. Subclasses whose objects 248 should be written to the project file in the 249 alternate single-line format, such as 250 PBXFileReference and PBXBuildFile, should 251 set this to True. 252 _encode_transforms: Used by _EncodeString to encode unprintable characters. 253 The index into this list is the ordinal of the 254 character to transform; each value is a string 255 used to represent the character in the output. XCObject 256 provides an _encode_transforms list suitable for most 257 XCObject subclasses. 258 _alternate_encode_transforms: Provided for subclasses that wish to use 259 the alternate encoding rules. Xcode seems 260 to use these rules when printing objects in 261 single-line format. Subclasses that desire 262 this behavior should set _encode_transforms 263 to _alternate_encode_transforms. 264 _hashables: A list of XCObject subclasses that can be hashed by ComputeIDs 265 to construct this object's ID. Most classes that need custom 266 hashing behavior should do it by overriding Hashables, 267 but in some cases an object's parent may wish to push a 268 hashable value into its child, and it can do so by appending 269 to _hashables. 270 Attributes: 271 id: The object's identifier, a 24-character uppercase hexadecimal string. 272 Usually, objects being created should not set id until the entire 273 project file structure is built. At that point, UpdateIDs() should 274 be called on the root object to assign deterministic values for id to 275 each object in the tree. 276 parent: The object's parent. This is set by a parent XCObject when a child 277 object is added to it. 278 _properties: The object's property dictionary. An object's properties are 279 described by its class' _schema variable. 280 """ 281 282 _schema = {} 283 _should_print_single_line = False 284 285 # See _EncodeString. 286 _encode_transforms = [] 287 i = 0 288 while i < ord(' '): 289 _encode_transforms.append('\\U%04x' % i) 290 i = i + 1 291 _encode_transforms[7] = '\\a' 292 _encode_transforms[8] = '\\b' 293 _encode_transforms[9] = '\\t' 294 _encode_transforms[10] = '\\n' 295 _encode_transforms[11] = '\\v' 296 _encode_transforms[12] = '\\f' 297 _encode_transforms[13] = '\\n' 298 299 _alternate_encode_transforms = list(_encode_transforms) 300 _alternate_encode_transforms[9] = chr(9) 301 _alternate_encode_transforms[10] = chr(10) 302 _alternate_encode_transforms[11] = chr(11) 303 304 def __init__(self, properties=None, id=None, parent=None): 305 self.id = id 306 self.parent = parent 307 self._properties = {} 308 self._hashables = [] 309 self._SetDefaultsFromSchema() 310 self.UpdateProperties(properties) 311 312 def __repr__(self): 313 try: 314 name = self.Name() 315 except NotImplementedError: 316 return '<%s at 0x%x>' % (self.__class__.__name__, id(self)) 317 return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) 318 319 def Copy(self): 320 """Make a copy of this object. 321 322 The new object will have its own copy of lists and dicts. Any XCObject 323 objects owned by this object (marked "strong") will be copied in the 324 new object, even those found in lists. If this object has any weak 325 references to other XCObjects, the same references are added to the new 326 object without making a copy. 327 """ 328 329 that = self.__class__(id=self.id, parent=self.parent) 330 for key, value in self._properties.items(): 331 is_strong = self._schema[key][2] 332 333 if isinstance(value, XCObject): 334 if is_strong: 335 new_value = value.Copy() 336 new_value.parent = that 337 that._properties[key] = new_value 338 else: 339 that._properties[key] = value 340 elif isinstance(value, basestring) or isinstance(value, int): 341 that._properties[key] = value 342 elif isinstance(value, list): 343 if is_strong: 344 # If is_strong is True, each element is an XCObject, so it's safe to 345 # call Copy. 346 that._properties[key] = [] 347 for item in value: 348 new_item = item.Copy() 349 new_item.parent = that 350 that._properties[key].append(new_item) 351 else: 352 that._properties[key] = value[:] 353 elif isinstance(value, dict): 354 # dicts are never strong. 355 if is_strong: 356 raise TypeError('Strong dict for key ' + key + ' in ' + \ 357 self.__class__.__name__) 358 else: 359 that._properties[key] = value.copy() 360 else: 361 raise TypeError('Unexpected type ' + value.__class__.__name__ + \ 362 ' for key ' + key + ' in ' + self.__class__.__name__) 363 364 return that 365 366 def Name(self): 367 """Return the name corresponding to an object. 368 369 Not all objects necessarily need to be nameable, and not all that do have 370 a "name" property. Override as needed. 371 """ 372 373 # If the schema indicates that "name" is required, try to access the 374 # property even if it doesn't exist. This will result in a KeyError 375 # being raised for the property that should be present, which seems more 376 # appropriate than NotImplementedError in this case. 377 if 'name' in self._properties or \ 378 ('name' in self._schema and self._schema['name'][3]): 379 return self._properties['name'] 380 381 raise NotImplementedError(self.__class__.__name__ + ' must implement Name') 382 383 def Comment(self): 384 """Return a comment string for the object. 385 386 Most objects just use their name as the comment, but PBXProject uses 387 different values. 388 389 The returned comment is not escaped and does not have any comment marker 390 strings applied to it. 391 """ 392 393 return self.Name() 394 395 def Hashables(self): 396 hashables = [self.__class__.__name__] 397 398 name = self.Name() 399 if name != None: 400 hashables.append(name) 401 402 hashables.extend(self._hashables) 403 404 return hashables 405 406 def HashablesForChild(self): 407 return None 408 409 def ComputeIDs(self, recursive=True, overwrite=True, seed_hash=None): 410 """Set "id" properties deterministically. 411 412 An object's "id" property is set based on a hash of its class type and 413 name, as well as the class type and name of all ancestor objects. As 414 such, it is only advisable to call ComputeIDs once an entire project file 415 tree is built. 416 417 If recursive is True, recurse into all descendant objects and update their 418 hashes. 419 420 If overwrite is True, any existing value set in the "id" property will be 421 replaced. 422 """ 423 424 def _HashUpdate(hash, data): 425 """Update hash with data's length and contents. 426 427 If the hash were updated only with the value of data, it would be 428 possible for clowns to induce collisions by manipulating the names of 429 their objects. By adding the length, it's exceedingly less likely that 430 ID collisions will be encountered, intentionally or not. 431 """ 432 433 hash.update(struct.pack('>i', len(data))) 434 hash.update(data.encode('utf-8')) 435 436 if seed_hash is None: 437 seed_hash = _new_sha1() 438 439 hash = seed_hash.copy() 440 441 hashables = self.Hashables() 442 assert len(hashables) > 0 443 for hashable in hashables: 444 _HashUpdate(hash, hashable) 445 446 if recursive: 447 hashables_for_child = self.HashablesForChild() 448 if hashables_for_child is None: 449 child_hash = hash 450 else: 451 assert len(hashables_for_child) > 0 452 child_hash = seed_hash.copy() 453 for hashable in hashables_for_child: 454 _HashUpdate(child_hash, hashable) 455 456 for child in self.Children(): 457 child.ComputeIDs(recursive, overwrite, child_hash) 458 459 if overwrite or self.id is None: 460 # Xcode IDs are only 96 bits (24 hex characters), but a SHA-1 digest is 461 # is 160 bits. Instead of throwing out 64 bits of the digest, xor them 462 # into the portion that gets used. 463 assert hash.digest_size % 4 == 0 464 digest_int_count = hash.digest_size // 4 465 digest_ints = struct.unpack('>' + 'I' * digest_int_count, hash.digest()) 466 id_ints = [0, 0, 0] 467 for index in range(0, digest_int_count): 468 id_ints[index % 3] ^= digest_ints[index] 469 self.id = '%08X%08X%08X' % tuple(id_ints) 470 471 def EnsureNoIDCollisions(self): 472 """Verifies that no two objects have the same ID. Checks all descendants. 473 """ 474 475 ids = {} 476 descendants = self.Descendants() 477 for descendant in descendants: 478 if descendant.id in ids: 479 other = ids[descendant.id] 480 raise KeyError( 481 'Duplicate ID %s, objects "%s" and "%s" in "%s"' % \ 482 (descendant.id, str(descendant._properties), 483 str(other._properties), self._properties['rootObject'].Name())) 484 ids[descendant.id] = descendant 485 486 def Children(self): 487 """Returns a list of all of this object's owned (strong) children.""" 488 489 children = [] 490 for property, attributes in self._schema.items(): 491 (is_list, property_type, is_strong) = attributes[0:3] 492 if is_strong and property in self._properties: 493 if not is_list: 494 children.append(self._properties[property]) 495 else: 496 children.extend(self._properties[property]) 497 return children 498 499 def Descendants(self): 500 """Returns a list of all of this object's descendants, including this 501 object. 502 """ 503 504 children = self.Children() 505 descendants = [self] 506 for child in children: 507 descendants.extend(child.Descendants()) 508 return descendants 509 510 def PBXProjectAncestor(self): 511 # The base case for recursion is defined at PBXProject.PBXProjectAncestor. 512 if self.parent: 513 return self.parent.PBXProjectAncestor() 514 return None 515 516 def _EncodeComment(self, comment): 517 """Encodes a comment to be placed in the project file output, mimicing 518 Xcode behavior. 519 """ 520 521 # This mimics Xcode behavior by wrapping the comment in "/*" and "*/". If 522 # the string already contains a "*/", it is turned into "(*)/". This keeps 523 # the file writer from outputting something that would be treated as the 524 # end of a comment in the middle of something intended to be entirely a 525 # comment. 526 527 return '/* ' + comment.replace('*/', '(*)/') + ' */' 528 529 def _EncodeTransform(self, match): 530 # This function works closely with _EncodeString. It will only be called 531 # by re.sub with match.group(0) containing a character matched by the 532 # the _escaped expression. 533 char = match.group(0) 534 535 # Backslashes (\) and quotation marks (") are always replaced with a 536 # backslash-escaped version of the same. Everything else gets its 537 # replacement from the class' _encode_transforms array. 538 if char == '\\': 539 return '\\\\' 540 if char == '"': 541 return '\\"' 542 return self._encode_transforms[ord(char)] 543 544 def _EncodeString(self, value): 545 """Encodes a string to be placed in the project file output, mimicing 546 Xcode behavior. 547 """ 548 549 # Use quotation marks when any character outside of the range A-Z, a-z, 0-9, 550 # $ (dollar sign), . (period), and _ (underscore) is present. Also use 551 # quotation marks to represent empty strings. 552 # 553 # Escape " (double-quote) and \ (backslash) by preceding them with a 554 # backslash. 555 # 556 # Some characters below the printable ASCII range are encoded specially: 557 # 7 ^G BEL is encoded as "\a" 558 # 8 ^H BS is encoded as "\b" 559 # 11 ^K VT is encoded as "\v" 560 # 12 ^L NP is encoded as "\f" 561 # 127 ^? DEL is passed through as-is without escaping 562 # - In PBXFileReference and PBXBuildFile objects: 563 # 9 ^I HT is passed through as-is without escaping 564 # 10 ^J NL is passed through as-is without escaping 565 # 13 ^M CR is passed through as-is without escaping 566 # - In other objects: 567 # 9 ^I HT is encoded as "\t" 568 # 10 ^J NL is encoded as "\n" 569 # 13 ^M CR is encoded as "\n" rendering it indistinguishable from 570 # 10 ^J NL 571 # All other characters within the ASCII control character range (0 through 572 # 31 inclusive) are encoded as "\U001f" referring to the Unicode code point 573 # in hexadecimal. For example, character 14 (^N SO) is encoded as "\U000e". 574 # Characters above the ASCII range are passed through to the output encoded 575 # as UTF-8 without any escaping. These mappings are contained in the 576 # class' _encode_transforms list. 577 578 if _unquoted.search(value) and not _quoted.search(value): 579 return value 580 581 return '"' + _escaped.sub(self._EncodeTransform, value) + '"' 582 583 def _XCPrint(self, file, tabs, line): 584 file.write('\t' * tabs + line) 585 586 def _XCPrintableValue(self, tabs, value, flatten_list=False): 587 """Returns a representation of value that may be printed in a project file, 588 mimicing Xcode's behavior. 589 590 _XCPrintableValue can handle str and int values, XCObjects (which are 591 made printable by returning their id property), and list and dict objects 592 composed of any of the above types. When printing a list or dict, and 593 _should_print_single_line is False, the tabs parameter is used to determine 594 how much to indent the lines corresponding to the items in the list or 595 dict. 596 597 If flatten_list is True, single-element lists will be transformed into 598 strings. 599 """ 600 601 printable = '' 602 comment = None 603 604 if self._should_print_single_line: 605 sep = ' ' 606 element_tabs = '' 607 end_tabs = '' 608 else: 609 sep = '\n' 610 element_tabs = '\t' * (tabs + 1) 611 end_tabs = '\t' * tabs 612 613 if isinstance(value, XCObject): 614 printable += value.id 615 comment = value.Comment() 616 elif isinstance(value, str): 617 printable += self._EncodeString(value) 618 # A python3 compatible way of saying isinstance(value, unicode). 619 # basestring is str in python3 so this is equivalent to the above 620 # isinstance. Thus if it failed above it will fail here. 621 # In python2 we test against str and unicode at this point. str has already 622 # failed in the above isinstance so we test against unicode. 623 elif isinstance(value, basestring): 624 printable += self._EncodeString(value.encode('utf-8')) 625 elif isinstance(value, int): 626 printable += str(value) 627 elif isinstance(value, list): 628 if flatten_list and len(value) <= 1: 629 if len(value) == 0: 630 printable += self._EncodeString('') 631 else: 632 printable += self._EncodeString(value[0]) 633 else: 634 printable = '(' + sep 635 for item in value: 636 printable += element_tabs + \ 637 self._XCPrintableValue(tabs + 1, item, flatten_list) + \ 638 ',' + sep 639 printable += end_tabs + ')' 640 elif isinstance(value, dict): 641 printable = '{' + sep 642 for item_key, item_value in sorted(value.items()): 643 printable += element_tabs + \ 644 self._XCPrintableValue(tabs + 1, item_key, flatten_list) + ' = ' + \ 645 self._XCPrintableValue(tabs + 1, item_value, flatten_list) + ';' + \ 646 sep 647 printable += end_tabs + '}' 648 else: 649 raise TypeError("Can't make " + value.__class__.__name__ + ' printable') 650 651 if comment != None: 652 printable += ' ' + self._EncodeComment(comment) 653 654 return printable 655 656 def _XCKVPrint(self, file, tabs, key, value): 657 """Prints a key and value, members of an XCObject's _properties dictionary, 658 to file. 659 660 tabs is an int identifying the indentation level. If the class' 661 _should_print_single_line variable is True, tabs is ignored and the 662 key-value pair will be followed by a space insead of a newline. 663 """ 664 665 if self._should_print_single_line: 666 printable = '' 667 after_kv = ' ' 668 else: 669 printable = '\t' * tabs 670 after_kv = '\n' 671 672 # Xcode usually prints remoteGlobalIDString values in PBXContainerItemProxy 673 # objects without comments. Sometimes it prints them with comments, but 674 # the majority of the time, it doesn't. To avoid unnecessary changes to 675 # the project file after Xcode opens it, don't write comments for 676 # remoteGlobalIDString. This is a sucky hack and it would certainly be 677 # cleaner to extend the schema to indicate whether or not a comment should 678 # be printed, but since this is the only case where the problem occurs and 679 # Xcode itself can't seem to make up its mind, the hack will suffice. 680 # 681 # Also see PBXContainerItemProxy._schema['remoteGlobalIDString']. 682 if key == 'remoteGlobalIDString' and isinstance(self, 683 PBXContainerItemProxy): 684 value_to_print = value.id 685 else: 686 value_to_print = value 687 688 # PBXBuildFile's settings property is represented in the output as a dict, 689 # but a hack here has it represented as a string. Arrange to strip off the 690 # quotes so that it shows up in the output as expected. 691 if key == 'settings' and isinstance(self, PBXBuildFile): 692 strip_value_quotes = True 693 else: 694 strip_value_quotes = False 695 696 # In another one-off, let's set flatten_list on buildSettings properties 697 # of XCBuildConfiguration objects, because that's how Xcode treats them. 698 if key == 'buildSettings' and isinstance(self, XCBuildConfiguration): 699 flatten_list = True 700 else: 701 flatten_list = False 702 703 try: 704 printable_key = self._XCPrintableValue(tabs, key, flatten_list) 705 printable_value = self._XCPrintableValue(tabs, value_to_print, 706 flatten_list) 707 if strip_value_quotes and len(printable_value) > 1 and \ 708 printable_value[0] == '"' and printable_value[-1] == '"': 709 printable_value = printable_value[1:-1] 710 printable += printable_key + ' = ' + printable_value + ';' + after_kv 711 except TypeError as e: 712 gyp.common.ExceptionAppend(e, 713 'while printing key "%s"' % key) 714 raise 715 716 self._XCPrint(file, 0, printable) 717 718 def Print(self, file=sys.stdout): 719 """Prints a reprentation of this object to file, adhering to Xcode output 720 formatting. 721 """ 722 723 self.VerifyHasRequiredProperties() 724 725 if self._should_print_single_line: 726 # When printing an object in a single line, Xcode doesn't put any space 727 # between the beginning of a dictionary (or presumably a list) and the 728 # first contained item, so you wind up with snippets like 729 # ...CDEF = {isa = PBXFileReference; fileRef = 0123... 730 # If it were me, I would have put a space in there after the opening 731 # curly, but I guess this is just another one of those inconsistencies 732 # between how Xcode prints PBXFileReference and PBXBuildFile objects as 733 # compared to other objects. Mimic Xcode's behavior here by using an 734 # empty string for sep. 735 sep = '' 736 end_tabs = 0 737 else: 738 sep = '\n' 739 end_tabs = 2 740 741 # Start the object. For example, '\t\tPBXProject = {\n'. 742 self._XCPrint(file, 2, self._XCPrintableValue(2, self) + ' = {' + sep) 743 744 # "isa" isn't in the _properties dictionary, it's an intrinsic property 745 # of the class which the object belongs to. Xcode always outputs "isa" 746 # as the first element of an object dictionary. 747 self._XCKVPrint(file, 3, 'isa', self.__class__.__name__) 748 749 # The remaining elements of an object dictionary are sorted alphabetically. 750 for property, value in sorted(self._properties.items()): 751 self._XCKVPrint(file, 3, property, value) 752 753 # End the object. 754 self._XCPrint(file, end_tabs, '};\n') 755 756 def UpdateProperties(self, properties, do_copy=False): 757 """Merge the supplied properties into the _properties dictionary. 758 759 The input properties must adhere to the class schema or a KeyError or 760 TypeError exception will be raised. If adding an object of an XCObject 761 subclass and the schema indicates a strong relationship, the object's 762 parent will be set to this object. 763 764 If do_copy is True, then lists, dicts, strong-owned XCObjects, and 765 strong-owned XCObjects in lists will be copied instead of having their 766 references added. 767 """ 768 769 if properties is None: 770 return 771 772 for property, value in properties.items(): 773 # Make sure the property is in the schema. 774 if not property in self._schema: 775 raise KeyError(property + ' not in ' + self.__class__.__name__) 776 777 # Make sure the property conforms to the schema. 778 (is_list, property_type, is_strong) = self._schema[property][0:3] 779 if is_list: 780 if value.__class__ != list: 781 raise TypeError( 782 property + ' of ' + self.__class__.__name__ + \ 783 ' must be list, not ' + value.__class__.__name__) 784 for item in value: 785 if not isinstance(item, property_type) and \ 786 not (isinstance(item, basestring) and property_type == str): 787 # Accept unicode where str is specified. str is treated as 788 # UTF-8-encoded. 789 raise TypeError( 790 'item of ' + property + ' of ' + self.__class__.__name__ + \ 791 ' must be ' + property_type.__name__ + ', not ' + \ 792 item.__class__.__name__) 793 elif not isinstance(value, property_type) and \ 794 not (isinstance(value, basestring) and property_type == str): 795 # Accept unicode where str is specified. str is treated as 796 # UTF-8-encoded. 797 raise TypeError( 798 property + ' of ' + self.__class__.__name__ + ' must be ' + \ 799 property_type.__name__ + ', not ' + value.__class__.__name__) 800 801 # Checks passed, perform the assignment. 802 if do_copy: 803 if isinstance(value, XCObject): 804 if is_strong: 805 self._properties[property] = value.Copy() 806 else: 807 self._properties[property] = value 808 elif isinstance(value, basestring) or isinstance(value, int): 809 self._properties[property] = value 810 elif isinstance(value, list): 811 if is_strong: 812 # If is_strong is True, each element is an XCObject, so it's safe 813 # to call Copy. 814 self._properties[property] = [] 815 for item in value: 816 self._properties[property].append(item.Copy()) 817 else: 818 self._properties[property] = value[:] 819 elif isinstance(value, dict): 820 self._properties[property] = value.copy() 821 else: 822 raise TypeError("Don't know how to copy a " + \ 823 value.__class__.__name__ + ' object for ' + \ 824 property + ' in ' + self.__class__.__name__) 825 else: 826 self._properties[property] = value 827 828 # Set up the child's back-reference to this object. Don't use |value| 829 # any more because it may not be right if do_copy is true. 830 if is_strong: 831 if not is_list: 832 self._properties[property].parent = self 833 else: 834 for item in self._properties[property]: 835 item.parent = self 836 837 def HasProperty(self, key): 838 return key in self._properties 839 840 def GetProperty(self, key): 841 return self._properties[key] 842 843 def SetProperty(self, key, value): 844 self.UpdateProperties({key: value}) 845 846 def DelProperty(self, key): 847 if key in self._properties: 848 del self._properties[key] 849 850 def AppendProperty(self, key, value): 851 # TODO(mark): Support ExtendProperty too (and make this call that)? 852 853 # Schema validation. 854 if not key in self._schema: 855 raise KeyError(key + ' not in ' + self.__class__.__name__) 856 857 (is_list, property_type, is_strong) = self._schema[key][0:3] 858 if not is_list: 859 raise TypeError(key + ' of ' + self.__class__.__name__ + ' must be list') 860 if not isinstance(value, property_type): 861 raise TypeError('item of ' + key + ' of ' + self.__class__.__name__ + \ 862 ' must be ' + property_type.__name__ + ', not ' + \ 863 value.__class__.__name__) 864 865 # If the property doesn't exist yet, create a new empty list to receive the 866 # item. 867 if not key in self._properties: 868 self._properties[key] = [] 869 870 # Set up the ownership link. 871 if is_strong: 872 value.parent = self 873 874 # Store the item. 875 self._properties[key].append(value) 876 877 def VerifyHasRequiredProperties(self): 878 """Ensure that all properties identified as required by the schema are 879 set. 880 """ 881 882 # TODO(mark): A stronger verification mechanism is needed. Some 883 # subclasses need to perform validation beyond what the schema can enforce. 884 for property, attributes in self._schema.items(): 885 (is_list, property_type, is_strong, is_required) = attributes[0:4] 886 if is_required and not property in self._properties: 887 raise KeyError(self.__class__.__name__ + ' requires ' + property) 888 889 def _SetDefaultsFromSchema(self): 890 """Assign object default values according to the schema. This will not 891 overwrite properties that have already been set.""" 892 893 defaults = {} 894 for property, attributes in self._schema.items(): 895 (is_list, property_type, is_strong, is_required) = attributes[0:4] 896 if is_required and len(attributes) >= 5 and \ 897 not property in self._properties: 898 default = attributes[4] 899 900 defaults[property] = default 901 902 if len(defaults) > 0: 903 # Use do_copy=True so that each new object gets its own copy of strong 904 # objects, lists, and dicts. 905 self.UpdateProperties(defaults, do_copy=True) 906 907 908class XCHierarchicalElement(XCObject): 909 """Abstract base for PBXGroup and PBXFileReference. Not represented in a 910 project file.""" 911 912 # TODO(mark): Do name and path belong here? Probably so. 913 # If path is set and name is not, name may have a default value. Name will 914 # be set to the basename of path, if the basename of path is different from 915 # the full value of path. If path is already just a leaf name, name will 916 # not be set. 917 _schema = XCObject._schema.copy() 918 _schema.update({ 919 'comments': [0, str, 0, 0], 920 'fileEncoding': [0, str, 0, 0], 921 'includeInIndex': [0, int, 0, 0], 922 'indentWidth': [0, int, 0, 0], 923 'lineEnding': [0, int, 0, 0], 924 'sourceTree': [0, str, 0, 1, '<group>'], 925 'tabWidth': [0, int, 0, 0], 926 'usesTabs': [0, int, 0, 0], 927 'wrapsLines': [0, int, 0, 0], 928 }) 929 930 def __init__(self, properties=None, id=None, parent=None): 931 # super 932 XCObject.__init__(self, properties, id, parent) 933 if 'path' in self._properties and not 'name' in self._properties: 934 path = self._properties['path'] 935 name = posixpath.basename(path) 936 if name != '' and path != name: 937 self.SetProperty('name', name) 938 939 if 'path' in self._properties and \ 940 (not 'sourceTree' in self._properties or \ 941 self._properties['sourceTree'] == '<group>'): 942 # If the pathname begins with an Xcode variable like "$(SDKROOT)/", take 943 # the variable out and make the path be relative to that variable by 944 # assigning the variable name as the sourceTree. 945 (source_tree, path) = SourceTreeAndPathFromPath(self._properties['path']) 946 if source_tree != None: 947 self._properties['sourceTree'] = source_tree 948 if path != None: 949 self._properties['path'] = path 950 if source_tree != None and path is None and \ 951 not 'name' in self._properties: 952 # The path was of the form "$(SDKROOT)" with no path following it. 953 # This object is now relative to that variable, so it has no path 954 # attribute of its own. It does, however, keep a name. 955 del self._properties['path'] 956 self._properties['name'] = source_tree 957 958 def Name(self): 959 if 'name' in self._properties: 960 return self._properties['name'] 961 elif 'path' in self._properties: 962 return self._properties['path'] 963 else: 964 # This happens in the case of the root PBXGroup. 965 return None 966 967 def Hashables(self): 968 """Custom hashables for XCHierarchicalElements. 969 970 XCHierarchicalElements are special. Generally, their hashes shouldn't 971 change if the paths don't change. The normal XCObject implementation of 972 Hashables adds a hashable for each object, which means that if 973 the hierarchical structure changes (possibly due to changes caused when 974 TakeOverOnlyChild runs and encounters slight changes in the hierarchy), 975 the hashes will change. For example, if a project file initially contains 976 a/b/f1 and a/b becomes collapsed into a/b, f1 will have a single parent 977 a/b. If someone later adds a/f2 to the project file, a/b can no longer be 978 collapsed, and f1 winds up with parent b and grandparent a. That would 979 be sufficient to change f1's hash. 980 981 To counteract this problem, hashables for all XCHierarchicalElements except 982 for the main group (which has neither a name nor a path) are taken to be 983 just the set of path components. Because hashables are inherited from 984 parents, this provides assurance that a/b/f1 has the same set of hashables 985 whether its parent is b or a/b. 986 987 The main group is a special case. As it is permitted to have no name or 988 path, it is permitted to use the standard XCObject hash mechanism. This 989 is not considered a problem because there can be only one main group. 990 """ 991 992 if self == self.PBXProjectAncestor()._properties['mainGroup']: 993 # super 994 return XCObject.Hashables(self) 995 996 hashables = [] 997 998 # Put the name in first, ensuring that if TakeOverOnlyChild collapses 999 # children into a top-level group like "Source", the name always goes 1000 # into the list of hashables without interfering with path components. 1001 if 'name' in self._properties: 1002 # Make it less likely for people to manipulate hashes by following the 1003 # pattern of always pushing an object type value onto the list first. 1004 hashables.append(self.__class__.__name__ + '.name') 1005 hashables.append(self._properties['name']) 1006 1007 # NOTE: This still has the problem that if an absolute path is encountered, 1008 # including paths with a sourceTree, they'll still inherit their parents' 1009 # hashables, even though the paths aren't relative to their parents. This 1010 # is not expected to be much of a problem in practice. 1011 path = self.PathFromSourceTreeAndPath() 1012 if path != None: 1013 components = path.split(posixpath.sep) 1014 for component in components: 1015 hashables.append(self.__class__.__name__ + '.path') 1016 hashables.append(component) 1017 1018 hashables.extend(self._hashables) 1019 1020 return hashables 1021 1022 def Compare(self, other): 1023 # Allow comparison of these types. PBXGroup has the highest sort rank; 1024 # PBXVariantGroup is treated as equal to PBXFileReference. 1025 valid_class_types = { 1026 PBXFileReference: 'file', 1027 PBXGroup: 'group', 1028 PBXVariantGroup: 'file', 1029 } 1030 self_type = valid_class_types[self.__class__] 1031 other_type = valid_class_types[other.__class__] 1032 1033 if self_type == other_type: 1034 # If the two objects are of the same sort rank, compare their names. 1035 return cmp(self.Name(), other.Name()) 1036 1037 # Otherwise, sort groups before everything else. 1038 if self_type == 'group': 1039 return -1 1040 return 1 1041 1042 def CompareRootGroup(self, other): 1043 # This function should be used only to compare direct children of the 1044 # containing PBXProject's mainGroup. These groups should appear in the 1045 # listed order. 1046 # TODO(mark): "Build" is used by gyp.generator.xcode, perhaps the 1047 # generator should have a way of influencing this list rather than having 1048 # to hardcode for the generator here. 1049 order = ['Source', 'Intermediates', 'Projects', 'Frameworks', 'Products', 1050 'Build'] 1051 1052 # If the groups aren't in the listed order, do a name comparison. 1053 # Otherwise, groups in the listed order should come before those that 1054 # aren't. 1055 self_name = self.Name() 1056 other_name = other.Name() 1057 self_in = isinstance(self, PBXGroup) and self_name in order 1058 other_in = isinstance(self, PBXGroup) and other_name in order 1059 if not self_in and not other_in: 1060 return self.Compare(other) 1061 if self_name in order and not other_name in order: 1062 return -1 1063 if other_name in order and not self_name in order: 1064 return 1 1065 1066 # If both groups are in the listed order, go by the defined order. 1067 self_index = order.index(self_name) 1068 other_index = order.index(other_name) 1069 if self_index < other_index: 1070 return -1 1071 if self_index > other_index: 1072 return 1 1073 return 0 1074 1075 def PathFromSourceTreeAndPath(self): 1076 # Turn the object's sourceTree and path properties into a single flat 1077 # string of a form comparable to the path parameter. If there's a 1078 # sourceTree property other than "<group>", wrap it in $(...) for the 1079 # comparison. 1080 components = [] 1081 if self._properties['sourceTree'] != '<group>': 1082 components.append('$(' + self._properties['sourceTree'] + ')') 1083 if 'path' in self._properties: 1084 components.append(self._properties['path']) 1085 1086 if len(components) > 0: 1087 return posixpath.join(*components) 1088 1089 return None 1090 1091 def FullPath(self): 1092 # Returns a full path to self relative to the project file, or relative 1093 # to some other source tree. Start with self, and walk up the chain of 1094 # parents prepending their paths, if any, until no more parents are 1095 # available (project-relative path) or until a path relative to some 1096 # source tree is found. 1097 xche = self 1098 path = None 1099 while isinstance(xche, XCHierarchicalElement) and \ 1100 (path is None or \ 1101 (not path.startswith('/') and not path.startswith('$'))): 1102 this_path = xche.PathFromSourceTreeAndPath() 1103 if this_path != None and path != None: 1104 path = posixpath.join(this_path, path) 1105 elif this_path != None: 1106 path = this_path 1107 xche = xche.parent 1108 1109 return path 1110 1111 1112class PBXGroup(XCHierarchicalElement): 1113 """ 1114 Attributes: 1115 _children_by_path: Maps pathnames of children of this PBXGroup to the 1116 actual child XCHierarchicalElement objects. 1117 _variant_children_by_name_and_path: Maps (name, path) tuples of 1118 PBXVariantGroup children to the actual child PBXVariantGroup objects. 1119 """ 1120 1121 _schema = XCHierarchicalElement._schema.copy() 1122 _schema.update({ 1123 'children': [1, XCHierarchicalElement, 1, 1, []], 1124 'name': [0, str, 0, 0], 1125 'path': [0, str, 0, 0], 1126 }) 1127 1128 def __init__(self, properties=None, id=None, parent=None): 1129 # super 1130 XCHierarchicalElement.__init__(self, properties, id, parent) 1131 self._children_by_path = {} 1132 self._variant_children_by_name_and_path = {} 1133 for child in self._properties.get('children', []): 1134 self._AddChildToDicts(child) 1135 1136 def Hashables(self): 1137 # super 1138 hashables = XCHierarchicalElement.Hashables(self) 1139 1140 # It is not sufficient to just rely on name and parent to build a unique 1141 # hashable : a node could have two child PBXGroup sharing a common name. 1142 # To add entropy the hashable is enhanced with the names of all its 1143 # children. 1144 for child in self._properties.get('children', []): 1145 child_name = child.Name() 1146 if child_name != None: 1147 hashables.append(child_name) 1148 1149 return hashables 1150 1151 def HashablesForChild(self): 1152 # To avoid a circular reference the hashables used to compute a child id do 1153 # not include the child names. 1154 return XCHierarchicalElement.Hashables(self) 1155 1156 def _AddChildToDicts(self, child): 1157 # Sets up this PBXGroup object's dicts to reference the child properly. 1158 child_path = child.PathFromSourceTreeAndPath() 1159 if child_path: 1160 if child_path in self._children_by_path: 1161 raise ValueError('Found multiple children with path ' + child_path) 1162 self._children_by_path[child_path] = child 1163 1164 if isinstance(child, PBXVariantGroup): 1165 child_name = child._properties.get('name', None) 1166 key = (child_name, child_path) 1167 if key in self._variant_children_by_name_and_path: 1168 raise ValueError('Found multiple PBXVariantGroup children with ' + \ 1169 'name ' + str(child_name) + ' and path ' + \ 1170 str(child_path)) 1171 self._variant_children_by_name_and_path[key] = child 1172 1173 def AppendChild(self, child): 1174 # Callers should use this instead of calling 1175 # AppendProperty('children', child) directly because this function 1176 # maintains the group's dicts. 1177 self.AppendProperty('children', child) 1178 self._AddChildToDicts(child) 1179 1180 def GetChildByName(self, name): 1181 # This is not currently optimized with a dict as GetChildByPath is because 1182 # it has few callers. Most callers probably want GetChildByPath. This 1183 # function is only useful to get children that have names but no paths, 1184 # which is rare. The children of the main group ("Source", "Products", 1185 # etc.) is pretty much the only case where this likely to come up. 1186 # 1187 # TODO(mark): Maybe this should raise an error if more than one child is 1188 # present with the same name. 1189 if not 'children' in self._properties: 1190 return None 1191 1192 for child in self._properties['children']: 1193 if child.Name() == name: 1194 return child 1195 1196 return None 1197 1198 def GetChildByPath(self, path): 1199 if not path: 1200 return None 1201 1202 if path in self._children_by_path: 1203 return self._children_by_path[path] 1204 1205 return None 1206 1207 def GetChildByRemoteObject(self, remote_object): 1208 # This method is a little bit esoteric. Given a remote_object, which 1209 # should be a PBXFileReference in another project file, this method will 1210 # return this group's PBXReferenceProxy object serving as a local proxy 1211 # for the remote PBXFileReference. 1212 # 1213 # This function might benefit from a dict optimization as GetChildByPath 1214 # for some workloads, but profiling shows that it's not currently a 1215 # problem. 1216 if not 'children' in self._properties: 1217 return None 1218 1219 for child in self._properties['children']: 1220 if not isinstance(child, PBXReferenceProxy): 1221 continue 1222 1223 container_proxy = child._properties['remoteRef'] 1224 if container_proxy._properties['remoteGlobalIDString'] == remote_object: 1225 return child 1226 1227 return None 1228 1229 def AddOrGetFileByPath(self, path, hierarchical): 1230 """Returns an existing or new file reference corresponding to path. 1231 1232 If hierarchical is True, this method will create or use the necessary 1233 hierarchical group structure corresponding to path. Otherwise, it will 1234 look in and create an item in the current group only. 1235 1236 If an existing matching reference is found, it is returned, otherwise, a 1237 new one will be created, added to the correct group, and returned. 1238 1239 If path identifies a directory by virtue of carrying a trailing slash, 1240 this method returns a PBXFileReference of "folder" type. If path 1241 identifies a variant, by virtue of it identifying a file inside a directory 1242 with an ".lproj" extension, this method returns a PBXVariantGroup 1243 containing the variant named by path, and possibly other variants. For 1244 all other paths, a "normal" PBXFileReference will be returned. 1245 """ 1246 1247 # Adding or getting a directory? Directories end with a trailing slash. 1248 is_dir = False 1249 if path.endswith('/'): 1250 is_dir = True 1251 path = posixpath.normpath(path) 1252 if is_dir: 1253 path = path + '/' 1254 1255 # Adding or getting a variant? Variants are files inside directories 1256 # with an ".lproj" extension. Xcode uses variants for localization. For 1257 # a variant path/to/Language.lproj/MainMenu.nib, put a variant group named 1258 # MainMenu.nib inside path/to, and give it a variant named Language. In 1259 # this example, grandparent would be set to path/to and parent_root would 1260 # be set to Language. 1261 variant_name = None 1262 parent = posixpath.dirname(path) 1263 grandparent = posixpath.dirname(parent) 1264 parent_basename = posixpath.basename(parent) 1265 (parent_root, parent_ext) = posixpath.splitext(parent_basename) 1266 if parent_ext == '.lproj': 1267 variant_name = parent_root 1268 if grandparent == '': 1269 grandparent = None 1270 1271 # Putting a directory inside a variant group is not currently supported. 1272 assert not is_dir or variant_name is None 1273 1274 path_split = path.split(posixpath.sep) 1275 if len(path_split) == 1 or \ 1276 ((is_dir or variant_name != None) and len(path_split) == 2) or \ 1277 not hierarchical: 1278 # The PBXFileReference or PBXVariantGroup will be added to or gotten from 1279 # this PBXGroup, no recursion necessary. 1280 if variant_name is None: 1281 # Add or get a PBXFileReference. 1282 file_ref = self.GetChildByPath(path) 1283 if file_ref != None: 1284 assert file_ref.__class__ == PBXFileReference 1285 else: 1286 file_ref = PBXFileReference({'path': path}) 1287 self.AppendChild(file_ref) 1288 else: 1289 # Add or get a PBXVariantGroup. The variant group name is the same 1290 # as the basename (MainMenu.nib in the example above). grandparent 1291 # specifies the path to the variant group itself, and path_split[-2:] 1292 # is the path of the specific variant relative to its group. 1293 variant_group_name = posixpath.basename(path) 1294 variant_group_ref = self.AddOrGetVariantGroupByNameAndPath( 1295 variant_group_name, grandparent) 1296 variant_path = posixpath.sep.join(path_split[-2:]) 1297 variant_ref = variant_group_ref.GetChildByPath(variant_path) 1298 if variant_ref != None: 1299 assert variant_ref.__class__ == PBXFileReference 1300 else: 1301 variant_ref = PBXFileReference({'name': variant_name, 1302 'path': variant_path}) 1303 variant_group_ref.AppendChild(variant_ref) 1304 # The caller is interested in the variant group, not the specific 1305 # variant file. 1306 file_ref = variant_group_ref 1307 return file_ref 1308 else: 1309 # Hierarchical recursion. Add or get a PBXGroup corresponding to the 1310 # outermost path component, and then recurse into it, chopping off that 1311 # path component. 1312 next_dir = path_split[0] 1313 group_ref = self.GetChildByPath(next_dir) 1314 if group_ref != None: 1315 assert group_ref.__class__ == PBXGroup 1316 else: 1317 group_ref = PBXGroup({'path': next_dir}) 1318 self.AppendChild(group_ref) 1319 return group_ref.AddOrGetFileByPath(posixpath.sep.join(path_split[1:]), 1320 hierarchical) 1321 1322 def AddOrGetVariantGroupByNameAndPath(self, name, path): 1323 """Returns an existing or new PBXVariantGroup for name and path. 1324 1325 If a PBXVariantGroup identified by the name and path arguments is already 1326 present as a child of this object, it is returned. Otherwise, a new 1327 PBXVariantGroup with the correct properties is created, added as a child, 1328 and returned. 1329 1330 This method will generally be called by AddOrGetFileByPath, which knows 1331 when to create a variant group based on the structure of the pathnames 1332 passed to it. 1333 """ 1334 1335 key = (name, path) 1336 if key in self._variant_children_by_name_and_path: 1337 variant_group_ref = self._variant_children_by_name_and_path[key] 1338 assert variant_group_ref.__class__ == PBXVariantGroup 1339 return variant_group_ref 1340 1341 variant_group_properties = {'name': name} 1342 if path != None: 1343 variant_group_properties['path'] = path 1344 variant_group_ref = PBXVariantGroup(variant_group_properties) 1345 self.AppendChild(variant_group_ref) 1346 1347 return variant_group_ref 1348 1349 def TakeOverOnlyChild(self, recurse=False): 1350 """If this PBXGroup has only one child and it's also a PBXGroup, take 1351 it over by making all of its children this object's children. 1352 1353 This function will continue to take over only children when those children 1354 are groups. If there are three PBXGroups representing a, b, and c, with 1355 c inside b and b inside a, and a and b have no other children, this will 1356 result in a taking over both b and c, forming a PBXGroup for a/b/c. 1357 1358 If recurse is True, this function will recurse into children and ask them 1359 to collapse themselves by taking over only children as well. Assuming 1360 an example hierarchy with files at a/b/c/d1, a/b/c/d2, and a/b/c/d3/e/f 1361 (d1, d2, and f are files, the rest are groups), recursion will result in 1362 a group for a/b/c containing a group for d3/e. 1363 """ 1364 1365 # At this stage, check that child class types are PBXGroup exactly, 1366 # instead of using isinstance. The only subclass of PBXGroup, 1367 # PBXVariantGroup, should not participate in reparenting in the same way: 1368 # reparenting by merging different object types would be wrong. 1369 while len(self._properties['children']) == 1 and \ 1370 self._properties['children'][0].__class__ == PBXGroup: 1371 # Loop to take over the innermost only-child group possible. 1372 1373 child = self._properties['children'][0] 1374 1375 # Assume the child's properties, including its children. Save a copy 1376 # of this object's old properties, because they'll still be needed. 1377 # This object retains its existing id and parent attributes. 1378 old_properties = self._properties 1379 self._properties = child._properties 1380 self._children_by_path = child._children_by_path 1381 1382 if not 'sourceTree' in self._properties or \ 1383 self._properties['sourceTree'] == '<group>': 1384 # The child was relative to its parent. Fix up the path. Note that 1385 # children with a sourceTree other than "<group>" are not relative to 1386 # their parents, so no path fix-up is needed in that case. 1387 if 'path' in old_properties: 1388 if 'path' in self._properties: 1389 # Both the original parent and child have paths set. 1390 self._properties['path'] = posixpath.join(old_properties['path'], 1391 self._properties['path']) 1392 else: 1393 # Only the original parent has a path, use it. 1394 self._properties['path'] = old_properties['path'] 1395 if 'sourceTree' in old_properties: 1396 # The original parent had a sourceTree set, use it. 1397 self._properties['sourceTree'] = old_properties['sourceTree'] 1398 1399 # If the original parent had a name set, keep using it. If the original 1400 # parent didn't have a name but the child did, let the child's name 1401 # live on. If the name attribute seems unnecessary now, get rid of it. 1402 if 'name' in old_properties and old_properties['name'] != None and \ 1403 old_properties['name'] != self.Name(): 1404 self._properties['name'] = old_properties['name'] 1405 if 'name' in self._properties and 'path' in self._properties and \ 1406 self._properties['name'] == self._properties['path']: 1407 del self._properties['name'] 1408 1409 # Notify all children of their new parent. 1410 for child in self._properties['children']: 1411 child.parent = self 1412 1413 # If asked to recurse, recurse. 1414 if recurse: 1415 for child in self._properties['children']: 1416 if child.__class__ == PBXGroup: 1417 child.TakeOverOnlyChild(recurse) 1418 1419 def SortGroup(self): 1420 self._properties['children'] = \ 1421 sorted(self._properties['children'], 1422 key=functools.cmp_to_key(XCHierarchicalElement.Compare)) 1423 1424 # Recurse. 1425 for child in self._properties['children']: 1426 if isinstance(child, PBXGroup): 1427 child.SortGroup() 1428 1429 1430class XCFileLikeElement(XCHierarchicalElement): 1431 # Abstract base for objects that can be used as the fileRef property of 1432 # PBXBuildFile. 1433 1434 def PathHashables(self): 1435 # A PBXBuildFile that refers to this object will call this method to 1436 # obtain additional hashables specific to this XCFileLikeElement. Don't 1437 # just use this object's hashables, they're not specific and unique enough 1438 # on their own (without access to the parent hashables.) Instead, provide 1439 # hashables that identify this object by path by getting its hashables as 1440 # well as the hashables of ancestor XCHierarchicalElement objects. 1441 1442 hashables = [] 1443 xche = self 1444 while xche != None and isinstance(xche, XCHierarchicalElement): 1445 xche_hashables = xche.Hashables() 1446 for index, xche_hashable in enumerate(xche_hashables): 1447 hashables.insert(index, xche_hashable) 1448 xche = xche.parent 1449 return hashables 1450 1451 1452class XCContainerPortal(XCObject): 1453 # Abstract base for objects that can be used as the containerPortal property 1454 # of PBXContainerItemProxy. 1455 pass 1456 1457 1458class XCRemoteObject(XCObject): 1459 # Abstract base for objects that can be used as the remoteGlobalIDString 1460 # property of PBXContainerItemProxy. 1461 pass 1462 1463 1464class PBXFileReference(XCFileLikeElement, XCContainerPortal, XCRemoteObject): 1465 _schema = XCFileLikeElement._schema.copy() 1466 _schema.update({ 1467 'explicitFileType': [0, str, 0, 0], 1468 'lastKnownFileType': [0, str, 0, 0], 1469 'name': [0, str, 0, 0], 1470 'path': [0, str, 0, 1], 1471 }) 1472 1473 # Weird output rules for PBXFileReference. 1474 _should_print_single_line = True 1475 # super 1476 _encode_transforms = XCFileLikeElement._alternate_encode_transforms 1477 1478 def __init__(self, properties=None, id=None, parent=None): 1479 # super 1480 XCFileLikeElement.__init__(self, properties, id, parent) 1481 if 'path' in self._properties and self._properties['path'].endswith('/'): 1482 self._properties['path'] = self._properties['path'][:-1] 1483 is_dir = True 1484 else: 1485 is_dir = False 1486 1487 if 'path' in self._properties and \ 1488 not 'lastKnownFileType' in self._properties and \ 1489 not 'explicitFileType' in self._properties: 1490 # TODO(mark): This is the replacement for a replacement for a quick hack. 1491 # It is no longer incredibly sucky, but this list needs to be extended. 1492 extension_map = { 1493 'a': 'archive.ar', 1494 'app': 'wrapper.application', 1495 'bdic': 'file', 1496 'bundle': 'wrapper.cfbundle', 1497 'c': 'sourcecode.c.c', 1498 'cc': 'sourcecode.cpp.cpp', 1499 'cpp': 'sourcecode.cpp.cpp', 1500 'css': 'text.css', 1501 'cxx': 'sourcecode.cpp.cpp', 1502 'dart': 'sourcecode', 1503 'dylib': 'compiled.mach-o.dylib', 1504 'framework': 'wrapper.framework', 1505 'gyp': 'sourcecode', 1506 'gypi': 'sourcecode', 1507 'h': 'sourcecode.c.h', 1508 'hxx': 'sourcecode.cpp.h', 1509 'icns': 'image.icns', 1510 'java': 'sourcecode.java', 1511 'js': 'sourcecode.javascript', 1512 'kext': 'wrapper.kext', 1513 'm': 'sourcecode.c.objc', 1514 'mm': 'sourcecode.cpp.objcpp', 1515 'nib': 'wrapper.nib', 1516 'o': 'compiled.mach-o.objfile', 1517 'pdf': 'image.pdf', 1518 'pl': 'text.script.perl', 1519 'plist': 'text.plist.xml', 1520 'pm': 'text.script.perl', 1521 'png': 'image.png', 1522 'py': 'text.script.python', 1523 'r': 'sourcecode.rez', 1524 'rez': 'sourcecode.rez', 1525 's': 'sourcecode.asm', 1526 'storyboard': 'file.storyboard', 1527 'strings': 'text.plist.strings', 1528 'swift': 'sourcecode.swift', 1529 'ttf': 'file', 1530 'xcassets': 'folder.assetcatalog', 1531 'xcconfig': 'text.xcconfig', 1532 'xcdatamodel': 'wrapper.xcdatamodel', 1533 'xcdatamodeld':'wrapper.xcdatamodeld', 1534 'xib': 'file.xib', 1535 'y': 'sourcecode.yacc', 1536 'tbd': 'sourcecode.text-based-dylib-definition', 1537 } 1538 1539 prop_map = { 1540 'dart': 'explicitFileType', 1541 'gyp': 'explicitFileType', 1542 'gypi': 'explicitFileType', 1543 } 1544 1545 if is_dir: 1546 file_type = 'folder' 1547 prop_name = 'lastKnownFileType' 1548 else: 1549 basename = posixpath.basename(self._properties['path']) 1550 (root, ext) = posixpath.splitext(basename) 1551 # Check the map using a lowercase extension. 1552 # TODO(mark): Maybe it should try with the original case first and fall 1553 # back to lowercase, in case there are any instances where case 1554 # matters. There currently aren't. 1555 if ext != '': 1556 ext = ext[1:].lower() 1557 1558 # TODO(mark): "text" is the default value, but "file" is appropriate 1559 # for unrecognized files not containing text. Xcode seems to choose 1560 # based on content. 1561 file_type = extension_map.get(ext, 'text') 1562 prop_name = prop_map.get(ext, 'lastKnownFileType') 1563 1564 self._properties[prop_name] = file_type 1565 1566 1567class PBXVariantGroup(PBXGroup, XCFileLikeElement): 1568 """PBXVariantGroup is used by Xcode to represent localizations.""" 1569 # No additions to the schema relative to PBXGroup. 1570 pass 1571 1572 1573# PBXReferenceProxy is also an XCFileLikeElement subclass. It is defined below 1574# because it uses PBXContainerItemProxy, defined below. 1575 1576 1577class XCBuildConfiguration(XCObject): 1578 _schema = XCObject._schema.copy() 1579 _schema.update({ 1580 'baseConfigurationReference': [0, PBXFileReference, 0, 0], 1581 'buildSettings': [0, dict, 0, 1, {}], 1582 'name': [0, str, 0, 1], 1583 }) 1584 1585 def HasBuildSetting(self, key): 1586 return key in self._properties['buildSettings'] 1587 1588 def GetBuildSetting(self, key): 1589 return self._properties['buildSettings'][key] 1590 1591 def SetBuildSetting(self, key, value): 1592 # TODO(mark): If a list, copy? 1593 self._properties['buildSettings'][key] = value 1594 1595 def AppendBuildSetting(self, key, value): 1596 if not key in self._properties['buildSettings']: 1597 self._properties['buildSettings'][key] = [] 1598 self._properties['buildSettings'][key].append(value) 1599 1600 def DelBuildSetting(self, key): 1601 if key in self._properties['buildSettings']: 1602 del self._properties['buildSettings'][key] 1603 1604 def SetBaseConfiguration(self, value): 1605 self._properties['baseConfigurationReference'] = value 1606 1607class XCConfigurationList(XCObject): 1608 # _configs is the default list of configurations. 1609 _configs = [ XCBuildConfiguration({'name': 'Debug'}), 1610 XCBuildConfiguration({'name': 'Release'}) ] 1611 1612 _schema = XCObject._schema.copy() 1613 _schema.update({ 1614 'buildConfigurations': [1, XCBuildConfiguration, 1, 1, _configs], 1615 'defaultConfigurationIsVisible': [0, int, 0, 1, 1], 1616 'defaultConfigurationName': [0, str, 0, 1, 'Release'], 1617 }) 1618 1619 def Name(self): 1620 return 'Build configuration list for ' + \ 1621 self.parent.__class__.__name__ + ' "' + self.parent.Name() + '"' 1622 1623 def ConfigurationNamed(self, name): 1624 """Convenience accessor to obtain an XCBuildConfiguration by name.""" 1625 for configuration in self._properties['buildConfigurations']: 1626 if configuration._properties['name'] == name: 1627 return configuration 1628 1629 raise KeyError(name) 1630 1631 def DefaultConfiguration(self): 1632 """Convenience accessor to obtain the default XCBuildConfiguration.""" 1633 return self.ConfigurationNamed(self._properties['defaultConfigurationName']) 1634 1635 def HasBuildSetting(self, key): 1636 """Determines the state of a build setting in all XCBuildConfiguration 1637 child objects. 1638 1639 If all child objects have key in their build settings, and the value is the 1640 same in all child objects, returns 1. 1641 1642 If no child objects have the key in their build settings, returns 0. 1643 1644 If some, but not all, child objects have the key in their build settings, 1645 or if any children have different values for the key, returns -1. 1646 """ 1647 1648 has = None 1649 value = None 1650 for configuration in self._properties['buildConfigurations']: 1651 configuration_has = configuration.HasBuildSetting(key) 1652 if has is None: 1653 has = configuration_has 1654 elif has != configuration_has: 1655 return -1 1656 1657 if configuration_has: 1658 configuration_value = configuration.GetBuildSetting(key) 1659 if value is None: 1660 value = configuration_value 1661 elif value != configuration_value: 1662 return -1 1663 1664 if not has: 1665 return 0 1666 1667 return 1 1668 1669 def GetBuildSetting(self, key): 1670 """Gets the build setting for key. 1671 1672 All child XCConfiguration objects must have the same value set for the 1673 setting, or a ValueError will be raised. 1674 """ 1675 1676 # TODO(mark): This is wrong for build settings that are lists. The list 1677 # contents should be compared (and a list copy returned?) 1678 1679 value = None 1680 for configuration in self._properties['buildConfigurations']: 1681 configuration_value = configuration.GetBuildSetting(key) 1682 if value is None: 1683 value = configuration_value 1684 else: 1685 if value != configuration_value: 1686 raise ValueError('Variant values for ' + key) 1687 1688 return value 1689 1690 def SetBuildSetting(self, key, value): 1691 """Sets the build setting for key to value in all child 1692 XCBuildConfiguration objects. 1693 """ 1694 1695 for configuration in self._properties['buildConfigurations']: 1696 configuration.SetBuildSetting(key, value) 1697 1698 def AppendBuildSetting(self, key, value): 1699 """Appends value to the build setting for key, which is treated as a list, 1700 in all child XCBuildConfiguration objects. 1701 """ 1702 1703 for configuration in self._properties['buildConfigurations']: 1704 configuration.AppendBuildSetting(key, value) 1705 1706 def DelBuildSetting(self, key): 1707 """Deletes the build setting key from all child XCBuildConfiguration 1708 objects. 1709 """ 1710 1711 for configuration in self._properties['buildConfigurations']: 1712 configuration.DelBuildSetting(key) 1713 1714 def SetBaseConfiguration(self, value): 1715 """Sets the build configuration in all child XCBuildConfiguration objects. 1716 """ 1717 1718 for configuration in self._properties['buildConfigurations']: 1719 configuration.SetBaseConfiguration(value) 1720 1721 1722class PBXBuildFile(XCObject): 1723 _schema = XCObject._schema.copy() 1724 _schema.update({ 1725 'fileRef': [0, XCFileLikeElement, 0, 1], 1726 'settings': [0, str, 0, 0], # hack, it's a dict 1727 }) 1728 1729 # Weird output rules for PBXBuildFile. 1730 _should_print_single_line = True 1731 _encode_transforms = XCObject._alternate_encode_transforms 1732 1733 def Name(self): 1734 # Example: "main.cc in Sources" 1735 return self._properties['fileRef'].Name() + ' in ' + self.parent.Name() 1736 1737 def Hashables(self): 1738 # super 1739 hashables = XCObject.Hashables(self) 1740 1741 # It is not sufficient to just rely on Name() to get the 1742 # XCFileLikeElement's name, because that is not a complete pathname. 1743 # PathHashables returns hashables unique enough that no two 1744 # PBXBuildFiles should wind up with the same set of hashables, unless 1745 # someone adds the same file multiple times to the same target. That 1746 # would be considered invalid anyway. 1747 hashables.extend(self._properties['fileRef'].PathHashables()) 1748 1749 return hashables 1750 1751 1752class XCBuildPhase(XCObject): 1753 """Abstract base for build phase classes. Not represented in a project 1754 file. 1755 1756 Attributes: 1757 _files_by_path: A dict mapping each path of a child in the files list by 1758 path (keys) to the corresponding PBXBuildFile children (values). 1759 _files_by_xcfilelikeelement: A dict mapping each XCFileLikeElement (keys) 1760 to the corresponding PBXBuildFile children (values). 1761 """ 1762 1763 # TODO(mark): Some build phase types, like PBXShellScriptBuildPhase, don't 1764 # actually have a "files" list. XCBuildPhase should not have "files" but 1765 # another abstract subclass of it should provide this, and concrete build 1766 # phase types that do have "files" lists should be derived from that new 1767 # abstract subclass. XCBuildPhase should only provide buildActionMask and 1768 # runOnlyForDeploymentPostprocessing, and not files or the various 1769 # file-related methods and attributes. 1770 1771 _schema = XCObject._schema.copy() 1772 _schema.update({ 1773 'buildActionMask': [0, int, 0, 1, 0x7fffffff], 1774 'files': [1, PBXBuildFile, 1, 1, []], 1775 'runOnlyForDeploymentPostprocessing': [0, int, 0, 1, 0], 1776 }) 1777 1778 def __init__(self, properties=None, id=None, parent=None): 1779 # super 1780 XCObject.__init__(self, properties, id, parent) 1781 1782 self._files_by_path = {} 1783 self._files_by_xcfilelikeelement = {} 1784 for pbxbuildfile in self._properties.get('files', []): 1785 self._AddBuildFileToDicts(pbxbuildfile) 1786 1787 def FileGroup(self, path): 1788 # Subclasses must override this by returning a two-element tuple. The 1789 # first item in the tuple should be the PBXGroup to which "path" should be 1790 # added, either as a child or deeper descendant. The second item should 1791 # be a boolean indicating whether files should be added into hierarchical 1792 # groups or one single flat group. 1793 raise NotImplementedError( 1794 self.__class__.__name__ + ' must implement FileGroup') 1795 1796 def _AddPathToDict(self, pbxbuildfile, path): 1797 """Adds path to the dict tracking paths belonging to this build phase. 1798 1799 If the path is already a member of this build phase, raises an exception. 1800 """ 1801 1802 if path in self._files_by_path: 1803 raise ValueError('Found multiple build files with path ' + path) 1804 self._files_by_path[path] = pbxbuildfile 1805 1806 def _AddBuildFileToDicts(self, pbxbuildfile, path=None): 1807 """Maintains the _files_by_path and _files_by_xcfilelikeelement dicts. 1808 1809 If path is specified, then it is the path that is being added to the 1810 phase, and pbxbuildfile must contain either a PBXFileReference directly 1811 referencing that path, or it must contain a PBXVariantGroup that itself 1812 contains a PBXFileReference referencing the path. 1813 1814 If path is not specified, either the PBXFileReference's path or the paths 1815 of all children of the PBXVariantGroup are taken as being added to the 1816 phase. 1817 1818 If the path is already present in the phase, raises an exception. 1819 1820 If the PBXFileReference or PBXVariantGroup referenced by pbxbuildfile 1821 are already present in the phase, referenced by a different PBXBuildFile 1822 object, raises an exception. This does not raise an exception when 1823 a PBXFileReference or PBXVariantGroup reappear and are referenced by the 1824 same PBXBuildFile that has already introduced them, because in the case 1825 of PBXVariantGroup objects, they may correspond to multiple paths that are 1826 not all added simultaneously. When this situation occurs, the path needs 1827 to be added to _files_by_path, but nothing needs to change in 1828 _files_by_xcfilelikeelement, and the caller should have avoided adding 1829 the PBXBuildFile if it is already present in the list of children. 1830 """ 1831 1832 xcfilelikeelement = pbxbuildfile._properties['fileRef'] 1833 1834 paths = [] 1835 if path != None: 1836 # It's best when the caller provides the path. 1837 if isinstance(xcfilelikeelement, PBXVariantGroup): 1838 paths.append(path) 1839 else: 1840 # If the caller didn't provide a path, there can be either multiple 1841 # paths (PBXVariantGroup) or one. 1842 if isinstance(xcfilelikeelement, PBXVariantGroup): 1843 for variant in xcfilelikeelement._properties['children']: 1844 paths.append(variant.FullPath()) 1845 else: 1846 paths.append(xcfilelikeelement.FullPath()) 1847 1848 # Add the paths first, because if something's going to raise, the 1849 # messages provided by _AddPathToDict are more useful owing to its 1850 # having access to a real pathname and not just an object's Name(). 1851 for a_path in paths: 1852 self._AddPathToDict(pbxbuildfile, a_path) 1853 1854 # If another PBXBuildFile references this XCFileLikeElement, there's a 1855 # problem. 1856 if xcfilelikeelement in self._files_by_xcfilelikeelement and \ 1857 self._files_by_xcfilelikeelement[xcfilelikeelement] != pbxbuildfile: 1858 raise ValueError('Found multiple build files for ' + \ 1859 xcfilelikeelement.Name()) 1860 self._files_by_xcfilelikeelement[xcfilelikeelement] = pbxbuildfile 1861 1862 def AppendBuildFile(self, pbxbuildfile, path=None): 1863 # Callers should use this instead of calling 1864 # AppendProperty('files', pbxbuildfile) directly because this function 1865 # maintains the object's dicts. Better yet, callers can just call AddFile 1866 # with a pathname and not worry about building their own PBXBuildFile 1867 # objects. 1868 self.AppendProperty('files', pbxbuildfile) 1869 self._AddBuildFileToDicts(pbxbuildfile, path) 1870 1871 def AddFile(self, path, settings=None): 1872 (file_group, hierarchical) = self.FileGroup(path) 1873 file_ref = file_group.AddOrGetFileByPath(path, hierarchical) 1874 1875 if file_ref in self._files_by_xcfilelikeelement and \ 1876 isinstance(file_ref, PBXVariantGroup): 1877 # There's already a PBXBuildFile in this phase corresponding to the 1878 # PBXVariantGroup. path just provides a new variant that belongs to 1879 # the group. Add the path to the dict. 1880 pbxbuildfile = self._files_by_xcfilelikeelement[file_ref] 1881 self._AddBuildFileToDicts(pbxbuildfile, path) 1882 else: 1883 # Add a new PBXBuildFile to get file_ref into the phase. 1884 if settings is None: 1885 pbxbuildfile = PBXBuildFile({'fileRef': file_ref}) 1886 else: 1887 pbxbuildfile = PBXBuildFile({'fileRef': file_ref, 'settings': settings}) 1888 self.AppendBuildFile(pbxbuildfile, path) 1889 1890 1891class PBXHeadersBuildPhase(XCBuildPhase): 1892 # No additions to the schema relative to XCBuildPhase. 1893 1894 def Name(self): 1895 return 'Headers' 1896 1897 def FileGroup(self, path): 1898 return self.PBXProjectAncestor().RootGroupForPath(path) 1899 1900 1901class PBXResourcesBuildPhase(XCBuildPhase): 1902 # No additions to the schema relative to XCBuildPhase. 1903 1904 def Name(self): 1905 return 'Resources' 1906 1907 def FileGroup(self, path): 1908 return self.PBXProjectAncestor().RootGroupForPath(path) 1909 1910 1911class PBXSourcesBuildPhase(XCBuildPhase): 1912 # No additions to the schema relative to XCBuildPhase. 1913 1914 def Name(self): 1915 return 'Sources' 1916 1917 def FileGroup(self, path): 1918 return self.PBXProjectAncestor().RootGroupForPath(path) 1919 1920 1921class PBXFrameworksBuildPhase(XCBuildPhase): 1922 # No additions to the schema relative to XCBuildPhase. 1923 1924 def Name(self): 1925 return 'Frameworks' 1926 1927 def FileGroup(self, path): 1928 (root, ext) = posixpath.splitext(path) 1929 if ext != '': 1930 ext = ext[1:].lower() 1931 if ext == 'o': 1932 # .o files are added to Xcode Frameworks phases, but conceptually aren't 1933 # frameworks, they're more like sources or intermediates. Redirect them 1934 # to show up in one of those other groups. 1935 return self.PBXProjectAncestor().RootGroupForPath(path) 1936 else: 1937 return (self.PBXProjectAncestor().FrameworksGroup(), False) 1938 1939 1940class PBXShellScriptBuildPhase(XCBuildPhase): 1941 _schema = XCBuildPhase._schema.copy() 1942 _schema.update({ 1943 'inputPaths': [1, str, 0, 1, []], 1944 'name': [0, str, 0, 0], 1945 'outputPaths': [1, str, 0, 1, []], 1946 'shellPath': [0, str, 0, 1, '/bin/sh'], 1947 'shellScript': [0, str, 0, 1], 1948 'showEnvVarsInLog': [0, int, 0, 0], 1949 }) 1950 1951 def Name(self): 1952 if 'name' in self._properties: 1953 return self._properties['name'] 1954 1955 return 'ShellScript' 1956 1957 1958class PBXCopyFilesBuildPhase(XCBuildPhase): 1959 _schema = XCBuildPhase._schema.copy() 1960 _schema.update({ 1961 'dstPath': [0, str, 0, 1], 1962 'dstSubfolderSpec': [0, int, 0, 1], 1963 'name': [0, str, 0, 0], 1964 }) 1965 1966 # path_tree_re matches "$(DIR)/path", "$(DIR)/$(DIR2)/path" or just "$(DIR)". 1967 # Match group 1 is "DIR", group 3 is "path" or "$(DIR2") or "$(DIR2)/path" 1968 # or None. If group 3 is "path", group 4 will be None otherwise group 4 is 1969 # "DIR2" and group 6 is "path". 1970 path_tree_re = re.compile(r'^\$\((.*?)\)(/(\$\((.*?)\)(/(.*)|)|(.*)|)|)$') 1971 1972 # path_tree_{first,second}_to_subfolder map names of Xcode variables to the 1973 # associated dstSubfolderSpec property value used in a PBXCopyFilesBuildPhase 1974 # object. 1975 path_tree_first_to_subfolder = { 1976 # Types that can be chosen via the Xcode UI. 1977 'BUILT_PRODUCTS_DIR': 16, # Products Directory 1978 'BUILT_FRAMEWORKS_DIR': 10, # Not an official Xcode macro. 1979 # Existed before support for the 1980 # names below was added. Maps to 1981 # "Frameworks". 1982 } 1983 1984 path_tree_second_to_subfolder = { 1985 'WRAPPER_NAME': 1, # Wrapper 1986 # Although Xcode's friendly name is "Executables", the destination 1987 # is demonstrably the value of the build setting 1988 # EXECUTABLE_FOLDER_PATH not EXECUTABLES_FOLDER_PATH. 1989 'EXECUTABLE_FOLDER_PATH': 6, # Executables. 1990 'UNLOCALIZED_RESOURCES_FOLDER_PATH': 7, # Resources 1991 'JAVA_FOLDER_PATH': 15, # Java Resources 1992 'FRAMEWORKS_FOLDER_PATH': 10, # Frameworks 1993 'SHARED_FRAMEWORKS_FOLDER_PATH': 11, # Shared Frameworks 1994 'SHARED_SUPPORT_FOLDER_PATH': 12, # Shared Support 1995 'PLUGINS_FOLDER_PATH': 13, # PlugIns 1996 # For XPC Services, Xcode sets both dstPath and dstSubfolderSpec. 1997 # Note that it re-uses the BUILT_PRODUCTS_DIR value for 1998 # dstSubfolderSpec. dstPath is set below. 1999 'XPCSERVICES_FOLDER_PATH': 16, # XPC Services. 2000 } 2001 2002 def Name(self): 2003 if 'name' in self._properties: 2004 return self._properties['name'] 2005 2006 return 'CopyFiles' 2007 2008 def FileGroup(self, path): 2009 return self.PBXProjectAncestor().RootGroupForPath(path) 2010 2011 def SetDestination(self, path): 2012 """Set the dstSubfolderSpec and dstPath properties from path. 2013 2014 path may be specified in the same notation used for XCHierarchicalElements, 2015 specifically, "$(DIR)/path". 2016 """ 2017 2018 path_tree_match = self.path_tree_re.search(path) 2019 if path_tree_match: 2020 path_tree = path_tree_match.group(1); 2021 if path_tree in self.path_tree_first_to_subfolder: 2022 subfolder = self.path_tree_first_to_subfolder[path_tree] 2023 relative_path = path_tree_match.group(3) 2024 if relative_path is None: 2025 relative_path = '' 2026 2027 if subfolder == 16 and path_tree_match.group(4) is not None: 2028 # BUILT_PRODUCTS_DIR (16) is the first element in a path whose 2029 # second element is possibly one of the variable names in 2030 # path_tree_second_to_subfolder. Xcode sets the values of all these 2031 # variables to relative paths so .gyp files must prefix them with 2032 # BUILT_PRODUCTS_DIR, e.g. 2033 # $(BUILT_PRODUCTS_DIR)/$(PLUGINS_FOLDER_PATH). Then 2034 # xcode_emulation.py can export these variables with the same values 2035 # as Xcode yet make & ninja files can determine the absolute path 2036 # to the target. Xcode uses the dstSubfolderSpec value set here 2037 # to determine the full path. 2038 # 2039 # An alternative of xcode_emulation.py setting the values to absolute 2040 # paths when exporting these variables has been ruled out because 2041 # then the values would be different depending on the build tool. 2042 # 2043 # Another alternative is to invent new names for the variables used 2044 # to match to the subfolder indices in the second table. .gyp files 2045 # then will not need to prepend $(BUILT_PRODUCTS_DIR) because 2046 # xcode_emulation.py can set the values of those variables to 2047 # the absolute paths when exporting. This is possibly the thinking 2048 # behind BUILT_FRAMEWORKS_DIR which is used in exactly this manner. 2049 # 2050 # Requiring prepending BUILT_PRODUCTS_DIR has been chosen because 2051 # this same way could be used to specify destinations in .gyp files 2052 # that pre-date this addition to GYP. However they would only work 2053 # with the Xcode generator. The previous version of xcode_emulation.py 2054 # does not export these variables. Such files will get the benefit 2055 # of the Xcode UI showing the proper destination name simply by 2056 # regenerating the projects with this version of GYP. 2057 path_tree = path_tree_match.group(4) 2058 relative_path = path_tree_match.group(6) 2059 separator = '/' 2060 2061 if path_tree in self.path_tree_second_to_subfolder: 2062 subfolder = self.path_tree_second_to_subfolder[path_tree] 2063 if relative_path is None: 2064 relative_path = '' 2065 separator = '' 2066 if path_tree == 'XPCSERVICES_FOLDER_PATH': 2067 relative_path = '$(CONTENTS_FOLDER_PATH)/XPCServices' \ 2068 + separator + relative_path 2069 else: 2070 # subfolder = 16 from above 2071 # The second element of the path is an unrecognized variable. 2072 # Include it and any remaining elements in relative_path. 2073 relative_path = path_tree_match.group(3); 2074 2075 else: 2076 # The path starts with an unrecognized Xcode variable 2077 # name like $(SRCROOT). Xcode will still handle this 2078 # as an "absolute path" that starts with the variable. 2079 subfolder = 0 2080 relative_path = path 2081 elif path.startswith('/'): 2082 # Special case. Absolute paths are in dstSubfolderSpec 0. 2083 subfolder = 0 2084 relative_path = path[1:] 2085 else: 2086 raise ValueError('Can\'t use path %s in a %s' % \ 2087 (path, self.__class__.__name__)) 2088 2089 self._properties['dstPath'] = relative_path 2090 self._properties['dstSubfolderSpec'] = subfolder 2091 2092 2093class PBXBuildRule(XCObject): 2094 _schema = XCObject._schema.copy() 2095 _schema.update({ 2096 'compilerSpec': [0, str, 0, 1], 2097 'filePatterns': [0, str, 0, 0], 2098 'fileType': [0, str, 0, 1], 2099 'isEditable': [0, int, 0, 1, 1], 2100 'outputFiles': [1, str, 0, 1, []], 2101 'script': [0, str, 0, 0], 2102 }) 2103 2104 def Name(self): 2105 # Not very inspired, but it's what Xcode uses. 2106 return self.__class__.__name__ 2107 2108 def Hashables(self): 2109 # super 2110 hashables = XCObject.Hashables(self) 2111 2112 # Use the hashables of the weak objects that this object refers to. 2113 hashables.append(self._properties['fileType']) 2114 if 'filePatterns' in self._properties: 2115 hashables.append(self._properties['filePatterns']) 2116 return hashables 2117 2118 2119class PBXContainerItemProxy(XCObject): 2120 # When referencing an item in this project file, containerPortal is the 2121 # PBXProject root object of this project file. When referencing an item in 2122 # another project file, containerPortal is a PBXFileReference identifying 2123 # the other project file. 2124 # 2125 # When serving as a proxy to an XCTarget (in this project file or another), 2126 # proxyType is 1. When serving as a proxy to a PBXFileReference (in another 2127 # project file), proxyType is 2. Type 2 is used for references to the 2128 # producs of the other project file's targets. 2129 # 2130 # Xcode is weird about remoteGlobalIDString. Usually, it's printed without 2131 # a comment, indicating that it's tracked internally simply as a string, but 2132 # sometimes it's printed with a comment (usually when the object is initially 2133 # created), indicating that it's tracked as a project file object at least 2134 # sometimes. This module always tracks it as an object, but contains a hack 2135 # to prevent it from printing the comment in the project file output. See 2136 # _XCKVPrint. 2137 _schema = XCObject._schema.copy() 2138 _schema.update({ 2139 'containerPortal': [0, XCContainerPortal, 0, 1], 2140 'proxyType': [0, int, 0, 1], 2141 'remoteGlobalIDString': [0, XCRemoteObject, 0, 1], 2142 'remoteInfo': [0, str, 0, 1], 2143 }) 2144 2145 def __repr__(self): 2146 props = self._properties 2147 name = '%s.gyp:%s' % (props['containerPortal'].Name(), props['remoteInfo']) 2148 return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) 2149 2150 def Name(self): 2151 # Admittedly not the best name, but it's what Xcode uses. 2152 return self.__class__.__name__ 2153 2154 def Hashables(self): 2155 # super 2156 hashables = XCObject.Hashables(self) 2157 2158 # Use the hashables of the weak objects that this object refers to. 2159 hashables.extend(self._properties['containerPortal'].Hashables()) 2160 hashables.extend(self._properties['remoteGlobalIDString'].Hashables()) 2161 return hashables 2162 2163 2164class PBXTargetDependency(XCObject): 2165 # The "target" property accepts an XCTarget object, and obviously not 2166 # NoneType. But XCTarget is defined below, so it can't be put into the 2167 # schema yet. The definition of PBXTargetDependency can't be moved below 2168 # XCTarget because XCTarget's own schema references PBXTargetDependency. 2169 # Python doesn't deal well with this circular relationship, and doesn't have 2170 # a real way to do forward declarations. To work around, the type of 2171 # the "target" property is reset below, after XCTarget is defined. 2172 # 2173 # At least one of "name" and "target" is required. 2174 _schema = XCObject._schema.copy() 2175 _schema.update({ 2176 'name': [0, str, 0, 0], 2177 'target': [0, None.__class__, 0, 0], 2178 'targetProxy': [0, PBXContainerItemProxy, 1, 1], 2179 }) 2180 2181 def __repr__(self): 2182 name = self._properties.get('name') or self._properties['target'].Name() 2183 return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) 2184 2185 def Name(self): 2186 # Admittedly not the best name, but it's what Xcode uses. 2187 return self.__class__.__name__ 2188 2189 def Hashables(self): 2190 # super 2191 hashables = XCObject.Hashables(self) 2192 2193 # Use the hashables of the weak objects that this object refers to. 2194 hashables.extend(self._properties['targetProxy'].Hashables()) 2195 return hashables 2196 2197 2198class PBXReferenceProxy(XCFileLikeElement): 2199 _schema = XCFileLikeElement._schema.copy() 2200 _schema.update({ 2201 'fileType': [0, str, 0, 1], 2202 'path': [0, str, 0, 1], 2203 'remoteRef': [0, PBXContainerItemProxy, 1, 1], 2204 }) 2205 2206 2207class XCTarget(XCRemoteObject): 2208 # An XCTarget is really just an XCObject, the XCRemoteObject thing is just 2209 # to allow PBXProject to be used in the remoteGlobalIDString property of 2210 # PBXContainerItemProxy. 2211 # 2212 # Setting a "name" property at instantiation may also affect "productName", 2213 # which may in turn affect the "PRODUCT_NAME" build setting in children of 2214 # "buildConfigurationList". See __init__ below. 2215 _schema = XCRemoteObject._schema.copy() 2216 _schema.update({ 2217 'buildConfigurationList': [0, XCConfigurationList, 1, 1, 2218 XCConfigurationList()], 2219 'buildPhases': [1, XCBuildPhase, 1, 1, []], 2220 'dependencies': [1, PBXTargetDependency, 1, 1, []], 2221 'name': [0, str, 0, 1], 2222 'productName': [0, str, 0, 1], 2223 }) 2224 2225 def __init__(self, properties=None, id=None, parent=None, 2226 force_outdir=None, force_prefix=None, force_extension=None): 2227 # super 2228 XCRemoteObject.__init__(self, properties, id, parent) 2229 2230 # Set up additional defaults not expressed in the schema. If a "name" 2231 # property was supplied, set "productName" if it is not present. Also set 2232 # the "PRODUCT_NAME" build setting in each configuration, but only if 2233 # the setting is not present in any build configuration. 2234 if 'name' in self._properties: 2235 if not 'productName' in self._properties: 2236 self.SetProperty('productName', self._properties['name']) 2237 2238 if 'productName' in self._properties: 2239 if 'buildConfigurationList' in self._properties: 2240 configs = self._properties['buildConfigurationList'] 2241 if configs.HasBuildSetting('PRODUCT_NAME') == 0: 2242 configs.SetBuildSetting('PRODUCT_NAME', 2243 self._properties['productName']) 2244 2245 def AddDependency(self, other): 2246 pbxproject = self.PBXProjectAncestor() 2247 other_pbxproject = other.PBXProjectAncestor() 2248 if pbxproject == other_pbxproject: 2249 # Add a dependency to another target in the same project file. 2250 container = PBXContainerItemProxy({'containerPortal': pbxproject, 2251 'proxyType': 1, 2252 'remoteGlobalIDString': other, 2253 'remoteInfo': other.Name()}) 2254 dependency = PBXTargetDependency({'target': other, 2255 'targetProxy': container}) 2256 self.AppendProperty('dependencies', dependency) 2257 else: 2258 # Add a dependency to a target in a different project file. 2259 other_project_ref = \ 2260 pbxproject.AddOrGetProjectReference(other_pbxproject)[1] 2261 container = PBXContainerItemProxy({ 2262 'containerPortal': other_project_ref, 2263 'proxyType': 1, 2264 'remoteGlobalIDString': other, 2265 'remoteInfo': other.Name(), 2266 }) 2267 dependency = PBXTargetDependency({'name': other.Name(), 2268 'targetProxy': container}) 2269 self.AppendProperty('dependencies', dependency) 2270 2271 # Proxy all of these through to the build configuration list. 2272 2273 def ConfigurationNamed(self, name): 2274 return self._properties['buildConfigurationList'].ConfigurationNamed(name) 2275 2276 def DefaultConfiguration(self): 2277 return self._properties['buildConfigurationList'].DefaultConfiguration() 2278 2279 def HasBuildSetting(self, key): 2280 return self._properties['buildConfigurationList'].HasBuildSetting(key) 2281 2282 def GetBuildSetting(self, key): 2283 return self._properties['buildConfigurationList'].GetBuildSetting(key) 2284 2285 def SetBuildSetting(self, key, value): 2286 return self._properties['buildConfigurationList'].SetBuildSetting(key, \ 2287 value) 2288 2289 def AppendBuildSetting(self, key, value): 2290 return self._properties['buildConfigurationList'].AppendBuildSetting(key, \ 2291 value) 2292 2293 def DelBuildSetting(self, key): 2294 return self._properties['buildConfigurationList'].DelBuildSetting(key) 2295 2296 2297# Redefine the type of the "target" property. See PBXTargetDependency._schema 2298# above. 2299PBXTargetDependency._schema['target'][1] = XCTarget 2300 2301 2302class PBXNativeTarget(XCTarget): 2303 # buildPhases is overridden in the schema to be able to set defaults. 2304 # 2305 # NOTE: Contrary to most objects, it is advisable to set parent when 2306 # constructing PBXNativeTarget. A parent of an XCTarget must be a PBXProject 2307 # object. A parent reference is required for a PBXNativeTarget during 2308 # construction to be able to set up the target defaults for productReference, 2309 # because a PBXBuildFile object must be created for the target and it must 2310 # be added to the PBXProject's mainGroup hierarchy. 2311 _schema = XCTarget._schema.copy() 2312 _schema.update({ 2313 'buildPhases': [1, XCBuildPhase, 1, 1, 2314 [PBXSourcesBuildPhase(), PBXFrameworksBuildPhase()]], 2315 'buildRules': [1, PBXBuildRule, 1, 1, []], 2316 'productReference': [0, PBXFileReference, 0, 1], 2317 'productType': [0, str, 0, 1], 2318 }) 2319 2320 # Mapping from Xcode product-types to settings. The settings are: 2321 # filetype : used for explicitFileType in the project file 2322 # prefix : the prefix for the file name 2323 # suffix : the suffix for the file name 2324 _product_filetypes = { 2325 'com.apple.product-type.application': ['wrapper.application', 2326 '', '.app'], 2327 'com.apple.product-type.application.watchapp': ['wrapper.application', 2328 '', '.app'], 2329 'com.apple.product-type.watchkit-extension': ['wrapper.app-extension', 2330 '', '.appex'], 2331 'com.apple.product-type.app-extension': ['wrapper.app-extension', 2332 '', '.appex'], 2333 'com.apple.product-type.bundle': ['wrapper.cfbundle', 2334 '', '.bundle'], 2335 'com.apple.product-type.framework': ['wrapper.framework', 2336 '', '.framework'], 2337 'com.apple.product-type.library.dynamic': ['compiled.mach-o.dylib', 2338 'lib', '.dylib'], 2339 'com.apple.product-type.library.static': ['archive.ar', 2340 'lib', '.a'], 2341 'com.apple.product-type.tool': ['compiled.mach-o.executable', 2342 '', ''], 2343 'com.apple.product-type.bundle.unit-test': ['wrapper.cfbundle', 2344 '', '.xctest'], 2345 'com.apple.product-type.bundle.ui-testing': ['wrapper.cfbundle', 2346 '', '.xctest'], 2347 'com.googlecode.gyp.xcode.bundle': ['compiled.mach-o.dylib', 2348 '', '.so'], 2349 'com.apple.product-type.kernel-extension': ['wrapper.kext', 2350 '', '.kext'], 2351 } 2352 2353 def __init__(self, properties=None, id=None, parent=None, 2354 force_outdir=None, force_prefix=None, force_extension=None): 2355 # super 2356 XCTarget.__init__(self, properties, id, parent) 2357 2358 if 'productName' in self._properties and \ 2359 'productType' in self._properties and \ 2360 not 'productReference' in self._properties and \ 2361 self._properties['productType'] in self._product_filetypes: 2362 products_group = None 2363 pbxproject = self.PBXProjectAncestor() 2364 if pbxproject != None: 2365 products_group = pbxproject.ProductsGroup() 2366 2367 if products_group != None: 2368 (filetype, prefix, suffix) = \ 2369 self._product_filetypes[self._properties['productType']] 2370 # Xcode does not have a distinct type for loadable modules that are 2371 # pure BSD targets (not in a bundle wrapper). GYP allows such modules 2372 # to be specified by setting a target type to loadable_module without 2373 # having mac_bundle set. These are mapped to the pseudo-product type 2374 # com.googlecode.gyp.xcode.bundle. 2375 # 2376 # By picking up this special type and converting it to a dynamic 2377 # library (com.apple.product-type.library.dynamic) with fix-ups, 2378 # single-file loadable modules can be produced. 2379 # 2380 # MACH_O_TYPE is changed to mh_bundle to produce the proper file type 2381 # (as opposed to mh_dylib). In order for linking to succeed, 2382 # DYLIB_CURRENT_VERSION and DYLIB_COMPATIBILITY_VERSION must be 2383 # cleared. They are meaningless for type mh_bundle. 2384 # 2385 # Finally, the .so extension is forcibly applied over the default 2386 # (.dylib), unless another forced extension is already selected. 2387 # .dylib is plainly wrong, and .bundle is used by loadable_modules in 2388 # bundle wrappers (com.apple.product-type.bundle). .so seems an odd 2389 # choice because it's used as the extension on many other systems that 2390 # don't distinguish between linkable shared libraries and non-linkable 2391 # loadable modules, but there's precedent: Python loadable modules on 2392 # Mac OS X use an .so extension. 2393 if self._properties['productType'] == 'com.googlecode.gyp.xcode.bundle': 2394 self._properties['productType'] = \ 2395 'com.apple.product-type.library.dynamic' 2396 self.SetBuildSetting('MACH_O_TYPE', 'mh_bundle') 2397 self.SetBuildSetting('DYLIB_CURRENT_VERSION', '') 2398 self.SetBuildSetting('DYLIB_COMPATIBILITY_VERSION', '') 2399 if force_extension is None: 2400 force_extension = suffix[1:] 2401 2402 if self._properties['productType'] == \ 2403 'com.apple.product-type-bundle.unit.test' or \ 2404 self._properties['productType'] == \ 2405 'com.apple.product-type-bundle.ui-testing': 2406 if force_extension is None: 2407 force_extension = suffix[1:] 2408 2409 if force_extension is not None: 2410 # If it's a wrapper (bundle), set WRAPPER_EXTENSION. 2411 # Extension override. 2412 suffix = '.' + force_extension 2413 if filetype.startswith('wrapper.'): 2414 self.SetBuildSetting('WRAPPER_EXTENSION', force_extension) 2415 else: 2416 self.SetBuildSetting('EXECUTABLE_EXTENSION', force_extension) 2417 2418 if filetype.startswith('compiled.mach-o.executable'): 2419 product_name = self._properties['productName'] 2420 product_name += suffix 2421 suffix = '' 2422 self.SetProperty('productName', product_name) 2423 self.SetBuildSetting('PRODUCT_NAME', product_name) 2424 2425 # Xcode handles most prefixes based on the target type, however there 2426 # are exceptions. If a "BSD Dynamic Library" target is added in the 2427 # Xcode UI, Xcode sets EXECUTABLE_PREFIX. This check duplicates that 2428 # behavior. 2429 if force_prefix is not None: 2430 prefix = force_prefix 2431 if filetype.startswith('wrapper.'): 2432 self.SetBuildSetting('WRAPPER_PREFIX', prefix) 2433 else: 2434 self.SetBuildSetting('EXECUTABLE_PREFIX', prefix) 2435 2436 if force_outdir is not None: 2437 self.SetBuildSetting('TARGET_BUILD_DIR', force_outdir) 2438 2439 # TODO(tvl): Remove the below hack. 2440 # http://code.google.com/p/gyp/issues/detail?id=122 2441 2442 # Some targets include the prefix in the target_name. These targets 2443 # really should just add a product_name setting that doesn't include 2444 # the prefix. For example: 2445 # target_name = 'libevent', product_name = 'event' 2446 # This check cleans up for them. 2447 product_name = self._properties['productName'] 2448 prefix_len = len(prefix) 2449 if prefix_len and (product_name[:prefix_len] == prefix): 2450 product_name = product_name[prefix_len:] 2451 self.SetProperty('productName', product_name) 2452 self.SetBuildSetting('PRODUCT_NAME', product_name) 2453 2454 ref_props = { 2455 'explicitFileType': filetype, 2456 'includeInIndex': 0, 2457 'path': prefix + product_name + suffix, 2458 'sourceTree': 'BUILT_PRODUCTS_DIR', 2459 } 2460 file_ref = PBXFileReference(ref_props) 2461 products_group.AppendChild(file_ref) 2462 self.SetProperty('productReference', file_ref) 2463 2464 def GetBuildPhaseByType(self, type): 2465 if not 'buildPhases' in self._properties: 2466 return None 2467 2468 the_phase = None 2469 for phase in self._properties['buildPhases']: 2470 if isinstance(phase, type): 2471 # Some phases may be present in multiples in a well-formed project file, 2472 # but phases like PBXSourcesBuildPhase may only be present singly, and 2473 # this function is intended as an aid to GetBuildPhaseByType. Loop 2474 # over the entire list of phases and assert if more than one of the 2475 # desired type is found. 2476 assert the_phase is None 2477 the_phase = phase 2478 2479 return the_phase 2480 2481 def HeadersPhase(self): 2482 headers_phase = self.GetBuildPhaseByType(PBXHeadersBuildPhase) 2483 if headers_phase is None: 2484 headers_phase = PBXHeadersBuildPhase() 2485 2486 # The headers phase should come before the resources, sources, and 2487 # frameworks phases, if any. 2488 insert_at = len(self._properties['buildPhases']) 2489 for index, phase in enumerate(self._properties['buildPhases']): 2490 if isinstance(phase, PBXResourcesBuildPhase) or \ 2491 isinstance(phase, PBXSourcesBuildPhase) or \ 2492 isinstance(phase, PBXFrameworksBuildPhase): 2493 insert_at = index 2494 break 2495 2496 self._properties['buildPhases'].insert(insert_at, headers_phase) 2497 headers_phase.parent = self 2498 2499 return headers_phase 2500 2501 def ResourcesPhase(self): 2502 resources_phase = self.GetBuildPhaseByType(PBXResourcesBuildPhase) 2503 if resources_phase is None: 2504 resources_phase = PBXResourcesBuildPhase() 2505 2506 # The resources phase should come before the sources and frameworks 2507 # phases, if any. 2508 insert_at = len(self._properties['buildPhases']) 2509 for index, phase in enumerate(self._properties['buildPhases']): 2510 if isinstance(phase, PBXSourcesBuildPhase) or \ 2511 isinstance(phase, PBXFrameworksBuildPhase): 2512 insert_at = index 2513 break 2514 2515 self._properties['buildPhases'].insert(insert_at, resources_phase) 2516 resources_phase.parent = self 2517 2518 return resources_phase 2519 2520 def SourcesPhase(self): 2521 sources_phase = self.GetBuildPhaseByType(PBXSourcesBuildPhase) 2522 if sources_phase is None: 2523 sources_phase = PBXSourcesBuildPhase() 2524 self.AppendProperty('buildPhases', sources_phase) 2525 2526 return sources_phase 2527 2528 def FrameworksPhase(self): 2529 frameworks_phase = self.GetBuildPhaseByType(PBXFrameworksBuildPhase) 2530 if frameworks_phase is None: 2531 frameworks_phase = PBXFrameworksBuildPhase() 2532 self.AppendProperty('buildPhases', frameworks_phase) 2533 2534 return frameworks_phase 2535 2536 def AddDependency(self, other): 2537 # super 2538 XCTarget.AddDependency(self, other) 2539 2540 static_library_type = 'com.apple.product-type.library.static' 2541 shared_library_type = 'com.apple.product-type.library.dynamic' 2542 framework_type = 'com.apple.product-type.framework' 2543 if isinstance(other, PBXNativeTarget) and \ 2544 'productType' in self._properties and \ 2545 self._properties['productType'] != static_library_type and \ 2546 'productType' in other._properties and \ 2547 (other._properties['productType'] == static_library_type or \ 2548 ((other._properties['productType'] == shared_library_type or \ 2549 other._properties['productType'] == framework_type) and \ 2550 ((not other.HasBuildSetting('MACH_O_TYPE')) or 2551 other.GetBuildSetting('MACH_O_TYPE') != 'mh_bundle'))): 2552 2553 file_ref = other.GetProperty('productReference') 2554 2555 pbxproject = self.PBXProjectAncestor() 2556 other_pbxproject = other.PBXProjectAncestor() 2557 if pbxproject != other_pbxproject: 2558 other_project_product_group = \ 2559 pbxproject.AddOrGetProjectReference(other_pbxproject)[0] 2560 file_ref = other_project_product_group.GetChildByRemoteObject(file_ref) 2561 2562 self.FrameworksPhase().AppendProperty('files', 2563 PBXBuildFile({'fileRef': file_ref})) 2564 2565 2566class PBXAggregateTarget(XCTarget): 2567 pass 2568 2569 2570class PBXProject(XCContainerPortal): 2571 # A PBXProject is really just an XCObject, the XCContainerPortal thing is 2572 # just to allow PBXProject to be used in the containerPortal property of 2573 # PBXContainerItemProxy. 2574 """ 2575 2576 Attributes: 2577 path: "sample.xcodeproj". TODO(mark) Document me! 2578 _other_pbxprojects: A dictionary, keyed by other PBXProject objects. Each 2579 value is a reference to the dict in the 2580 projectReferences list associated with the keyed 2581 PBXProject. 2582 """ 2583 2584 _schema = XCContainerPortal._schema.copy() 2585 _schema.update({ 2586 'attributes': [0, dict, 0, 0], 2587 'buildConfigurationList': [0, XCConfigurationList, 1, 1, 2588 XCConfigurationList()], 2589 'compatibilityVersion': [0, str, 0, 1, 'Xcode 3.2'], 2590 'hasScannedForEncodings': [0, int, 0, 1, 1], 2591 'mainGroup': [0, PBXGroup, 1, 1, PBXGroup()], 2592 'projectDirPath': [0, str, 0, 1, ''], 2593 'projectReferences': [1, dict, 0, 0], 2594 'projectRoot': [0, str, 0, 1, ''], 2595 'targets': [1, XCTarget, 1, 1, []], 2596 }) 2597 2598 def __init__(self, properties=None, id=None, parent=None, path=None): 2599 self.path = path 2600 self._other_pbxprojects = {} 2601 # super 2602 return XCContainerPortal.__init__(self, properties, id, parent) 2603 2604 def Name(self): 2605 name = self.path 2606 if name[-10:] == '.xcodeproj': 2607 name = name[:-10] 2608 return posixpath.basename(name) 2609 2610 def Path(self): 2611 return self.path 2612 2613 def Comment(self): 2614 return 'Project object' 2615 2616 def Children(self): 2617 # super 2618 children = XCContainerPortal.Children(self) 2619 2620 # Add children that the schema doesn't know about. Maybe there's a more 2621 # elegant way around this, but this is the only case where we need to own 2622 # objects in a dictionary (that is itself in a list), and three lines for 2623 # a one-off isn't that big a deal. 2624 if 'projectReferences' in self._properties: 2625 for reference in self._properties['projectReferences']: 2626 children.append(reference['ProductGroup']) 2627 2628 return children 2629 2630 def PBXProjectAncestor(self): 2631 return self 2632 2633 def _GroupByName(self, name): 2634 if not 'mainGroup' in self._properties: 2635 self.SetProperty('mainGroup', PBXGroup()) 2636 2637 main_group = self._properties['mainGroup'] 2638 group = main_group.GetChildByName(name) 2639 if group is None: 2640 group = PBXGroup({'name': name}) 2641 main_group.AppendChild(group) 2642 2643 return group 2644 2645 # SourceGroup and ProductsGroup are created by default in Xcode's own 2646 # templates. 2647 def SourceGroup(self): 2648 return self._GroupByName('Source') 2649 2650 def ProductsGroup(self): 2651 return self._GroupByName('Products') 2652 2653 # IntermediatesGroup is used to collect source-like files that are generated 2654 # by rules or script phases and are placed in intermediate directories such 2655 # as DerivedSources. 2656 def IntermediatesGroup(self): 2657 return self._GroupByName('Intermediates') 2658 2659 # FrameworksGroup and ProjectsGroup are top-level groups used to collect 2660 # frameworks and projects. 2661 def FrameworksGroup(self): 2662 return self._GroupByName('Frameworks') 2663 2664 def ProjectsGroup(self): 2665 return self._GroupByName('Projects') 2666 2667 def RootGroupForPath(self, path): 2668 """Returns a PBXGroup child of this object to which path should be added. 2669 2670 This method is intended to choose between SourceGroup and 2671 IntermediatesGroup on the basis of whether path is present in a source 2672 directory or an intermediates directory. For the purposes of this 2673 determination, any path located within a derived file directory such as 2674 PROJECT_DERIVED_FILE_DIR is treated as being in an intermediates 2675 directory. 2676 2677 The returned value is a two-element tuple. The first element is the 2678 PBXGroup, and the second element specifies whether that group should be 2679 organized hierarchically (True) or as a single flat list (False). 2680 """ 2681 2682 # TODO(mark): make this a class variable and bind to self on call? 2683 # Also, this list is nowhere near exhaustive. 2684 # INTERMEDIATE_DIR and SHARED_INTERMEDIATE_DIR are used by 2685 # gyp.generator.xcode. There should probably be some way for that module 2686 # to push the names in, rather than having to hard-code them here. 2687 source_tree_groups = { 2688 'DERIVED_FILE_DIR': (self.IntermediatesGroup, True), 2689 'INTERMEDIATE_DIR': (self.IntermediatesGroup, True), 2690 'PROJECT_DERIVED_FILE_DIR': (self.IntermediatesGroup, True), 2691 'SHARED_INTERMEDIATE_DIR': (self.IntermediatesGroup, True), 2692 } 2693 2694 (source_tree, path) = SourceTreeAndPathFromPath(path) 2695 if source_tree != None and source_tree in source_tree_groups: 2696 (group_func, hierarchical) = source_tree_groups[source_tree] 2697 group = group_func() 2698 return (group, hierarchical) 2699 2700 # TODO(mark): make additional choices based on file extension. 2701 2702 return (self.SourceGroup(), True) 2703 2704 def AddOrGetFileInRootGroup(self, path): 2705 """Returns a PBXFileReference corresponding to path in the correct group 2706 according to RootGroupForPath's heuristics. 2707 2708 If an existing PBXFileReference for path exists, it will be returned. 2709 Otherwise, one will be created and returned. 2710 """ 2711 2712 (group, hierarchical) = self.RootGroupForPath(path) 2713 return group.AddOrGetFileByPath(path, hierarchical) 2714 2715 def RootGroupsTakeOverOnlyChildren(self, recurse=False): 2716 """Calls TakeOverOnlyChild for all groups in the main group.""" 2717 2718 for group in self._properties['mainGroup']._properties['children']: 2719 if isinstance(group, PBXGroup): 2720 group.TakeOverOnlyChild(recurse) 2721 2722 def SortGroups(self): 2723 # Sort the children of the mainGroup (like "Source" and "Products") 2724 # according to their defined order. 2725 self._properties['mainGroup']._properties['children'] = \ 2726 sorted(self._properties['mainGroup']._properties['children'], 2727 key=functools.cmp_to_key(XCHierarchicalElement.CompareRootGroup)) 2728 2729 # Sort everything else by putting group before files, and going 2730 # alphabetically by name within sections of groups and files. SortGroup 2731 # is recursive. 2732 for group in self._properties['mainGroup']._properties['children']: 2733 if not isinstance(group, PBXGroup): 2734 continue 2735 2736 if group.Name() == 'Products': 2737 # The Products group is a special case. Instead of sorting 2738 # alphabetically, sort things in the order of the targets that 2739 # produce the products. To do this, just build up a new list of 2740 # products based on the targets. 2741 products = [] 2742 for target in self._properties['targets']: 2743 if not isinstance(target, PBXNativeTarget): 2744 continue 2745 product = target._properties['productReference'] 2746 # Make sure that the product is already in the products group. 2747 assert product in group._properties['children'] 2748 products.append(product) 2749 2750 # Make sure that this process doesn't miss anything that was already 2751 # in the products group. 2752 assert len(products) == len(group._properties['children']) 2753 group._properties['children'] = products 2754 else: 2755 group.SortGroup() 2756 2757 def AddOrGetProjectReference(self, other_pbxproject): 2758 """Add a reference to another project file (via PBXProject object) to this 2759 one. 2760 2761 Returns [ProductGroup, ProjectRef]. ProductGroup is a PBXGroup object in 2762 this project file that contains a PBXReferenceProxy object for each 2763 product of each PBXNativeTarget in the other project file. ProjectRef is 2764 a PBXFileReference to the other project file. 2765 2766 If this project file already references the other project file, the 2767 existing ProductGroup and ProjectRef are returned. The ProductGroup will 2768 still be updated if necessary. 2769 """ 2770 2771 if not 'projectReferences' in self._properties: 2772 self._properties['projectReferences'] = [] 2773 2774 product_group = None 2775 project_ref = None 2776 2777 if not other_pbxproject in self._other_pbxprojects: 2778 # This project file isn't yet linked to the other one. Establish the 2779 # link. 2780 product_group = PBXGroup({'name': 'Products'}) 2781 2782 # ProductGroup is strong. 2783 product_group.parent = self 2784 2785 # There's nothing unique about this PBXGroup, and if left alone, it will 2786 # wind up with the same set of hashables as all other PBXGroup objects 2787 # owned by the projectReferences list. Add the hashables of the 2788 # remote PBXProject that it's related to. 2789 product_group._hashables.extend(other_pbxproject.Hashables()) 2790 2791 # The other project reports its path as relative to the same directory 2792 # that this project's path is relative to. The other project's path 2793 # is not necessarily already relative to this project. Figure out the 2794 # pathname that this project needs to use to refer to the other one. 2795 this_path = posixpath.dirname(self.Path()) 2796 projectDirPath = self.GetProperty('projectDirPath') 2797 if projectDirPath: 2798 if posixpath.isabs(projectDirPath[0]): 2799 this_path = projectDirPath 2800 else: 2801 this_path = posixpath.join(this_path, projectDirPath) 2802 other_path = gyp.common.RelativePath(other_pbxproject.Path(), this_path) 2803 2804 # ProjectRef is weak (it's owned by the mainGroup hierarchy). 2805 project_ref = PBXFileReference({ 2806 'lastKnownFileType': 'wrapper.pb-project', 2807 'path': other_path, 2808 'sourceTree': 'SOURCE_ROOT', 2809 }) 2810 self.ProjectsGroup().AppendChild(project_ref) 2811 2812 ref_dict = {'ProductGroup': product_group, 'ProjectRef': project_ref} 2813 self._other_pbxprojects[other_pbxproject] = ref_dict 2814 self.AppendProperty('projectReferences', ref_dict) 2815 2816 # Xcode seems to sort this list case-insensitively 2817 self._properties['projectReferences'] = \ 2818 sorted(self._properties['projectReferences'], 2819 key=lambda x: x['ProjectRef'].Name().lower()) 2820 else: 2821 # The link already exists. Pull out the relevnt data. 2822 project_ref_dict = self._other_pbxprojects[other_pbxproject] 2823 product_group = project_ref_dict['ProductGroup'] 2824 project_ref = project_ref_dict['ProjectRef'] 2825 2826 self._SetUpProductReferences(other_pbxproject, product_group, project_ref) 2827 2828 inherit_unique_symroot = self._AllSymrootsUnique(other_pbxproject, False) 2829 targets = other_pbxproject.GetProperty('targets') 2830 if all(self._AllSymrootsUnique(t, inherit_unique_symroot) for t in targets): 2831 dir_path = project_ref._properties['path'] 2832 product_group._hashables.extend(dir_path) 2833 2834 return [product_group, project_ref] 2835 2836 def _AllSymrootsUnique(self, target, inherit_unique_symroot): 2837 # Returns True if all configurations have a unique 'SYMROOT' attribute. 2838 # The value of inherit_unique_symroot decides, if a configuration is assumed 2839 # to inherit a unique 'SYMROOT' attribute from its parent, if it doesn't 2840 # define an explicit value for 'SYMROOT'. 2841 symroots = self._DefinedSymroots(target) 2842 for s in self._DefinedSymroots(target): 2843 if (s is not None and not self._IsUniqueSymrootForTarget(s) or 2844 s is None and not inherit_unique_symroot): 2845 return False 2846 return True if symroots else inherit_unique_symroot 2847 2848 def _DefinedSymroots(self, target): 2849 # Returns all values for the 'SYMROOT' attribute defined in all 2850 # configurations for this target. If any configuration doesn't define the 2851 # 'SYMROOT' attribute, None is added to the returned set. If all 2852 # configurations don't define the 'SYMROOT' attribute, an empty set is 2853 # returned. 2854 config_list = target.GetProperty('buildConfigurationList') 2855 symroots = set() 2856 for config in config_list.GetProperty('buildConfigurations'): 2857 setting = config.GetProperty('buildSettings') 2858 if 'SYMROOT' in setting: 2859 symroots.add(setting['SYMROOT']) 2860 else: 2861 symroots.add(None) 2862 if len(symroots) == 1 and None in symroots: 2863 return set() 2864 return symroots 2865 2866 def _IsUniqueSymrootForTarget(self, symroot): 2867 # This method returns True if all configurations in target contain a 2868 # 'SYMROOT' attribute that is unique for the given target. A value is 2869 # unique, if the Xcode macro '$SRCROOT' appears in it in any form. 2870 uniquifier = ['$SRCROOT', '$(SRCROOT)'] 2871 if any(x in symroot for x in uniquifier): 2872 return True 2873 return False 2874 2875 def _SetUpProductReferences(self, other_pbxproject, product_group, 2876 project_ref): 2877 # TODO(mark): This only adds references to products in other_pbxproject 2878 # when they don't exist in this pbxproject. Perhaps it should also 2879 # remove references from this pbxproject that are no longer present in 2880 # other_pbxproject. Perhaps it should update various properties if they 2881 # change. 2882 for target in other_pbxproject._properties['targets']: 2883 if not isinstance(target, PBXNativeTarget): 2884 continue 2885 2886 other_fileref = target._properties['productReference'] 2887 if product_group.GetChildByRemoteObject(other_fileref) is None: 2888 # Xcode sets remoteInfo to the name of the target and not the name 2889 # of its product, despite this proxy being a reference to the product. 2890 container_item = PBXContainerItemProxy({ 2891 'containerPortal': project_ref, 2892 'proxyType': 2, 2893 'remoteGlobalIDString': other_fileref, 2894 'remoteInfo': target.Name() 2895 }) 2896 # TODO(mark): Does sourceTree get copied straight over from the other 2897 # project? Can the other project ever have lastKnownFileType here 2898 # instead of explicitFileType? (Use it if so?) Can path ever be 2899 # unset? (I don't think so.) Can other_fileref have name set, and 2900 # does it impact the PBXReferenceProxy if so? These are the questions 2901 # that perhaps will be answered one day. 2902 reference_proxy = PBXReferenceProxy({ 2903 'fileType': other_fileref._properties['explicitFileType'], 2904 'path': other_fileref._properties['path'], 2905 'sourceTree': other_fileref._properties['sourceTree'], 2906 'remoteRef': container_item, 2907 }) 2908 2909 product_group.AppendChild(reference_proxy) 2910 2911 def SortRemoteProductReferences(self): 2912 # For each remote project file, sort the associated ProductGroup in the 2913 # same order that the targets are sorted in the remote project file. This 2914 # is the sort order used by Xcode. 2915 2916 for other_pbxproject, ref_dict in self._other_pbxprojects.items(): 2917 # Build up a list of products in the remote project file, ordered the 2918 # same as the targets that produce them. 2919 remote_products = [] 2920 for target in other_pbxproject._properties['targets']: 2921 if not isinstance(target, PBXNativeTarget): 2922 continue 2923 remote_products.append(target._properties['productReference']) 2924 2925 # Sort the PBXReferenceProxy children according to the list of remote 2926 # products. 2927 product_group = ref_dict['ProductGroup'] 2928 product_group._properties['children'] = sorted( 2929 product_group._properties['children'], 2930 key=lambda x: remote_products.index(x._properties['remoteRef']._properties['remoteGlobalIDString'])) 2931 2932 2933class XCProjectFile(XCObject): 2934 _schema = XCObject._schema.copy() 2935 _schema.update({ 2936 'archiveVersion': [0, int, 0, 1, 1], 2937 'classes': [0, dict, 0, 1, {}], 2938 'objectVersion': [0, int, 0, 1, 46], 2939 'rootObject': [0, PBXProject, 1, 1], 2940 }) 2941 2942 def ComputeIDs(self, recursive=True, overwrite=True, hash=None): 2943 # Although XCProjectFile is implemented here as an XCObject, it's not a 2944 # proper object in the Xcode sense, and it certainly doesn't have its own 2945 # ID. Pass through an attempt to update IDs to the real root object. 2946 if recursive: 2947 self._properties['rootObject'].ComputeIDs(recursive, overwrite, hash) 2948 2949 def Print(self, file=sys.stdout): 2950 self.VerifyHasRequiredProperties() 2951 2952 # Add the special "objects" property, which will be caught and handled 2953 # separately during printing. This structure allows a fairly standard 2954 # loop do the normal printing. 2955 self._properties['objects'] = {} 2956 self._XCPrint(file, 0, '// !$*UTF8*$!\n') 2957 if self._should_print_single_line: 2958 self._XCPrint(file, 0, '{ ') 2959 else: 2960 self._XCPrint(file, 0, '{\n') 2961 for property, value in sorted(self._properties.items()): 2962 if property == 'objects': 2963 self._PrintObjects(file) 2964 else: 2965 self._XCKVPrint(file, 1, property, value) 2966 self._XCPrint(file, 0, '}\n') 2967 del self._properties['objects'] 2968 2969 def _PrintObjects(self, file): 2970 if self._should_print_single_line: 2971 self._XCPrint(file, 0, 'objects = {') 2972 else: 2973 self._XCPrint(file, 1, 'objects = {\n') 2974 2975 objects_by_class = {} 2976 for object in self.Descendants(): 2977 if object == self: 2978 continue 2979 class_name = object.__class__.__name__ 2980 if not class_name in objects_by_class: 2981 objects_by_class[class_name] = [] 2982 objects_by_class[class_name].append(object) 2983 2984 for class_name in sorted(objects_by_class): 2985 self._XCPrint(file, 0, '\n') 2986 self._XCPrint(file, 0, '/* Begin ' + class_name + ' section */\n') 2987 for object in sorted(objects_by_class[class_name], 2988 key=lambda x: x.id): 2989 object.Print(file) 2990 self._XCPrint(file, 0, '/* End ' + class_name + ' section */\n') 2991 2992 if self._should_print_single_line: 2993 self._XCPrint(file, 0, '}; ') 2994 else: 2995 self._XCPrint(file, 1, '};\n') 2996