-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-dmg.sh
executable file
·510 lines (447 loc) · 15.5 KB
/
build-dmg.sh
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
#!/bin/bash
QT_DIR_OVERRIDE="NONE"
QT_FRAMEWORKS_DIR_OVERRIDE="NONE"
# -- DMG builder (if any) to use
USEPKGDMG="FALSE"
USECREATEDMG="FALSE"
usage()
{
echo "Usage: $0 -d QT_DIR -f QT_FRAMEWORKS_DIR <PROJECT.dmginfo> [-p] [-c]"
echo " Command-line args override those provided in dmginfo."
exit 1
}
# Parse options
while getopts "d:f:pc" opt
do
case $opt in
d)
QT_DIR_OVERRIDE=${OPTARG}
echo "Qt in $QT_DIR_OVERRIDE will be used."
;;
f)
QT_FRAMEWORKS_DIR_OVERRIDE=${OPTARG}
echo "Qt frameworks in $QT_FRAMEWORKS_DIR_OVERRIDE will be used."
;;
p)
USEPKGDMG="TRUE"
USECREATEDMG="FALSE"
echo "Will use pkg-dmg to create disk image."
;;
c)
USEPKGDMG="FALSE"
USECREATEDMG="TRUE"
echo "Will use create-dmg to create disk image."
;;
*)
usage
;;
esac
done
# Get filename arg
shift $(( OPTIND - 1 ));
DMGINFO=$@
# Enable erroring
set -e
# /-----------------\
# | Define defaults |
# \-----------------/
# -- APP : The name of the app being packaged
APP_NAME="NONE"
# -- APP_VERSION : Should contain the version / revision number of the package (for renaming purposes)
APP_VERSION="0.1"
# -- APP_BIN : List of binaries (space-separated, with paths) to include in the bundle
APP_BIN="NONE"
# -- APP_PLIST : Specify the Info.plist file to use in the bundle
APP_PLIST="NONE"
# -- APP_ICON : A 1024x1024 png file from which to create an icon set
APP_ICON="NONE"
# -- APP_LICENSE : License information file to put in the bundle
APP_LICENSE="NONE"
# -- APP_EXTRA : Directory containing additional files to contain in bundle (or NONE)
APP_EXTRA="NONE"
# -- APP_DSSTORE : Specifies a directory containing DS_Store and background image for dmg (or NONE)
APP_DSSTORE="NONE"
# -- USE_QT : Set to TRUE if this is a Qt app, and macdeployqt should be used
USE_QT="FALSE"
QT_DIR=/Developer/Applications/Qt
QT_FRAMEWORKS_DIR=/Library/Frameworks
QT_NO_DYLIBS="TRUE"
QT_EXTRA_FRAMEWORKS=""
QT_EXTRA_IMAGEFORMATS=""
QT_VERSION="5"
# -- QT_STD_FRAMEWORKS to seek and rewrite links to in binaries / libs
QT_STD_FRAMEWORKS="QtSvg QtXml QtCore QtGui QtWidgets"
# -- EXTRA_DYLIBS : Extra dylibs to be copied in to the bundle (or NONE)
# -- : Format is "<input dylib | NONE>,<input dylib | NONE>,<output dylib>"
EXTRA_DYLIBS="NONE"
# /---------------------------------------------------\
# | Source provided dmginfo file, and check variables |
# \---------------------------------------------------/
if ! source $1
then
echo "Error sourcing dmginfo file $1"
exit 1
fi
# Set overrides
if [ "$QT_DIR_OVERRIDE" != "NONE" ]
then
QT_DIR=${QT_DIR_OVERRIDE}
fi
if [ "$QT_FRAMEWORKS_DIR_OVERRIDE" != "NONE" ]
then
QT_FRAMEWORKS_DIR=${QT_FRAMEWORKS_DIR_OVERRIDE}
fi
# -- Check for NONE being specified for a critical variable
if [ "$APP_NAME" = "NONE" ] || [ "$APP_BIN" = "NONE" ] || [ "$APP_PLIST" = "NONE" ]
then
echo "One or more critical variables have not been defined."
echo "name=[$APP_NAME] bin=[$APP_BIN] plist=[$APP_PLIST]"
exit 1
fi
# /------------------\
# | Retrieve pkg-dmg |
# \------------------/
if [ "$USEPKGDMG" = "TRUE" ]
then
echo -e "\nRetrieving pkg-dmg...\n"
wget -q https://raw.githubusercontent.com/trisyoungs/scripts/master/pkg-dmg -O ./pkg-dmg
chmod u+x ./pkg-dmg
fi
# /-------------------\
# | Create bundle dir |
# \-------------------/
echo -e "\nCreating bundle directory structure....\n"
# -- Construct full name of the package directory and create dir
APP_ROOT=${APP_NAME}-${APP_VERSION}
# -- Create new directory structure for our bundle
if [ -e $APP_ROOT ]; then rm -rf $APP_ROOT; fi
APP_CONTENTS=$APP_ROOT/${APP_NAME}.app/Contents
mkdir -vp $APP_CONTENTS
APP_FRAMEWORKS=$APP_CONTENTS/Frameworks
APP_BINARIES=$APP_CONTENTS/MacOS
APP_LIBRARIES=$APP_CONTENTS/lib
APP_PLUGINS=$APP_CONTENTS/PlugIns
APP_RESOURCES=$APP_CONTENTS/Resources
APP_SHAREDSUPPORT=$APP_CONTENTS/SharedSupport
mkdir -v $APP_FRAMEWORKS $APP_BINARIES $APP_LIBRARIES $APP_PLUGINS $APP_RESOURCES
# /-----------------\
# | Copy plist file |
# \-----------------/
echo -e "\nCopy plist file....\n"
if [ ! -e $APP_PLIST ]; then
echo "Error: Specified plist file does not exist: $APP_PLIST"
exit 1
fi
if ! cp -v $APP_PLIST $APP_CONTENTS; then
echo "Error: Failed to copy plist file: $APP_PLIST"
exit 1
fi
# /---------------\
# | Copy binaries |
# \---------------/
echo -e "\nCopying binary files....\n"
# -- Loop over files
for binary in $APP_BIN;
do
if [ ! -e $binary ]; then
echo "Error: Specified binary does not exist: $binary"
exit 1
fi
if ! cp -v $binary $APP_BINARIES; then
echo "Error: Failed to copy binary: $binary"
exit 1
fi
done
# /--------------\
# | Copy Qt data |
# \--------------/
if [ "$USE_QT" = "TRUE" ]
then
echo -e "\nRunning macdeployqt....\n"
# -- Run macdeployqt to setup framework and plugin data
$QT_DIR/bin/macdeployqt $APP_ROOT/${APP_NAME}.app -verbose=2
# -- Remove any dylibs which Qt may have copied in to the Frameworks dir?
if [ "$QT_NO_DYLIBS" = "TRUE" ]
then
echo "Removing dylibs copied in by macdeployqt..."
rm -v $APP_FRAMEWORKS/*dylib
fi
# -- Copy in missing frameworks and imageformats, and change DyLib links to reference the package
echo -e "\nCopying in additional Qt frameworks...\n"
for framework in $QT_EXTRA_FRAMEWORKS
do
if [ -e $QT_FRAMEWORKS_DIR/$framework.framework/Versions/$QT_VERSION/$framework ]
then
mkdir -vp $APP_FRAMEWORKS/$framework.framework/Resources $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION
cp -va $QT_FRAMEWORKS_DIR/$framework.framework/Versions/$QT_VERSION/$framework $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION/
chmod u+rw $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION/$framework
install_name_tool -id "@executable_path/../Frameworks/$framework.framework/Versions/$QT_VERSION/$framework" $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION/$framework
# Rewrite dylib links in new framework
for lib in $QT_STD_FRAMEWORKS
do
binlib=`otool -L $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION/$framework | grep $lib | awk '{print $1}'`
if [ "x$binlib" != "x" ]
then
echo "Rewriting dylib link for $lib in $framework (from $binlib to @executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib)"
install_name_tool -change "$binlib" "@executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib" $APP_FRAMEWORKS/$framework.framework/Versions/$QT_VERSION/$framework
fi
done
else
echo "Qt framework $QT_FRAMEWORKS_DIR/$framework.framework/Versions/$QT_VERSION/$framework not found."
exit 1
fi
done
echo -e "\nCopying in additional Qt imageformats...\n"
for imageformat in $QT_EXTRA_IMAGEFORMATS
do
if [ -e $QT_DIR/plugins/imageformats/$imageformat ]
then
cp -v $QT_DIR/plugins/imageformats/$imageformat $APP_PLUGINS/imageformats/
# Rewrite dylib links in imageformat
for lib in $QT_STD_FRAMEWORKS
do
binlib=`otool -L $APP_PLUGINS/imageformats/$imageformat | grep $lib | awk '{print $1}'`
if [ "x$binlib" != "x" ]
then
echo "Rewriting dylib link for $lib in $imageformat (from $binlib to @executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib)"
install_name_tool -change "$binlib" "@executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib" $APP_PLUGINS/imageformats/$imageformat
fi
done
else
echo "Qt imageformat $imageformat not found."
exit 1
fi
done
# cp -v $QT_DIR/plugins/imageformats/libqsvg.dylib $APP_PLUGINS/imageformats/
# -- Rewrite DyLib links in newly-copied frameworks / plugins to reference bundle frameworks
# for target in $APP_PLUGINS/imageformats/*.dylib
# do
# for lib in QtSvg QtXml QtCore QtGui
# do
# binlib=`otool -L $target | grep $lib | awk '{print $1}'`
# if [ "x$binlib" != "x" ]
# then
# echo "Rewriting dylib link for $lib in $target (from $binlib to @executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib)"
# install_name_tool -change "$binlib" "@executable_path/../Frameworks/$lib.framework/Versions/$QT_VERSION/$lib" $target
# fi
# done
fi
# /-------------------\
# | Copy extra dylibs |
# \-------------------/
echo -e "\nCopying extra dylibs to bundle....\n"
if [ "x$EXTRA_DYLIBS" != "xNONE" ]
then
for a in $EXTRA_DYLIBS
do
# Split arguments up (comma-delimited)
args=(${a//,/ })
dylib1=${args[0]}
dylibname1=`basename $dylib1`
dylib2=${args[1]}
dylibname2=`basename $dylib2`
lib=${args[2]}
if [ "x${args[3]}" != "x" ]; then
searchname=${args[3]}
else
searchname="NONE"
fi
echo -e "Incorporating ${lib} from ${dylib1} and ${dylib2}}...\n"
if [ "x$dylib1" != "xNONE" ]; then
if ! cp -rv $dylib1 ./$dylibname1.1
then
echo "Error: Failed to copy 32-bit library $dylib1."
exit 1
fi
chmod u+rw ./$dylibname1.1
fi
if [ "x$dylib2" != "xNONE" ]; then
if ! cp -rv $dylib2 ./$dylibname2.2
then
echo "Error: Failed to copy 64-bit library $dylib2."
exit 1
fi
chmod u+rw ./$dylibname2.2
fi
# Change local id in dylibs
if [ "$dylib1" != "NONE" ]; then install_name_tool -id "@executable_path/../lib/$dylibname1" $dylibname1.1; fi
if [ "$dylib2" != "NONE" ]; then install_name_tool -id "@executable_path/../lib/$dylibname2" $dylibname2.2; fi
# Combine into one lib (if two libs were supplied)
if [ "$dylib1" = "NONE" ]; then mv $dylibname2.2 $APP_LIBRARIES/$lib
elif [ "$dylib2" = "NONE" ]; then mv $dylibname1.1 $APP_LIBRARIES/$lib
else
if ! lipo -create -output $APP_LIBRARIES/$lib ./$dylibname1.1 ./$dylibname2.2
then
echo "Failed to create universal dylib for $lib."
exit 1
fi
fi
# Remove temporary files
if [ -e ./$dylibname1.1 ]; then rm -v ./$dylibname1.1; fi
if [ -e ./$dylibname2.2 ]; then rm -v ./$dylibname2.2; fi
done
# Change library references amongst the dylibs we have just copied over, and the executables for the bundle
for a in $EXTRA_DYLIBS
do
# Split arguments up (comma-delimited)
args=(${a//,/ })
dylib1=${args[0]}
dylibname1=`basename $dylib1`
dylib2=${args[1]}
dylibname2=`basename $dylib2`
lib=${args[2]}
if [ "x${args[3]}" != "x" ]; then
searchname=${args[3]}
else
searchname=$lib
fi
# -- Other dylibs
for target in $APP_LIBRARIES/*
do
# First need to see if this library depends on the current dylib...
binlib=`otool -L $target | grep $searchname | awk '{print $1}'`
if [ "x$binlib" != "x" ]
then
echo "-- Rewriting reference for $lib in dylib $target (from $binlib to @executeable_path/../lib/$lib)"
if ! install_name_tool -change "$binlib" "@executable_path/../lib/$lib" $target
then
echo "Error: Failed to rewrite reference to $lib in dylib $target."
exit 1
fi
fi
# Make sure that all dylibs reference system libgcc_s.1.dylib...
#binlib=`otool -L $target | grep libgcc_s.1.dylib | awk '{print $1}'`
# if [ "x$binlib" != "x" ] && [ "$binlib" != "/usr/lib/libgcc_s.1.dylib" ]
# then
# echo "-- Rewriting reference for libgcc_s.1.dylib in dylib $target (from $binlib to /usr/lib/libgcc_s.1.dylib)"
# if ! install_name_tool -change "$binlib" "/usr/lib/libgcc_s.1.dylib" $target
# then
# echo "Error: Failed to rewrite reference to $lib in dylib $target."
# exit 1
# fi
# fi
done
# -- Executables
for target in $APP_BINARIES/*
do
# First need to see if this library depends on the current dylib...
binlib=`otool -L $target | grep $searchname | awk '{print $1}'`
if [ "x$binlib" != "x" ]
then
echo "-- Rewriting reference for $lib in binary $target (from $binlib to @executeable_path/../lib/$lib)"
if ! install_name_tool -change "$binlib" "@executable_path/../lib/$lib" $target
then
echo "Error: Failed to rewrite reference to $lib in binary $target."
exit 1
fi
fi
done
done
fi
# /------------------\
# | Generate iconset |
# \------------------/
if [ "x$APP_ICON" != "xNONE" ];
then
echo -e "\nGenerating iconset....\n"
if [ ! -e $APP_ICON ]; then
echo "Error: Specified icon file does not exist: $APP_ICON"
exit 1
fi
mkdir ${APP_NAME}.iconset
sips -z 16 16 $APP_ICON --out ${APP_NAME}.iconset/icon_16x16.png
sips -z 32 32 $APP_ICON --out ${APP_NAME}.iconset/[email protected]
sips -z 32 32 $APP_ICON --out ${APP_NAME}.iconset/icon_32x32.png
sips -z 64 64 $APP_ICON --out ${APP_NAME}.iconset/[email protected]
sips -z 128 128 $APP_ICON --out ${APP_NAME}.iconset/icon_128x128.png
sips -z 256 256 $APP_ICON --out ${APP_NAME}.iconset/[email protected]
sips -z 256 256 $APP_ICON --out ${APP_NAME}.iconset/icon_256x256.png
sips -z 512 512 $APP_ICON --out ${APP_NAME}.iconset/[email protected]
sips -z 512 512 $APP_ICON --out ${APP_NAME}.iconset/icon_512x512.png
cp -v $APP_ICON ${APP_NAME}.iconset/[email protected]
iconutil -c icns ${APP_NAME}.iconset
if ! cp -v ${APP_NAME}.icns $APP_RESOURCES; then
echo "Error: Failed to copy icon file: $APP_ICON"
exit 1
fi
if ! cp -v ${APP_NAME}.icns $APP_ROOT/.VolumeIcon.icns; then
echo "Error: Failed to copy icon file: $APP_ICON"
exit 1
fi
rm -rf ${APP_NAME}.iconset ${APP_NAME}.icns
fi
# /--------------\
# | Copy license |
# \--------------/
echo -e "\nCopying license....\n"
if [ x$APP_LICENSE != "xNONE" ];
then
if [ ! -e $APP_LICENSE ]; then
echo "Error: Specified license file does not exist: $APP_LICENSE"
exit 1
fi
if ! cp -v $APP_LICENSE $APP_ROOT/.COPYING; then
echo "Error: Failed to copy license file: $APP_LICENSE"
exit 1
fi
fi
# /-----------------\
# | Copy extra data |
# \-----------------/
if [ "x$APP_EXTRA" != "xNONE" ];
then
echo -e "\nCopying extra data....($APP_EXTRA)\n"
mkdir -v $APP_SHAREDSUPPORT
for a in $APP_EXTRA;
do
if [ ! -e $a ]; then
echo "Error: Specified extra data does not exist: $a"
exit 1
fi
if ! cp -rv $APP_EXTRA $APP_SHAREDSUPPORT; then
echo "Error: Failed to copy extra data from: $APP_EXTRA"
exit 1
fi
done
fi
# /--------------------------\
# | Copy background/DS_Store |
# \--------------------------/
if [ x$APP_DSSTORE != "xNONE" ];
then
echo -e "\nCopying DS_Store information....\n"
if [ ! -e $APP_DSSTORE ]; then
echo "Error: Specified DS_Store file does not exist: $APP_DSSTORE"
exit 1
fi
if ! cp -rv $APP_DSSTORE/ $APP_ROOT; then
echo "Error: Failed to copy DS_Store files."
exit 1
fi
fi
# /------------------\
# | Create final DMG |
# \------------------/
echo -e "\nCreating dmg file....\n"
if [ "$USEPKGDMG" = "TRUE" ]
then
ARGS="--source $APP_ROOT --target ${APP_ROOT}.dmg --volname ${APP_ROOT}"
if [ "$APP_ICON" != "NONE" ]; then ARGS="$ARGS --icon ${APP_ROOT}/.VolumeIcon.icns"; fi
if [ "$APP_LICENSE" != "NONE" ]; then ARGS="$ARGS --license ${APP_ROOT}/.COPYING"; fi
./pkg-dmg $ARGS --symlink /Applications:"/Applications"
fi
if [ "$USECREATEDMG" = "TRUE" ]
then
ARGS="--volname ${APP_ROOT} --no-internet-enable"
if [ "$APP_ICON" != "NONE" ]; then ARGS="$ARGS --volicon ${APP_ROOT}/.VolumeIcon.icns"; fi
if [ "$APP_LICENSE" != "NONE" ]; then ARGS="$ARGS --eula ${APP_ROOT}/.COPYING"; fi
create-dmg $ARGS ${APP_ROOT}.dmg ${APP_ROOT}
fi
# /---------\
# | Cleanup |
# \---------/
if [ "$USEPKGDMG" = "TRUE" ] || [ "$USECREATEDMG" = "TRUE" ]
then
rm -rf $APP_ROOT
fi
exit 0