| 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 |
{ macOS specific utilities. } |
| 17 |
unit ToolMacOS; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleUtils, CastleStringUtils, |
| 23 |
ToolProject; |
| 24 |
|
| 25 |
{ Create AppBundle to run the project in castle-engine-output. |
| 26 |
This is the only reliable way to run GUI applications on macOS. |
| 27 |
|
| 28 |
@param(BundleParenPath Path (may, but doesn't have to, end with PathDelim) |
| 29 |
where the xxx.app directory (app bundle) will be placed.) |
| 30 |
|
| 31 |
@param(SymlinkToFiles If @true we'll only make (relative) symlinks to actual project |
| 32 |
executable, icon (if already in icns format) and data, |
| 33 |
otherwise we will copy them into the bundle.) } |
| 34 |
procedure CreateMacAppBundle(const Project: TCastleProject; const BundleParenPath: String; |
| 35 |
const SymlinkToFiles: Boolean; out ExeInBundle: String); |
| 36 |
procedure CreateMacAppBundle(const Project: TCastleProject; const BundleParenPath: String; |
| 37 |
const SymlinkToFiles: Boolean); |
| 38 |
|
| 39 |
{ Zip the bundle created by CreateMacAppBundle. |
| 40 |
|
| 41 |
BundleParenPath should contain aaa.app directory (app bundle), |
| 42 |
just like made by CreateMacAppBundle. |
| 43 |
|
| 44 |
BundleParenPath will also contain the resulting zip file, |
| 45 |
with name PackageFileName (PackageFileName should not contain any path, |
| 46 |
be like 'foo.zip'). } |
| 47 |
procedure ZipMacAppBundle(const Project: TCastleProject; const BundleParenPath, PackageFileName: String); |
| 48 |
|
| 49 |
implementation |
| 50 |
|
| 51 |
uses {$ifdef UNIX} BaseUnix, {$endif} SysUtils, |
| 52 |
CastleFilesUtils, CastleLog, CastleImages, |
| 53 |
ToolArchitectures, ToolCommonUtils, ToolUtils, ToolEmbeddedImages; |
| 54 |
|
| 55 |
procedure SaveResized(const Image: TCastleImage; const Size: Integer; const OutputFileName: string); |
| 56 |
var |
| 57 |
R: TCastleImage; |
| 58 |
begin |
| 59 |
R := Image.MakeResized(Size, Size, BestInterpolation); |
| 60 |
try |
| 61 |
SaveImage(R, OutputFileName); |
| 62 |
finally FreeAndNil(R) end; |
| 63 |
end; |
| 64 |
|
| 65 |
{ Turn a TCastleImage -> ICNS. |
| 66 |
|
| 67 |
Based on |
| 68 |
https://www.codingforentrepreneurs.com/blog/create-icns-icons-for-macos-apps/ |
| 69 |
https://stackoverflow.com/questions/12306223/how-to-manually-create-icns-files-using-iconutil/20703594#20703594 |
| 70 |
https://gist.github.com/jamieweavis/b4c394607641e1280d447deed5fc85fc |
| 71 |
|
| 72 |
We don't use sips, ImageMagick etc., instead process image using our TCastleImage. |
| 73 |
This way we control the resize quality and the output messages. |
| 74 |
} |
| 75 |
function ImageToIcns(const Project: TCastleProject; const Image: TCastleImage): String; |
| 76 |
var |
| 77 |
TempPath, IconsetDir: String; |
| 78 |
begin |
| 79 |
{ create clean TempPath } |
| 80 |
TempPath := TempOutputPath(Project.Path) + |
| 81 |
'macos' + PathDelim + 'icns_generation' + PathDelim; |
| 82 |
if DirectoryExists(TempPath) then |
| 83 |
RemoveNonEmptyDir(TempPath); |
| 84 |
|
| 85 |
IconsetDir := TempPath + Project.Name + '.iconset'; |
| 86 |
ForceDirectories(IconsetDir); |
| 87 |
|
| 88 |
SaveResized(Image, 16, IconsetDir + PathDelim + 'icon_16x16.png'); |
| 89 |
SaveResized(Image, 32, IconsetDir + PathDelim + '[email protected]'); |
| 90 |
SaveResized(Image, 32, IconsetDir + PathDelim + 'icon_32x32.png'); |
| 91 |
SaveResized(Image, 64, IconsetDir + PathDelim + '[email protected]'); |
| 92 |
SaveResized(Image, 128, IconsetDir + PathDelim + 'icon_128x128.png'); |
| 93 |
SaveResized(Image, 256, IconsetDir + PathDelim + '[email protected]'); |
| 94 |
SaveResized(Image, 256, IconsetDir + PathDelim + 'icon_256x256.png'); |
| 95 |
SaveResized(Image, 512, IconsetDir + PathDelim + '[email protected]'); |
| 96 |
SaveResized(Image, 512, IconsetDir + PathDelim + 'icon_512x512.png'); |
| 97 |
SaveResized(Image, 1024, IconsetDir + PathDelim + '[email protected]'); |
| 98 |
|
| 99 |
RunCommandSimple(TempPath, 'iconutil', ['-c', 'icns', Project.Name + '.iconset']); |
| 100 |
|
| 101 |
Result := TempPath + Project.Name + '.icns'; |
| 102 |
end; |
| 103 |
|
| 104 |
{ Create App Bundle to run on macOS, with reliable input, menu etc. |
| 105 |
|
| 106 |
See |
| 107 |
https://wiki.freepascal.org/Application_Bundle |
| 108 |
https://wiki.freepascal.org/macOS_property_list_files |
| 109 |
https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html |
| 110 |
} |
| 111 |
procedure CreateMacAppBundle(const Project: TCastleProject; const BundleParenPath: String; |
| 112 |
const SymlinkToFiles: Boolean; out ExeInBundle: String); |
| 113 |
|
| 114 |
procedure SymlinkCheck(const Src, Dst: String); |
| 115 |
begin |
| 116 |
{$ifdef UNIX} |
| 117 |
OSCheck(FpSymlink(PChar(Src), PChar(Dst)) <> -1, |
| 118 |
Format('Cannot create symlink "%s" -> "%s"', [ |
| 119 |
Src, |
| 120 |
Dst |
| 121 |
])); |
| 122 |
{$else} |
| 123 |
raise Exception.CreateFmt('Cannot create symlink on non-Unix systems. Tried to create symlink "%s" -> "%s"', [ |
| 124 |
Src, |
| 125 |
Dst |
| 126 |
]); |
| 127 |
{$endif} |
| 128 |
end; |
| 129 |
|
| 130 |
procedure CopyOrSymlinkFile(const Src, Dst: String); |
| 131 |
begin |
| 132 |
if SymlinkToFiles then |
| 133 |
SymlinkCheck(Src, Dst) |
| 134 |
else |
| 135 |
CheckCopyFile(Src, Dst); |
| 136 |
end; |
| 137 |
|
| 138 |
procedure CopyOrSymlinkData(const Dst: String); |
| 139 |
begin |
| 140 |
if SymlinkToFiles then |
| 141 |
SymlinkCheck(ExclPathDelim(Project.DataPath), ExclPathDelim(Dst)) |
| 142 |
else |
| 143 |
Project.CopyData(Dst, cpDesktop); |
| 144 |
end; |
| 145 |
|
| 146 |
var |
| 147 |
OutputBundlePath, OutputBundleExePath, OutputBundleResourcesPath, IconIcns, IconPng: String; |
| 148 |
LoadedIcon: TCastleImage; |
| 149 |
begin |
| 150 |
{ create clean OutputBundlePath } |
| 151 |
OutputBundlePath := InclPathDelim(BundleParenPath) + Project.Caption + '.app' + PathDelim; |
| 152 |
if DirectoryExists(OutputBundlePath) then |
| 153 |
RemoveNonEmptyDir(OutputBundlePath); |
| 154 |
|
| 155 |
Project.ExtractTemplate('macos/app_bundle', OutputBundlePath); |
| 156 |
|
| 157 |
{ Check Info.plist correctness, following https://wiki.freepascal.org/macOS_property_list_files. |
| 158 |
See https://www.unix.com/man-page/osx/1/plutil/ for command-line options: |
| 159 |
-s means "Don't print anything on success." } |
| 160 |
RunCommandSimple('plutil', ['-s', OutputBundlePath + 'Contents' + PathDelim + 'Info.plist']); |
| 161 |
|
| 162 |
OutputBundleExePath := OutputBundlePath + |
| 163 |
'Contents' + PathDelim + 'MacOS' + PathDelim; |
| 164 |
OutputBundleResourcesPath := OutputBundlePath + |
| 165 |
'Contents' + PathDelim + 'Resources' + PathDelim; |
| 166 |
|
| 167 |
ForceDirectories(OutputBundleExePath); |
| 168 |
ForceDirectories(OutputBundleResourcesPath); |
| 169 |
|
| 170 |
ExeInBundle := OutputBundleExePath + Project.ExecutableName; |
| 171 |
CopyOrSymlinkFile(Project.Path + Project.ExecutableName, ExeInBundle); |
| 172 |
DoMakeExecutable(ExeInBundle); |
| 173 |
|
| 174 |
IconIcns := Project.Icons.FindExtension(['.icns']); |
| 175 |
if IconIcns <> '' then |
| 176 |
begin |
| 177 |
{ Icons.FindExtension is relative to project path, |
| 178 |
we need absolute path -- as current dir may not be project path, |
| 179 |
and also for symlink we need absolute path. } |
| 180 |
IconIcns := CombinePaths(Project.Path, IconIcns); |
| 181 |
end else |
| 182 |
begin |
| 183 |
IconPng := Project.Icons.FindExtension(['.png']); |
| 184 |
if IconPng <> '' then |
| 185 |
begin |
| 186 |
LoadedIcon := LoadImage(IconPng); |
| 187 |
try |
| 188 |
IconIcns := ImageToIcns(Project, LoadedIcon); |
| 189 |
finally FreeAndNil(LoadedIcon) end; |
| 190 |
end else |
| 191 |
IconIcns := ImageToIcns(Project, DefaultIcon); |
| 192 |
end; |
| 193 |
CopyOrSymlinkFile(IconIcns, OutputBundleResourcesPath + Project.Name + '.icns'); |
| 194 |
|
| 195 |
if Project.DataExists then |
| 196 |
CopyOrSymlinkData(OutputBundleResourcesPath + 'data' + PathDelim); |
| 197 |
|
| 198 |
Writeln(Format('Created macOS AppBundle %s"%s"', [ |
| 199 |
Iff(SymlinkToFiles, '(using symlinks to actual data, in temp dir) ', ''), |
| 200 |
ExtractFileName(ExclPathDelim(OutputBundlePath)) |
| 201 |
])); |
| 202 |
end; |
| 203 |
|
| 204 |
procedure CreateMacAppBundle(const Project: TCastleProject; const BundleParenPath: String; |
| 205 |
const SymlinkToFiles: Boolean); |
| 206 |
var |
| 207 |
IgnoreExeInBundle: String; |
| 208 |
begin |
| 209 |
CreateMacAppBundle(Project, BundleParenPath, SymlinkToFiles, IgnoreExeInBundle); |
| 210 |
end; |
| 211 |
|
| 212 |
procedure ZipMacAppBundle(const Project: TCastleProject; const BundleParenPath, PackageFileName: String); |
| 213 |
begin |
| 214 |
//RunCommandSimple(BundleParenPath, 'zip', ['-q', '-r', PackageFileName, Project.Caption + '.app']); |
| 215 |
// Better use internal zip, that doesn't require any tool installed: |
| 216 |
ZipDirectory( |
| 217 |
CombinePaths(BundleParenPath, PackageFileName), |
| 218 |
CombinePaths(BundleParenPath, Project.Caption + '.app')); |
| 219 |
|
| 220 |
Writeln(Format('Packed to "%s"', [PackageFileName])); |
| 221 |
end; |
| 222 |
|
| 223 |
end. |