]> git.lyx.org Git - lyx.git/blob - development/LyX-Mac-binary-release.sh
776d1eeca3915f6d462dbdfffce492996c2d06b8
[lyx.git] / development / LyX-Mac-binary-release.sh
1 #!/bin/sh
2
3 # set -x
4
5 # This script automates creating universal binaries of LyX on Mac.
6 # Author: Bennett Helm (and extended by Konrad Hofbauer)
7 # latest changes by Stephan Witt
8 # Last modified: January 2011
9
10 MAC_API=-cocoa
11 Qt4Version="4.6.3"
12 Qt4SourceVersion="qt-everywhere-opensource-src-${Qt4Version}"
13 Qt4BuildSubDir="qt-${Qt4Version}-build${MAC_API}"
14
15 # Prerequisite:
16 # * a decent checkout of LyX sources (probably you have it already)
17 # * Qt4 - build with shared or static libraries for the used platforms (default: i386 and ppc)
18 #    or - an unpacked source tree of Qt4 in $QT4SOURCEDIR or in the sibling directory (variable Qt4SourceVersion)
19 # * for aspell support:
20 #   the aspell sources placed in a sibling directory (variable ASpellSourceVersion)
21 # * for hunspell support:
22 #   the hunspell sources placed in a sibling directory (variable HunSpellSourceVersion)
23 # * for dictionary deployment (per default thesauri only):
24 #   - aspell:   the dictionary files of macports (in /opt/local/share/aspell and /opt/local/lib/aspell-0.60)
25 #   - hunspell: the dictionary files in the sibling directory dictionaries/dicts
26 #   - mythes:   the data and idx files in the sibling directory dictionaries/thes
27
28 LyXConfigureOptions="--enable-warnings --enable-optimization=-Os --with-included-gettext --with-x=no"
29 AspellConfigureOptions="--enable-warnings --enable-optimization=-O0 --enable-debug --disable-nls --enable-compile-in-filters --disable-pspell-compatibility"
30 HunspellConfigureOptions="--with-warnings --disable-nls --with-included-gettext --disable-static"
31 Qt4ConfigureOptions="-opensource -silent -shared -release -fast -no-exceptions"
32 Qt4ConfigureOptions="${Qt4ConfigureOptions} -no-webkit -no-qt3support -no-javascript-jit -no-dbus"
33 Qt4ConfigureOptions="${Qt4ConfigureOptions} -nomake examples -nomake demos -nomake docs -nomake tools"
34 Qt4ConfigureOptions="${Qt4ConfigureOptions} ${MAC_API}"
35
36 aspell_dictionaries="no"
37 hunspell_dictionaries="no"
38
39 aspell_deployment="yes"
40 hunspell_deployment="yes"
41 thesaurus_deployment="yes"
42
43 qt4_deployment="yes"
44
45 MACOSX_DEPLOYMENT_TARGET="10.4" # Tiger support is default
46 SDKROOT="/Developer/SDKs/MacOSX10.5.sdk" # Leopard build is default
47
48 # detection of script home
49 LyxSourceDir=`dirname "$0"`
50 if [ ! -d "${LyxSourceDir}" ]; then
51         echo Missing LyX source directory.
52         exit 2
53 fi
54 case "${LyxSourceDir}" in
55 /*/development)
56         LyxSourceDir=`dirname "${LyxSourceDir}"`
57         ;;
58 /*)
59         ;;
60 */development|development)
61         LyxSourceDir=`dirname "${LyxSourceDir}"`
62         LyxSourceDir=`cd "${LyxSourceDir}";pwd`
63         ;;
64 *)
65         LyxSourceDir=`cd "${LyxSourceDir}";pwd`
66         ;;
67 esac
68
69 usage() {
70         echo "*" Build script for LyX on Mac OS X
71         echo
72         echo Optional arguments:
73         echo " --aspell-deployment=yes|no ." default yes
74         echo " --with-qt4-frameworks=yes|no" default no
75         echo " --qt4-deployment=yes|no ...." default yes
76         echo " --with-macosx-target=TARGET " default 10.4 "(Tiger)"
77         echo " --with-sdkroot=SDKROOT ....." default 10.5 "(Leopard)"
78         echo " --with-arch=ARCH ..........." default ppc,i386
79         echo " --with-build-path=PATH ....." default \${lyx-src-dir}/../lyx-build
80         echo " --with-dmg-location=PATH ..." default \${build-path}
81         echo
82         echo "All other arguments with -- are passed to configure"
83         echo "including the defaults: ${LyXConfigureOptions}"
84         if [ -x "${LyxSourceDir}/configure" ]; then
85                 echo
86                 echo "*" Configure options of LyX
87                 echo
88                 "${LyxSourceDir}/configure" --help
89         fi
90         exit 0
91 }
92
93 while [ $# -gt 0 ]; do
94         case "${1}" in
95         --with-qt4-frameworks=*)
96                 configure_qt4_frameworks=`echo ${1}|cut -d= -f2`
97                 if [ "$configure_qt4_frameworks" = "yes" ]; then
98                         unset QTDIR
99                         qt4_deployment="no"
100                 fi
101                 shift
102                 ;;
103         --with-qt4-dir=*)
104                 QTDIR=`echo ${1}|cut -d= -f2`
105                 shift
106                 ;;
107         --with-macosx-target=*)
108                 MACOSX_DEPLOYMENT_TARGET=`echo ${1}|cut -d= -f2`
109                 shift
110                 ;;
111         --with-sdkroot=*)
112                 SDKROOT=`echo ${1}|cut -d= -f2`
113                 case "${SDKROOT}" in
114                 10.4)
115                         SDKROOT="/Developer/SDKs/MacOSX10.4u.sdk"
116                         export CC=gcc-4.0
117                         export OBJC=gcc-4.0
118                         export CXX=g++-4.0
119                         ;;
120                 10.5|10.6)
121                         SDKROOT="/Developer/SDKs/MacOSX${SDKROOT}.sdk"
122                         ;;
123                 *)
124                         usage
125                         ;;
126                 esac
127                 shift
128                 ;;
129         --aspell-deployment=*)
130                 aspell_deployment=`echo ${1}|cut -d= -f2`
131                 aspell_dictionaries=$aspell_deployment
132                 shift
133                 ;;
134         --hunspell-deployment=*)
135                 hunspell_deployment=`echo ${1}|cut -d= -f2`
136                 hunspell_dictionaries=$hunspell_deployment
137                 shift
138                 ;;
139         --thesaurus-deployment=*)
140                 thesaurus_deployment=`echo ${1}|cut -d= -f2`
141                 shift
142                 ;;
143         --qt4-deployment=*)
144                 qt4_deployment=`echo ${1}|cut -d= -f2`
145                 shift
146                 ;;
147         --with-arch=*)
148                 ARCH=`echo ${1}|cut -d= -f2|tr ',' ' '`
149                 ARCH_LIST="${ARCH_LIST} ${ARCH}"
150                 shift
151                 ;;
152         --with-dmg-location=*)
153                 DMGLocation=`echo ${1}|cut -d= -f2`
154                 shift
155                 ;;
156         --with-build-path=*)
157                 LyxBuildDir=`echo ${1}|cut -d= -f2`
158                 shift
159                 ;;
160         --help|--help=*)
161                 usage
162                 ;;
163         --without-aspell)
164                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
165                 aspell_deployment="no"
166                 shift
167                 ;;
168         --without-hunspell)
169                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
170                 hunspell_deployment="no"
171                 shift
172                 ;;
173         --only-package=*)
174                 LyxOnlyPackage=`echo ${1}|cut -d= -f2`
175                 shift
176                 ;;
177         --*)
178                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
179                 shift
180                 ;;
181         *)
182                 break
183                 ;;
184         esac
185 done
186
187 if [ "${configure_qt4_frameworks}" != "yes" ]; then
188         QtInstallDir=${QTDIR:-"/opt/qt4"}
189 fi
190 QtFrameworkVersion="4"
191 ASpellSourceVersion="aspell-0.60.6"
192 HunSpellSourceVersion="hunspell-1.2.12"
193
194 ARCH_LIST=${ARCH_LIST:-"ppc i386"}
195
196 strip="-strip"
197 aspellstrip=
198
199 LyxBuildDir=${LyxBuildDir:-`dirname "${LyxSourceDir}"`/lyx-build}
200 DMGLocation=${DMGLocation:-"${LyxBuildDir}"}
201
202 ASpellSourceDir=${ASPELLDIR:-`dirname "${LyxSourceDir}"`/${ASpellSourceVersion}}
203 ASpellInstallDir=${ASpellInstallDir:-"${LyxBuildDir}"/SpellChecker.lib}
204 HunSpellSourceDir=${HUNSPELLDIR:-`dirname "${LyxSourceDir}"`/${HunSpellSourceVersion}}
205 HunSpellInstallDir=${HunSpellInstallDir:-"${LyxBuildDir}"/SpellChecker.lib}
206 Qt4SourceDir=${QT4SOURCEDIR:-`dirname "${LyxSourceDir}"`/${Qt4SourceVersion}}
207 Qt4BuildDir=${Qt4BuildDir:-"${LyxBuildDir}"/${Qt4BuildSubDir:-"qt4-build"}}
208 DictionarySourceDir=${DICTIONARYDIR:-`dirname "${LyxSourceDir}"`/dictionaries}
209 DocumentationDir=`dirname "${LyxSourceDir}"`/Documents
210 DmgBackground="${LyxSourceDir}"/development/MacOSX/dmg-background.png
211
212 ASpellInstallHdr="${ASpellInstallDir}/include/aspell.h"
213 HunSpellInstallHdr="${HunSpellInstallDir}/include/hunspell/hunspell.h"
214
215 if [ -z "${LyXVersion}" ]; then
216         LyXVersion=`grep AC_INIT "${LyxSourceDir}"/configure.ac | cut -d, -f2 | tr -d " ()"`
217 fi
218 LyXVersionSuffix=${LyXVersionSuffix:-`echo "${LyXVersion}" | cut -d. -f1-2`}
219
220 LyxName="LyX"
221 LyxBase="${LyxName}-${LyXVersion}"
222 LyxApp="${LyxBase}.app"
223 LyxAppDir="${LyxBuildDir}"/"${LyxBase}"
224 LyxBuildDir="${LyxAppDir}.build"
225 LyxAppPrefix="${LyxAppDir}.app"
226 # if zip file is needed... remove the comment sign
227 #LyxAppZip="${LyxAppPrefix}.zip"
228
229 # ---------------------------------
230 # DON'T MODIFY ANYTHING BELOW HERE!
231 # ---------------------------------
232
233 # don't change order here...
234 QtLibraries="QtSvg QtXml QtGui QtNetwork QtCore"
235
236 DMGNAME="${LyxBase}"
237 DMGSIZE="550m"
238
239 # Check for existing SDKs
240 SDKs=`echo /Developer/SDKs/MacOSX10*sdk`
241 case "$SDKs" in
242 ${SDKROOT})
243         ;;
244 *10.6*)
245         MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-"10.5"}; export MACOSX_DEPLOYMENT_TARGET
246         case "${MACOSX_DEPLOYMENT_TARGET}" in
247         10.6)
248                 SDKROOT="/Developer/SDKs/MacOSX10.6.sdk"; export SDKROOT
249                 ;;
250         10.5|10.4)
251                 SDKROOT=${SDKROOT:-"/Developer/SDKs/MacOSX10.5.sdk"}; export SDKROOT
252                 ;;
253         esac
254         ;;
255 *10.5*)
256         MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-"10.4"}; export MACOSX_DEPLOYMENT_TARGET
257         SDKROOT=${SDKROOT:-"/Developer/SDKs/MacOSX10.5.sdk"}; export SDKROOT
258         ;;
259 *)
260         echo Unknown or missing SDK for Mac OS X.
261         exit 1
262         ;;
263 esac
264 MYCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET}"
265
266 # These variables define the identifiers of the
267 # system (both Intel and PowerPC) to compile for.
268 # (Note: darwin8 is 10.4; darwin9 is 10.5.)
269 # Only change these if necessary
270
271 HostSystem_i386="i686-apple-darwin8"
272 HostSystem_ppc="powerpc-apple-darwin8"
273
274 if [ "${configure_qt4_frameworks}" != "yes" -a -d "${Qt4SourceDir}" -a ! \( -d "${Qt4BuildDir}" -a -d "${QtInstallDir}" \) ]; then
275         echo Build Qt4 library ${Qt4SourceDir}
276         if [ "${QtInstallDir}" = "${Qt4BuildDir}" ]; then
277                 echo Bad install directory for Qt.
278                 echo Must be different from build directory "${Qt4BuildDir}".
279                 exit 1
280         fi
281         (
282                 mkdir -p "${Qt4BuildDir}" && cd "${Qt4BuildDir}"
283                 for arch in ${ARCH_LIST} ; do
284                         ARCHS="${ARCHS} -arch ${arch}"
285                 done
286                 echo configure options:
287                 echo ${Qt4ConfigureOptions} ${ARCHS} -prefix "${QtInstallDir}"
288                 echo yes | "${Qt4SourceDir}"/configure ${Qt4ConfigureOptions} ${ARCHS} -prefix "${QtInstallDir}"
289                 make && make install
290         )
291         cd "${QtInstallDir}" && (
292                 mkdir -p include
293                 cd include
294                 for libnm in ${QtLibraries} ; do
295                         test -d ${libnm} -o -L ${libnm} || ln -s ../lib/${libnm}.framework/Headers ${libnm}
296                 done
297         )
298 fi
299
300 if [ -d "${HunSpellSourceDir}" -a ! -f "${HunSpellInstallHdr}" ]; then
301         # we have a private HunSpell source tree at hand...
302         # so let's build and install it
303         if [ -z "${HunSpellVersion}" ]; then
304                 HunSpellVersion=`grep AC_INIT "${HunSpellSourceDir}"/configure.ac | cut -d, -f2|tr -d " ()"`
305         fi
306
307         HunSpellName="Hunspell"
308         HunSpellBase="${HunSpellName}-${HunSpellVersion}"
309
310         echo Build hunspell library ${HunSpellBase}
311         echo configure options:
312         echo --prefix="${HunSpellInstallDir}" ${HunspellConfigureOptions}
313
314         cd "${HunSpellSourceDir}"
315
316         # ----------------------------------------
317         # Build HunSpell for different architectures
318         # ----------------------------------------
319         FILE_LIST="libhunspell-1.2.0.dylib"
320
321         for arch in ${ARCH_LIST} ; do
322                 make distclean
323                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export CPPFLAGS
324                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export LDFLAGS
325                 HOSTSYSTEM=`eval "echo \\$HostSystem_$arch"`
326                 "${HunSpellSourceDir}/configure"\
327                         --prefix="${HunSpellInstallDir}"\
328                         ${HunspellConfigureOptions}
329                 make && make install${strip}
330                 for file in ${FILE_LIST} ; do
331                         if [ -f "${HunSpellInstallDir}"/lib/${file} ]; then
332                                 mv "${HunSpellInstallDir}"/lib/${file}\
333                                         "${HunSpellInstallDir}"/lib/${file}-${arch} 
334                         else
335                                 echo Cannot build and install HunSpell for ${arch}.
336                                 exit 1
337                         fi
338                 done
339         done
340         # -------------------------
341         # Create universal binaries
342         # -------------------------
343         for file in ${FILE_LIST} ; do
344                 OBJ_LIST=
345                 for arch in ${ARCH_LIST} ; do
346                         OBJ_LIST="${OBJ_LIST} lib/${file}-${arch}"
347                 done
348                 (
349                         cd "${HunSpellInstallDir}"
350                         lipo -create ${OBJ_LIST} -o lib/${file}
351                         # check for the "missing link"...
352                         test -f lib/libhunspell.dylib || (cd lib ; ln -s libhunspell-1.2.dylib libhunspell.dylib)
353                 )
354         done
355         # --------
356         # Clean up
357         # --------
358         for arch in ${ARCH_LIST} ; do
359                 rm -f "${HunSpellInstallDir}"/lib/*-${arch}
360         done
361 fi
362
363 if [ -d "${ASpellSourceDir}" -a ! -f "${ASpellInstallHdr}" -a "yes" = "${aspell_deployment}" ]; then
364         # we have a private ASpell source tree at hand...
365         # so let's build and install it
366         if [ -z "${ASpellVersion}" ]; then
367                 ASpellVersion=`grep AC_INIT "${ASpellSourceDir}"/configure.ac | cut -d, -f2|tr -d " ()"`
368         fi
369
370         ASpellName="Aspell"
371         ASpellBase="${ASpellName}-${ASpellVersion}"
372
373         echo Build aspell library ${ASpellBase}
374         echo configure options:
375         echo --prefix="${ASpellInstallDir}" ${AspellConfigureOptions}
376
377         # ASpell builds inplace only :(
378         cd "${ASpellSourceDir}"
379
380         # ----------------------------------------
381         # Build ASpell for different architectures
382         # ----------------------------------------
383         FILE_LIST="libaspell.15.dylib"
384
385         for arch in ${ARCH_LIST} ; do
386                 make distclean
387                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export CPPFLAGS
388                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export LDFLAGS
389                 HOSTSYSTEM=`eval "echo \\$HostSystem_$arch"`
390                 CXXFLAGS=-g "${ASpellSourceDir}/configure"\
391                         --prefix="${ASpellInstallDir}"\
392                         ${AspellConfigureOptions}
393                 make && make install${aspellstrip}
394                 for file in ${FILE_LIST} ; do
395                         if [ -f "${ASpellInstallDir}"/lib/${file} ]; then
396                                 mv "${ASpellInstallDir}"/lib/${file}\
397                                         "${ASpellInstallDir}"/lib/${file}-${arch} 
398                         else
399                                 echo Cannot build and install ASpell for ${arch}.
400                                 exit 1
401                         fi
402                 done
403         done
404         # -------------------------
405         # Create universal binaries
406         # -------------------------
407         for file in ${FILE_LIST} ; do
408                 OBJ_LIST=
409                 for arch in ${ARCH_LIST} ; do
410                         OBJ_LIST="${OBJ_LIST} lib/${file}-${arch}"
411                 done
412                 (
413                         cd "${ASpellInstallDir}"
414                         lipo -create ${OBJ_LIST} -o lib/${file}
415                 )
416         done
417         # --------
418         # Clean up
419         # --------
420         for arch in ${ARCH_LIST} ; do
421                 rm -f "${ASpellInstallDir}"/lib/*-${arch}
422         done
423 fi
424
425
426 framework_name() {
427         echo "Frameworks/${1}.framework"
428 }
429
430 LYX_FILE_LIST="lyx lyxclient tex2lyx"
431 BUNDLE_PATH="Contents/MacOS"
432 LYX_BUNDLE_PATH="${LyxAppPrefix}/${BUNDLE_PATH}"
433 build_lyx() {
434         # Clear Output
435         if [ -n "${LyxAppZip}" -a -f "${LyxAppZip}" ]; then rm "${LyxAppZip}"; fi
436         if [ -d "${LyxAppPrefix}" ]; then rm -rf "${LyxAppPrefix}"; fi
437
438         # -------------------------------------
439         # Automate configure check
440         # -------------------------------------
441         if [ ! -f "${LyxSourceDir}"/configure -o "${LyxSourceDir}"/configure -ot "${LyxSourceDir}"/configure.ac ]; then
442                 ( cd "${LyxSourceDir}" && sh autogen.sh )
443         else
444                 find "${LyxSourceDir}" -name Makefile.am -print | while read file ; do
445                         dname=`dirname "$file"`
446                         if [ -f "$dname/Makefile.in" -a "$dname/Makefile.in" -ot "$file" ]; then
447                                 ( cd "${LyxSourceDir}" && sh autogen.sh )
448                                 break
449                         fi
450                 done
451         fi
452         # -------------------------------------
453         # Build LyX for different architectures
454         # -------------------------------------
455
456         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_deployment}" ]; then
457                 ConfigureExtraInc="--with-extra-inc=${ASpellInstallDir}/include"
458                 ConfigureExtraLib="--with-extra-lib=${ASpellInstallDir}/lib"
459         fi
460
461         if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_deployment}" ]; then
462                 HunSpellFramework=`framework_name Hunspell`
463                 HunSpellFramework=`basename "${HunSpellFramework}"`
464                 ConfigureExtraInc="--with-extra-inc=${HunSpellInstallDir}/include"
465                 ConfigureExtraLib="--with-extra-lib=${HunSpellInstallDir}/lib"
466                 # LyXConfigureOptions="${LyXConfigureOptions} --with-hunspell-framework=${HunSpellFramework}"
467         fi
468         LyXConfigureOptions="${LyXConfigureOptions} ${ConfigureExtraInc}"
469         LyXConfigureOptions="${LyXConfigureOptions} ${ConfigureExtraLib}"
470
471         for arch in ${ARCH_LIST} ; do
472
473                 if [ -d "${LyxBuildDir}" ];  then rm -r "${LyxBuildDir}"; fi
474                 mkdir -p "${LyxBuildDir}" && cd "${LyxBuildDir}"
475
476                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"
477                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"
478                 HOSTSYSTEM=`eval "echo \\$HostSystem_$arch"`
479
480                 if [ "$configure_qt4_frameworks" = "yes" ]; then
481                         export QT4_CORE_CFLAGS="-FQtCore"
482                         export QT4_CORE_LIBS="-framework QtCore"
483                         export QT4_FRONTEND_CFLAGS="-FQtGui"
484                         export QT4_FRONTEND_LIBS="-framework QtGui"
485                         export PKG_CONFIG=""
486                         CPPFLAGS="${CPPFLAGS} -I${SDKROOT}/Library/Frameworks/QtCore.framework/Headers"
487                         CPPFLAGS="${CPPFLAGS} -I${SDKROOT}/Library/Frameworks/QtGui.framework/Headers"
488                 fi
489
490                 echo LDFLAGS="${LDFLAGS}"
491                 export LDFLAGS
492                 echo CPPFLAGS="${CPPFLAGS}"
493                 export CPPFLAGS
494                 echo CONFIGURE_OPTIONS="${LyXConfigureOptions}" ${QtInstallDir:+"--with-qt4-dir=${QtInstallDir}"}
495                 "${LyxSourceDir}/configure"\
496                         --prefix="${LyxAppPrefix}" --with-version-suffix="-${LyXVersionSuffix}"\
497                         ${QtInstallDir:+"--with-qt4-dir=${QtInstallDir}"} \
498                         ${LyXConfigureOptions}\
499                         --enable-build-type=rel && \
500                 make -j2 && make install${strip}
501                 for file in ${LYX_FILE_LIST} ; do
502                         if [ -f "${LYX_BUNDLE_PATH}/${file}" ]; then
503                                 mv "${LYX_BUNDLE_PATH}/${file}"\
504                                         "${LYX_BUNDLE_PATH}/${file}-${arch}" 
505                         else
506                                 echo ERROR: Cannot build and install LyX for ${arch}.
507                                 exit 1
508                         fi
509                 done
510         done
511 }
512
513 content_directory() {
514         target="$1"
515         content=`dirname "${target}"`
516         content=`dirname "${content}"`
517         echo "${content}"
518 }
519
520 private_framework() {
521         fwdir=`framework_name "$1"`
522         source="$2"
523         target="$3"
524         condir=`content_directory "${target}"`
525         libnm=`basename "${source}"`
526         mkdir -p "${condir}/${fwdir}"
527         if [ ! -f "${condir}/${fwdir}/${libnm}" ]; then
528                 cp -p "${source}" "${condir}/${fwdir}"
529                 echo Set library id in "${condir}/${fwdir}/${libnm}"
530                 install_name_tool -id "@executable_path/../${fwdir}/${libnm}" "${condir}/${fwdir}/${libnm}"
531         fi
532         echo Correct library id reference to "${libnm}" in "${target}"
533         install_name_tool -change "${source}" "@executable_path/../${fwdir}/${libnm}" "${target}"
534 }
535
536 deploy_qtlibs() {
537         source="${QtInstallDir}"
538         target="$1"
539         version="Versions/${QtFrameworkVersion}/"
540         condir=`content_directory "${target}"`
541         mkdir -p "${condir}/Resources"
542         test -f "${condir}/Resources/qt.conf" || cat - > "${condir}/Resources/qt.conf" <<-EOF
543 [Paths]
544 Plugins = PlugIns
545 Translations = translations
546 EOF
547         if [ ! -d "${condir}/PlugIns" ]; then
548                 mkdir -p "${condir}/PlugIns"
549                 find "${source}/plugins" -name \*.dylib -print | while read libname ; do
550                         echo Copy plugin "${libname}"
551                         dylib=`basename "${libname}"`
552                         dirname=`dirname "${libname}"`
553                         dirname=`basename "${dirname}"`
554                         mkdir -p "${condir}/PlugIns/${dirname}"
555                         cp -p "${libname}" "${condir}/PlugIns/${dirname}"
556                 done
557         fi
558         for libnm in ${QtLibraries} ; do
559                 fwdir=`framework_name "$libnm"`
560                 dirname=`dirname "${fwdir}"`
561                 mkdir -p "${condir}/${dirname}"
562                 dirname=`basename "${fwdir}"`
563                 test -d "${condir}/${fwdir}" || (
564                         echo Copy framework "${source}/lib/"`basename "${fwdir}"`
565                         cp -pR "${source}/lib/"`basename "${fwdir}"` "${condir}/${fwdir}"
566                         echo Set library id in "${condir}/${fwdir}/${version}${libnm}"
567                         install_name_tool -id "@executable_path/../${fwdir}/${version}${libnm}" "${condir}/${fwdir}/${version}${libnm}"
568                         find "${condir}/PlugIns" "${condir}/"`dirname "${fwdir}"` -name Headers -prune -o -type f -print | while read filename ; do
569                                 if [ "${filename}" != "${target}" ]; then
570                                         otool -L "${filename}" 2>/dev/null | sort -u | while read library ; do
571                                                 # pattern match for: /path/to/qt4/lib/QtGui.framework/Versions/4/QtGui (compatibility version 4.6.0, current version 4.6.2)
572                                                 case "${library}" in
573                                                 *"${libnm}"*"("*version*")"*)
574                                                         echo Correct library id reference to "${libnm}" in "${filename}"
575                                                         install_name_tool -change\
576                                                                 "${source}/lib/${dirname}/${version}${libnm}"\
577                                                                 "@executable_path/../${fwdir}/${version}${libnm}"\
578                                                                 "${filename}"
579                                                         ;;
580                                                 esac
581                                         done
582                                 fi
583                         done
584                 )
585                 echo Correct library id reference to "${libnm}" in "${target}"
586                 install_name_tool -change\
587                         "${source}/lib/${dirname}/${version}${libnm}"\
588                         "@executable_path/../${fwdir}/${version}${libnm}"\
589                         "${target}"
590         done
591         if [ ! -d "${condir}/translations" ]; then
592                 mkdir -p "${condir}/translations"
593         fi
594         echo Copy Qt translations to "${condir}/translations"
595         cp -p "${source}"/translations/qt_*.qm "${condir}/translations"
596 }
597
598 # -------------------------
599 # Create universal binaries
600 # -------------------------
601 convert_universal() {
602         cd "${LyxAppPrefix}"
603         for file in ${LYX_FILE_LIST} ; do
604                 OBJ_LIST=
605                 for arch in ${ARCH_LIST} ; do
606                         if [ -f "${BUNDLE_PATH}/${file}-${arch}" ]; then
607                                 OBJ_LIST="${OBJ_LIST} ${BUNDLE_PATH}/${file}-${arch}"
608                         fi
609                 done
610                 if [ -n "${OBJ_LIST}" ]; then
611                         lipo -create ${OBJ_LIST} -o "${BUNDLE_PATH}/${file}"
612                 fi
613                 if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_deployment}" ]; then
614                         private_framework Aspell "${ASpellInstallDir}/lib/libaspell.15.dylib" "${LYX_BUNDLE_PATH}/${file}"
615                 fi
616                 if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_deployment}" ]; then
617                         private_framework Hunspell "${HunSpellInstallDir}/lib/libhunspell-1.2.0.dylib" "${LYX_BUNDLE_PATH}/${file}"
618                 fi
619                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
620                         deploy_qtlibs "${LYX_BUNDLE_PATH}/${file}"
621                 fi
622                 otool -L "${BUNDLE_PATH}/${file}" | while read reference ; do
623                         case "${reference}" in
624                         *"${LyxBuildDir}"*"("*")")
625                                 echo ERROR: Bad reference to "${reference}" found!!
626                                 ;;
627                         esac
628                 done
629         done
630         for arch in ${ARCH_LIST} ; do
631                 rm -f ${BUNDLE_PATH}/*-${arch}
632         done
633 }
634
635 copy_dictionaries() {
636         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_dictionaries}" ]; then
637                 ASpellResources="${LyxAppPrefix}/Contents/Resources"
638                 # try to reuse macports dictionaries for now
639                 if [ -d /opt/local/lib/aspell-0.60 ]; then ASpellInstallDir=/opt/local ; fi
640                 mkdir -p "${ASpellResources}"
641                 echo Copy Aspell dictionaries from "${ASpellInstallDir}"
642                 mkdir -p "${ASpellResources}"/data "${ASpellResources}"/dicts
643                 cp -p -r "${ASpellInstallDir}/lib/aspell-0.60"/* "${ASpellResources}"/data
644                 cp -p -r "${ASpellInstallDir}/share/aspell"/* "${ASpellResources}"/dicts
645         fi
646         if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_dictionaries}" ]; then
647                 HunSpellResources="${LyxAppPrefix}/Contents/Resources"
648                 if [ -d "${DictionarySourceDir}" ]; then
649                         ( cd "${DictionarySourceDir}" && find dicts -name .svn -prune -o -type f -print | cpio -pmdv "${HunSpellResources}" )
650                 fi
651         fi
652         if [ -d "${DictionarySourceDir}" -a "yes" = "${thesaurus_deployment}" ]; then
653                 MyThesResources="${LyxAppPrefix}/Contents/Resources"
654                 ( cd "${DictionarySourceDir}" && find thes -name .svn -prune -o -type f -print | cpio -pmdv "${MyThesResources}" )
655         fi
656 }
657
658 set_bundle_display_options() {
659         X_BOUNDS=$2
660         Y_BOUNDS=$3
661         Y_POSITION=$((Y_BOUNDS - 65))
662         Y_BOUNDS=$((Y_BOUNDS + 20))
663         LYX_X_POSITION=$((X_BOUNDS / 4))
664         LYX_Y_POSITION=$Y_POSITION
665         APP_X_POSITION=$((3 * X_BOUNDS / 4))
666         APP_Y_POSITION=$Y_POSITION
667         osascript <<-EOF
668         tell application "Finder"
669         set f to POSIX file ("${1}" as string) as alias
670         tell folder f
671             open
672             tell container window
673                 set toolbar visible to false
674                 set statusbar visible to false
675                 set current view to icon view
676                 delay 1 -- sync
677                 set the bounds to {20, 50, $X_BOUNDS, $Y_BOUNDS}
678             end tell
679             delay 1 -- sync
680             set icon size of the icon view options of container window to 64
681             set arrangement of the icon view options of container window to not arranged
682             set position of item "Documents" to {$LYX_X_POSITION,0}
683             set position of item "${LyxName}.app" to {$LYX_X_POSITION,$LYX_Y_POSITION}
684             set position of item "Applications" to {$APP_X_POSITION,$APP_Y_POSITION}
685             set background picture of the icon view options\
686                                         of container window to file "background.png" of folder "Pictures"
687             set the bounds of the container window to {0, 0, $X_BOUNDS, $Y_BOUNDS}
688             update without registering applications
689             delay 5 -- sync
690             close
691         end tell
692         delay 5 -- sync
693     end tell
694 EOF
695 }
696
697 make_dmg() {
698         cd "${1}"
699
700         BGSIZE=`file "$DmgBackground" | awk -F , '/PNG/{print $2 }' | tr x ' '`
701         BG_W=`echo ${BGSIZE} | awk '{print $1 }'`
702         BG_H=`echo ${BGSIZE} | awk '{print $2 }'`
703
704         rm -f "${DMGNAME}.sparseimage" "${DMGNAME}.dmg"
705
706         hdiutil create -type SPARSE -size ${DMGSIZE:-"250m"} -fs HFS+ -volname "${LyxBase}" "${DMGNAME}"
707         # Unmount currently mounted disk image
708         test -d /Volumes/"${LyxBase}" && umount /Volumes/"${LyxBase}"
709
710         # Mount the disk image
711         hdiutil attach "${DMGNAME}.sparseimage"
712
713         # Obtain device information
714         DEVS=$(hdiutil attach "${DMGNAME}.sparseimage" | cut -f 1)
715         DEV=$(echo $DEVS | cut -f 1 -d ' ')
716         VOLUME=$(mount |grep ${DEV} | cut -f 3 -d ' ')
717
718         # copy in the application bundle
719         cp -Rp "${LyxAppDir}.app" "${VOLUME}/${LyxName}.app"
720
721         # copy in background image
722         mkdir -p "${VOLUME}/Pictures"
723         cp "${DmgBackground}" "${VOLUME}/Pictures/background.png"
724         # symlink applications
725         ln -s /Applications/ "${VOLUME}"/Applications
726         test -d "${DocumentationDir}" && cp -r "${DocumentationDir}" "${VOLUME}"
727         set_bundle_display_options "${VOLUME}" ${BG_W} ${BG_H}
728         /Developer/Tools/SetFile -a C "${VOLUME}"
729         mv "${VOLUME}/Pictures" "${VOLUME}/.Pictures"
730
731         # Unmount the disk image
732         hdiutil detach ${DEV}
733
734         # Convert the disk image to read-only
735         hdiutil convert "${DMGNAME}.sparseimage" -format UDBZ -o "${DMGNAME}.dmg"
736         rm -f "${DMGNAME}.sparseimage"
737 }
738
739 # ------------------------------
740 # Building distribution packages
741 # ------------------------------
742
743 build_package() {
744         test -n "${LyxAppZip}" && (
745                 cd "${LyxAppPrefix}" && zip -r "${LyxAppZip}" .
746         )
747
748         test -n "${DMGLocation}" && (
749                 make_dmg "${DMGLocation}"
750                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
751                         rm -f "${DMGLocation}/${DMGNAME}+qt4.dmg"
752                         echo move to "${DMGLocation}/${DMGNAME}+qt4${MAC_API}.dmg"
753                         mv "${DMGLocation}/${DMGNAME}.dmg" "${DMGLocation}/${DMGNAME}+qt4${MAC_API}.dmg"
754                 fi
755         )
756 }
757
758 # ------------------------------
759 # main block
760 # ------------------------------
761
762 if [ ${LyxOnlyPackage:-"no"} = "no" ]; then
763         build_lyx
764         convert_universal
765         copy_dictionaries
766 fi
767 build_package