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