-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPBXProject.cs
More file actions
1365 lines (1168 loc) · 57.7 KB
/
PBXProject.cs
File metadata and controls
1365 lines (1168 loc) · 57.7 KB
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
#if UNITY_IOS
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System;
using UnityEditor.iOS.Xcode.PBX;
using UnityEngine;
namespace UnityEditor.iOS.Xcode.Stickers
{
using PBXBuildFileSection = KnownSectionBase<PBXBuildFileData>;
using PBXFileReferenceSection = KnownSectionBase<PBXFileReferenceData>;
using PBXGroupSection = KnownSectionBase<PBXGroupData>;
using PBXContainerItemProxySection = KnownSectionBase<PBXContainerItemProxyData>;
using PBXReferenceProxySection = KnownSectionBase<PBXReferenceProxyData>;
using PBXSourcesBuildPhaseSection = KnownSectionBase<PBXSourcesBuildPhaseData>;
using PBXFrameworksBuildPhaseSection= KnownSectionBase<PBXFrameworksBuildPhaseData>;
using PBXResourcesBuildPhaseSection = KnownSectionBase<PBXResourcesBuildPhaseData>;
using PBXCopyFilesBuildPhaseSection = KnownSectionBase<PBXCopyFilesBuildPhaseData>;
using PBXShellScriptBuildPhaseSection = KnownSectionBase<PBXShellScriptBuildPhaseData>;
using PBXVariantGroupSection = KnownSectionBase<PBXVariantGroupData>;
using PBXNativeTargetSection = KnownSectionBase<PBXNativeTargetData>;
using PBXTargetDependencySection = KnownSectionBase<PBXTargetDependencyData>;
using XCBuildConfigurationSection = KnownSectionBase<XCBuildConfigurationData>;
using XCConfigurationListSection = KnownSectionBase<XCConfigurationListData>;
using UnknownSection = KnownSectionBase<PBXObjectData>;
/*// Determines the tree the given path is relative to
public enum PBXSourceTree
{
Absolute, // The path is absolute
Source, // The path is relative to the source folder
Group, // The path is relative to the folder it's in. This enum is used only internally,
// do not use it as function parameter
Build, // The path is relative to the build products folder
Developer, // The path is relative to the developer folder
Sdk // The path is relative to the sdk folder
};*/
public class PBXProject
{
PBXProjectData m_Data = new PBXProjectData();
// convenience accessors for public members of data. This is temporary; will be fixed by an interface change
// of PBXProjectData
PBXContainerItemProxySection containerItems
{
get { return m_Data.containerItems; }
}
PBXReferenceProxySection references
{
get { return m_Data.references; }
}
PBXSourcesBuildPhaseSection sources
{
get { return m_Data.sources; }
}
PBXFrameworksBuildPhaseSection frameworks
{
get { return m_Data.frameworks; }
}
PBXResourcesBuildPhaseSection resources
{
get { return m_Data.resources; }
}
PBXCopyFilesBuildPhaseSection copyFiles
{
get { return m_Data.copyFiles; }
}
PBXShellScriptBuildPhaseSection shellScripts
{
get { return m_Data.shellScripts; }
}
PBXNativeTargetSection nativeTargets
{
get { return m_Data.nativeTargets; }
}
PBXTargetDependencySection targetDependencies
{
get { return m_Data.targetDependencies; }
}
PBXVariantGroupSection variantGroups
{
get { return m_Data.variantGroups; }
}
XCBuildConfigurationSection buildConfigs
{
get { return m_Data.buildConfigs; }
}
XCConfigurationListSection configs
{
get { return m_Data.configs; }
}
PBXProjectSection projectSection
{
get { return m_Data.project; }
}
PBXBuildFileData BuildFilesGet(string guid)
{
return m_Data.BuildFilesGet(guid);
}
void BuildFilesAdd(string targetGuid, PBXBuildFileData buildFile)
{
m_Data.BuildFilesAdd(targetGuid, buildFile);
}
void BuildFilesRemove(string targetGuid, string fileGuid)
{
m_Data.BuildFilesRemove(targetGuid, fileGuid);
}
PBXBuildFileData BuildFilesGetForSourceFile(string targetGuid, string fileGuid)
{
return m_Data.BuildFilesGetForSourceFile(targetGuid, fileGuid);
}
IEnumerable<PBXBuildFileData> BuildFilesGetAll()
{
return m_Data.BuildFilesGetAll();
}
void FileRefsAdd(string realPath, string projectPath, PBXGroupData parent, PBXFileReferenceData fileRef)
{
m_Data.FileRefsAdd(realPath, projectPath, parent, fileRef);
}
PBXFileReferenceData FileRefsGet(string guid)
{
return m_Data.FileRefsGet(guid);
}
PBXFileReferenceData FileRefsGetByRealPath(string path, PBXSourceTree sourceTree)
{
return m_Data.FileRefsGetByRealPath(path, sourceTree);
}
PBXFileReferenceData FileRefsGetByProjectPath(string path)
{
return m_Data.FileRefsGetByProjectPath(path);
}
void FileRefsRemove(string guid)
{
m_Data.FileRefsRemove(guid);
}
PBXGroupData GroupsGet(string guid)
{
return m_Data.GroupsGet(guid);
}
PBXGroupData GroupsGetByChild(string childGuid)
{
return m_Data.GroupsGetByChild(childGuid);
}
PBXGroupData GroupsGetMainGroup()
{
return m_Data.GroupsGetMainGroup();
}
PBXGroupData GroupsGetByProjectPath(string sourceGroup)
{
return m_Data.GroupsGetByProjectPath(sourceGroup);
}
void GroupsAdd(string projectPath, PBXGroupData parent, PBXGroupData gr)
{
m_Data.GroupsAdd(projectPath, parent, gr);
}
void GroupsAddDuplicate(PBXGroupData gr)
{
m_Data.GroupsAddDuplicate(gr);
}
void GroupsRemove(string guid)
{
m_Data.GroupsRemove(guid);
}
FileGUIDListBase BuildSectionAny(PBXNativeTargetData target, string path, bool isFolderRef)
{
return m_Data.BuildSectionAny(target, path, isFolderRef);
}
public static string GetPBXProjectPath(string buildPath)
{
return PBX.Utils.CombinePaths(buildPath, "Unity-iPhone.xcodeproj/project.pbxproj");
}
public static string GetUnityTargetName()
{
return "Unity-iPhone";
}
public static string GetUnityTestTargetName()
{
return "Unity-iPhone Tests";
}
internal string ProjectGuid()
{
return projectSection.project.guid;
}
/// Returns a guid identifying native target with name @a name
public string TargetGuidByName(string name)
{
foreach (var entry in nativeTargets.GetEntries())
if (entry.Value.name == name)
return entry.Key;
return null;
}
/// Returns the name of the native target identified with guid @a guid
public string TargetNameByGuid(string guid)
{
var target = nativeTargets[guid];
if (target == null)
return null;
else
return target.name;
}
public static bool IsKnownExtension(string ext)
{
return FileTypeUtils.IsKnownExtension(ext);
}
public static bool IsBuildable(string ext)
{
return FileTypeUtils.IsBuildableFile(ext);
}
// The same file can be referred to by more than one project path.
private string AddFileImpl(string path, string projectPath, PBXSourceTree tree, bool isFolderReference)
{
path = PBX.Utils.FixSlashesInPath(path);
projectPath = PBX.Utils.FixSlashesInPath(projectPath);
if (!isFolderReference && Path.GetExtension(path) != Path.GetExtension(projectPath))
throw new Exception("Project and real path extensions do not match");
string guid = FindFileGuidByProjectPath(projectPath);
if (guid == null)
guid = FindFileGuidByRealPath(path);
if (guid == null)
{
PBXFileReferenceData fileRef;
if (isFolderReference)
fileRef = PBXFileReferenceData.CreateFromFolderReference(path, PBX.Utils.GetFilenameFromPath(projectPath), tree);
else
fileRef = PBXFileReferenceData.CreateFromFile(path, PBX.Utils.GetFilenameFromPath(projectPath), tree);
PBXGroupData parent = CreateSourceGroup(PBX.Utils.GetDirectoryFromPath(projectPath));
parent.children.AddGUID(fileRef.guid);
FileRefsAdd(path, projectPath, parent, fileRef);
guid = fileRef.guid;
}
return guid;
}
// The extension of the files identified by path and projectPath must be the same.
public string AddFile(string path, string projectPath)
{
return AddFileImpl(path, projectPath, PBXSourceTree.Source, false);
}
// sourceTree must not be PBXSourceTree.Group
public string AddFile(string path, string projectPath, PBXSourceTree sourceTree)
{
if (sourceTree == PBXSourceTree.Group)
throw new Exception("sourceTree must not be PBXSourceTree.Group");
return AddFileImpl(path, projectPath, sourceTree, false);
}
public string AddFolderReference(string path, string projectPath)
{
return AddFileImpl(path, projectPath, PBXSourceTree.Source, true);
}
// sourceTree must not be PBXSourceTree.Group
public string AddFolderReference(string path, string projectPath, PBXSourceTree sourceTree)
{
if (sourceTree == PBXSourceTree.Group)
throw new Exception("sourceTree must not be PBXSourceTree.Group");
return AddFileImpl(path, projectPath, sourceTree, true);
}
private void AddBuildFileImpl(string targetGuid, string fileGuid, bool weak, string compileFlags)
{
PBXNativeTargetData target = nativeTargets[targetGuid];
PBXFileReferenceData fileRef = FileRefsGet(fileGuid);
string ext = Path.GetExtension(fileRef.path);
if (FileTypeUtils.IsBuildable(ext, fileRef.isFolderReference) &&
BuildFilesGetForSourceFile(targetGuid, fileGuid) == null)
{
PBXBuildFileData buildFile = PBXBuildFileData.CreateFromFile(fileGuid, weak, compileFlags);
BuildFilesAdd(targetGuid, buildFile);
BuildSectionAny(target, ext, fileRef.isFolderReference).files.AddGUID(buildFile.guid);
}
}
public void AddFileToBuild(string targetGuid, string fileGuid)
{
AddBuildFileImpl(targetGuid, fileGuid, false, null);
}
public void AddFileToBuildWithFlags(string targetGuid, string fileGuid, string compileFlags)
{
AddBuildFileImpl(targetGuid, fileGuid, false, compileFlags);
}
// returns null on error
// FIXME: at the moment returns all flags as the first element of the array
public List<string> GetCompileFlagsForFile(string targetGuid, string fileGuid)
{
var buildFile = BuildFilesGetForSourceFile(targetGuid, fileGuid);
if (buildFile == null)
return null;
if (buildFile.compileFlags == null)
return new List<string>();
return new List<string> {buildFile.compileFlags};
}
public void SetCompileFlagsForFile(string targetGuid, string fileGuid, List<string> compileFlags)
{
var buildFile = BuildFilesGetForSourceFile(targetGuid, fileGuid);
if (buildFile == null)
return;
if (compileFlags == null)
buildFile.compileFlags = null;
else
buildFile.compileFlags = string.Join(" ", compileFlags.ToArray());
}
public void AddAssetTagForFile(string targetGuid, string fileGuid, string tag)
{
var buildFile = BuildFilesGetForSourceFile(targetGuid, fileGuid);
if (buildFile == null)
return;
if (!buildFile.assetTags.Contains(tag))
buildFile.assetTags.Add(tag);
if (!projectSection.project.knownAssetTags.Contains(tag))
projectSection.project.knownAssetTags.Add(tag);
}
public void RemoveAssetTagForFile(string targetGuid, string fileGuid, string tag)
{
var buildFile = BuildFilesGetForSourceFile(targetGuid, fileGuid);
if (buildFile == null)
return;
buildFile.assetTags.Remove(tag);
// remove from known tags if this was the last one
foreach (var buildFile2 in BuildFilesGetAll())
{
if (buildFile2.assetTags.Contains(tag))
return;
}
projectSection.project.knownAssetTags.Remove(tag);
}
public void AddAssetTagToDefaultInstall(string targetGuid, string tag)
{
if (!projectSection.project.knownAssetTags.Contains(tag))
return;
AddBuildProperty(targetGuid, "ON_DEMAND_RESOURCES_INITIAL_INSTALL_TAGS", tag);
}
public void RemoveAssetTagFromDefaultInstall(string targetGuid, string tag)
{
UpdateBuildProperty(targetGuid, "ON_DEMAND_RESOURCES_INITIAL_INSTALL_TAGS", null, new string[] {tag});
}
public void RemoveAssetTag(string tag)
{
foreach (var buildFile in BuildFilesGetAll())
buildFile.assetTags.Remove(tag);
foreach (var targetGuid in nativeTargets.GetGuids())
RemoveAssetTagFromDefaultInstall(targetGuid, tag);
projectSection.project.knownAssetTags.Remove(tag);
}
public bool ContainsFileByRealPath(string path)
{
return FindFileGuidByRealPath(path) != null;
}
// sourceTree must not be PBXSourceTree.Group
public bool ContainsFileByRealPath(string path, PBXSourceTree sourceTree)
{
if (sourceTree == PBXSourceTree.Group)
throw new Exception("sourceTree must not be PBXSourceTree.Group");
return FindFileGuidByRealPath(path, sourceTree) != null;
}
public bool ContainsFileByProjectPath(string path)
{
return FindFileGuidByProjectPath(path) != null;
}
public bool HasFramework(string framework)
{
return ContainsFileByRealPath("System/Library/Frameworks/" + framework);
}
/// The framework must be specified with the '.framework' extension
public void AddFrameworkToProject(string targetGuid, string framework, bool weak)
{
string fileGuid = AddFile("System/Library/Frameworks/" + framework, "Frameworks/" + framework, PBXSourceTree.Sdk);
AddBuildFileImpl(targetGuid, fileGuid, weak, null);
}
/// The framework must be specified with the '.framework' extension
// FIXME: targetGuid is ignored at the moment
public void RemoveFrameworkFromProject(string targetGuid, string framework)
{
string fileGuid = FindFileGuidByRealPath("System/Library/Frameworks/" + framework);
if (fileGuid != null)
RemoveFile(fileGuid);
}
// sourceTree must not be PBXSourceTree.Group
public string FindFileGuidByRealPath(string path, PBXSourceTree sourceTree)
{
//if (sourceTree == PBXSourceTree.Group)
// throw new Exception("sourceTree must not be PBXSourceTree.Group");
path = PBX.Utils.FixSlashesInPath(path);
var fileRef = FileRefsGetByRealPath(path, sourceTree);
if (fileRef != null)
return fileRef.guid;
return null;
}
public string FindFileGuidByRealPath(string path)
{
path = PBX.Utils.FixSlashesInPath(path);
foreach (var tree in FileTypeUtils.AllAbsoluteSourceTrees())
{
string res = FindFileGuidByRealPath(path, tree);
if (res != null)
return res;
}
return null;
}
public string FindFileGuidByProjectPath(string path)
{
path = PBX.Utils.FixSlashesInPath(path);
var fileRef = FileRefsGetByProjectPath(path);
if (fileRef != null)
return fileRef.guid;
return null;
}
public void RemoveFileFromBuild(string targetGuid, string fileGuid)
{
var buildFile = BuildFilesGetForSourceFile(targetGuid, fileGuid);
if (buildFile == null)
return;
BuildFilesRemove(targetGuid, fileGuid);
string buildGuid = buildFile.guid;
if (buildGuid != null)
{
foreach (var section in sources.GetEntries())
section.Value.files.RemoveGUID(buildGuid);
foreach (var section in resources.GetEntries())
section.Value.files.RemoveGUID(buildGuid);
foreach (var section in copyFiles.GetEntries())
section.Value.files.RemoveGUID(buildGuid);
foreach (var section in frameworks.GetEntries())
section.Value.files.RemoveGUID(buildGuid);
}
}
public void RemoveFile(string fileGuid)
{
if (fileGuid == null)
return;
// remove from parent
PBXGroupData parent = GroupsGetByChild(fileGuid);
if (parent != null)
parent.children.RemoveGUID(fileGuid);
RemoveGroupIfEmpty(parent);
// remove actual file
foreach (var target in nativeTargets.GetEntries())
RemoveFileFromBuild(target.Value.guid, fileGuid);
FileRefsRemove(fileGuid);
}
void RemoveGroupIfEmpty(PBXGroupData gr)
{
if (gr.children.Count == 0 && gr != GroupsGetMainGroup())
{
// remove from parent
PBXGroupData parent = GroupsGetByChild(gr.guid);
parent.children.RemoveGUID(gr.guid);
RemoveGroupIfEmpty(parent);
// remove actual group
GroupsRemove(gr.guid);
}
}
private void RemoveGroupChildrenRecursive(PBXGroupData parent)
{
List<string> children = new List<string>(parent.children);
parent.children.Clear();
foreach (string guid in children)
{
PBXFileReferenceData file = FileRefsGet(guid);
if (file != null)
{
foreach (var target in nativeTargets.GetEntries())
RemoveFileFromBuild(target.Value.guid, guid);
FileRefsRemove(guid);
continue;
}
PBXGroupData gr = GroupsGet(guid);
if (gr != null)
{
RemoveGroupChildrenRecursive(gr);
GroupsRemove(gr.guid);
continue;
}
}
}
internal void RemoveFilesByProjectPathRecursive(string projectPath)
{
projectPath = PBX.Utils.FixSlashesInPath(projectPath);
PBXGroupData gr = GroupsGetByProjectPath(projectPath);
if (gr == null)
return;
RemoveGroupChildrenRecursive(gr);
RemoveGroupIfEmpty(gr);
}
// Returns null on error
internal List<string> GetGroupChildrenFiles(string projectPath)
{
projectPath = PBX.Utils.FixSlashesInPath(projectPath);
PBXGroupData gr = GroupsGetByProjectPath(projectPath);
if (gr == null)
return null;
var res = new List<string>();
foreach (var guid in gr.children)
{
var fileRef = FileRefsGet(guid);
if (fileRef != null)
res.Add(fileRef.name);
}
return res;
}
private PBXGroupData GetPBXGroupChildByName(PBXGroupData group, string name)
{
foreach (string guid in group.children)
{
var gr = GroupsGet(guid);
if (gr != null && gr.name == name)
return gr;
}
return null;
}
/// Creates source group identified by sourceGroup, if needed, and returns it.
/// If sourceGroup is empty or null, root group is returned
private PBXGroupData CreateSourceGroup(string sourceGroup)
{
sourceGroup = PBX.Utils.FixSlashesInPath(sourceGroup);
if (sourceGroup == null || sourceGroup == "")
return GroupsGetMainGroup();
PBXGroupData gr = GroupsGetByProjectPath(sourceGroup);
if (gr != null)
return gr;
// the group does not exist -- create new
gr = GroupsGetMainGroup();
var elements = PBX.Utils.SplitPath(sourceGroup);
string projectPath = null;
foreach (string pathEl in elements)
{
if (projectPath == null)
projectPath = pathEl;
else
projectPath += "/" + pathEl;
PBXGroupData child = GetPBXGroupChildByName(gr, pathEl);
if (child != null)
gr = child;
else
{
PBXGroupData newGroup = PBXGroupData.Create(pathEl, pathEl, PBXSourceTree.Group);
gr.children.AddGUID(newGroup.guid);
GroupsAdd(projectPath, gr, newGroup);
gr = newGroup;
}
}
return gr;
}
// sourceTree must not be PBXSourceTree.Group
public void AddExternalProjectDependency(string path, string projectPath, PBXSourceTree sourceTree)
{
if (sourceTree == PBXSourceTree.Group)
throw new Exception("sourceTree must not be PBXSourceTree.Group");
path = PBX.Utils.FixSlashesInPath(path);
projectPath = PBX.Utils.FixSlashesInPath(projectPath);
// note: we are duplicating products group for the project reference. Otherwise Xcode crashes.
PBXGroupData productGroup = PBXGroupData.CreateRelative("Products");
GroupsAddDuplicate(productGroup); // don't use GroupsAdd here
PBXFileReferenceData fileRef = PBXFileReferenceData.CreateFromFile(path, Path.GetFileName(projectPath),
sourceTree);
FileRefsAdd(path, projectPath, null, fileRef);
CreateSourceGroup(PBX.Utils.GetDirectoryFromPath(projectPath)).children.AddGUID(fileRef.guid);
projectSection.project.AddReference(productGroup.guid, fileRef.guid);
}
/** This function must be called only after the project the library is in has
been added as a dependency via AddExternalProjectDependency. projectPath must be
the same as the 'path' parameter passed to the AddExternalProjectDependency.
remoteFileGuid must be the guid of the referenced file as specified in
PBXFileReference section of the external project
TODO: what. is remoteInfo entry in PBXContainerItemProxy? Is in referenced project name or
referenced library name without extension?
*/
public void AddExternalLibraryDependency(string targetGuid, string filename, string remoteFileGuid, string projectPath,
string remoteInfo)
{
PBXNativeTargetData target = nativeTargets[targetGuid];
filename = PBX.Utils.FixSlashesInPath(filename);
projectPath = PBX.Utils.FixSlashesInPath(projectPath);
// find the products group to put the new library in
string projectGuid = FindFileGuidByRealPath(projectPath);
if (projectGuid == null)
throw new Exception("No such project");
string productsGroupGuid = null;
foreach (var proj in projectSection.project.projectReferences)
{
if (proj.projectRef == projectGuid)
{
productsGroupGuid = proj.group;
break;
}
}
if (productsGroupGuid == null)
throw new Exception("Malformed project: no project in project references");
PBXGroupData productGroup = GroupsGet(productsGroupGuid);
// verify file extension
string ext = Path.GetExtension(filename);
if (!FileTypeUtils.IsBuildableFile(ext))
throw new Exception("Wrong file extension");
// create ContainerItemProxy object
var container = PBXContainerItemProxyData.Create(projectGuid, "2", remoteFileGuid, remoteInfo);
containerItems.AddEntry(container);
// create a reference and build file for the library
string typeName = FileTypeUtils.GetTypeName(ext);
var libRef = PBXReferenceProxyData.Create(filename, typeName, container.guid, "BUILT_PRODUCTS_DIR");
references.AddEntry(libRef);
PBXBuildFileData libBuildFile = PBXBuildFileData.CreateFromFile(libRef.guid, false, null);
BuildFilesAdd(targetGuid, libBuildFile);
BuildSectionAny(target, ext, false).files.AddGUID(libBuildFile.guid);
// add to products folder
productGroup.children.AddGUID(libRef.guid);
}
private void SetDefaultAppExtensionReleaseBuildFlags(XCBuildConfigurationData config, string infoPlistPath)
{
config.AddProperty("ALWAYS_SEARCH_USER_PATHS", "NO");
config.AddProperty("CLANG_CXX_LANGUAGE_STANDARD", "gnu++0x");
config.AddProperty("CLANG_CXX_LIBRARY", "libc++");
config.AddProperty("CLANG_ENABLE_MODULES", "YES");
config.AddProperty("CLANG_ENABLE_OBJC_ARC", "YES");
config.AddProperty("CLANG_WARN_BOOL_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_CONSTANT_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_DIRECT_OBJC_ISA_USAGE", "YES_ERROR");
config.AddProperty("CLANG_WARN_EMPTY_BODY", "YES");
config.AddProperty("CLANG_WARN_ENUM_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_INT_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_OBJC_ROOT_CLASS", "YES_ERROR");
config.AddProperty("CLANG_WARN_UNREACHABLE_CODE", "YES");
config.AddProperty("CLANG_WARN__DUPLICATE_METHOD_MATCH", "YES");
config.AddProperty("COPY_PHASE_STRIP", "YES");
config.AddProperty("DEVELOPMENT_TEAM", "");
config.AddProperty("ENABLE_NS_ASSERTIONS", "NO");
config.AddProperty("ENABLE_STRICT_OBJC_MSGSEND", "YES");
config.AddProperty("GCC_C_LANGUAGE_STANDARD", "gnu99");
config.AddProperty("GCC_WARN_64_TO_32_BIT_CONVERSION", "YES");
config.AddProperty("GCC_WARN_ABOUT_RETURN_TYPE", "YES_ERROR");
config.AddProperty("GCC_WARN_UNDECLARED_SELECTOR", "YES");
config.AddProperty("GCC_WARN_UNINITIALIZED_AUTOS", "YES_AGGRESSIVE");
config.AddProperty("GCC_WARN_UNUSED_FUNCTION", "YES");
config.AddProperty("INFOPLIST_FILE", infoPlistPath);
config.AddProperty("IPHONEOS_DEPLOYMENT_TARGET", "8.0");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "$(inherited)");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "@executable_path/../../Frameworks");
config.AddProperty("MTL_ENABLE_DEBUG_INFO", "NO");
config.AddProperty("PRODUCT_NAME", "$(TARGET_NAME)");
config.AddProperty("SKIP_INSTALL", "YES");
config.AddProperty("VALIDATE_PRODUCT", "YES");
}
private void SetDefaultAppExtensionDebugBuildFlags(XCBuildConfigurationData config, string infoPlistPath)
{
config.AddProperty("ALWAYS_SEARCH_USER_PATHS", "NO");
config.AddProperty("CLANG_CXX_LANGUAGE_STANDARD", "gnu++0x");
config.AddProperty("CLANG_CXX_LIBRARY", "libc++");
config.AddProperty("CLANG_ENABLE_MODULES", "YES");
config.AddProperty("CLANG_ENABLE_OBJC_ARC", "YES");
config.AddProperty("CLANG_WARN_BOOL_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_CONSTANT_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_DIRECT_OBJC_ISA_USAGE", "YES_ERROR");
config.AddProperty("CLANG_WARN_EMPTY_BODY", "YES");
config.AddProperty("CLANG_WARN_ENUM_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_INT_CONVERSION", "YES");
config.AddProperty("CLANG_WARN_OBJC_ROOT_CLASS", "YES_ERROR");
config.AddProperty("CLANG_WARN_UNREACHABLE_CODE", "YES");
config.AddProperty("CLANG_WARN__DUPLICATE_METHOD_MATCH", "YES");
config.AddProperty("COPY_PHASE_STRIP", "NO");
config.AddProperty("DEVELOPMENT_TEAM", "");
config.AddProperty("ENABLE_STRICT_OBJC_MSGSEND", "YES");
config.AddProperty("GCC_C_LANGUAGE_STANDARD", "gnu99");
config.AddProperty("GCC_DYNAMIC_NO_PIC", "NO");
config.AddProperty("GCC_OPTIMIZATION_LEVEL", "0");
config.AddProperty("GCC_PREPROCESSOR_DEFINITIONS", "DEBUG=1");
config.AddProperty("GCC_PREPROCESSOR_DEFINITIONS", "$(inherited)");
config.AddProperty("GCC_SYMBOLS_PRIVATE_EXTERN", "NO");
config.AddProperty("GCC_WARN_64_TO_32_BIT_CONVERSION", "YES");
config.AddProperty("GCC_WARN_ABOUT_RETURN_TYPE", "YES_ERROR");
config.AddProperty("GCC_WARN_UNDECLARED_SELECTOR", "YES");
config.AddProperty("GCC_WARN_UNINITIALIZED_AUTOS", "YES_AGGRESSIVE");
config.AddProperty("GCC_WARN_UNUSED_FUNCTION", "YES");
config.AddProperty("INFOPLIST_FILE", infoPlistPath);
config.AddProperty("IPHONEOS_DEPLOYMENT_TARGET", "8.0");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "$(inherited)");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
config.AddProperty("LD_RUNPATH_SEARCH_PATHS", "@executable_path/../../Frameworks");
config.AddProperty("MTL_ENABLE_DEBUG_INFO", "YES");
config.AddProperty("ONLY_ACTIVE_ARCH", "YES");
config.AddProperty("PRODUCT_NAME", "$(TARGET_NAME)");
config.AddProperty("SKIP_INSTALL", "YES");
}
// Returns the guid of the new target
public string CreateNewTarget(string name, string ext, string type)
{
// create build configurations
var releaseBuildConfig = XCBuildConfigurationData.Create("Release");
buildConfigs.AddEntry(releaseBuildConfig);
var debugBuildConfig = XCBuildConfigurationData.Create("Debug");
buildConfigs.AddEntry(debugBuildConfig);
var buildConfigList = XCConfigurationListData.Create();
configs.AddEntry(buildConfigList);
buildConfigList.buildConfigs.AddGUID(releaseBuildConfig.guid);
buildConfigList.buildConfigs.AddGUID(debugBuildConfig.guid);
// create build file reference
string fullName = name + ext;
var productFileRef = AddFile(fullName, "Products/" + fullName, PBXSourceTree.Build);
var newTarget = PBXNativeTargetData.Create(name, productFileRef, type, buildConfigList.guid);
nativeTargets.AddEntry(newTarget);
projectSection.project.targets.Add(newTarget.guid);
return newTarget.guid;
}
// Returns the guid of the new target
public string AddAppExtension(string mainTarget, string name, string type, string infoPlistPath)
{
string ext = ".appex";
string newTargetGuid = CreateNewTarget(name, ext, type);
var newTarget = nativeTargets[newTargetGuid];
SetDefaultAppExtensionReleaseBuildFlags(buildConfigs[BuildConfigByName(newTarget.guid, "Release")], infoPlistPath);
SetDefaultAppExtensionDebugBuildFlags(buildConfigs[BuildConfigByName(newTarget.guid, "Debug")], infoPlistPath);
var sourcesBuildPhase = PBXSourcesBuildPhaseData.Create();
sources.AddEntry(sourcesBuildPhase);
newTarget.phases.AddGUID(sourcesBuildPhase.guid);
var resourcesBuildPhase = PBXResourcesBuildPhaseData.Create();
resources.AddEntry(resourcesBuildPhase);
newTarget.phases.AddGUID(resourcesBuildPhase.guid);
var frameworksBuildPhase = PBXFrameworksBuildPhaseData.Create();
frameworks.AddEntry(frameworksBuildPhase);
newTarget.phases.AddGUID(frameworksBuildPhase.guid);
var copyFilesBuildPhase = PBXCopyFilesBuildPhaseData.Create("Embed App Extensions", "13");
copyFiles.AddEntry(copyFilesBuildPhase);
nativeTargets[mainTarget].phases.AddGUID(copyFilesBuildPhase.guid);
var containerProxy = PBXContainerItemProxyData.Create(projectSection.project.guid, "1", newTarget.guid, name);
containerItems.AddEntry(containerProxy);
var targetDependency = PBXTargetDependencyData.Create(newTarget.guid, containerProxy.guid);
targetDependencies.AddEntry(targetDependency);
nativeTargets[mainTarget].dependencies.AddGUID(targetDependency.guid);
var buildAppCopy = PBXBuildFileData.CreateFromFile(FindFileGuidByProjectPath("Products/" + name + ext), false, "");
BuildFilesAdd(mainTarget, buildAppCopy);
copyFilesBuildPhase.files.AddGUID(buildAppCopy.guid);
AddFile(infoPlistPath, infoPlistPath, PBXSourceTree.Source);
return newTarget.guid;
}
public string BuildConfigByName(string targetGuid, string name)
{
PBXNativeTargetData target = nativeTargets[targetGuid];
foreach (string guid in configs[target.buildConfigList].buildConfigs)
{
var buildConfig = buildConfigs[guid];
if (buildConfig != null && buildConfig.name == name)
return buildConfig.guid;
}
return null;
}
public void AddFileToResourcesForTarget(string targetGuid, string filePath)
{
var target = nativeTargets[targetGuid];
if (target == null)
throw new Exception("Invalid target guid");
Dictionary<string, PBXResourcesBuildPhaseData> resourcesDictionary = (Dictionary<string, PBXResourcesBuildPhaseData>) resources.GetEntries();
PBXResourcesBuildPhaseData resource = null;
foreach (var section in target.phases)
{
if (resourcesDictionary.ContainsKey(section))
{
resource = resourcesDictionary[section];
}
}
if (resource == null)
throw new Exception("No PBXResourcesBuildPhaseData for target");
var buildFileData = PBXBuildFileData.CreateFromFile(FindFileGuidByProjectPath(filePath), false, "");
BuildFilesAdd(targetGuid, buildFileData);
resource.files.AddGUID(buildFileData.guid);
}
string GetConfigListForTarget(string targetGuid)
{
if (targetGuid == projectSection.project.guid)
return projectSection.project.buildConfigList;
else
return nativeTargets[targetGuid].buildConfigList;
}
// Adds an item to a build property that contains a value list. Duplicate build properties
// are ignored. Values for name "LIBRARY_SEARCH_PATHS" are quoted if they contain spaces.
// targetGuid may refer to PBXProject object
public void AddBuildProperty(string targetGuid, string name, string value)
{
foreach (string guid in configs[GetConfigListForTarget(targetGuid)].buildConfigs)
AddBuildPropertyForConfig(guid, name, value);
}
public void AddBuildProperty(IEnumerable<string> targetGuids, string name, string value)
{
foreach (string t in targetGuids)
AddBuildProperty(t, name, value);
}
public void AddBuildPropertyForConfig(string configGuid, string name, string value)
{
buildConfigs[configGuid].AddProperty(name, value);
}
public void AddBuildPropertyForConfig(IEnumerable<string> configGuids, string name, string value)
{
foreach (string guid in configGuids)
AddBuildPropertyForConfig(guid, name, value);
}
// targetGuid may refer to PBXProject object
public void SetBuildProperty(string targetGuid, string name, string value)
{
foreach (string guid in configs[GetConfigListForTarget(targetGuid)].buildConfigs)
SetBuildPropertyForConfig(guid, name, value);
}
public void SetBuildProperty(IEnumerable<string> targetGuids, string name, string value)
{
foreach (string t in targetGuids)
SetBuildProperty(t, name, value);
}
public void SetBuildPropertyForConfig(string configGuid, string name, string value)
{
buildConfigs[configGuid].SetProperty(name, value);
}
public void SetBuildPropertyForConfig(IEnumerable<string> configGuids, string name, string value)
{
foreach (string guid in configGuids)
SetBuildPropertyForConfig(guid, name, value);
}
internal void RemoveBuildProperty(string targetGuid, string name)
{
foreach (string guid in configs[GetConfigListForTarget(targetGuid)].buildConfigs)
RemoveBuildPropertyForConfig(guid, name);
}
internal void RemoveBuildProperty(IEnumerable<string> targetGuids, string name)
{
foreach (string t in targetGuids)
RemoveBuildProperty(t, name);
}
internal void RemoveBuildPropertyForConfig(string configGuid, string name)
{
buildConfigs[configGuid].RemoveProperty(name);
}
internal void RemoveBuildPropertyForConfig(IEnumerable<string> configGuids, string name)
{
foreach (string guid in configGuids)
RemoveBuildPropertyForConfig(guid, name);
}
/// Interprets the value of the given property as a set of space-delimited strings, then
/// removes strings equal to items to removeValues and adds strings in addValues.
public void UpdateBuildProperty(string targetGuid, string name,
IEnumerable<string> addValues, IEnumerable<string> removeValues)
{
foreach (string guid in configs[GetConfigListForTarget(targetGuid)].buildConfigs)
UpdateBuildPropertyForConfig(guid, name, addValues, removeValues);
}
public void UpdateBuildProperty(IEnumerable<string> targetGuids, string name,
IEnumerable<string> addValues, IEnumerable<string> removeValues)
{
foreach (string t in targetGuids)
UpdateBuildProperty(t, name, addValues, removeValues);
}
public void UpdateBuildPropertyForConfig(string configGuid, string name,
IEnumerable<string> addValues, IEnumerable<string> removeValues)
{
var config = buildConfigs[configGuid];
if (config != null)
{
if (removeValues != null)
foreach (var v in removeValues)
config.RemovePropertyValue(name, v);
if (addValues != null)
foreach (var v in addValues)
config.AddProperty(name, v);
}
}