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