forked from jverkoey/nimbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbxproj.py
1157 lines (823 loc) · 40.2 KB
/
pbxproj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# encoding: utf-8
"""
pbxproj.py
Working with the pbxproj file format is a pain in the ass.
This script provides a couple basic features for parsing pbxproj files:
* Getting a dependency list
* Adding one pbxproj to another pbxproj as a dependency
Version 1.2.
History:
1.0 - October 20, 2010: Initial hacked-together version finished. It is alive!
1.1 - January 11, 2011: Add configuration settings to all configurations by default.
1.2 - June 7, 2011: Rewrote the pbxproj family of code as an ios module and made the class
more generic (no assumptions made about the project layout).
Branched from Three20's ttmodule script 2011-06-07.
Created by Jeff Verkoeyen on 2010-10-18.
Copyright 2011 Jeff Verkoeyen
Copyright 2009-2011 Facebook
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import hashlib
import logging
import os
import re
import sys
from relpath import relpath
pbxproj_cache = {}
class PbxprojTarget(object):
def __init__(self, name, project, guid = None):
self._name = name
self._project = project
# This target's GUID.
self._guid = guid
# The configuration list GUID points to the list of configurations for a given target.
self._configuration_list_guid = None
# The list of configuration GUIDs for this target.
self._configuration_guids = None
# The GUID for the resources build phase.
self._resources_build_phase_guid = None
# The GUID for the frameworks builds phase.
self._frameworks_build_phase_guid = None
# An array of dependency GUIDs.
self._dependency_guids = None
# An array of dependency names in the same order as the dependency guids array.
self._dependency_names = None
# An array of path:target strings.
self._dependency_paths = None
# This target's product GUID.
self._product_guid = None
# This target's product name.
self._product_name = None
def name(self):
return self._name
def configuration_list_guid(self):
if self._configuration_list_guid is None:
project_data = project.get_project_data()
result = re.search('[A-Z0-9]+ \/\* '+re.escape(self._name)+' \*\/ = {\n[ \t]+isa = PBXNativeTarget;(?:.|\n)+?buildConfigurationList = ([A-Z0-9]+) \/\* Build configuration list for PBXNativeTarget "'+re.escape(self._name)+'" \*\/;',
project_data)
if result:
(self._configuration_list_guid, ) = result.groups()
else:
# False indicates that we could not find a configuration list GUID.
self._configuration_list_guid = False
return self._configuration_list_guid
def configuration_guids(self):
if not self._configuration_guids and self.configuration_list_guid():
project_data = project.get_project_data()
match = re.search(re.escape(self.configuration_list_guid())+' \/\* Build configuration list for PBXNativeTarget "'+re.escape(self._name)+'" \*\/ = \{\n[ \t]+isa = XCConfigurationList;\n[ \t]+buildConfigurations = \(\n((?:.|\n)+?)\);', project_data)
if not match:
logging.error("Couldn't find the configuration list for the project.")
return False
(configuration_list,) = match.groups()
self._configuration_guids = re.findall('[ \t]+([A-Z0-9]+) \/\* (.+) \*\/,\n', configuration_list)
return self._configuration_guids
def guid(self):
if not self._guid:
project_data = self._project.get_project_data()
result = re.search('([A-Z0-9]+) \/\* '+re.escape(self._name)+' \*\/ = {\n[ \t]+isa = PBXNativeTarget;(?:.|\n)+?buildPhases =',
project_data)
if not result:
logging.error("Can't recover: Unable to find the GUID for the target named \""+self._name+"\" from the project loaded from: "+self._project.path())
return False
(self._guid, ) = result.groups()
return self._guid
def _gather_build_phases(self):
project_data = self._project.get_project_data()
result = re.search('[A-Z0-9]+ \/\* '+re.escape(self._name)+' \*\/ = {\n[ \t]+isa = PBXNativeTarget;(?:.|\n)+?buildPhases = \(\n((?:.|\n)+?)\);',
project_data)
if not result:
logging.error("Can't recover: Unable to find the build phases for the target named \""+self._name+"\" from the project loaded from: "+self._project.path())
return False
(build_phases, ) = result.groups()
# Get the build phases we care about.
match = re.search('([A-Z0-9]+) \/\* Resources \*\/', build_phases)
if match:
(self._resources_build_phase_guid, ) = match.groups()
else:
self._resources_build_phase_guid = False
match = re.search('([A-Z0-9]+) \/\* Frameworks \*\/', build_phases)
if not match:
logging.error("Couldn't find the Frameworks phase for the target named \""+self._name+"\" from the project loaded from: "+self._project.path())
logging.error("Please add a New Link Binary With Libraries Build Phase to your target")
logging.error("Right click your target in the project, then click Add, then New Build Phase,")
logging.error(" \"New Link Binary With Libraries Build Phase\"")
return False
(self._frameworks_build_phase_guid, ) = match.groups()
def resources_build_phase_guid(self):
if not self._resources_build_phase_guid:
self._gather_build_phases()
return self._resources_build_phase_guid
def frameworks_build_phase_guid(self):
if not self._frameworks_build_phase_guid:
self._gather_build_phases()
return self._frameworks_build_phase_guid
def dependency_guids(self):
if not self._dependency_guids:
project_data = self._project.get_project_data()
result = re.search(re.escape(self.guid())+' \/\* '+re.escape(self._name)+' \*\/ = {\n[ \t]+isa = PBXNativeTarget;(?:.|\n)+?dependencies = \(\n((?:[ \t]+[A-Z0-9]+ \/\* PBXTargetDependency \*\/,\n)*)[ \t]*\);\n',
project_data)
if not result:
logging.error("Unable to get dependencies from: "+self._project.path())
return False
(dependency_set, ) = result.groups()
self._dependency_guids = re.findall('[ \t]+([A-Z0-9]+) \/\* PBXTargetDependency \*\/,\n', dependency_set)
return self._dependency_guids
def dependency_names(self):
if not self._dependency_names:
project_data = self._project.get_project_data()
dependency_names = []
for guid in self.dependency_guids():
result = re.search(guid+' \/\* PBXTargetDependency \*\/ = \{\n[ \t]+isa = PBXTargetDependency;\n[ \t]*name = (["a-zA-Z0-9\.\-]+);',
project_data)
if result:
(dependency_name, ) = result.groups()
dependency_names.append(dependency_name)
self._dependency_names = dependency_names
return self._dependency_names
def dependency_paths(self):
if self._dependency_paths:
return self._dependency_paths
dependency_guids = self.dependency_guids()
if dependency_guids:
project_data = self._project.get_project_data()
target_proxy_guids = []
for guid in dependency_guids:
result = re.search(guid+' \/\* PBXTargetDependency \*\/ = \{\n(?:.|\n)+?targetProxy = ([A-Z0-9]+) \/\* PBXContainerItemProxy \*\/;',
project_data)
if result:
(target_proxy_guid, ) = result.groups()
target_proxy_guids.append(target_proxy_guid)
container_portal_guids = []
remote_guids = []
for guid in target_proxy_guids:
result = re.search(guid+' \/\* PBXContainerItemProxy \*\/ = \{\n(?:.|\n)+?containerPortal = ([A-Z0-9]+) \/\* .+? \*\/;(?:.|\n)+?remoteGlobalIDString = ([A-Z0-9]+)',
project_data)
if result:
(container_portal_guid, remote_guid, ) = result.groups()
container_portal_guids.append(container_portal_guid)
remote_guids.append(remote_guid)
dependency_paths = []
index = 0
for guid in container_portal_guids:
result = re.search(guid+' \/\* .+? \*\/ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = .+?; path = (.+?); sourceTree = .+?; };',
project_data)
if result:
(dependency_path, ) = result.groups()
dependency_path = dependency_path + ":" + remote_guids[index]
dependency_paths.append(dependency_path)
else:
logging.error("Unable to find the path for GUID: "+guid)
index += 1
if len(dependency_paths) != len(container_portal_guids):
logging.error("Unable to load all dependency information from the project.")
return False
self._dependency_paths = dependency_paths
elif dependency_guids is not None and dependency_guids is not False and len(dependency_guids) == 0:
self._dependency_paths = []
return self._dependency_paths
def _gather_product_details(self):
project_data = self._project.get_project_data()
result = re.search(re.escape(self.guid())+' \/\* '+re.escape(self._name)+' \*\/ = {\n[ \t]+isa = PBXNativeTarget;(?:.|\n)+?productReference = ([A-Z0-9]+) \/\* (.+?) \*\/;',
project_data)
if not result:
logging.error("Unable to get product guid from: "+self.path())
return False
(self._product_guid, self._product_name, ) = result.groups()
return True
def product_guid(self):
if not self._product_guid:
self._gather_product_details()
return self._product_guid
def product_name(self):
if not self._product_name:
self._gather_product_details()
return self._product_name
class pbxproj(object):
@staticmethod
def get_pbxproj_by_path(path, xcode_version = None):
if path not in pbxproj_cache:
pbxproj_cache[path] = pbxproj(path, xcode_version = xcode_version)
return pbxproj_cache[path]
def __init__(self, path, xcode_version = None):
# The contents of the pbxproj file loaded into memory.
self._project_data = None
self._active_target = None
self._project_name = os.path.basename(os.path.dirname(path)).replace('.xcodeproj', '')
# The path to the pbxproj file.
self._path = path
# Mapping of target names to PbxprojTarget objects.
self._targets_by_name = {}
# Mapping of target guids to PbxprojTarget objects.
self._targets_by_guid = {}
# ???
self._xcode_version = xcode_version
# The file format version for this project.
self._file_format_version = None
self._is_loaded = self._load_from_disk()
def __str__(self):
details = "\t path: \""+str(self._path)+"\"\n\ttargets:"
for target_name in self._targets_by_name:
target = self._targets_by_name[target_name]
details += "\n -> "+target.name() + " ("+target.guid()+")"
if self._active_target:
details += "\n active target: "+self._active_target
return details
def is_loaded(self):
return self._is_loaded
def uniqueid_for_target(self, target):
return self._path + ':' + target
def path(self):
return self._path
def active_target(self):
if self._active_target is None:
return None
return self.target_by_name(self._active_target)
def set_active_target(self, target_name):
self._active_target = target_name
# A pbxproj file is contained within an xcodeproj file.
# This method simply strips off the project.pbxproj part of the path.
def xcodeprojpath(self):
return os.path.dirname(self.path())
def version(self):
if not self._file_format_version:
result = re.search('\tobjectVersion = ([0-9]+);', project_data)
if not result:
logging.error("Can't recover: unable to find the project version for your target at: "+self.path())
return False
(self._file_format_version,) = result.groups()
self._file_format_version = int(self._file_format_version)
return self._file_format_version
# Fetch a specific target by its name.
def target_by_name(self, name):
if name in self._targets_by_guid:
target = self._targets_by_guid[name]
target._guid = name
return target
if name not in self._targets_by_name:
target = PbxprojTarget(name, self)
self._targets_by_name[name] = target
self._targets_by_guid[target.guid()] = target
return self._targets_by_name[name]
# Load the project data from disk.
def get_project_data(self):
if self._project_data is None:
if not os.path.exists(self.path()):
logging.info("Couldn't find the project at this path:")
logging.info(self.path())
return None
project_file = open(self.path(), 'r')
self._project_data = project_file.read()
return self._project_data
# Write the project data to disk.
def set_project_data(self, project_data, flush=False):
if self._project_data != project_data or flush:
self._project_data = project_data
if flush:
project_file = open(self.path(), 'w')
project_file.write(self._project_data)
def _load_from_disk(self):
project_data = self.get_project_data()
if project_data is None:
logging.error("Can't recover: unable to load the project data from disk, check the path:\n path: \""+self.path()+"\"")
return False
self._gather_all_targets()
return True
def _gather_all_targets(self):
project_data = self.get_project_data()
result = re.search('targets = \(\n((?:.|\n)+?)\);',
project_data)
if not result:
logging.error("Couldn't find any targets.")
return None
(target_list, ) = result.groups()
targets = re.findall('([A-Z0-9]+) \/\* (.+?) \*\/', target_list)
if not targets:
logging.error("Unable to read the targets.")
return None
for target in targets:
name = target[1]
target = PbxprojTarget(name, project = self, guid = target[0])
self._targets_by_name[name] = target
self._targets_by_guid[target.guid()] = target
return True
def dependency_names_for_target_name(self, target_name):
target = self.target_by_name(target_name)
return target.dependency_names()
def dependency_paths_for_target_name(self, target_name):
target = self.target_by_name(target_name)
return target.dependency_paths()
# Add a line to the PBXBuildFile section.
#
# <default_guid> /* <name> in Frameworks */ = {isa = PBXBuildFile; fileRef = <file_ref_hash> /* <name> */; };
#
# Returns: <default_guid> if a line was added.
# Otherwise, the existing guid is returned.
def add_buildfile(self, name, file_ref_hash, default_guid):
project_data = self.get_project_data()
match = re.search('\/\* Begin PBXBuildFile section \*\/\n((?:.|\n)+?)\/\* End PBXBuildFile section \*\/', project_data)
if not match:
logging.error("Couldn't find PBXBuildFile section.")
return None
(subtext, ) = match.groups()
buildfile_hash = None
match = re.search('([A-Z0-9]+).+?fileRef = '+re.escape(file_ref_hash), subtext)
if match:
(buildfile_hash, ) = match.groups()
logging.info("This build file already exists: "+buildfile_hash)
if buildfile_hash is None:
match = re.search('\/\* Begin PBXBuildFile section \*\/\n', project_data)
buildfile_hash = default_guid
libfiletext = "\t\t"+buildfile_hash+" /* "+name+" in Frameworks */ = {isa = PBXBuildFile; fileRef = "+file_ref_hash+" /* "+name+" */; };\n"
project_data = project_data[:match.end()] + libfiletext + project_data[match.end():]
self.set_project_data(project_data)
return buildfile_hash
# Add a line to the PBXFileReference section.
#
# <default_guid> /* <name> */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.<file_type>"; name = <name>; path = <rel_path>; sourceTree = <source_tree>; };
#
# Returns: <default_guid> if a line was added.
# Otherwise, the existing guid is returned.
def add_filereference(self, name, file_type, default_guid, rel_path, source_tree):
project_data = self.get_project_data()
quoted_rel_path = '"'+rel_path.strip('"')+'"'
fileref_hash = None
match = re.search('([A-Z0-9]+) \/\* '+re.escape(name)+' \*\/ = \{isa = PBXFileReference; lastKnownFileType = "wrapper.'+file_type+'"; name = '+re.escape(name)+'; path = '+re.escape(rel_path)+';', project_data)
if not match:
# Check again for quoted versions, just to be sure.
match = re.search('([A-Z0-9]+) \/\* '+re.escape(name)+' \*\/ = \{isa = PBXFileReference; lastKnownFileType = "wrapper.'+file_type+'"; name = '+re.escape(name)+'; path = '+re.escape(quoted_rel_path)+';', project_data)
if match:
logging.info("This file has already been added.")
(fileref_hash, ) = match.groups()
else:
match = re.search('\/\* Begin PBXFileReference section \*\/\n', project_data)
if not match:
logging.error("Couldn't find the PBXFileReference section.")
return False
fileref_hash = default_guid
pbxfileref = "\t\t"+fileref_hash+" /* "+name+" */ = {isa = PBXFileReference; lastKnownFileType = \"wrapper."+file_type+"\"; name = "+name+"; path = "+quoted_rel_path+"; sourceTree = "+source_tree+"; };\n"
project_data = project_data[:match.end()] + pbxfileref + project_data[match.end():]
self.set_project_data(project_data)
return fileref_hash
# Add a file to the given PBXGroup.
#
# <guid> /* <name> */,
def add_file_to_group(self, name, guid, group):
project_data = self.get_project_data()
match = re.search('\/\* '+re.escape(group)+' \*\/ = \{\n[ \t]+isa = PBXGroup;\n[ \t]+children = \(\n((?:.|\n)+?)\);', project_data)
if not match:
logging.error("Couldn't find the "+group+" children.")
return False
(children,) = match.groups()
match = re.search(re.escape(guid), children)
if match:
logging.info("This file is already a member of the "+name+" group.")
else:
match = re.search('\/\* '+re.escape(group)+' \*\/ = \{\n[ \t]+isa = PBXGroup;\n[ \t]+children = \(\n', project_data)
if not match:
logging.error("Couldn't find the "+group+" group.")
return False
pbxgroup = "\t\t\t\t"+guid+" /* "+name+" */,\n"
project_data = project_data[:match.end()] + pbxgroup + project_data[match.end():]
self.set_project_data(project_data)
return True
# Add a file to the Frameworks PBXGroup.
#
# <guid> /* <name> */,
def add_file_to_frameworks(self, name, guid):
return self.add_file_to_group(name, guid, 'Frameworks')
# Add a file to the Resources PBXGroup.
#
# <guid> /* <name> */,
def add_file_to_resources(self, name, guid):
match = re.search('\/\* '+re.escape('Resources')+' \*\/ = \{\n[ \t]+isa = PBXGroup;\n[ \t]+children = \(\n((?:.|\n)+?)\);', self.get_project_data())
if not match:
return self.add_file_to_group(name, guid, 'Supporting Files')
return self.add_file_to_group(name, guid, 'Resources')
def add_file_to_phase(self, name, guid, phase_guid, phase):
project_data = self.get_project_data()
match = re.search(re.escape(phase_guid)+" \/\* "+re.escape(phase)+" \*\/ = {(?:.|\n)+?files = \(((?:.|\n)+?)\);", project_data)
if not match:
logging.error("Couldn't find the "+phase+" phase.")
return False
(files, ) = match.groups()
match = re.search(re.escape(guid), files)
if match:
logging.info("The file has already been added.")
else:
match = re.search(re.escape(phase_guid)+" \/\* "+phase+" \*\/ = {(?:.|\n)+?files = \(\n", project_data)
if not match:
logging.error("Couldn't find the "+phase+" files")
return False
frameworktext = "\t\t\t\t"+guid+" /* "+name+" in "+phase+" */,\n"
project_data = project_data[:match.end()] + frameworktext + project_data[match.end():]
self.set_project_data(project_data)
return True
def get_rel_path_to_products_dir(self):
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
build_path = os.path.join(os.path.join(os.path.dirname(Paths.src_dir), 'Build'), 'Products')
return relpath(project_path, build_path)
def add_file_to_frameworks_phase(self, name, guid):
target = self.active_target()
return self.add_file_to_phase(name, guid, target.frameworks_build_phase_guid(), 'Frameworks')
def add_file_to_resources_phase(self, name, guid):
if self._resources_guid is None:
logging.error("No resources build phase found in the destination project")
logging.error("Please add a New Copy Bundle Resources Build Phase to your target")
logging.error("Right click your target in the project, Add, New Build Phase,")
logging.error(" \"New Copy Bundle Resources Build Phase\"")
return False
return self.add_file_to_phase(name, guid, self._resources_guid, 'Resources')
def add_header_search_path(self, configuration):
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
build_path = os.path.join(os.path.join(os.path.join(os.path.dirname(Paths.src_dir), 'Build'), 'Products'), 'three20')
rel_path = relpath(project_path, build_path)
did_add_build_setting = self.add_build_setting(configuration, 'HEADER_SEARCH_PATHS', '"'+rel_path+'"')
if not did_add_build_setting:
return did_add_build_setting
# Version 46 is Xcode 4's file format.
try:
primary_version = int(self._xcode_version.split('.')[0])
except ValueError, e:
primary_version = 0
if self._file_format_version >= 46 or primary_version >= 4:
did_add_build_setting = self.add_build_setting(configuration, 'HEADER_SEARCH_PATHS', '"$(BUILT_PRODUCTS_DIR)/../../three20"')
if not did_add_build_setting:
return did_add_build_setting
did_add_build_setting = self.add_build_setting(configuration, 'HEADER_SEARCH_PATHS', '"$(BUILT_PRODUCTS_DIR)/../three20"')
if not did_add_build_setting:
return did_add_build_setting
return did_add_build_setting
def add_build_setting(self, configuration, setting_name, value):
project_data = self.get_project_data()
match = re.search('\/\* '+configuration+' \*\/ = {\n[ \t]+isa = XCBuildConfiguration;\n[ \t]+buildSettings = \{\n((?:.|\n)+?)\};', project_data)
if not match:
logging.error("Couldn't find this configuration.")
return False
settings_start = match.start(1)
settings_end = match.end(1)
(build_settings, ) = match.groups()
match = re.search(re.escape(setting_name)+' = ((?:.|\n)+?);', build_settings)
if not match:
# Add a brand new build setting. No checking for existing settings necessary.
settingtext = '\t\t\t\t'+setting_name+' = '+value+';\n'
project_data = project_data[:settings_start] + settingtext + project_data[settings_start:]
else:
# Build settings already exist. Is there one or many?
(search_paths,) = match.groups()
if re.search('\(\n', search_paths):
# Many
match = re.search(re.escape(value), search_paths)
if not match:
# If value has any spaces in it, Xcode will split it up into
# multiple entries.
escaped_value = re.escape(value).replace(' ', '",\n[ \t]+"')
match = re.search(escaped_value, search_paths)
if not match and not re.search(re.escape(value.strip('"')), search_paths):
match = re.search(re.escape(setting_name)+' = \(\n', build_settings)
build_settings = build_settings[:match.end()] + '\t\t\t\t\t'+value+',\n' + build_settings[match.end():]
project_data = project_data[:settings_start] + build_settings + project_data[settings_end:]
else:
# One
if search_paths.strip('"') != value.strip('"'):
existing_path = search_paths
path_set = '(\n\t\t\t\t\t'+value+',\n\t\t\t\t\t'+existing_path+'\n\t\t\t\t)'
build_settings = build_settings[:match.start(1)] + path_set + build_settings[match.end(1):]
project_data = project_data[:settings_start] + build_settings + project_data[settings_end:]
self.set_project_data(project_data)
return True
def get_hash_base(self, uniquename):
examplehash = '320FFFEEEDDDCCCBBBAAA000'
uniquehash = hashlib.sha224(uniquename).hexdigest().upper()
uniquehash = uniquehash[:len(examplehash) - 4]
return '320'+uniquehash
def add_framework(self, framework):
tthash_base = self.get_hash_base(framework)
fileref_hash = self.add_filereference(framework, 'frameworks', tthash_base+'0', 'System/Library/Frameworks/'+framework, 'SDKROOT')
libfile_hash = self.add_buildfile(framework, fileref_hash, tthash_base+'1')
if not self.add_file_to_frameworks(framework, fileref_hash):
return False
if not self.add_file_to_frameworks_phase(framework, libfile_hash):
return False
return True
def add_bundle(self):
tthash_base = self.get_hash_base('Three20.bundle')
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
build_path = os.path.join(Paths.src_dir, 'Three20.bundle')
rel_path = relpath(project_path, build_path)
fileref_hash = self.add_filereference('Three20.bundle', 'plug-in', tthash_base+'0', rel_path, 'SOURCE_ROOT')
libfile_hash = self.add_buildfile('Three20.bundle', fileref_hash, tthash_base+'1')
if not self.add_file_to_resources('Three20.bundle', fileref_hash):
return False
if not self.add_file_to_resources_phase('Three20.bundle', libfile_hash):
return False
return True
# Get the PBXFileReference from the given PBXBuildFile guid.
def get_filerefguid_from_buildfileguid(self, buildfileguid):
project_data = self.get_project_data()
match = re.search(buildfileguid+' \/\* .+ \*\/ = {isa = PBXBuildFile; fileRef = ([A-Z0-9]+) \/\* .+ \*\/;', project_data)
if not match:
logging.error("Couldn't find PBXBuildFile row.")
return None
(filerefguid, ) = match.groups()
return filerefguid
def get_filepath_from_filerefguid(self, filerefguid):
project_data = self.get_project_data()
match = re.search(filerefguid+' \/\* .+ \*\/ = {isa = PBXFileReference; .+ path = (.+); .+ };', project_data)
if not match:
logging.error("Couldn't find PBXFileReference row.")
return None
(path, ) = match.groups()
return path
# Get all source files that are "built" in this project. This includes files built for
# libraries, executables, and unit testing.
def get_built_sources(self):
project_data = self.get_project_data()
match = re.search('\/\* Begin PBXSourcesBuildPhase section \*\/\n((?:.|\n)+?)\/\* End PBXSourcesBuildPhase section \*\/', project_data)
if not match:
logging.error("Couldn't find PBXSourcesBuildPhase section.")
return None
(buildphasedata, ) = match.groups()
buildfileguids = re.findall('[ \t]+([A-Z0-9]+) \/\* .+ \*\/,\n', buildphasedata)
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
filenames = []
for buildfileguid in buildfileguids:
filerefguid = self.get_filerefguid_from_buildfileguid(buildfileguid)
filepath = self.get_filepath_from_filerefguid(filerefguid)
filenames.append(os.path.join(project_path, filepath.strip('"')))
return filenames
# Get all header files that are "built" in this project. This includes files built for
# libraries, executables, and unit testing.
def get_built_headers(self):
project_data = self.get_project_data()
match = re.search('\/\* Begin PBXHeadersBuildPhase section \*\/\n((?:.|\n)+?)\/\* End PBXHeadersBuildPhase section \*\/', project_data)
if not match:
logging.error("Couldn't find PBXHeadersBuildPhase section.")
return None
(buildphasedata, ) = match.groups()
buildfileguids = re.findall('[ \t]+([A-Z0-9]+) \/\* .+ \*\/,\n', buildphasedata)
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
filenames = []
for buildfileguid in buildfileguids:
filerefguid = self.get_filerefguid_from_buildfileguid(buildfileguid)
filepath = self.get_filepath_from_filerefguid(filerefguid)
filenames.append(os.path.join(project_path, filepath.strip('"')))
return filenames
def add_dependency(self, dep):
project_data = self.get_project_data()
dep_data = dep.get_project_data()
project_target = self.active_target()
dep_target = dep.active_target()
if project_data is None or dep_data is None:
return False
logging.info("\nAdding "+str(dep)+"\nto\n"+str(self))
project_path = os.path.dirname(os.path.abspath(self.xcodeprojpath()))
dep_path = os.path.abspath(dep.xcodeprojpath())
rel_path = relpath(project_path, dep_path)
logging.info("")
logging.info("Project path: "+project_path)
logging.info("Dependency path: "+dep_path)
logging.info("Relative path: "+rel_path)
tthash_base = self.get_hash_base(dep.uniqueid_for_target(dep._active_target))
###############################################
logging.info("")
logging.info("Step 1: Add file reference to the dependency...")
pbxfileref_hash = self.add_filereference(dep._project_name+'.xcodeproj', 'pb-project', tthash_base+'0', rel_path, 'SOURCE_ROOT')
project_data = self.get_project_data()
logging.info("Done: Added file reference: "+pbxfileref_hash)
self.set_project_data(project_data)
###############################################
logging.info("")
logging.info("Step 2: Add file to Frameworks group...")
if not self.add_file_to_frameworks(dep._project_name+".xcodeproj", pbxfileref_hash):
return False
project_data = self.get_project_data()
logging.info("Done: Added file to Frameworks group.")
self.set_project_data(project_data)
###############################################
logging.info("")
logging.info("Step 3: Add dependencies...")
pbxtargetdependency_hash = None
pbxcontaineritemproxy_hash = None
match = re.search('\/\* Begin PBXTargetDependency section \*\/\n((?:.|\n)+?)\/\* End PBXTargetDependency section \*\/', project_data)
if not match:
logging.info("\tAdding a PBXTargetDependency section...")
match = re.search('\/\* End PBXSourcesBuildPhase section \*\/\n', project_data)
if not match:
logging.error("Couldn't find the PBXSourcesBuildPhase section.")
return False
project_data = project_data[:match.end()] + "\n/* Begin PBXTargetDependency section */\n\n/* End PBXTargetDependency section */\n" + project_data[match.end():]
else:
(subtext, ) = match.groups()
match = re.search('([A-Z0-9]+) \/\* PBXTargetDependency \*\/ = {\n[ \t]+isa = PBXTargetDependency;\n[ \t]+name = '+re.escape(dep._project_name)+';\n[ \t]+targetProxy = ([A-Z0-9]+) \/\* PBXContainerItemProxy \*\/;', project_data)
if match:
(pbxtargetdependency_hash, pbxcontaineritemproxy_hash,) = match.groups()
logging.info("This dependency already exists.")
self.set_project_data(project_data)
if pbxtargetdependency_hash is None or pbxcontaineritemproxy_hash is None:
match = re.search('\/\* Begin PBXTargetDependency section \*\/\n', project_data)
pbxtargetdependency_hash = tthash_base+'1'
pbxcontaineritemproxy_hash = tthash_base+'2'
pbxtargetdependency = "\t\t"+pbxtargetdependency_hash+" /* PBXTargetDependency */ = {\n\t\t\tisa = PBXTargetDependency;\n\t\t\tname = "+dep._project_name+";\n\t\t\ttargetProxy = "+pbxcontaineritemproxy_hash+" /* PBXContainerItemProxy */;\n\t\t};\n"
project_data = project_data[:match.end()] + pbxtargetdependency + project_data[match.end():]
logging.info("Done: Added dependency.")
self.set_project_data(project_data)
###############################################
logging.info("")
logging.info("Step 3.1: Add container proxy for dependencies...")
containerExists = False
match = re.search('\/\* Begin PBXContainerItemProxy section \*\/\n((?:.|\n)+?)\/\* End PBXContainerItemProxy section \*\/', project_data)
if not match:
logging.info("\tAdding a PBXContainerItemProxy section...")
match = re.search('\/\* End PBXBuildFile section \*\/\n', project_data)
if not match:
logging.error("Couldn't find the PBXBuildFile section.")
return False
project_data = project_data[:match.end()] + "\n/* Begin PBXContainerItemProxy section */\n\n/* End PBXContainerItemProxy section */\n" + project_data[match.end():]
else:
(subtext, ) = match.groups()
match = re.search(re.escape(pbxcontaineritemproxy_hash), subtext)
if match:
logging.info("This container proxy already exists.")
containerExists = True
self.set_project_data(project_data)
if not containerExists:
match = re.search('\/\* Begin PBXContainerItemProxy section \*\/\n', project_data)
pbxcontaineritemproxy = "\t\t"+pbxcontaineritemproxy_hash+" /* PBXContainerItemProxy */ = {\n\t\t\tisa = PBXContainerItemProxy;\n\t\t\tcontainerPortal = "+pbxfileref_hash+" /* "+dep._project_name+".xcodeproj */;\n\t\t\tproxyType = 1;\n\t\t\tremoteGlobalIDString = "+dep_target.guid()+";\n\t\t\tremoteInfo = "+dep._project_name+";\n\t\t};\n"
project_data = project_data[:match.end()] + pbxcontaineritemproxy + project_data[match.end():]
logging.info("Done: Added container proxy.")
self.set_project_data(project_data)
###############################################
logging.info("")
logging.info("Step 3.2: Add module to the dependency list...")
match = re.search(project_target.guid()+' \/\* .+? \*\/ = {\n[ \t]+(?:.|\n)+?[ \t]+dependencies = \(\n((?:.|\n)+?)\);', project_data)
dependency_exists = False
if not match:
logging.error("Couldn't find the dependency list.")
return False
else:
(dependencylist, ) = match.groups()
match = re.search(re.escape(pbxtargetdependency_hash), dependencylist)
if match:
logging.info("This dependency has already been added.")
dependency_exists = True
if not dependency_exists:
match = re.search(project_target.guid()+' \/\* .+? \*\/ = {\n[ \t]+(?:.|\n)+?[ \t]+dependencies = \(\n', project_data)
if not match:
logging.error("Couldn't find the dependency list.")
return False
dependency_item = '\t\t\t\t'+pbxtargetdependency_hash+' /* PBXTargetDependency */,\n'
project_data = project_data[:match.end()] + dependency_item + project_data[match.end():]
logging.info("Done: Added module to the dependency list.")
self.set_project_data(project_data)
###############################################
logging.info("")
logging.info("Step 4: Create project references...")
match = re.search('\/\* Begin PBXProject section \*\/\n((?:.|\n)+?)\/\* End PBXProject section \*\/', project_data)
if not match:
logging.error("Couldn't find the project section.")
return False
project_start = match.start(1)
project_end = match.end(1)
(project_section, ) = match.groups()
reference_exists = False
did_change = False
productgroup_hash = None
match = re.search('projectReferences = \(\n((?:.|\n)+?)\n[ \t]+\);', project_section)
if not match:
logging.info("Creating project references...")
match = re.search('projectDirPath = ".*?";\n', project_section)
if not match:
logging.error("Couldn't find project references anchor.")
return False
did_change = True
project_section = project_section[:match.end()] + '\t\t\tprojectReferences = (\n\t\t\t);\n' + project_section[match.end():]
else:
(refs, ) = match.groups()