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