]> git.lyx.org Git - lyx.git/blob - development/LyX-Mac-binary-release.sh
Add visible space. After long discussion the solution is part of InsetSpace.
[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 EOF
547         if [ ! -d "${condir}/PlugIns" ]; then
548                 mkdir -p "${condir}/PlugIns"
549                 find "${source}/plugins" -name \*.dylib -print | while read libname ; do
550                         echo Copy plugin "${libname}"
551                         dylib=`basename "${libname}"`
552                         dirname=`dirname "${libname}"`
553                         dirname=`basename "${dirname}"`
554                         mkdir -p "${condir}/PlugIns/${dirname}"
555                         cp -p "${libname}" "${condir}/PlugIns/${dirname}"
556                 done
557         fi
558         for libnm in ${QtLibraries} ; do
559                 fwdir=`framework_name "$libnm"`
560                 dirname=`dirname "${fwdir}"`
561                 mkdir -p "${condir}/${dirname}"
562                 dirname=`basename "${fwdir}"`
563                 test -d "${condir}/${fwdir}" || (
564                         echo Copy framework "${source}/lib/"`basename "${fwdir}"`
565                         cp -pR "${source}/lib/"`basename "${fwdir}"` "${condir}/${fwdir}"
566                         echo Set library id in "${condir}/${fwdir}/${version}${libnm}"
567                         install_name_tool -id "@executable_path/../${fwdir}/${version}${libnm}" "${condir}/${fwdir}/${version}${libnm}"
568                         find "${condir}/PlugIns" "${condir}/"`dirname "${fwdir}"` -name Headers -prune -o -type f -print | while read filename ; do
569                                 if [ "${filename}" != "${target}" ]; then
570                                         otool -L "${filename}" 2>/dev/null | sort -u | while read library ; do
571                                                 # pattern match for: /path/to/qt4/lib/QtGui.framework/Versions/4/QtGui (compatibility version 4.6.0, current version 4.6.2)
572                                                 case "${library}" in
573                                                 *"${libnm}"*"("*version*")"*)
574                                                         echo Correct library id reference to "${libnm}" in "${filename}"
575                                                         install_name_tool -change\
576                                                                 "${source}/lib/${dirname}/${version}${libnm}"\
577                                                                 "@executable_path/../${fwdir}/${version}${libnm}"\
578                                                                 "${filename}"
579                                                         ;;
580                                                 esac
581                                         done
582                                 fi
583                         done
584                 )
585                 echo Correct library id reference to "${libnm}" in "${target}"
586                 install_name_tool -change\
587                         "${source}/lib/${dirname}/${version}${libnm}"\
588                         "@executable_path/../${fwdir}/${version}${libnm}"\
589                         "${target}"
590         done
591 }
592
593 # -------------------------
594 # Create universal binaries
595 # -------------------------
596 convert_universal() {
597         cd "${LyxAppPrefix}"
598         for file in ${LYX_FILE_LIST} ; do
599                 OBJ_LIST=
600                 for arch in ${ARCH_LIST} ; do
601                         if [ -f "${BUNDLE_PATH}/${file}-${arch}" ]; then
602                                 OBJ_LIST="${OBJ_LIST} ${BUNDLE_PATH}/${file}-${arch}"
603                         fi
604                 done
605                 if [ -n "${OBJ_LIST}" ]; then
606                         lipo -create ${OBJ_LIST} -o "${BUNDLE_PATH}/${file}"
607                 fi
608                 if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_deployment}" ]; then
609                         private_framework Aspell "${ASpellInstallDir}/lib/libaspell.15.dylib" "${LYX_BUNDLE_PATH}/${file}"
610                 fi
611                 if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_deployment}" ]; then
612                         private_framework Hunspell "${HunSpellInstallDir}/lib/libhunspell-1.2.0.dylib" "${LYX_BUNDLE_PATH}/${file}"
613                 fi
614                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
615                         deploy_qtlibs "${LYX_BUNDLE_PATH}/${file}"
616                 fi
617                 otool -L "${BUNDLE_PATH}/${file}" | while read reference ; do
618                         case "${reference}" in
619                         *"${LyxBuildDir}"*"("*")")
620                                 echo ERROR: Bad reference to "${reference}" found!!
621                                 ;;
622                         esac
623                 done
624         done
625         for arch in ${ARCH_LIST} ; do
626                 rm -f ${BUNDLE_PATH}/*-${arch}
627         done
628 }
629
630 copy_dictionaries() {
631         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_dictionaries}" ]; then
632                 ASpellResources="${LyxAppPrefix}/Contents/Resources"
633                 # try to reuse macports dictionaries for now
634                 if [ -d /opt/local/lib/aspell-0.60 ]; then ASpellInstallDir=/opt/local ; fi
635                 mkdir -p "${ASpellResources}"
636                 echo Copy Aspell dictionaries from "${ASpellInstallDir}"
637                 mkdir -p "${ASpellResources}"/data "${ASpellResources}"/dicts
638                 cp -p -r "${ASpellInstallDir}/lib/aspell-0.60"/* "${ASpellResources}"/data
639                 cp -p -r "${ASpellInstallDir}/share/aspell"/* "${ASpellResources}"/dicts
640         fi
641         if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_dictionaries}" ]; then
642                 HunSpellResources="${LyxAppPrefix}/Contents/Resources"
643                 if [ -d "${DictionarySourceDir}" ]; then
644                         ( cd "${DictionarySourceDir}" && find dicts -name .svn -prune -o -type f -print | cpio -pmdv "${HunSpellResources}" )
645                 fi
646         fi
647         if [ -d "${DictionarySourceDir}" -a "yes" = "${thesaurus_deployment}" ]; then
648                 MyThesResources="${LyxAppPrefix}/Contents/Resources"
649                 ( cd "${DictionarySourceDir}" && find thes -name .svn -prune -o -type f -print | cpio -pmdv "${MyThesResources}" )
650         fi
651 }
652
653 set_bundle_display_options() {
654         X_BOUNDS=$2
655         Y_BOUNDS=$3
656         Y_POSITION=$((Y_BOUNDS - 65))
657         Y_BOUNDS=$((Y_BOUNDS + 20))
658         LYX_X_POSITION=$((X_BOUNDS / 4))
659         LYX_Y_POSITION=$Y_POSITION
660         APP_X_POSITION=$((3 * X_BOUNDS / 4))
661         APP_Y_POSITION=$Y_POSITION
662         osascript <<-EOF
663         tell application "Finder"
664         set f to POSIX file ("${1}" as string) as alias
665         tell folder f
666             open
667             tell container window
668                 set toolbar visible to false
669                 set statusbar visible to false
670                 set current view to icon view
671                 delay 1 -- sync
672                 set the bounds to {20, 50, $X_BOUNDS, $Y_BOUNDS}
673             end tell
674             delay 1 -- sync
675             set icon size of the icon view options of container window to 64
676             set arrangement of the icon view options of container window to not arranged
677             set position of item "Documents" to {$LYX_X_POSITION,0}
678             set position of item "${LyxName}.app" to {$LYX_X_POSITION,$LYX_Y_POSITION}
679             set position of item "Applications" to {$APP_X_POSITION,$APP_Y_POSITION}
680             set background picture of the icon view options\
681                                         of container window to file "background.png" of folder "Pictures"
682             set the bounds of the container window to {0, 0, $X_BOUNDS, $Y_BOUNDS}
683             update without registering applications
684             delay 5 -- sync
685             close
686         end tell
687         delay 5 -- sync
688     end tell
689 EOF
690 }
691
692 make_dmg() {
693         cd "${1}"
694
695         BGSIZE=`file "$DmgBackground" | awk -F , '/PNG/{print $2 }' | tr x ' '`
696         BG_W=`echo ${BGSIZE} | awk '{print $1 }'`
697         BG_H=`echo ${BGSIZE} | awk '{print $2 }'`
698
699         rm -f "${DMGNAME}.sparseimage" "${DMGNAME}.dmg"
700
701         hdiutil create -type SPARSE -size ${DMGSIZE:-"250m"} -fs HFS+ -volname "${LyxBase}" "${DMGNAME}"
702         # Unmount currently mounted disk image
703         test -d /Volumes/"${LyxBase}" && umount /Volumes/"${LyxBase}"
704
705         # Mount the disk image
706         hdiutil attach "${DMGNAME}.sparseimage"
707
708         # Obtain device information
709         DEVS=$(hdiutil attach "${DMGNAME}.sparseimage" | cut -f 1)
710         DEV=$(echo $DEVS | cut -f 1 -d ' ')
711         VOLUME=$(mount |grep ${DEV} | cut -f 3 -d ' ')
712
713         # copy in the application bundle
714         cp -Rp "${LyxAppDir}.app" "${VOLUME}/${LyxName}.app"
715
716         # copy in background image
717         mkdir -p "${VOLUME}/Pictures"
718         cp "${DmgBackground}" "${VOLUME}/Pictures/background.png"
719         # symlink applications
720         ln -s /Applications/ "${VOLUME}"/Applications
721         test -d "${DocumentationDir}" && cp -r "${DocumentationDir}" "${VOLUME}"
722         set_bundle_display_options "${VOLUME}" ${BG_W} ${BG_H}
723         /Developer/Tools/SetFile -a C "${VOLUME}"
724         mv "${VOLUME}/Pictures" "${VOLUME}/.Pictures"
725
726         # Unmount the disk image
727         hdiutil detach ${DEV}
728
729         # Convert the disk image to read-only
730         hdiutil convert "${DMGNAME}.sparseimage" -format UDBZ -o "${DMGNAME}.dmg"
731         rm -f "${DMGNAME}.sparseimage"
732 }
733
734 # ------------------------------
735 # Building distribution packages
736 # ------------------------------
737
738 build_package() {
739         test -n "${LyxAppZip}" && (
740                 cd "${LyxAppPrefix}" && zip -r "${LyxAppZip}" .
741         )
742
743         test -n "${DMGLocation}" && (
744                 make_dmg "${DMGLocation}"
745                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt4_deployment}" ]; then
746                         rm -f "${DMGLocation}/${DMGNAME}+qt4.dmg"
747                         echo move to "${DMGLocation}/${DMGNAME}+qt4.dmg"
748                         mv "${DMGLocation}/${DMGNAME}.dmg" "${DMGLocation}/${DMGNAME}+qt4${MAC_API}.dmg"
749                 fi
750         )
751 }
752
753 # ------------------------------
754 # main block
755 # ------------------------------
756
757 if [ ${LyxOnlyPackage:-"no"} = "no" ]; then
758         build_lyx
759         convert_universal
760         copy_dictionaries
761 fi
762 build_package