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