Debian Codegraph @

tools/build-tool/code/tooliospbxgeneration.pas

raw
1 {
2 Copyright 2017-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Generation of the pbxproj project file, part of the iOS Xcode project. }
17 unit ToolIosPbxGeneration;
18
19 interface
20
21 uses SysUtils, Generics.Collections,
22 CastleFindFiles;
23
24 type
25 TXcodeProjectFile = class;
26 TXcodeProject = class;
27
28 TXcodeProjectFileList = specialize TObjectList<TXcodeProjectFile>;
29
30 { Framework within an Xcode project. }
31 TXcodeProjectFramework = class
32 Name: string;
33 FileUuid, BuildUuid: string;
34 constructor Create(const AName: string);
35 end;
36
37 { File or directory within an Xcode project. }
38 TXcodeProjectFile = class
39 Name: string;
40 FileUuid, BuildUuid: string;
41 { Children references.
42 List instance itself is owned, but items on this list are not owned by this object. }
43 Children: TXcodeProjectFileList;
44 Directory: boolean;
45 Project: TXcodeProject;
46 constructor Create; overload;
47 constructor Create(const FileInfo: TFileInfo); overload;
48 destructor Destroy; override;
49 function FileType: string;
50 function BuildGroup: string;
51 procedure AddChildFile(const FileInfo: TFileInfo; var StopSearch: boolean);
52 end;
53
54 TXcodeProjectFrameworkList = specialize TObjectList<TXcodeProjectFramework>;
55
56 { Xcode project things (files, frameworks...) information,
57 useful to generate xxx.pbxproj file. }
58 TXcodeProject = class
59 strict private
60 TopLevelDir: TXcodeProjectFile;
61
62 function SectionPBXFileReference: string;
63 function SectionPBXBuildFile: string;
64 function SectionPBXGroup: string;
65
66 function SectionPBXFrameworksBuildPhase: string;
67 function SectionPBXResourcesBuildPhase: string;
68 function SectionPBXSourcesBuildPhase: string;
69 public
70 { Flattened list of *all* files within a project. Owns items. }
71 Files: TXcodeProjectFileList;
72 Frameworks: TXcodeProjectFrameworkList;
73 constructor Create;
74 destructor Destroy; override;
75 procedure AddTopLevelDir(const Path, Name: string);
76 { Generated contents to be inserted into the pbxproj file. }
77 function PBXContents: string;
78 end;
79
80 implementation
81
82 uses StrUtils,
83 CastleStringUtils, CastleUtils, CastleLog;
84
85 var
86 XcodeUuidNext: Int64;
87
88 { UUID in Xcode project: 12 bytes for unique id.
89 We use 6 bytes for random number, 6 bytes for the next sequential number
90 (thus guaranteeing that within a single run, the numbers will be different,
91 no matter about the random quality). }
92 function GenXcodeUuid: string;
93 var
94 RandomPart: Int64;
95 begin
96 RandomPart := Random(Int64(1) shl (6 * 8));
97 Inc(XcodeUuidNext);
98 Result :=
99 IntToHex(RandomPart, 6 * 2) +
100 IntToHex(XcodeUuidNext, 6 * 2);
101 end;
102
103 { TXcodeProjectFramework ---------------------------------------------------------- }
104
105 constructor TXcodeProjectFramework.Create(const AName: string);
106 begin
107 inherited Create;
108 FileUuid := GenXcodeUuid;
109 BuildUuid := GenXcodeUuid;
110 Name := AName;
111 end;
112
113 { TXcodeProjectFile ---------------------------------------------------------- }
114
115 constructor TXcodeProjectFile.Create;
116 begin
117 inherited Create;
118 Children := TXcodeProjectFileList.Create(false);
119 FileUuid := GenXcodeUuid;
120 BuildUuid := GenXcodeUuid;
121 end;
122
123 constructor TXcodeProjectFile.Create(const FileInfo: TFileInfo);
124 begin
125 Create;
126 Name := FileInfo.Name;
127 Directory := FileInfo.Directory;
128 end;
129
130 destructor TXcodeProjectFile.Destroy;
131 begin
132 FreeAndNil(Children);
133 inherited;
134 end;
135
136 function TXcodeProjectFile.FileType: string;
137 var
138 E: string;
139 begin
140 E := LowerCase(ExtractFileExt(Name));
141 if E = '.c' then
142 Result := 'sourcecode.c.c'
143 else
144 if E = '.m' then
145 Result := 'sourcecode.c.objc'
146 else
147 if (E = '.h') or (E = '.pch') then
148 Result := 'sourcecode.c.h'
149 else
150 if E = '.txt' then
151 Result := 'text'
152 else
153 if E = '.plist' then
154 Result := 'text.plist.xml'
155 else
156 if E = '.entitlements' then
157 Result := 'text.plist.entitlements'
158 else
159 if E = '.storyboard' then
160 Result := 'file.storyboard'
161 else
162 if E = '.png' then
163 Result := 'image.png'
164 else
165 begin
166 WarningWrite('Unrecognized file extension in Xcode project: "%s" on file "%s". Assuming a text file.',
167 [E, Name]);
168 Result := 'text';
169 end;
170 end;
171
172 function TXcodeProjectFile.BuildGroup: string;
173 var
174 E: string;
175 begin
176 E := LowerCase(ExtractFileExt(Name));
177 if (E = '.c') or (E = '.m') then
178 Result := 'Sources'
179 else
180 // Use new (since iOS 8) launch image in storyboard
181 if (E = '.storyboard') or (Name = 'LaunchScreenImage.png') then
182 Result := 'Resources'
183 else
184 // Uncomment this to copy .txt files to the final application.
185 // if E = '.txt' then
186 // Result := 'Resources'
187 // else
188 Result := '';
189 end;
190
191 procedure TXcodeProjectFile.AddChildFile(
192 const FileInfo: TFileInfo; var StopSearch: boolean);
193 var
194 F: TXcodeProjectFile;
195 begin
196 // These are deliberately treated exceptionally by the generation code
197 if SameText(FileInfo.Name, 'en.lproj') or
198 SameText(FileInfo.Name, 'Images.xcassets') then
199 Exit;
200
201 if IsWild(FileInfo.Name, '*~', true) or
202 IsWild(FileInfo.Name, '*.a', true) or
203 SameText(FileInfo.Name, 'data') then
204 begin
205 WritelnWarning('File name "' + FileInfo.Name + '" should not be present in the iOS project template directory, ignoring');
206 Exit;
207 end;
208
209 F := TXcodeProjectFile.Create(FileInfo);
210 Children.Add(F);
211 Project.Files.Add(F);
212 F.Project := Project;
213
214 { recursively scan children }
215 if FileInfo.Directory then
216 FindFiles(FileInfo.AbsoluteName, '*', true, @F.AddChildFile, []);
217 end;
218
219 { TXcodeProject -------------------------------------------------------------- }
220
221 constructor TXcodeProject.Create;
222 begin
223 inherited;
224 Files := TXcodeProjectFileList.Create(true);
225 Frameworks := TXcodeProjectFrameworkList.Create(true);
226 end;
227
228 destructor TXcodeProject.Destroy;
229 begin
230 FreeAndNil(Files);
231 FreeAndNil(Frameworks);
232 inherited;
233 end;
234
235 procedure TXcodeProject.AddTopLevelDir(const Path, Name: string);
236 begin
237 TopLevelDir := TXcodeProjectFile.Create;
238 TopLevelDir.Name := Name;
239 TopLevelDir.Directory := true;
240 TopLevelDir.Project := Self;
241 Files.Add(TopLevelDir);
242
243 { recursively scan children }
244 FindFiles(InclPathDelim(Path) + Name, '*', true, @TopLevelDir.AddChildFile, []);
245 end;
246
247 function TXcodeProject.SectionPBXFileReference: string;
248 var
249 F: TXcodeProjectFile;
250 Fr: TXcodeProjectFramework;
251 FrameworkInfo: String;
252 begin
253 Result := '/* Begin PBXFileReference section */' + NL;
254
255 for F in Files do
256 if not F.Directory then
257 begin
258 Result := Result + Format(
259 #9#9'%s /* %s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = %s; path = "%s"; sourceTree = "<group>"; };' + NL,
260 [F.FileUuid, F.Name, F.FileType, F.Name]);
261 end;
262
263 for Fr in Frameworks do
264 begin
265 case ExtractFileExt(Fr.Name) of
266 '.framework': FrameworkInfo := Format(
267 'lastKnownFileType = wrapper.framework; name = %s; path = System/Library/Frameworks/%s; sourceTree = SDKROOT;',
268 [Fr.Name, Fr.Name]);
269 '.a': FrameworkInfo := Format(
270 'lastKnownFileType = archive.ar; path = %s; sourceTree = "<group>";',
271 [Fr.Name]);
272 '.tbd': FrameworkInfo := Format(
273 'lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "%s"; path = "usr/lib/%s"; sourceTree = SDKROOT;',
274 [Fr.Name, Fr.Name]);
275 else raise EInternalError.CreateFmt('Framework file type not supported: %s', [Fr.Name]);
276 end;
277 Result := Result + Format(
278 #9#9'%s /* %s */ = {isa = PBXFileReference; %s };' + NL,
279 [Fr.FileUuid, Fr.Name, FrameworkInfo]);
280 end;
281
282 Result := Result +
283 { specials }
284 #9#9'4D629DF41916B0EB0082689B /* ${CAPTION}.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "${CAPTION}.app"; sourceTree = BUILT_PRODUCTS_DIR; };' + NL +
285 #9#9'4D629E011916B0EB0082689B /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };' + NL +
286 #9#9'4D629E091916B0EB0082689B /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };' + NL +
287 #9#9'4D629E2A1916B7A40082689B /* ${IOS_LIBRARY_BASE_NAME} */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = ${IOS_LIBRARY_BASE_NAME}; sourceTree = "<group>"; };' + NL +
288 #9#9'4D90CC2119197A82004E90CC /* data */ = {isa = PBXFileReference; lastKnownFileType = folder; name = data; path = ${NAME}/data; sourceTree = "<group>"; };' + NL +
289
290 '/* End PBXFileReference section */' + NL + NL;
291 end;
292
293 function TXcodeProject.SectionPBXBuildFile: string;
294 var
295 F: TXcodeProjectFile;
296 Fr: TXcodeProjectFramework;
297 BuildGroup: string;
298 begin
299 Result := '/* Begin PBXBuildFile section */' + NL;
300
301 for F in Files do
302 if not F.Directory then
303 begin
304 BuildGroup := F.BuildGroup;
305 if BuildGroup <> '' then
306 Result := Result + Format(
307 #9#9'%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };' + NL,
308 [F.BuildUuid, F.Name, BuildGroup, F.FileUuid, F.Name]);
309 end;
310
311 for Fr in Frameworks do
312 Result := Result + Format(
313 #9#9'%s /* %s in Frameworks */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };' + NL,
314 [Fr.BuildUuid, Fr.Name, Fr.FileUuid, Fr.Name]);
315
316 Result := Result +
317 { specials }
318 #9#9'4D629E021916B0EB0082689B /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4D629E001916B0EB0082689B /* InfoPlist.strings */; };' + NL +
319 #9#9'4D629E0A1916B0EB0082689B /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4D629E091916B0EB0082689B /* Images.xcassets */; };' + NL +
320 #9#9'4D629E2B1916B7A40082689B /* ${IOS_LIBRARY_BASE_NAME} in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D629E2A1916B7A40082689B /* ${IOS_LIBRARY_BASE_NAME} */; };' + NL +
321 #9#9'4D90CC2219197A82004E90CC /* data in Resources */ = {isa = PBXBuildFile; fileRef = 4D90CC2119197A82004E90CC /* data */; };' + NL +
322
323 '/* End PBXBuildFile section */' + NL + NL;
324 end;
325
326 function TXcodeProject.SectionPBXGroup: string;
327 var
328 F, Child: TXcodeProjectFile;
329 Fr: TXcodeProjectFramework;
330 SupportingFiles: TXcodeProjectFileList;
331 E: string;
332 begin
333 Result := '/* Begin PBXGroup section */' + NL;
334 SupportingFiles := TXcodeProjectFileList.Create(false);
335
336 { each directory is a group }
337 for F in Files do
338 if F.Directory then
339 begin
340 Result := Result + Format(
341 #9#9'%s /* %s */ = {' + NL +
342 #9#9#9'isa = PBXGroup;' + NL +
343 #9#9#9'children = (' + NL,
344 [F.FileUuid, F.Name]);
345
346 for Child in F.Children do
347 begin
348 { some files should be placed in "Supporting Files" instead }
349 E := LowerCase(ExtractFileExt(Child.Name));
350 if (E = '.plist') or (E = '.pch') then
351 SupportingFiles.Add(Child)
352 else
353 begin
354 Result := Result + Format(
355 #9#9#9#9'%s /* %s */,' + NL,
356 [Child.FileUuid, Child.Name]);
357 end;
358 end;
359
360 if F = TopLevelDir then
361 begin
362 Result := Result +
363 #9#9#9#9'4D629E091916B0EB0082689B /* Images.xcassets */,' + NL +
364 #9#9#9#9'4D629DFE1916B0EB0082689B /* Supporting Files */,' + NL;
365 end;
366
367 Result := Result + Format(
368 #9#9#9');' + NL +
369 #9#9#9'path = "%s";' + NL +
370 #9#9#9'sourceTree = "<group>";' + NL +
371 #9#9'};' + NL,
372 [F.Name]);
373 end;
374
375 { Frameworks group }
376 Result := Result +
377 #9#9'4D629DF61916B0EB0082689B /* Frameworks */ = {' + NL +
378 #9#9#9'isa = PBXGroup;' + NL +
379 #9#9#9'children = (' + NL +
380 #9#9#9#9'4D629E2A1916B7A40082689B /* ${IOS_LIBRARY_BASE_NAME} */,' + NL;
381 for Fr in Frameworks do
382 Result := Result + Format(
383 #9#9#9#9'%s /* %s */,' + NL,
384 [Fr.FileUuid, Fr.Name]);
385 Result := Result +
386 #9#9#9');' + NL +
387 #9#9#9'name = Frameworks;' + NL +
388 #9#9#9'sourceTree = "<group>";' + NL +
389 #9#9'};' + NL;
390
391 { special main (nameless) group }
392 Result := Result +
393 #9#9'4D629DEB1916B0EA0082689B = {' + NL +
394 #9#9#9'isa = PBXGroup;' + NL +
395 #9#9#9'children = (' + NL +
396 #9#9#9#9 + TopLevelDir.FileUuid + ' /* ${NAME} */,' + NL +
397 #9#9#9#9'4D90CC2119197A82004E90CC /* data */,' + NL +
398 #9#9#9#9'4D629DF61916B0EB0082689B /* Frameworks */,' + NL +
399 #9#9#9#9'4D629DF51916B0EB0082689B /* Products */,' + NL +
400 #9#9#9');' + NL +
401 #9#9#9'sourceTree = "<group>";' + NL +
402 #9#9'};' + NL;
403
404 { Products group }
405 Result := Result +
406 #9#9'4D629DF51916B0EB0082689B /* Products */ = {' + NL +
407 #9#9#9'isa = PBXGroup;' + NL +
408 #9#9#9'children = (' + NL +
409 #9#9#9#9'4D629DF41916B0EB0082689B /* ${CAPTION}.app */,' + NL +
410 #9#9#9');' + NL +
411 #9#9#9'name = Products;' + NL +
412 #9#9#9'sourceTree = "<group>";' + NL +
413 #9#9'};' + NL;
414
415 { Supporting Files group }
416 Result := Result +
417 #9#9'4D629DFE1916B0EB0082689B /* Supporting Files */ = {' + NL +
418 #9#9#9'isa = PBXGroup;' + NL +
419 #9#9#9'children = (' + NL +
420 #9#9#9#9'4D629E001916B0EB0082689B /* InfoPlist.strings */,' + NL;
421 for Child in SupportingFiles do
422 Result := Result + Format(
423 #9#9#9#9'%s /* %s */,' + NL,
424 [Child.FileUuid, Child.Name]);
425 Result := Result +
426 #9#9#9');' + NL +
427 #9#9#9'name = "Supporting Files";' + NL +
428 #9#9#9'sourceTree = "<group>";' + NL +
429 #9#9'};' + NL;
430
431 Result := Result +
432 '/* End PBXGroup section */' + NL + NL;
433
434 FreeAndNil(SupportingFiles);
435 end;
436
437 function TXcodeProject.SectionPBXFrameworksBuildPhase: string;
438 var
439 Fr: TXcodeProjectFramework;
440 begin
441 Result := '/* Begin PBXFrameworksBuildPhase section */' + NL +
442 #9#9'4D629DF11916B0EB0082689B /* Frameworks */ = {' + NL +
443 #9#9#9'isa = PBXFrameworksBuildPhase;' + NL +
444 #9#9#9'buildActionMask = 2147483647;' + NL +
445 #9#9#9'files = (' + NL +
446 #9#9#9#9'4D629E2B1916B7A40082689B /* ${IOS_LIBRARY_BASE_NAME} in Frameworks */,' + NL;
447
448 for Fr in Frameworks do
449 Result := Result + Format(
450 #9#9#9#9'%s /* %s in Frameworks */,' + NL,
451 [Fr.BuildUuid, Fr.Name]);
452
453 Result := Result +
454 #9#9#9');' + NL +
455 #9#9#9'runOnlyForDeploymentPostprocessing = 0;' + NL +
456 #9#9'};' + NL +
457 '/* End PBXFrameworksBuildPhase section */' + NL + NL;
458 end;
459
460 function TXcodeProject.SectionPBXResourcesBuildPhase: string;
461 var
462 F: TXcodeProjectFile;
463 begin
464 Result := '/* Begin PBXResourcesBuildPhase section */' + NL +
465 #9#9'4D629DF21916B0EB0082689B /* Resources */ = {' + NL +
466 #9#9#9'isa = PBXResourcesBuildPhase;' + NL +
467 #9#9#9'buildActionMask = 2147483647;' + NL +
468 #9#9#9'files = (' + NL +
469 #9#9#9#9'4D629E021916B0EB0082689B /* InfoPlist.strings in Resources */,' + NL +
470 #9#9#9#9'4D90CC2219197A82004E90CC /* data in Resources */,' + NL +
471 #9#9#9#9'4D629E0A1916B0EB0082689B /* Images.xcassets in Resources */,' + NL;
472
473 { The loop below adds 'Resources', which includes launch image storyboard + png. }
474 for F in Files do
475 if F.BuildGroup = 'Resources' then
476 Result := Result + Format(
477 #9#9#9#9'%s /* %s in %s */,' + NL,
478 [F.BuildUuid, F.Name, F.BuildGroup]);
479
480 Result := Result +
481 #9#9#9');' + NL +
482 #9#9#9'runOnlyForDeploymentPostprocessing = 0;' + NL +
483 #9#9'};' + NL +
484 '/* End PBXResourcesBuildPhase section */' + NL + NL;
485 end;
486
487 function TXcodeProject.SectionPBXSourcesBuildPhase: string;
488 var
489 F: TXcodeProjectFile;
490 begin
491 Result := '/* Begin PBXSourcesBuildPhase section */' + NL +
492 #9#9'4D629DF01916B0EB0082689B /* Sources */ = {' + NL +
493 #9#9#9'isa = PBXSourcesBuildPhase;' + NL +
494 #9#9#9'buildActionMask = 2147483647;' + NL +
495 #9#9#9'files = (' + NL;
496
497 for F in Files do
498 if F.BuildGroup = 'Sources' then
499 Result := Result + Format(
500 #9#9#9#9'%s /* %s in %s */,' + NL,
501 [F.BuildUuid, F.Name, F.BuildGroup]);
502
503 Result := Result +
504 #9#9#9');' + NL +
505 #9#9#9'runOnlyForDeploymentPostprocessing = 0;' + NL +
506 #9#9'};' + NL +
507 '/* End PBXSourcesBuildPhase section */' + NL + NL;
508 end;
509
510 function TXcodeProject.PBXContents: string;
511 begin
512 Result :=
513 SectionPBXFileReference +
514 SectionPBXBuildFile +
515 SectionPBXGroup +
516 SectionPBXFrameworksBuildPhase +
517 SectionPBXResourcesBuildPhase +
518 SectionPBXSourcesBuildPhase;
519 end;
520
521 end.