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