~suv-lp/+junk/inkscape-osxapp-stacked

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/bin/bash
#
# USAGE
# osx-app [-s] [-l /path/to/libraries] -py /path/to/python/modules [-l /path/to/libraries] -b /path/to/bin/inkscape -p /path/to/Info.plist
#
# This script attempts to build an Inkscape.app package for OS X, resolving
# dynamic libraries, etc.
# 
# If the '-s' option is given, then the libraries and executable are stripped.
# 
# The Info.plist file can be found in the base inkscape directory once
# configure has been run.
#
# AUTHORS
#		 Kees Cook <kees@outflux.net>
#		 Michael Wybrow <mjwybrow@users.sourceforge.net>
#		 Jean-Olivier Irisson <jo.irisson@gmail.com>
#		 ~suv <suv-sf at users.sourceforge.net>
# 
# Copyright (C) 2005 Kees Cook
# Copyright (C) 2005-2009 Michael Wybrow
# Copyright (C) 2007-2009 Jean-Olivier Irisson
#
# Released under GNU GPL, read the file 'COPYING' for more information
#
# Thanks to GNUnet's "build_app" script for help with library dep resolution.
#		https://gnunet.org/svn/GNUnet/contrib/OSX/build_app
# 
# NB:
# When packaging Inkscape for OS X, configure should be run with the 
# "--enable-osxapp" option which sets the correct paths for support
# files inside the app bundle.
# 

# Defaults
strip=false
add_python=false
python_dir=""

# If LIBPREFIX is not already set (by osx-build.sh for example) set it to blank (one should use the command line argument to set it correctly)
if [ -z $LIBPREFIX ]; then
	LIBPREFIX=""
fi


# Help message
#----------------------------------------------------------
help()
{
echo -e "
Create an app bundle for OS X

\033[1mUSAGE\033[0m
	$0 [-s] [-l /path/to/libraries] -py /path/to/python/modules -b /path/to/bin/inkscape -p /path/to/Info.plist

\033[1mOPTIONS\033[0m
	\033[1m-h,--help\033[0m 
		display this help message
	\033[1m-s\033[0m
		strip the libraries and executables from debugging symbols
	\033[1m-py,--with-python\033[0m
		add python modules (numpy, lxml) from given directory
		inside the app bundle
	\033[1m-l,--libraries\033[0m
		specify the path to the librairies Inkscape depends on
		(typically /sw or /opt/local)
	\033[1m-b--binary\033[0m
		specify the path to Inkscape's binary. By default it is in
		Build/bin/ at the base of the source code directory
	\033[1m-p,--plist\033[0m
		specify the path to Info.plist. Info.plist can be found
		in the base directory of the source code once configure
		has been run

\033[1mEXAMPLE\033[0m
	$0 -s -py ~/python-modules -l /opt/local -b ../../Build/bin/inkscape -p ../../Info.plist
"
}


# Parse command line arguments
#----------------------------------------------------------
while [ "$1" != "" ]
do
	case $1 in
		-py|--with-python)
			add_python=true
			python_dir="$2"
			shift 1 ;;
		-s)
			strip=true ;;
		-l|--libraries)
			LIBPREFIX="$2"
			shift 1 ;;
		-b|--binary)
			binary="$2"
			shift 1 ;;
		-p|--plist)
			plist="$2"
			shift 1 ;;
		-h|--help)
			help
			exit 0 ;;
		*)
			echo "Invalid command line option: $1" 
			exit 2 ;;
	esac
	shift 1
done

echo -e "\n\033[1mCREATE INKSCAPE APP BUNDLE\033[0m\n"

# Safety tests

if [ "x$binary" == "x" ]; then
	echo "Inkscape binary path not specified." >&2
	exit 1
fi

if [ ! -x "$binary" ]; then
	echo "Inkscape executable not not found at $binary." >&2
	exit 1
fi

if [ "x$plist" == "x" ]; then
	echo "Info.plist file not specified." >&2
	exit 1
fi

if [ ! -f "$plist" ]; then
	echo "Info.plist file not found at $plist." >&2
	exit 1
fi

# TODO: cleanup & unify usage of OS X VERSION information
VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`

if [ "$VERSION" -lt "7" ]; then
	# On Mac OS X 10.6 Snow Leopard and below, use python modules 
	# from 'Python-packages.dmg', requires to run 32bit Python <= 2.6

	#PYTHONPACKURL="http://inkscape.modevia.com/macosx-snap/Python-packages.dmg"
	# TODO: find new host for 'Python-packages.dmg' (and osx nightlies)
	#       new temporary download via DropBox:
	PYTHONPACKURL="http://dl.dropbox.com/u/65084033/devel/macosx/Python-packages.dmg"

	if [ "x$python_dir" == "x" ]; then
		echo "Python modules directory not specified." >&2
		echo "Python modules can be downloaded from:" >&2
		echo "    $PYTHONPACKURL" >&2
		exit 1
	fi

	if [ ! -e "$python_dir/i386" -o ! -e "$python_dir/ppc" ]; then
		echo "Directory does not appear to contain the i386 and ppc python modules:" >&2
		echo "    $python_dir" >&2
		echo "Python modules can be downloaded from:" >&2
		echo "    $PYTHONPACKURL" >&2
		exit 1
	fi
else
	# On OS X 10.7 Lion, build lxml statically with system python and 
	# add it to the app bundle. Apple's python already includes numpy, 
	# no need to add it to the app bundle.

	# TODO: integrate building lxml on Snow leopard and later
	echo "run './osx-lxml.sh' to install python module lxml into 'Resources/lib/'"
	echo "'Resources/lib/' will be added to the app bundle automatically."
	echo ""
	echo "TODO: integrate 'osx-lxml.sh'"
	echo ""
fi

if [ ! -e "$LIBPREFIX" ]; then
	echo "Cannot find the directory containing the libraires: $LIBPREFIX" >&2
	exit 1
fi

if ! pkg-config --exists gtk-engines-2; then
	echo "Missing gtk-engines2 -- please install gtk-engines2 and try again." >&2
	exit 1
fi

if [ ! -e "$LIBPREFIX/share/gtk-engines/murrine.xml" ]; then
	echo "Missing murrine gtk theme engine -- please install gtk2-murrine and try again." >&2
	exit 1
fi

if ! pkg-config --exists gnome-vfs-2.0; then
	echo "Missing gnome-vfs2 -- please install gnome-vfs2 and try again." >&2
	exit 1
fi

if ! pkg-config --exists poppler; then
	echo "Missing poppler -- please install poppler and try again." >&2
	exit 1
fi

if ! pkg-config --modversion ImageMagick >/dev/null 2>&1; then
	echo "Missing ImageMagick -- please install ImageMagick and try again." >&2
	exit 1
fi

if [ ! -e "$LIBPREFIX/lib/aspell-0.60/en.dat" ]; then
	echo "Missing aspell en dictionary -- please install at least 'aspell-dict-en', but" >&2
	echo "preferably all dictionaries ('aspell-dict-*') and try again." >&2
	exit 1
fi


# Handle some version specific details.
# TODO: cleanup & unify usage of OS X VERSION information
VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`
if [ "$VERSION" -ge "6" ]; then
	# We're on Snow Leopard (10.6) or later.
	SCRIPTEXECSRC="ScriptExec"
	XCODEFLAGS="-configuration Deployment"
	SCRIPTEXECDIR="$SCRIPTEXECSRC/build/Deployment/ScriptExec.app/Contents/MacOS"
	EXTRALIBS=""
elif [ "$VERSION" -ge "4" ]; then
	# We're on Tiger (10.4) or later.
	# XCode behaves a little differently in Tiger and later.
	SCRIPTEXECSRC="ScriptExec"
	XCODEFLAGS="-configuration Deployment"
	SCRIPTEXECDIR="$SCRIPTEXECSRC/build/Deployment/ScriptExec.app/Contents/MacOS"
	EXTRALIBS=""
else
	# Panther (10.3) or earlier.
	SCRIPTEXECSRC="ScriptExec"
	XCODEFLAGS="-buildstyle Deployment"
	SCRIPTEXECDIR="$SCRIPTEXECSRC/build/ScriptExec.app/Contents/MacOS"
	EXTRALIBS=""
fi


# Package always has the same name. Version information is stored in
# the Info.plist file which is filled in by the configure script.
package="Inkscape.app"

# Remove a previously existing package if necessary
if [ -d $package ]; then
	echo "Removing previous Inkscape.app"
	rm -Rf $package
fi


# Set the 'macosx' directory, usually the current directory.
resdir=`pwd`


# Prepare Package
#----------------------------------------------------------
pkgexec="$package/Contents/MacOS"
pkgbin="$package/Contents/Resources/bin"
pkglib="$package/Contents/Resources/lib"
pkgetc="$package/Contents/Resources/etc"
pkgshare="$package/Contents/Resources/share"
pkglocale="$package/Contents/Resources/share/locale"
pkgpython="$package/Contents/Resources/python/site-packages/"
pkgresources="$package/Contents/Resources"

mkdir -p "$pkgexec"
mkdir -p "$pkgbin"
mkdir -p "$pkglib"
mkdir -p "$pkgetc"
mkdir -p "$pkgshare"
mkdir -p "$pkglocale"
mkdir -p "$pkgpython"

#mkdir -p "$pkgresources/Dutch.lprj"
#mkdir -p "$pkgresources/English.lprj"
#mkdir -p "$pkgresources/French.lprj"
#mkdir -p "$pkgresources/German.lprj"
#mkdir -p "$pkgresources/Italian.lprj"
#mkdir -p "$pkgresources/Spanish.lprj"
#mkdir -p "$pkgresources/fi.lprj"
#mkdir -p "$pkgresources/no.lprj"
#mkdir -p "$pkgresources/sv.lprj"

# Build and add the launcher
#----------------------------------------------------------
(
	# Build fails if CC happens to be set (to anything other than CompileC)
	unset CC
	
	cd "$resdir/$SCRIPTEXECSRC"
	echo -e "\033[1mBuilding launcher...\033[0m\n"
	xcodebuild $XCODEFLAGS clean build
)
cp "$resdir/$SCRIPTEXECDIR/ScriptExec" "$pkgexec/Inkscape"


# Copy all files into the bundle
#----------------------------------------------------------
echo -e "\n\033[1mFilling app bundle...\033[0m\n"

binary_name=`basename "$binary"`
binary_dir=`dirname "$binary"`

# Inkscape's binary
binpath="$pkgbin/inkscape-bin"
cp -v "$binary" "$binpath"
# TODO Add a "$verbose" variable and command line switch, which sets wether these commands are verbose or not

# Share files
rsync -av "$binary_dir/../share/$binary_name"/* "$pkgshare/$binary_name/"
cp "$plist" "$package/Contents/Info.plist"
rsync -av "$binary_dir/../share/locale"/* "$pkglocale"

# Copy GTK shared mime information
cp -rp "$LIBPREFIX/share/mime" "$pkgshare/"

# Copy GTK hicolor icon theme index file
mkdir -p "$pkgresources/share/icons/hicolor"
cp -p "$LIBPREFIX/share/icons/hicolor/index.theme"  "$pkgresources/share/icons/hicolor/"

# Icons and the rest of the script framework
# TODO: additional excludes (e.g. vim backup files)
# TODO: minimize number of copied files for python module lxml
rsync -av --exclude ".svn" "$resdir"/Resources/* "$pkgresources/"

# Update the ImageMagick path in startup script.
IMAGEMAGICKVER=`pkg-config --modversion ImageMagick`
sed -e "s,IMAGEMAGICKVER,$IMAGEMAGICKVER,g" -i "" $pkgbin/inkscape

# Add python modules if requested
if [ ${add_python} = "true" ]; then
	# copy python site-packages. They need to be organized in a hierarchical set of directories, by architecture and python major+minor version, e.g. i386/2.3/ for Ptyhon 2.3 on Intel; ppc/2.4/ for Python 2.4 on PPC
	cp -rvf "$python_dir"/* "$pkgpython"
fi

# PkgInfo must match bundle type and creator code from Info.plist
echo "APPLInks" > $package/Contents/PkgInfo

# Pull in extra requirements for Pango and GTK
mkdir -p $pkgetc/pango
cp $LIBPREFIX/etc/pango/pangox.aliases $pkgetc/pango/
# Need to adjust path and quote in case of spaces in path.
sed -e "s,$LIBPREFIX,\"\${CWD},g" -e 's,\.so ,.so" ,g' $LIBPREFIX/etc/pango/pango.modules > $pkgetc/pango/pango.modules
cat > $pkgetc/pango/pangorc <<END_PANGO
[Pango]
ModuleFiles=\${HOME}/.cache/inkscape/etc/pango.modules
[PangoX]
AliasFiles=\${HOME}/.cache/inkscape/etc/pangox.aliases
END_PANGO

# We use a modified fonts.conf file so only need the dtd
mkdir -p $pkgetc/fonts
cp $LIBPREFIX/etc/fonts/fonts.dtd $pkgetc/fonts/
cp -r $LIBPREFIX/etc/fonts/conf.avail $pkgetc/fonts/
cp -r $LIBPREFIX/etc/fonts/conf.d $pkgetc/fonts/
# disable bitmap fonts (bug #714859)
(cd $pkgetc/fonts/conf.d && ln -s ../conf.avail/70-no-bitmaps.conf . )

mkdir -p $pkgetc/gtk-2.0
sed -e "s,$LIBPREFIX,\${CWD},g" $LIBPREFIX/etc/gtk-2.0/gdk-pixbuf.loaders > $pkgetc/gtk-2.0/gdk-pixbuf.loaders
sed -e "s,$LIBPREFIX,\${CWD},g" $LIBPREFIX/etc/gtk-2.0/gtk.immodules > $pkgetc/gtk-2.0/gtk.immodules

for item in gnome-vfs-mime-magic gnome-vfs-2.0
do
	cp -r $LIBPREFIX/etc/$item $pkgetc/
done

pango_version=`pkg-config --variable=pango_module_version pango`
mkdir -p $pkglib/pango/$pango_version/modules
cp $LIBPREFIX/lib/pango/$pango_version/modules/*.so $pkglib/pango/$pango_version/modules/

gtk_version=`pkg-config --variable=gtk_binary_version gtk+-2.0`
mkdir -p $pkglib/gtk-2.0/$gtk_version/{engines,immodules,loaders,printbackends}
cp -r $LIBPREFIX/lib/gtk-2.0/$gtk_version/* $pkglib/gtk-2.0/$gtk_version/

mkdir -p $pkglib/gnome-vfs-2.0/modules
cp $LIBPREFIX/lib/gnome-vfs-2.0/modules/*.so $pkglib/gnome-vfs-2.0/modules/

mkdir -p $pkglib/gdk-pixbuf-2.0/$gtk_version/loaders
cp $LIBPREFIX/lib/gdk-pixbuf-2.0/$gtk_version/loaders/*.so $pkglib/gdk-pixbuf-2.0/$gtk_version/loaders/

cp -r "$LIBPREFIX/lib/ImageMagick-$IMAGEMAGICKVER" "$pkglib/"
cp -r "$LIBPREFIX/share/ImageMagick-$IMAGEMAGICKVER" "$pkgresources/share/"

# Copy aspell dictionary files:
cp -r "$LIBPREFIX/lib/aspell-0.60" "$pkglib/"
cp -r "$LIBPREFIX/share/aspell" "$pkgresources/share/"

# TODO: investigate adding optional gtk themes

# TODO: investigate adding other tools required for extensions
#       e.g. ghostscript, pstoedit, pdftocairo

# Find out libs we need from fink, darwinports, or from a custom install
# (i.e. $LIBPREFIX), then loop until no changes.
a=1
nfiles=0
endl=true
while $endl; do
	echo -e "\033[1mLooking for dependencies.\033[0m Round" $a
	libs="`otool -L $pkglib/gtk-2.0/$gtk_version/{engines,immodules,loaders,printbackends}/*.{dylib,so} $pkglib/gdk-pixbuf-2.0/$gtk_version/loaders/* $pkglib/pango/$pango_version/modules/* $pkglib/gnome-vfs-2.0/modules/* $package/Contents/Resources/lib/* $pkglib/ImageMagick-$IMAGEMAGICKVER/modules-Q16/{filters,coders}/*.so $binary 2>/dev/null | fgrep compatibility | cut -d\( -f1 | grep $LIBPREFIX | sort | uniq`"
	cp -f $libs $package/Contents/Resources/lib
	let "a+=1"	
	nnfiles=`ls $package/Contents/Resources/lib | wc -l`
	if [ $nnfiles = $nfiles ]; then
		endl=false
	else
		nfiles=$nnfiles
	fi
done

# Add extra libraries of necessary
for libfile in $EXTRALIBS
do
	cp -f $libfile $package/Contents/Resources/lib
done

# Some libraries don't seem to have write permission, fix this.
chmod -R u+w $package/Contents/Resources/lib

# Strip libraries and executables if requested
#----------------------------------------------------------
if [ "$strip" = "true" ]; then
	echo -e "\n\033[1mStripping debugging symbols...\033[0m\n"
	chmod +w "$pkglib"/*.dylib
	strip -x "$pkglib"/*.dylib
	strip -ur "$binpath"
fi

# Rewrite id and paths of linked libraries
#----------------------------------------------------------
# extract level for relative path to libs
LIBPREFIX_levels=`echo $LIBPREFIX|awk -F/ '{print NF+1}'`

fixlib () {
	#
	# new version of fixlib()
	# ~suv <suv-sf@users.sourceforge.net>, 2010-03-04
	#
	# changes:
	# - change file permissions (openssl dylibs are installed with 'r-x')
	# - allow arbitrary directory levels for custom MacPorts prefix
	# - remove redundant second 'for'-loop for linked libraries
	# - change id only once for each dylib (not as part of 'for'-loop)
	# - don't exclude libcrypto
	# - tmp: debug echo statements
	#
	# Fix a given executable to be relocatable
	if [ ! -d "$1" ]; then

		#echo "  $1"
		libPath="`echo $2 | sed 's/.*Resources//'`"

		## fix file permissions
		##echo "changing file permissions of $1 to 755"
		#chmod 755 "$1"

		# change install name
		to="@executable_path/..$libPath/$1"
		#echo "changing install name for $1 to: $to"
		/usr/bin/install_name_tool -id $to $1

		# rewrite paths of linked shared libraries
		libs="`otool -L $1 | fgrep compatibility | cut -d\( -f1`"
		for lib in $libs; do
			#echo "	$lib"
			first="`echo $lib | cut -d/ -f1-3`"
			if [ $first != /usr/lib -a $first != /usr/X11 -a $first != /System/Library ]; then
				relative="`echo $lib | cut -d/ -f$LIBPREFIX_levels-`"
				to="@executable_path/../$relative"
				#echo "  level 1: /usr/bin/install_name_tool -change $lib $to $1"
				/usr/bin/install_name_tool -change "$lib" "$to" "$1"
			fi
		done
	fi
}

rewritelibpaths () {
	# 
	# Fix package deps
	(cd "$package/Contents/Resources/lib/gtk-2.0/$gtk_version/loaders"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/gtk-2.0/$gtk_version/engines"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/gtk-2.0/$gtk_version/immodules"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/gtk-2.0/$gtk_version/printbackends"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/gnome-vfs-2.0/modules"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/gdk-pixbuf-2.0/$gtk_version/loaders"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/pango/$pango_version/modules"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/ImageMagick-$IMAGEMAGICKVER/modules-Q16/filters"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/lib/ImageMagick-$IMAGEMAGICKVER/modules-Q16/coders"
	for file in *.so; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
	(cd "$package/Contents/Resources/bin"
	for file in *; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	cd ../lib
	for file in *.dylib; do
		echo "Rewriting dylib paths for $file..."
		fixlib "$file" "`pwd`"
	done
	)
}

PATHLENGTH=`echo $LIBPREFIX | wc -c`
if [ "$PATHLENGTH" -ge "50" ]; then
	# If the LIBPREFIX path is long enough to allow 
	# path rewriting, then do this.
	rewritelibpaths
else
	echo "Could not rewrite dylb paths for bundled libraries.  This requires" >&2
	echo "Macports to be installed in a PREFIX of at least 50 characters in length." >&2
	echo "" >&2
	echo "The package will still work if the following line is uncommented in" >&2
	echo "Inkscape.app/Contents/Resources/bin/inkscape:" >&2
	echo '        export DYLD_LIBRARY_PATH="$TOP/lib"' >&2
	exit 1

fi

exit 0