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