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