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