]> git.lyx.org Git - lyx.git/blob - development/LyX-Mac-binary-release.sh
Implement move row/column in tabular inset
[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="${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                 make -j2 && make install${strip}
607                 for file in ${LYX_FILE_LIST} ; do
608                         if [ -f "${LYX_BUNDLE_PATH}/${file}" ]; then
609                                 mv "${LYX_BUNDLE_PATH}/${file}"\
610                                         "${LYX_BUNDLE_PATH}/${file}-${arch}" 
611                         else
612                                 echo ERROR: Cannot build and install LyX for ${arch}.
613                                 exit 1
614                         fi
615                 done
616         done
617 }
618
619 content_directory() {
620         target="$1"
621         content=$(dirname "${target}")
622         content=$(dirname "${content}")
623         echo "${content}"
624 }
625
626 private_framework() {
627         fwdir=$(framework_name "$1")
628         source="$2"
629         target="$3"
630         condir=$(content_directory "${target}")
631         libnm=$(basename "${source}")
632         mkdir -p "${condir}/${fwdir}"
633         if [ ! -f "${condir}/${fwdir}/${libnm}" ]; then
634                 cp -p "${source}" "${condir}/${fwdir}"
635                 echo Set library id in "${condir}/${fwdir}/${libnm}"
636                 install_name_tool -id "@executable_path/../${fwdir}/${libnm}" "${condir}/${fwdir}/${libnm}"
637         fi
638         echo Correct library id reference to "${libnm}" in "${target}"
639         install_name_tool -change "${source}" "@executable_path/../${fwdir}/${libnm}" "${target}"
640 }
641
642 deploy_qtlibs() {
643         source="${QtInstallDir}"
644         target="$1"
645         version="Versions/${QtFrameworkVersion}/"
646         condir=$(content_directory "${target}")
647         mkdir -p "${condir}/Resources"
648         test -f "${condir}/Resources/qt.conf" || cat - > "${condir}/Resources/qt.conf" <<-EOF
649 [Paths]
650 Plugins = PlugIns
651 Translations = translations
652 EOF
653         if [ ! -d "${condir}/PlugIns" ]; then
654                 mkdir -p "${condir}/PlugIns"
655                 find "${source}/plugins" -name \*.dylib -print | while read libname ; do
656                         echo Copy plugin "${libname}"
657                         dylib=$(basename "${libname}")
658                         dirname=$(dirname "${libname}")
659                         dirname=$(basename "${dirname}")
660                         mkdir -p "${condir}/PlugIns/${dirname}"
661                         cp -p "${libname}" "${condir}/PlugIns/${dirname}"
662                 done
663         fi
664         for libnm in ${QtLibraries} ; do
665                 fwdir=$(framework_name "$libnm")
666                 dirname=$(dirname "${fwdir}")
667                 mkdir -p "${condir}/${dirname}"
668                 dirname=$(basename "${fwdir}")
669                 test -d "${condir}/${fwdir}" || (
670                         echo Copy framework "${source}/lib/"$(basename "${fwdir}")
671                         cp -pR "${source}/lib/"$(basename "${fwdir}") "${condir}/${fwdir}"
672                         echo Set library id in "${condir}/${fwdir}/${version}${libnm}"
673                         install_name_tool -id "@executable_path/../${fwdir}/${version}${libnm}" "${condir}/${fwdir}/${version}${libnm}"
674                         find "${condir}/PlugIns" "${condir}/"$(dirname "${fwdir}") -name Headers -prune -o -type f -print | while read filename ; do
675                                 if [ "${filename}" != "${target}" ]; then
676                                         otool -L "${filename}" 2>/dev/null | sort -u | while read library ; do
677                                                 # pattern match for: /path/to/qt4/lib/QtGui.framework/Versions/4/QtGui (compatibility version 4.6.0, current version 4.6.2)
678                                                 case "${library}" in
679                                                 *"${libnm}"*"("*version*")"*)
680                                                         echo Correct library id reference to "${libnm}" in "${filename}"
681                                                         install_name_tool -change\
682                                                                 "${source}/lib/${dirname}/${version}${libnm}"\
683                                                                 "@executable_path/../${fwdir}/${version}${libnm}"\
684                                                                 "${filename}"
685                                                         ;;
686                                                 esac
687                                         done
688                                 fi
689                         done
690                 )
691                 echo Correct library id reference to "${libnm}" in "${target}"
692                 install_name_tool -change\
693                         "${source}/lib/${dirname}/${version}${libnm}"\
694                         "@executable_path/../${fwdir}/${version}${libnm}"\
695                         "${target}"
696         done
697         if [ ! -d "${condir}/translations" ]; then
698                 mkdir -p "${condir}/translations"
699         fi
700         echo Copy Qt translations to "${condir}/translations"
701         cp -p "${source}"/translations/qt_*.qm "${condir}/translations"
702 }
703
704 # -------------------------
705 # Create universal binaries
706 # -------------------------
707 convert_universal() {
708         cd "${LyxAppPrefix}"
709         for file in ${LYX_FILE_LIST} ; do
710                 OBJ_LIST=
711                 for arch in ${ARCH_LIST} ; do
712                         if [ -f "${BUNDLE_PATH}/${file}-${arch}" ]; then
713                                 OBJ_LIST="${OBJ_LIST} ${BUNDLE_PATH}/${file}-${arch}"
714                         fi
715                 done
716                 if [ -n "${OBJ_LIST}" ]; then
717                         lipo -create ${OBJ_LIST} -o "${BUNDLE_PATH}/${file}"
718                 fi
719                 if [ -f "${GettextInstallDir}/lib/${GettextLibrary}" -a "yes" = "${gettext_deployment}" ]; then
720                         private_framework Gettext "${GettextInstallDir}/lib/${GettextLibrary}" "${LYX_BUNDLE_PATH}/${file}"
721                 fi
722                 if [ -f "${ASpellInstallDir}/lib/${ASpellLibrary}" -a "yes" = "${aspell_deployment}" ]; then
723                         private_framework Aspell "${ASpellInstallDir}/lib/${ASpellLibrary}" "${LYX_BUNDLE_PATH}/${file}"
724                 fi
725                 if [ -f "${HunSpellInstallDir}/lib/${HunSpellLibrary}" -a "yes" = "${hunspell_deployment}" ]; then
726                         private_framework Hunspell "${HunSpellInstallDir}/lib/${HunSpellLibrary}" "${LYX_BUNDLE_PATH}/${file}"
727                 fi
728                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
729                         deploy_qtlibs "${LYX_BUNDLE_PATH}/${file}"
730                 fi
731                 otool -L "${BUNDLE_PATH}/${file}" | while read reference ; do
732                         case "${reference}" in
733                         *"${LyxBuildDir}"*"("*")")
734                                 echo ERROR: Bad reference to "${reference}" found!!
735                                 ;;
736                         esac
737                 done
738         done
739         for arch in ${ARCH_LIST} ; do
740                 rm -f ${BUNDLE_PATH}/*-${arch}
741         done
742 }
743
744 copy_dictionaries() {
745         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_dictionaries}" ]; then
746                 ASpellResources="${LyxAppPrefix}/Contents/Resources"
747                 # try to reuse macports dictionaries for now
748                 if [ -d /opt/local/lib/aspell-0.60 ]; then ASpellInstallDir=/opt/local ; fi
749                 mkdir -p "${ASpellResources}"
750                 echo Copy Aspell dictionaries from "${ASpellInstallDir}"
751                 mkdir -p "${ASpellResources}"/data "${ASpellResources}"/dicts
752                 cp -p -r "${ASpellInstallDir}/lib/aspell-0.60"/* "${ASpellResources}"/data
753                 cp -p -r "${ASpellInstallDir}/share/aspell"/* "${ASpellResources}"/dicts
754         fi
755         if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_dictionaries}" ]; then
756                 HunSpellResources="${LyxAppPrefix}/Contents/Resources"
757                 if [ -d "${DictionarySourceDir}" ]; then
758                         ( cd "${DictionarySourceDir}" && find dicts -name .svn -prune -o -type f -print | cpio -pmdv "${HunSpellResources}" )
759                 fi
760         fi
761         if [ -d "${DictionarySourceDir}" -a "yes" = "${thesaurus_deployment}" ]; then
762                 MyThesResources="${LyxAppPrefix}/Contents/Resources"
763                 ( cd "${DictionarySourceDir}" && find thes -name .svn -prune -o -type f -print | cpio -pmdv "${MyThesResources}" )
764         fi
765 }
766
767 set_bundle_display_options() {
768         X_BOUNDS=$2
769         Y_BOUNDS=$3
770         Y_POSITION=$((Y_BOUNDS - 65))
771         Y_BOUNDS=$((Y_BOUNDS + 20))
772         LYX_X_POSITION=$((X_BOUNDS / 4))
773         LYX_Y_POSITION=$Y_POSITION
774         APP_X_POSITION=$((3 * X_BOUNDS / 4))
775         APP_Y_POSITION=$Y_POSITION
776         osascript <<-EOF
777         tell application "Finder"
778         set f to POSIX file ("${1}" as string) as alias
779         tell folder f
780             open
781             tell container window
782                 set toolbar visible to false
783                 set statusbar visible to false
784                 set current view to icon view
785                 delay 1 -- sync
786                 set the bounds to {20, 50, $X_BOUNDS, $Y_BOUNDS}
787             end tell
788             delay 1 -- sync
789             set icon size of the icon view options of container window to 64
790             set arrangement of the icon view options of container window to not arranged
791             set position of item "Documents" to {$LYX_X_POSITION,0}
792             set position of item "${LyxName}.app" to {$LYX_X_POSITION,$LYX_Y_POSITION}
793             set position of item "Applications" to {$APP_X_POSITION,$APP_Y_POSITION}
794             set background picture of the icon view options\
795                                         of container window to file "background.png" of folder "Pictures"
796             set the bounds of the container window to {0, 0, $X_BOUNDS, $Y_BOUNDS}
797             update without registering applications
798             delay 5 -- sync
799             close
800         end tell
801         delay 5 -- sync
802     end tell
803 EOF
804 }
805
806 make_dmg() {
807         cd "${1}"
808
809         BGSIZE=$(file "$DmgBackground" | awk -F , '/PNG/{print $2 }' | tr x ' ')
810         BG_W=$(echo ${BGSIZE} | awk '{print $1 }')
811         BG_H=$(echo ${BGSIZE} | awk '{print $2 }')
812
813         rm -f "${DMGNAME}.sparseimage" "${DMGNAME}.dmg"
814
815         hdiutil create -type SPARSE -size ${DMGSIZE:-"250m"} -fs HFS+ -volname "${LyxBase}" "${DMGNAME}"
816         # Unmount currently mounted disk image
817         test -d /Volumes/"${LyxBase}" && umount /Volumes/"${LyxBase}"
818
819         # Mount the disk image
820         hdiutil attach "${DMGNAME}.sparseimage"
821
822         # Obtain device information
823         DEVS=$(hdiutil attach "${DMGNAME}.sparseimage" | cut -f 1)
824         DEV=$(echo $DEVS | cut -f 1 -d ' ')
825         VOLUME=$(mount |grep ${DEV} | cut -f 3 -d ' ')
826
827         # copy in the application bundle
828         cp -Rp "${LyxAppDir}.app" "${VOLUME}/${LyxName}.app"
829
830         # copy in background image
831         mkdir -p "${VOLUME}/Pictures"
832         cp "${DmgBackground}" "${VOLUME}/Pictures/background.png"
833         # symlink applications
834         ln -s /Applications/ "${VOLUME}"/Applications
835         test -d "${DocumentationDir}" && cp -r "${DocumentationDir}" "${VOLUME}"
836         set_bundle_display_options "${VOLUME}" ${BG_W} ${BG_H}
837         ${XCODE_DEVELOPER}/Tools/SetFile -a C "${VOLUME}"
838         mv "${VOLUME}/Pictures" "${VOLUME}/.Pictures"
839
840         # Unmount the disk image
841         hdiutil detach ${DEV}
842
843         # Convert the disk image to read-only
844         hdiutil convert "${DMGNAME}.sparseimage" -format UDBZ -o "${DMGNAME}.dmg"
845         rm -f "${DMGNAME}.sparseimage"
846 }
847
848 # ------------------------------
849 # Building distribution packages
850 # ------------------------------
851
852 build_package() {
853         test -n "${LyxAppZip}" && (
854                 cd "${LyxAppPrefix}" && zip -r "${LyxAppZip}" .
855         )
856
857         test -n "${DMGLocation}" && (
858                 make_dmg "${DMGLocation}"
859                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
860                         rm -f "${DMGLocation}/${DMGNAME}+qt4.dmg"
861                         echo move to "${DMGLocation}/${DMGNAME}+qt4${Qt4API}.dmg"
862                         mv "${DMGLocation}/${DMGNAME}.dmg" "${DMGLocation}/${DMGNAME}+qt4${Qt4API}.dmg"
863                 fi
864         )
865 }
866
867 # ------------------------------
868 # main block
869 # ------------------------------
870
871 if [ ${LyxOnlyPackage:-"no"} = "no" ]; then
872         build_lyx
873         convert_universal
874         copy_dictionaries
875 fi
876 build_package