Debian Codegraph @

tools/build-tool/code/tooldebian.pas

raw
1 {
2 Copyright 2021-2022 Yevhen Loza, 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 { Package a project to a DEB (package format of Debian and derivatives). }
17 unit ToolDebian;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses
24 CastleFindFiles,
25 ToolArchitectures, ToolManifest, ToolPackage;
26
27 { Create Debian package (DEB).
28
29 Assumes that PackagedPath has been created by TPackageDirectory,
30 and it contains the final executable and data files.
31
32 PackageOutputPath, PackageFileName have the same meaning
33 as for TPackageDirectory.Make. }
34 procedure PackageDebian(const PackagedPath: String;
35 const PackageOutputPath, PackageFileName: String;
36 const Cpu: TCpu; const Manifest: TCastleManifest);
37
38 implementation
39
40 uses
41 SysUtils, Process, {$ifdef UNIX} BaseUnix, {$endif}
42 CastleUtils, CastleFilesUtils, CastleDownload, CastleImages, CastleLog,
43 CastleStringUtils,
44 ToolCommonUtils, ToolUtils;
45
46 { FindFileCheck ----------------------------------------------------- }
47
48 { Various checks on files/dirs in package main directory. }
49 procedure FindFileCheck(const FileInfo: TFileInfo; Data: Pointer; var StopSearch: Boolean);
50 const
51 ErrPackagedDescription = 'In Debian package, we only package the main executable and "data" subdirectory now. If you include any other files, they are not placed in the final Debian package.';
52 var
53 Manifest: TCastleManifest;
54 begin
55 Manifest := TCastleManifest(Data);
56
57 if FileInfo.Name = Manifest.ExecutableName then
58 begin
59 if FileInfo.Directory then
60 WritelnWarning('"%s" should not be a directory', [FileInfo.Name]);
61 end else
62 if FileInfo.Name = 'data' then
63 begin
64 if not FileInfo.Directory then
65 WritelnWarning('"%s" should be a directory', [FileInfo.Name]);
66 end else
67 begin
68 if FileInfo.Directory then
69 WritelnWarning('Directory "%s" will not be packaged. ' + ErrPackagedDescription, [FileInfo.Name])
70 else
71 WritelnWarning('File "%s" will not be packaged. ' + ErrPackagedDescription, [FileInfo.Name]);
72 end;
73 end;
74
75 { PackageDebian -------------------------------------------------------------- }
76
77 procedure PackageDebian(const PackagedPath: String;
78 const PackageOutputPath, PackageFileName: String;
79 const Cpu: TCpu; const Manifest: TCastleManifest);
80
81 function CpuToArchitectureString(const Cpu: TCpu): String;
82 begin
83 case Cpu of
84 { Architectures supported by https://packages.debian.org/buster/bash }
85 x86_64: Result := 'amd64';
86 aarch64: Result := 'arm64';
87 arm:
88 begin
89 Result := 'armel';
90 WriteLnWarning('Architecture ' + CpuToString(Cpu) + ' is ambiguous between "armel" and "armhf" in Debian.');
91 end;
92 i386: Result := 'i386';
93 mips: Result := 'mips';
94 mipsel: Result := 'mipsel';
95 powerpc64:
96 begin
97 Result := 'ppc64el';
98 WriteLnWarning('Architecture ' + CpuToString(Cpu) + ' is ambiguous between "ppc64el" and "ppc64" in Debian.');
99 end;
100 // "mips64el"
101 // "s390x"
102 { Unofficial Debian ports https://packages.debian.org/sid/bash }
103 m68k: Result := 'm68k';
104 {$ifdef VER3_0} // FPC 3.2.2 removed riscv64, see https://github.com/castle-engine/castle-engine/commit/04fed13723a985f37f53b3a852d7d84d7e4b975b
105 riscv64: Result := 'riscv64';
106 {$endif}
107 sparc64: Result := 'sparc64';
108 // "alpha"
109 // "sh4"
110 // "x32" = X32 is an ABI for amd64/x86_64 CPUs using 32-bit integers, longs and pointers
111 else {powerpc,sparc,avr,jvm,i8086,riscv32,armeb}
112 begin
113 Result := CpuToString(Cpu);
114 WriteLnWarning('Architecture ' + CpuToString(Cpu) + ' is not officially supported by Debian.');
115 end;
116 end;
117 end;
118
119 const
120 DefaultIconXpmString = {$I ../embedded_images/tooldefaulticonxpm.inc};
121 AllowedDebianPackageChars = ['a'..'z', '0'..'9', '-', '+', '.'];
122 var
123 DebianPackageName, TempPath: String;
124 BinariesSize: Int64;
125 PathToExecutableLocal, PathToExecutableUnix: String;
126 PathToIconFileLocal, PathToIconFileUnix: String;
127 ShareDirLocal, ShareDirUrl: String;
128 PackageDirBaseName, PackageDirLocal, PackageDirUrl: String;
129 ImageMagickExe, PngIcon: String;
130 DebianDescription: String;
131 begin
132 TempPath := InclPathDelim(CreateTemporaryDir);
133
134 // Use FindFileCheck to warn about files not packaged into DEB
135 FindFiles(PackagedPath, '*', false, {$ifdef FPC}@{$endif}FindFileCheck, Manifest, []);
136
137 // Calculate DebianPackageName, avoid using _ in package name -- not allowed
138 DebianPackageName := SReplaceChars(Manifest.Name, '_', '-');
139 { dpkg-deb only accepts package names with these characters. }
140 if CharsPos(AllChars - AllowedDebianPackageChars, DebianPackageName) <> 0 then
141 raise Exception.CreateFmt('Debiian package name is "%s", but for Debian package it can only contain lowercase letters, digits, and -+.', [
142 DebianPackageName
143 ]);
144
145 PackageDirBaseName := DeleteFileExt(PackageFileName);
146 PackageDirLocal := TempPath + PackageDirBaseName;
147 PackageDirUrl := StringReplace(PackageDirLocal, PathDelim, '/', [rfReplaceAll]);
148 if DirectoryExists(PackageDirLocal) then
149 RemoveNonEmptyDir(PackageDirLocal);
150
151 // Copy the executable to /usr/bin/<project-name>
152 PathToExecutableUnix := '/usr/bin/' + Manifest.ExecutableName;
153 PathToExecutableLocal := StringReplace(PathToExecutableUnix, '/', PathDelim, [rfReplaceAll]);
154 CheckForceDirectories(PackageDirLocal + PathDelim + 'usr' + PathDelim + 'bin');
155 CheckCopyFile(InclPathDelim(PackagedPath) + Manifest.ExecutableName,
156 PackageDirLocal + PathDelim + PathToExecutableLocal);
157 { This should be enough for file to be packaged as executable. }
158 DoMakeExecutable(PackageDirLocal + PathDelim + PathToExecutableLocal);
159
160 BinariesSize := FileSize(PackageDirLocal + PathDelim + PathToExecutableLocal);
161
162 // Copy the data to /usr/share/<project-data> (ApplicationData is prepared to detect it by default)
163 ShareDirLocal := PackageDirLocal + PathDelim + 'usr' + PathDelim + 'share';
164 ShareDirUrl := StringReplace(ShareDirLocal, PathDelim, '/', [rfReplaceAll]);
165 CheckForceDirectories(ShareDirLocal);
166
167 if DirectoryExists(InclPathDelim(PackagedPath) + 'data') then
168 begin
169 { ApplicationData expects to find data under /usr/share/<project-name>,
170 so using here Manifest.Name. }
171 CopyDirectory(InclPathDelim(PackagedPath) + 'data', InclPathDelim(ShareDirLocal) + Manifest.Name);
172 BinariesSize += DirectorySize(InclPathDelim(PackagedPath) + 'data');
173 end;
174
175 // Copy or generate XPM icon
176
177 CheckForceDirectories(ShareDirLocal + PathDelim + 'pixmaps');
178 PathToIconFileUnix := '/usr/share/pixmaps/' + Manifest.ExecutableName + '.xpm';
179 PathToIconFileLocal := StringReplace(PathToIconFileUnix, '/', PathDelim, [rfReplaceAll]);
180 if Manifest.Icons.FindExtension(['.xpm']) <> '' then
181 CheckCopyFile(Manifest.Icons.FindExtension(['.xpm']), PackageDirLocal + PathToIconFileLocal)
182 else
183 begin
184 PngIcon := Manifest.Icons.FindExtension(['.png']);
185 // using ImageMagick - FPWriteXPM first doesn't properly write alpha channel, second uses palette char size = 2 which results in large files
186 ImageMagickExe := FindExe('convert');
187 if (PngIcon <> '') and (ImageMagickExe <> '') then
188 begin
189 WritelnVerbose('Converting PNG icon to XPM using ImageMagick.');
190 // 96 colors in XPM still produces 1 symbol per color, larger values double the file size
191 RunCommandSimple(ImageMagickExe, ['-colors', '96', PngIcon, PackageDirLocal + PathToIconFileLocal])
192 end else
193 begin
194 WritelnVerbose('Using default XPM icon.' + NL +
195 ' Reason:' + NL +
196 ' - We did not find in CastleEngineManifest.xml XPM icon.' + NL +
197 ' - We also did not find in CastleEngineManifest.xml PNG icon or we did not find ImageMagick.');
198 StringToFile(PackageDirLocal + PathToIconFileLocal, DefaultIconXpmString);
199 end;
200 end;
201
202 // Create menu item for the game
203
204 CheckForceDirectories(ShareDirLocal + PathDelim + 'menu');
205 StringToFile(
206 ShareDirUrl + '/menu/' + Manifest.ExecutableName,
207 '?package(' + DebianPackageName + '): \' + NL +
208 'needs="X11" \' + NL +
209 'section="' + Manifest.DebianMenuSection + '" \' + NL +
210 'title="' + Manifest.Caption + '" \' + NL +
211 { Note: this assumes that the application is installed under $PATH,
212 and that application will find its own system-wide data using standard
213 ApplicationData logic on Linux. }
214 'command="' + Manifest.ExecutableName + '" \' + NL +
215 'icon="' + PathToIconFileUnix +'"'
216 );
217
218 CheckForceDirectories(ShareDirLocal + PathDelim + 'applications');
219 StringToFile(
220 ShareDirUrl + '/applications/' + Manifest.ExecutableName + '.desktop',
221 '[Desktop Entry]' + NL +
222 'Version=' + Manifest.Version.DisplayValue + NL +
223 'Terminal=false' + NL +
224 'Exec=' + Manifest.ExecutableName + NL +
225 'Icon=' + PathToIconFileUnix + NL +
226 'Type=Application' + NL +
227 'Categories=' + Manifest.FreeDesktopCategories + NL +
228 'Name=' + Manifest.Caption + NL +
229 'Comment=' + Manifest.FreeDesktopComment
230 );
231
232 if Trim(Manifest.Author) = '' then
233 WriteLnWarning('No "author" was provided in CastleEngineManifest.xml. This field is recommended to build a Debian package.');
234 if Trim(Manifest.FreeDesktopComment) = '' then
235 begin
236 WriteLnWarning('No "comment" was provided in CastleEngineManifest.xml. This field is recommended to build a Debian package.');
237 DebianDescription := 'No comment provided';
238 end else
239 DebianDescription := Manifest.FreeDesktopComment;
240
241 CheckForceDirectories(PackageDirLocal + PathDelim + 'DEBIAN');
242 StringToFile(
243 PackageDirUrl + '/DEBIAN/control',
244 'Package: ' + DebianPackageName + NL +
245 'Version: ' + Manifest.Version.DisplayValue + NL +
246 'Section: ' + Manifest.DebianControlSection + NL +
247 'Priority: optional' + NL +
248 'Architecture: ' + CpuToArchitectureString(Cpu) + NL +
249 'Maintainer: ' + Manifest.Author + NL +
250 'Installed-Size: ' + IntToStr(BinariesSize div 1024) + NL +
251 { TODO: This should reflect Project.Dependencies. }
252 'Depends: libopenal1, libpng16-16, zlib1g, libvorbis0a, libvorbisfile3, libfreetype6, libgl1-mesa-dri, libgtk2.0-0' + NL +
253 'Description: ' + Manifest.Caption + NL +
254 ' ' + DebianDescription + NL //final new line
255 );
256
257 // Calculate MD5 checksums
258
259 RunCommandSimple(TempPath + PackageDirBaseName, 'bash', ['-c',
260 'find -type f | egrep -v ''^\./DEBIAN'' | xargs --replace=hh -n1 md5sum "hh" | sed ''s/\ \.\///'' > DEBIAN/md5sums']);
261
262 // Package DEB
263
264 RunCommandSimple(TempPath, 'dpkg-deb', ['--build', PackageDirBaseName]);
265
266 CheckRenameFile(TempPath + PackageDirBaseName + '.deb', CombinePaths(PackageOutputPath, PackageFileName));
267
268 // clean up the temporary files
269 RemoveNonEmptyDir(TempPath);
270 end;
271
272 end.