Debian Codegraph @

tools/build-tool/code/toolcache.pas

raw
1 {
2 Copyright 2022-2022 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 { Compilation cache operations. }
17 unit ToolCache;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses ToolArchitectures, ToolCompile, ToolManifest;
24
25 procedure CacheCreate(const OverrideCompiler: TCompiler;
26 const Target: TTarget; const OS: TOS; const CPU: TCPU);
27
28 procedure CacheClean;
29
30 implementation
31
32 uses SysUtils,
33 CastleUtils, CastleFilesUtils, CastleTimeUtils, CastleStringUtils,
34 ToolProject, ToolUtils, ToolCommonUtils, ToolAndroid, ToolIos;
35
36 procedure CacheCreate(const OverrideCompiler: TCompiler;
37 const Target: TTarget; const OS: TOS; const CPU: TCPU);
38 var
39 ProjectTemplateDir: String;
40
41 { Make cache knowing OS, CPU (does not need to be concerned with Target). }
42 procedure CacheForOsCpu(const OS: TOS; const CPU: TCPU);
43
44 { Make cache knowing OS, CPU, Mode. }
45 procedure CacheForMode(const Mode: TCompilationMode);
46 const
47 Compiler = coFpc;
48 var
49 CacheProject: TCastleProject;
50 CacheProjectDir, CacheOutputPath, CachePathFull: String;
51 begin
52 CacheProjectDir := InclPathDelim(GetTempDir(false)) +
53 'castle-engine-cache-project-' + IntToStr(Random(1000000));
54
55 Writeln(Format('Creating compilation cache for mode "%s" using temporary dir "%s"', [
56 CompilationModeToStr[Mode],
57 CacheProjectDir
58 ]));
59
60 // copy project template to CacheProjectDir
61 if DirectoryExists(CacheProjectDir) then
62 RemoveNonEmptyDir(CacheProjectDir); // clean first
63 CopyDirectory(ProjectTemplateDir, CacheProjectDir);
64
65 // we do SetCurrentDir as this is the only way for now to set TCastleProject location
66 if not SetCurrentDir(CacheProjectDir) then
67 raise Exception.CreateFmt('Cannot enter project directory "%s"', [CacheProjectDir]);
68
69 CacheProject := TCastleProject.Create;
70 try
71 CacheProject.DoCompile(Compiler, Target, OS, CPU, Mode, nil,
72 { do not allow to use cache when building for cache } false);
73 CacheOutputPath := CompilationOutputPath(Compiler, OS, CPU, CacheProjectDir);
74 finally FreeAndNil(CacheProject) end;
75
76 CachePathFull := CachePath +
77 CPUToString(CPU) + '-' + OSToString(OS) + PathDelim +
78 CompilationModeToStr[Mode] + PathDelim;
79 if not ForceDirectories(CachePathFull) then
80 raise Exception.CreateFmt('Cannot create directory for config file: "%s"', [CachePathFull]);
81
82 Writeln(Format('Storing cache in "%s"', [
83 CachePathFull
84 ]));
85 CopyDirectory(CacheOutputPath, CachePathFull);
86
87 { change current directory to CacheProjectDir parent before trying to remove CacheProjectDir,
88 Windows prevents removal otherwise. }
89 {$warnings off} // using ParentPath, should be internal
90 if not SetCurrentDir(ParentPath(CacheProjectDir)) then
91 raise Exception.CreateFmt('Cannot enter project parent directory "%s"', [ParentPath(CacheProjectDir)]);
92 {$warnings on}
93 RemoveNonEmptyDir(CacheProjectDir, true);
94 end;
95
96 var
97 Mode: TCompilationMode;
98 begin
99 for Mode := Low(Mode) to High(Mode) do
100 CacheForMode(Mode);
101 end;
102
103 var
104 CgePath: String;
105 TimeStart: TProcessTimerResult;
106 Seconds: TFloatTime;
107 CacheSize: QWord;
108 AndroidCPU: TCPU;
109 begin
110 TimeStart := ProcessTimer;
111
112 if not (OverrideCompiler in [coAutodetect, coFpc]) then
113 raise Exception.Create('TODO: Only caching for FPC is supported now');
114
115 CgePath := CastleEnginePath;
116 if CgePath = '' then
117 raise Exception.Create(SCannotFindCgePath);
118 ProjectTemplateDir := CgePath + 'tools' + PathDelim + 'build-tool' + PathDelim +
119 'data' + PathDelim + 'castle_cache';
120
121 case Target of
122 targetAndroid:
123 begin
124 for AndroidCPU in DetectAndroidCPUS do
125 CacheForOsCpu(Android, AndroidCPU);
126 end;
127 targetIOS:
128 begin
129 if IosSimulatorSupport then
130 begin
131 CacheForOsCpu(iphonesim, i386);
132 CacheForOsCpu(iphonesim, x86_64);
133 end;
134 CacheForOsCpu(iOS, arm);
135 CacheForOsCpu(iOS, aarch64);
136 end;
137 targetCustom: CacheForOsCpu(OS, CPU);
138 else raise Exception.Create('Caching for this target is not supported now');
139 end;
140
141 CacheSize := DirectorySize(CachePath);
142 Seconds := ProcessTimerSeconds(ProcessTimer, TimeStart);
143 Writeln(Format('Cache created in %f seconds, size on disk: %s', [
144 Seconds,
145 SizeToStr(CacheSize)
146 ]));
147 end;
148
149 procedure CacheClean;
150 var
151 S: String;
152 begin
153 S := CachePath;
154 if DirectoryExists(S) then
155 begin
156 Writeln('Removing cache dir ', S);
157 RemoveNonEmptyDir(S);
158 end;
159 end;
160
161 end.