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