We don't have an "--enable-osxapp" configure option; update a comment.
[metze/wireshark/wip.git] / packaging / macosx / osx-app.sh
1 #!/bin/bash
2 #
3 # $Id$
4 #
5 # USAGE
6 # osx-app [-s] [-l /path/to/libraries] -bp /path/to/wireshark/bin -p /path/to/Info.plist
7 #
8 # This script attempts to build an Wireshark.app package for OS X, resolving
9 # dynamic libraries, etc.
10 # It strips the executable and libraries if '-s' is given.
11 # It adds python modules if the '-py option' is given
12 # The Info.plist file can be found in the base wireshark directory once
13 # configure has been run.
14 #
15 # AUTHORS
16 #                Kees Cook <kees@outflux.net>
17 #                Michael Wybrow <mjwybrow@users.sourceforge.net>
18 #                Jean-Olivier Irisson <jo.irisson@gmail.com>
19 #
20 # Copyright (C) 2005 Kees Cook
21 # Copyright (C) 2005-2007 Michael Wybrow
22 # Copyright (C) 2007 Jean-Olivier Irisson
23 #
24 # Released under GNU GPL, read the file 'COPYING' for more information
25 #
26 # Thanks to GNUnet's "build_app" script for help with library dep resolution.
27 #               https://gnunet.org/svn/GNUnet/contrib/OSX/build_app
28 #
29 # NB:
30 # This originally came from Inkscape; Inkscape's configure script has an
31 # "--enable-osxapp", which causes some of Inkscape's installation data
32 # files to have OS X-ish paths under Contents/Resources of the package
33 # or under /Library/Application Support.  We don't have such an option;
34 # we just put them in "bin", "etc", "lib", and "share" directories
35 # under Contents/Resources, rather than in the "bin", "etc", "lib",
36 # and "share" directories under the installation directory.
37 #
38
39 # Defaults
40 strip=false
41 binary_path="/tmp/inst/bin"
42 plist="./Info.plist"
43 util_dir="./Utilities"
44 cli_dir="$util_dir/Command Line"
45 chmodbpf_dir="$util_dir/ChmodBPF"
46
47 binary_list="
48         capinfos
49         dftest
50         dumpcap
51         editcap
52         idl2wrs
53         mergecap
54         randpkt
55         rawshark
56         text2pcap
57         tshark
58         wireshark
59 "
60
61 # Location for libraries (MacPorts defaults to /opt/local)
62 if [ -z $LIBPREFIX ]; then
63         LIBPREFIX="/opt/local"
64 fi
65
66
67 # Help message
68 #----------------------------------------------------------
69 help()
70 {
71 echo -e "
72 Create an app bundle for OS X
73
74 USAGE
75         $0 [-s] [-l /path/to/libraries] -bp /path/to/wireshark/binaries -p /path/to/Info.plist
76
77 OPTIONS
78         -h,--help
79                 display this help message
80         -s
81                 strip the libraries and executables from debugging symbols
82         -l,--libraries
83                 specify the path to the librairies Wireshark depends on
84                 (typically /sw or /opt/local)
85         -bp,--binary-path
86                 specify the path to the Wireshark binaries. By default it
87                 is in $binary_path
88         -p,--plist
89                 specify the path to Info.plist. Info.plist can be found
90                 in the base directory of the source code once configure
91                 has been run
92
93 EXAMPLE
94         $0 -s -l /opt/local -bp ../../Build/bin -p Info.plist
95 "
96 }
97
98
99 # Parse command line arguments
100 #----------------------------------------------------------
101 while [ "$1" != "" ]
102 do
103         case $1 in
104                 -s)
105                         strip=true ;;
106                 -l|--libraries)
107                         LIBPREFIX="$2"
108                         shift 1 ;;
109                 -bp|--binary-path)
110                         binary_path="$2"
111                         shift 1 ;;
112                 -p|--plist)
113                         plist="$2"
114                         shift 1 ;;
115                 -h|--help)
116                         help
117                         exit 0 ;;
118                 *)
119                         echo "Invalid command line option: $1"
120                         exit 2 ;;
121         esac
122         shift 1
123 done
124
125 echo -e "\nCREATE WIRESHARK APP BUNDLE\n"
126
127 # Safety tests
128 if [ ! -e "$LIBPREFIX" ]; then
129         echo "Cannot find the directory containing the libraries: $LIBPREFIX" >&2
130         exit 1
131 fi
132
133 for binary in wireshark $binary_list ; do
134         if [ ! -x "$binary_path/$binary" ]; then
135                 echo "Couldn't find $binary (or it's not executable)" >&2
136                 exit 1
137         fi
138 done
139
140 if [ ! -f "$plist" ]; then
141         echo "Need plist file" >&2
142         exit 1
143 fi
144
145
146 # Handle some version specific details.
147 VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`
148 if [ "$VERSION" -ge "4" ]; then
149         # We're on Tiger (10.4) or later.
150         # XCode behaves a little differently in Tiger and later.
151         XCODEFLAGS="-configuration Deployment"
152         SCRIPTEXECDIR="ScriptExec/build/Deployment/ScriptExec.app/Contents/MacOS"
153         EXTRALIBS=""
154 else
155         # Panther (10.3) or earlier.
156         XCODEFLAGS="-buildstyle Deployment"
157         SCRIPTEXECDIR="ScriptExec/build/ScriptExec.app/Contents/MacOS"
158         EXTRALIBS=""
159 fi
160
161
162 # Package always has the same name. Version information is stored in
163 # the Info.plist file which is filled in by the configure script.
164 package="Wireshark.app"
165
166 # Remove a previously existing package if necessary
167 if [ -d $package ]; then
168         echo "Removing previous Wireshark.app"
169         rm -Rf $package
170 fi
171
172 # Remove a previously existing utility directory if necessary
173 if [ -d "$util_dir" ]; then
174         echo "Removing $util_dir directory"
175         rm -Rf "$util_dir"
176 fi
177
178 # Set the 'macosx' directory, usually the current directory.
179 resdir=`pwd`
180
181
182 # Prepare Package
183 #----------------------------------------------------------
184 pkgexec="$package/Contents/MacOS"
185 pkgres="$package/Contents/Resources"
186 pkgbin="$pkgres/bin"
187 pkglib="$pkgres/lib"
188 pkgplugin="$pkglib/wireshark/plugins"
189 pkgpython="$pkglib/wireshark/python"
190
191 mkdir -p "$pkgexec"
192 mkdir -p "$pkgbin"
193 mkdir -p "$pkgplugin"
194 mkdir -p "$pkgpython"
195
196 mkdir -p "$cli_dir"
197
198 # Build and add the launcher
199 #----------------------------------------------------------
200 (
201         # Build fails if CC happens to be set (to anything other than CompileC)
202         unset CC
203
204         cd "$resdir/ScriptExec"
205         echo -e "Building launcher...\n"
206         xcodebuild $XCODEFLAGS clean build
207 )
208 cp "$resdir/$SCRIPTEXECDIR/ScriptExec" "$pkgexec/Wireshark"
209
210
211 # Copy all files into the bundle
212 #----------------------------------------------------------
213 echo -e "\nFilling app bundle and utility directory...\n"
214
215 # Wireshark executables
216 for binary in $binary_list ; do
217         # Copy the binary to its destination
218         dest_path="$pkgbin/$binary-bin"
219         cp -v "$binary_path/$binary" "$dest_path"
220         # TODO Add a "$verbose" variable and command line switch, which sets wether these commands are verbose or not
221
222         case $binary in
223         wireshark)
224                 cp -v utility-launcher "$cli_dir/$binary"
225                 ;;
226         *)
227                 ln -sv ./wireshark "$pkgbin/$binary"
228                 ln -sv ./wireshark "$cli_dir/$binary"
229                 ;;
230         esac
231 done
232
233 # ChmodBPF
234 mkdir -p "$chmodbpf_dir"
235 cp -v ChmodBPF/* "$chmodbpf_dir"
236 chmod -R g-w "$chmodbpf_dir"
237
238 # The rest of the Wireshark installation (we handled bin above)
239 rsync -av \
240         --exclude bin/ \
241         --exclude lib/wireshark/plugins/ \
242         --exclude lib/wireshark/python/ \
243         "$binary_path/.."/* "$pkgres"
244
245 # Remove the version number from the plugin path
246 find "$binary_path/../lib/wireshark/plugins" -type f \
247         -exec cp -fv "{}" "$pkgplugin/" \;
248
249 # Remove the version number from the python path
250 find "$binary_path/../lib/wireshark/python" -type f \
251         -exec cp -fv "{}" "$pkgpython/" \;
252
253 cp "$plist" "$package/Contents/Info.plist"
254
255 # Icons and the rest of the script framework
256 rsync -av --exclude ".svn" "$resdir"/Resources/* "$package"/Contents/Resources/
257
258 # PkgInfo must match bundle type and creator code from Info.plist
259 echo "APPLWshk" > $package/Contents/PkgInfo
260
261 # Pull in extra requirements for Pango and GTK
262 pkgetc="$package/Contents/Resources/etc"
263 mkdir -p $pkgetc/pango
264 cp $LIBPREFIX/etc/pango/pangox.aliases $pkgetc/pango/
265 # Need to adjust path and quote in case of spaces in path.
266 sed -e "s,$LIBPREFIX,\"\${CWD},g" -e 's,\.so ,.so" ,g' $LIBPREFIX/etc/pango/pango.modules > $pkgetc/pango/pango.modules
267 cat > $pkgetc/pango/pangorc <<END_PANGO
268 [Pango]
269 ModuleFiles=\${HOME}/.wireshark-etc/pango.modules
270 [PangoX]
271 AliasFiles=\${HOME}/.wireshark-etc/pangox.aliases
272 END_PANGO
273
274 # We use a modified fonts.conf file so only need the dtd
275 mkdir -p $pkgetc/fonts
276 cp $LIBPREFIX/etc/fonts/fonts.dtd $pkgetc/fonts/
277 cp -r $LIBPREFIX/etc/fonts/conf.avail $pkgetc/fonts/
278 cp -r $LIBPREFIX/etc/fonts/conf.d $pkgetc/fonts/
279
280 mkdir -p $pkgetc/gtk-2.0
281 #
282 # In newer versions of GTK+, the gdk-pixbuf library was split off from
283 # GTK+, and the gdk-pixbuf.loaders file moved, so we check for its
284 # existence here.
285 #
286 # The file is ultimately copied to the user's home directory, with
287 # the pathnames adjusted to refer to the installed package, so we
288 # always put it in the same location in the installed package,
289 # regardless of where it lives in the machine on which it's built.
290 #
291 if [ -e $LIBPREFIX/etc/gtk-2.0/gdk-pixbuf.loaders ]
292 then
293         sed -e "s,$LIBPREFIX,\${CWD},g" $LIBPREFIX/etc/gtk-2.0/gdk-pixbuf.loaders > $pkgetc/gtk-2.0/gdk-pixbuf.loaders
294 fi
295 sed -e "s,$LIBPREFIX,\${CWD},g" $LIBPREFIX/etc/gtk-2.0/gtk.immodules > $pkgetc/gtk-2.0/gtk.immodules
296
297 pango_version=`pkg-config --variable=pango_module_version pango`
298 mkdir -p $pkglib/pango/$pango_version/modules
299 cp $LIBPREFIX/lib/pango/$pango_version/modules/*.so $pkglib/pango/$pango_version/modules/
300
301 gtk_version=`pkg-config --variable=gtk_binary_version gtk+-2.0`
302 mkdir -p $pkglib/gtk-2.0/$gtk_version/{engines,immodules,loaders}
303 cp -r $LIBPREFIX/lib/gtk-2.0/$gtk_version/* $pkglib/gtk-2.0/$gtk_version/
304
305 gdk_pixbuf_version=`pkg-config --variable=gdk_pixbuf_binary_version gdk-pixbuf-2.0`
306 if [ ! -z $gdk_pixbuf_version ]; then
307         mkdir -p $pkglib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders
308         #
309         # As per the above, check whether we have a loaders.cache file
310         # in $LIBPREFIX/lib/gdk-pixbuf-2.0/$gdk_pixbuf_version, as
311         # that's where the output of gdk-pixbuf-query-loaders gets
312         # put if gdk-pixbuf and GTK+ are separated.
313         #
314         # The file is ultimately copied to the user's home directory,
315         # with the pathnames adjusted to refer to the installed package,
316         # so we always put it in the same location in the installed
317         # package, regardless of where it lives in the machine on which
318         # it's built.
319         #
320         if [ -e $LIBPREFIX/lib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders.cache ]
321         then
322                 sed -e "s,$LIBPREFIX,\${CWD},g" $LIBPREFIX/lib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders.cache > $pkgetc/gtk-2.0/gdk-pixbuf.loaders
323         fi
324         cp -r $LIBPREFIX/lib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders/* $pkglib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders
325 fi
326
327 # Find out libs we need from Fink, MacPorts, or from a custom install
328 # (i.e. $LIBPREFIX), then loop until no changes.
329 a=1
330 nfiles=0
331 endl=true
332 lib_dep_search_list="
333         $pkglib/gtk-2.0/$gtk_version/loaders/*
334         $pkglib/gtk-2.0/$gtk_version/immodules/*
335         $pkglib/gtk-2.0/$gtk_version/engines/*.so
336         $pkglib/pango/$pango_version/modules/*
337         $pkglib/gdk-pixbuf-2.0/$gdk_pixbuf_version/loaders/*
338         $package/Contents/Resources/lib/*
339         $pkgbin/*-bin
340         "
341 while $endl; do
342         echo -e "Looking for dependencies. Round" $a
343         libs="`otool -L $lib_dep_search_list 2>/dev/null | fgrep compatibility | cut -d\( -f1 | grep $LIBPREFIX | sort | uniq`"
344         cp -vn $libs "$pkglib"
345         let "a+=1"
346         nnfiles=`ls "$pkglib" | wc -l`
347         if [ $nnfiles = $nfiles ]; then
348                 endl=false
349         else
350                 nfiles=$nnfiles
351         fi
352 done
353
354 # Add extra libraries of necessary
355 for libfile in $EXTRALIBS
356 do
357         cp -f $libfile "$pkglib"
358 done
359
360 # Strip libraries and executables if requested
361 #----------------------------------------------------------
362 if [ "$strip" = "true" ]; then
363         echo -e "\nStripping debugging symbols...\n"
364         chmod +w "$pkglib"/*.dylib
365         strip -x "$pkglib"/*.dylib
366         strip -ur "$binpath"
367 fi
368
369 # NOTE: This works for all the dylibs but causes GTK to crash at startup.
370 #                               Instead we leave them with their original install_names and set
371 #                               DYLD_LIBRARY_PATH within the app bundle before running Wireshark.
372 #
373 # fixlib () {
374 #               # Fix a given executable or library to be relocatable
375 #               if [ ! -d "$1" ]; then
376 #                       echo $1
377 #                       libs="`otool -L $1 | fgrep compatibility | cut -d\( -f1`"
378 #                       for lib in $libs; do
379 #                               echo "  $lib"
380 #                               base=`echo $lib | awk -F/ '{print $NF}'`
381 #                               first=`echo $lib | cut -d/ -f1-3`
382 #                               to=@executable_path/../lib/$base
383 #                               if [ $first != /usr/lib -a $first != /usr/X11R6 ]; then
384 #                                       /usr/bin/install_name_tool -change $lib $to $1
385 #                                       if [ "`echo $lib | fgrep libcrypto`" = "" ]; then
386 #                                               /usr/bin/install_name_tool -id $to ../lib/$base
387 #                                               for ll in $libs; do
388 #                                                       base=`echo $ll | awk -F/ '{print $NF}'`
389 #                                                       first=`echo $ll | cut -d/ -f1-3`
390 #                                                       to=@executable_path/../lib/$base
391 #                                                       if [ $first != /usr/lib -a $first != /usr/X11R6 -a "`echo $ll | fgrep libcrypto`" = "" ]; then
392 #                                                               /usr/bin/install_name_tool -change $ll $to ../lib/$base
393 #                                                       fi
394 #                                               done
395 #                                       fi
396 #                               fi
397 #                       done
398 #               fi
399 # }
400 #
401 # Fix package deps
402 #(cd "$package/Contents/MacOS/bin"
403 # for file in *; do
404 #                fixlib "$file"
405 # done
406 # cd ../lib
407 # for file in *; do
408 #                fixlib "$file"
409 # done)
410
411 exit 0