]> git.lyx.org Git - lyx.git/blob - development/LyX-Mac-binary-release.sh
Autotools: optimize with -Og when debugging
[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: February 2020
9
10 QtAPI=${QtAPI:-"-cocoa"}
11 QtVersion=${QtVersion:-"4.6.3"}
12 QtSourceVersion=${QtSourceVersion:-"qt-everywhere-opensource-src-${QtVersion}"}
13 QtBuildSubDir="qt-${QtVersion}-build${QtAPI}"
14 QtConfigureOptions=${QtConfigureOptions:-"-release"}
15 QtSkipComponents=${QtSkipComponents:-"qtconnectivity qtscript qtquickcontrols qtmultimedia qtvirtualkeyboard qtwayland qtwebengine"}
16 LibMagicVersion=${LibMagicVersion:-"5.19"}
17 LibMagicSource="file-${LibMagicVersion}"
18 LibMagicLibrary="libmagic.1.dylib"
19
20 ASpellVersion=${ASpellVersion:-"0.60.6.1"}
21 ASpellSource="aspell-${ASpellVersion}"
22 ASpellLibrary="libaspell.15.dylib"
23
24 HunSpellVersion=${HunSpellVersion:-"1.3.3"}
25 HunSpellSource="hunspell-${HunSpellVersion}"
26 HunSpellLibrary="libhunspell-1.3.0.dylib"
27
28 unset DYLD_LIBRARY_PATH LD_LIBRARY_PATH
29
30 # Prerequisite:
31 # * a decent checkout of LyX sources (probably you have it already)
32 # * Qt - build with shared or static libraries for the used platforms (default: i386 and ppc)
33 #   or - an unpacked source tree of Qt in $QTSOURCEDIR or in the sibling directory (variable QtSourceVersion)
34 # * for aspell support:
35 #   the aspell sources placed in a sibling directory (variable ASpellSource)
36 # * for hunspell support:
37 #   the hunspell sources placed in a sibling directory (variable HunSpellSource)
38 # * for dictionary deployment (only hunspell dicts and mythes thesauri are deployed per default):
39 #   - aspell:   the dictionary files of macports (in /opt/local/share/aspell and /opt/local/lib/aspell-0.60)
40 #   - hunspell: the dictionary files in the sibling directory dictionaries/dicts
41 #   - mythes:   the data and idx files in the sibling directory dictionaries/thes
42 # * for magic file type detection support:
43 #   the libmagic sources placed in a sibling directory (variable LibMagicSource)
44
45 LyXConfigureOptions="--enable-warnings --enable-optimization=-Os --with-x=no"
46 LyXConfigureOptions="${LyXConfigureOptions} --disable-stdlib-debug"
47 AspellConfigureOptions="--enable-warnings --enable-optimization=-O0 --enable-debug --disable-nls --enable-compile-in-filters --disable-pspell-compatibility"
48 HunspellConfigureOptions="--with-warnings --disable-nls --disable-static"
49
50 QtMajorVersion=$(echo "${QtVersion}"|cut -d. -f1)
51 QtConfigureOptions="${QtConfigureOptions} -opensource -silent -shared -confirm-license"
52 # stupid special case...
53 case "${QtVersion}:${QtAPI}" in
54 4.6*:-carbon)
55         QtConfigureOptions="${QtConfigureOptions} -fast -no-exceptions"
56         QtConfigureOptions="${QtConfigureOptions} -no-webkit -no-qt3support -no-javascript-jit -no-dbus"
57         QtConfigureOptions="${QtConfigureOptions} -nomake examples -nomake demos -nomake docs -nomake tools"
58         for arch in ${ARCH_LIST} ; do
59                 QTARCHS="${QTARCHS} -arch ${arch}"
60         done
61         ;;
62 5.0*)
63         QtConfigureOptions="${QtConfigureOptions} -fast -no-strip"
64         QtConfigureOptions="${QtConfigureOptions} -no-javascript-jit -no-pkg-config"
65         QtConfigureOptions="${QtConfigureOptions} -nomake examples -nomake demos -nomake docs -nomake tools"
66         ;;
67 5.6*|5.7*)
68         QtConfigureOptions="${QtConfigureOptions} -no-strip"
69         QtConfigureOptions="${QtConfigureOptions} -no-kms -no-pkg-config"
70         QtConfigureOptions="${QtConfigureOptions} -nomake examples -nomake tools"
71         QtConfigureOptions="${QtConfigureOptions} -skip qtconnectivity -skip qtscript"
72         QtConfigureOptions="${QtConfigureOptions} -skip qtquickcontrols"
73         QtConfigureOptions="${QtConfigureOptions} -skip qttools"
74         QtConfigureOptions="${QtConfigureOptions} -skip qtdeclarative"
75         ;;
76 5.*)
77         QtConfigureOptions="${QtConfigureOptions} -no-strip"
78         QtConfigureOptions="${QtConfigureOptions} -no-kms -no-pkg-config"
79         QtConfigureOptions="${QtConfigureOptions} -nomake examples -nomake tools"
80         for component in ${QtSkipComponents} ; do
81                 QtConfigureOptions="${QtConfigureOptions} -skip ${component}"
82         done
83         ;;
84 *)
85         QtConfigureOptions="${QtConfigureOptions} -fast -no-exceptions"
86         QtConfigureOptions="${QtConfigureOptions} -no-webkit -no-qt3support -no-javascript-jit -no-dbus"
87         QtConfigureOptions="${QtConfigureOptions} -nomake examples -nomake demos -nomake docs -nomake tools"
88         QtConfigureOptions="${QtConfigureOptions} ${QtAPI}"
89         for arch in ${ARCH_LIST} ; do
90                 QTARCHS="${QTARCHS} -arch ${arch}"
91         done
92         ;;
93 esac
94
95 aspell_dictionaries="no"
96 hunspell_dictionaries="yes"
97
98 libmagic_deployment="yes"
99 aspell_deployment="yes"
100 hunspell_deployment="yes"
101 thesaurus_deployment="yes"
102
103 qt_deployment="yes"
104
105 # auto detect Xcode location
106 DEVELOPER_SDKS=$(dirname $(xcrun --show-sdk-path))
107 if [ -n "${DEVELOPER_SDKS}" ]; then
108         XCODE_DEVELOPER=$(dirname $(dirname $(xcrun --show-sdk-platform-path)))
109         MACOSX_DEPLOYMENT_TARGET="10.7" # Lion support is default
110         SDKROOT="${DEVELOPER_SDKS}/MacOSX$(xcrun --show-sdk-version).sdk" # use default SDK
111 elif [ -d "/Developer/SDKs" ]; then
112         DEVELOPER_SDKS="/Developer/SDKs"
113         XCODE_DEVELOPER="/Developer"
114         MACOSX_DEPLOYMENT_TARGET="10.4" # Tiger support is default
115         SDKROOT="${DEVELOPER_SDKS}/MacOSX10.5.sdk" # Leopard build is default
116 fi
117
118 # detection of script home
119 LyxSourceDir=$(dirname "$0")
120 if [ ! -d "${LyxSourceDir}" ]; then
121         echo Missing LyX source directory.
122         exit 2
123 fi
124 case "${LyxSourceDir}" in
125 /*/development)
126         LyxSourceDir=$(dirname "${LyxSourceDir}")
127         ;;
128 /*)
129         ;;
130 */development|development)
131         LyxSourceDir=$(dirname "${LyxSourceDir}")
132         LyxSourceDir=$(cd "${LyxSourceDir}";pwd)
133         ;;
134 *)
135         LyxSourceDir=$(cd "${LyxSourceDir}";pwd)
136         ;;
137 esac
138
139 usage() {
140         echo "*" Build script for LyX on Mac OS X
141         echo
142         echo Optional arguments:
143         echo " --aspell-deployment=yes|no ." default yes
144         echo " --with-qt-frameworks=yes|no." default no
145         echo " --qt-deployment=yes|no ....." default yes
146         echo " --with-sdkroot=SDKROOT ....." default 10.5 "(Leopard)"
147         echo " --with-arch=ARCH ..........." default ppc,i386
148         echo " --with-build-path=PATH ....." default \${lyx-src-dir}/../lyx-build
149         echo " --with-dmg-location=PATH ..." default \${build-path}
150         echo " --with-binary-strip=yes ...." default no
151         echo " --codesign-identity=CSID ..." default is w/o CSID
152         echo
153         echo "All other arguments with -- are passed to configure"
154         echo "including the defaults: ${LyXConfigureOptions}"
155         case "${1}" in
156         --help=short)
157                 ;;
158         *)
159                 if [ -x "${LyxSourceDir}/configure" ]; then
160                         echo
161                         echo "*" Configure options of LyX
162                         echo
163                         "${LyxSourceDir}/configure" --help
164                 fi
165         esac
166         exit 0
167 }
168
169 NCPU=$(sysctl -n hw.ncpu)
170 NCPU=$((NCPU / 2))
171 if [ $NCPU -gt 1 ]; then
172         MAKEJOBS=-j${NCPU}
173 fi
174
175 while [ $# -gt 0 ]; do
176         case "${1}" in
177         --with-qt-frameworks=*)
178                 configure_qt_frameworks=$(echo ${1}|cut -d= -f2)
179                 if [ "$configure_qt_frameworks" = "yes" ]; then
180                         unset QTDIR
181                         qt_deployment="no"
182                 fi
183                 shift
184                 ;;
185         --with-qt-dir=*)
186                 QTDIR=$(echo ${1}|cut -d= -f2)
187                 shift
188                 ;;
189         --with-macos-deployment-target=*)
190                 MACOSX_DEPLOYMENT_TARGET=$(echo ${1}|cut -d= -f2)
191                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
192                 shift
193                 ;;
194         --with-sdkroot=*)
195                 SDKROOT=$(echo ${1}|cut -d= -f2)
196                 case "${SDKROOT}" in
197                 10.4)
198                         SDKROOT="${DEVELOPER_SDKS}/MacOSX10.4u.sdk"
199                         export CC=gcc-4.0
200                         export OBJC=gcc-4.0
201                         export CXX=g++-4.0
202                         ;;
203                 *)
204                         SDKROOT="${DEVELOPER_SDKS}/MacOSX${SDKROOT}.sdk"
205                         if [ ! -d "${SDKROOT}" ]; then
206                                 echo Invalid SDKROOT given: "${SDKROOT}"
207                                 usage --help=short
208                         fi
209                         ;;
210                 esac
211                 shift
212                 ;;
213         --codesign-identity=*)
214                 CODESIGN_IDENTITY=$(echo "${1}"|cut -d= -f2)
215                 shift
216                 ;;
217         --libmagic-deployment=*)
218                 libmagic_deployment=$(echo ${1}|cut -d= -f2)
219                 shift
220                 ;;
221         --aspell-deployment=*)
222                 aspell_deployment=$(echo ${1}|cut -d= -f2)
223                 aspell_dictionaries=$aspell_deployment
224                 shift
225                 ;;
226         --hunspell-deployment=*)
227                 hunspell_deployment=$(echo ${1}|cut -d= -f2)
228                 hunspell_dictionaries=$hunspell_deployment
229                 shift
230                 ;;
231         --thesaurus-deployment=*)
232                 thesaurus_deployment=$(echo ${1}|cut -d= -f2)
233                 shift
234                 ;;
235         --qt-deployment=*)
236                 qt_deployment=$(echo ${1}|cut -d= -f2)
237                 shift
238                 ;;
239         --with-arch=*)
240                 ARCH=$(echo ${1}|cut -d= -f2|tr ',' ' ')
241                 ARCH_LIST="${ARCH_LIST} ${ARCH}"
242                 shift
243                 ;;
244         --with-dmg-location=*)
245                 DMGLocation=$(echo ${1}|cut -d= -f2)
246                 shift
247                 ;;
248         --with-binary-strip=yes)
249                 strip="-strip"
250                 shift
251                 ;;
252         --with-build-path=*)
253                 LyxBuildDir=$(echo ${1}|cut -d= -f2)
254                 shift
255                 ;;
256         --with-util-dir=*)
257                 LyXUtilitiesDir=$(echo ${1}|cut -d= -f2)
258                 shift
259                 ;;
260         --help|--help=*)
261                 usage "${1}"
262                 ;;
263         --without-aspell)
264                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
265                 aspell_deployment="no"
266                 shift
267                 ;;
268         --with-included-hunspell)
269                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
270                 hunspell_deployment="no"
271                 shift
272                 ;;
273         --without-hunspell)
274                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
275                 hunspell_deployment="no"
276                 hunspell_dictionaries="no"
277                 shift
278                 ;;
279         --only-qt*=*)
280                 QtOnlyPackage=$(echo ${1}|cut -d= -f2)
281                 shift
282                 ;;
283         --only-package=*)
284                 LyxOnlyPackage=$(echo ${1}|cut -d= -f2)
285                 shift
286                 ;;
287         --enable-cxx11|--enable-cxx-mode=*)
288                 EnableCXXMode="${1}"
289                 shift
290                 ;;
291         --*)
292                 LyXConfigureOptions="${LyXConfigureOptions} ${1}"
293                 shift
294                 ;;
295         *)
296                 break
297                 ;;
298         esac
299 done
300
301 if [ "${configure_qt_frameworks}" != "yes" ]; then
302         QtInstallDir=${QTDIR}
303 fi
304
305 ARCH_LIST=${ARCH_LIST:-"ppc i386"}
306
307 aspellstrip=
308
309 LyxBuildDir=${LyxBuildDir:-$(dirname "${LyxSourceDir}")/lyx-build}
310 DMGLocation=${DMGLocation:-"${LyxBuildDir}"}
311
312 LyXUtilitiesDir=${LyXUtilitiesDir:-"${LyxBuildDir}"/utilities}
313
314 LibMagicSourceDir=${LIBMAGICDIR:-$(dirname "${LyxSourceDir}")/${LibMagicSource}}
315 LibMagicBuildDir="${LyxBuildDir}"/"${LibMagicSource}"
316 LibMagicInstallDir=${LibMagicInstallDir:-"${LyXUtilitiesDir}"}
317 LibMagicInstallHdr="${LibMagicInstallDir}/include/magic.h"
318
319 ASpellSourceDir=${ASPELLDIR:-$(dirname "${LyxSourceDir}")/${ASpellSource}}
320 ASpellBuildDir="${ASpellSourceDir}"
321 ASpellInstallDir=${ASpellInstallDir:-"${LyXUtilitiesDir}"}
322 ASpellInstallHdr="${ASpellInstallDir}/include/aspell.h"
323
324 HunSpellSourceDir=${HUNSPELLDIR:-$(dirname "${LyxSourceDir}")/${HunSpellSource}}
325 HunSpellBuildDir="${HunSpellSourceDir}"
326 HunSpellInstallDir=${HunSpellInstallDir:-"${LyXUtilitiesDir}"}
327 HunSpellInstallHdr="${HunSpellInstallDir}/include/hunspell/hunspell.h"
328
329 QtSourceDir=${QTSOURCEDIR:-$(dirname "${LyxSourceDir}")/${QtSourceVersion}}
330 QtBuildDir=${QtBuildDir:-"${LyxBuildDir}"/${QtBuildSubDir:-"qt-build"}}
331
332 DictionarySourceDir=${DICTIONARYDIR:-$(dirname "${LyxSourceDir}")/dictionaries}
333 DocumentationDir=$(dirname "${LyxSourceDir}")/Documents
334 DmgBackground="${LyxSourceDir}"/development/MacOSX/dmg-background.tiff
335
336 if [ -z "${LyXVersion}" ]; then
337         LyXVersion=$(grep AC_INIT "${LyxSourceDir}"/configure.ac | cut -d, -f2 | tr -d " []()")
338 fi
339 LyXVersionSuffix=${LyXVersionSuffix:-$(echo "${LyXVersion}" | cut -d. -f1-2)}
340 case "${LyXVersion}" in
341 *dev*)
342         LyXGitCommitHash=$(cd "${LyxSourceDir}" ; git log -1 --pretty=format:%h)
343         ;;
344 esac
345
346 LyxName="LyX"
347 LyxBase="${LyxName}-${LyXVersion}"
348 LyxApp="${LyxBase}.app"
349 LyxAppDir="${LyxBuildDir}"/"${LyxBase}"
350 LyxBuildDir="${LyxAppDir}.build"
351 LyxAppPrefix="${LyxAppDir}.app"
352 # if zip file is needed... remove the comment sign
353 #LyxAppZip="${LyxAppPrefix}.zip"
354
355 # ---------------------------------
356 # DON'T MODIFY ANYTHING BELOW HERE!
357 # ---------------------------------
358
359 # don't change order here...
360 case "${QtVersion}" in
361 5.0.*|5.1.*)
362         QtLibraries=${QtLibraries:-"QtSvg QtXml QtPrintSupport QtWidgets QtGui QtNetwork QtConcurrent QtCore"}
363         QtFrameworkVersion="5"
364         ;;
365 5.1[2-5].*)
366         QtLibraries=${QtLibraries:-"QtDBus QtSvg QtXml QtPrintSupport QtMacExtras QtWidgets QtGui QtNetwork QtConcurrent QtCore"}
367         QtFrameworkVersion="5"
368         ;;
369 5*)
370         QtLibraries=${QtLibraries:-"QtSvg QtXml QtPrintSupport QtMacExtras QtWidgets QtGui QtNetwork QtConcurrent QtCore"}
371         QtFrameworkVersion="5"
372         ;;
373 6*)
374         QtLibraries=${QtLibraries:-"QtDBus QtSvg QtXml QtPrintSupport QtSvgWidgets QtWidgets QtGui QtNetwork QtConcurrent QtCore"}
375         QtFrameworkVersion="A"
376         case "${EnableCXXMode}" in
377         --enable-cxx11|--enable-cxx-mode=11)
378                 echo Warning: Adjust cxx standard "${EnableCXXMode}" for Qt 6. C++17 or better is required.
379                 EnableCXXMode="--enable-cxx-mode=17"
380                 ;;
381         esac
382         ;;
383 *)
384         QtLibraries=${QtLibraries:-"QtSvg QtXml QtGui QtNetwork QtCore"}
385         QtFrameworkVersion="4"
386         ;;
387 esac
388
389 DMGNAME="${LyxBase}${LyXGitCommitHash:+-}${LyXGitCommitHash}"
390 DMGSIZE="550m"
391
392 # Check for existing SDKs
393 SDKs=$(echo ${DEVELOPER_SDKS}/MacOSX1[01]*sdk)
394 case $SDKs in
395 *${SDKROOT}*)
396         ;;
397 *10.6*)
398         MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-"10.5"}; export MACOSX_DEPLOYMENT_TARGET
399         case "${MACOSX_DEPLOYMENT_TARGET}" in
400         10.6)
401                 SDKROOT="${DEVELOPER_SDKS}/MacOSX10.6.sdk"; export SDKROOT
402                 ;;
403         10.5|10.4)
404                 SDKROOT=${SDKROOT:-"${DEVELOPER_SDKS}/MacOSX10.5.sdk"}; export SDKROOT
405                 ;;
406         esac
407         ;;
408 *10.5*)
409         MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-"10.4"}; export MACOSX_DEPLOYMENT_TARGET
410         SDKROOT=${SDKROOT:-"${DEVELOPER_SDKS}/MacOSX10.5.sdk"}; export SDKROOT
411         ;;
412 *)
413         echo Unknown or missing SDK for Mac OS X.
414         exit 1
415         ;;
416 esac
417 MYCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET}"
418 MYLDFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET}"
419
420 case "${EnableCXXMode}" in
421 --enable-cxx11|--enable-cxx-mode=11)
422         export CC=cc
423         export CXX="c++ -stdlib=libc++"
424         export CXXFLAGS=-std=c++11
425         LyXConfigureOptions="${LyXConfigureOptions} --enable-cxx-mode=11"
426         ;;
427 --enable-cxx-mode=17)
428         export CC=cc
429         export CXX="c++ -stdlib=libc++"
430         export CXXFLAGS=-std=c++17
431         LyXConfigureOptions="${LyXConfigureOptions} ${EnableCXXMode}"
432         ;;
433 esac
434
435 build_qt() {
436         echo Build Qt library ${QtSourceDir}
437         if [ "${QtInstallDir}" = "${QtBuildDir}" ]; then
438                 echo Bad install directory for Qt.
439                 echo Must be different from build directory "${QtBuildDir}".
440                 exit 1
441         fi
442         (
443                 mkdir -p "${QtBuildDir}" && cd "${QtBuildDir}"
444                 echo configure options:
445                 echo ${QtConfigureOptions} ${QTARCHS} -prefix "${QtInstallDir}"
446                 "${QtSourceDir}"/configure ${QtConfigureOptions} ${QTARCHS} -prefix "${QtInstallDir}"
447                 make -j1 && make -j1 install
448         )
449 }
450
451 case ${QtOnlyPackage:-"no"} in
452 y*)
453         build_qt
454         exit 0
455         ;;
456 *)
457         if [ "${configure_qt_frameworks}" != "yes" -a -d "${QtSourceDir}" -a ! \( -d "${QtBuildDir}" -a -d "${QtInstallDir}" \) ]; then
458                 build_qt
459         fi
460         ;;
461 esac
462
463 if [ -d "${LibMagicSourceDir}" -a ! -f "${LibMagicInstallHdr}" ]; then
464         # we have a private libmagic (file(1)) source tree at hand...
465         # so let's build and install it
466         if [ -z "${LibMagicVersion}" ]; then
467                 LibMagicVersion=$(grep AC_INIT "${LibMagicSourceDir}"/configure.ac | cut -d, -f2|tr -d " []()")
468         fi
469
470         LibMagicName="LibMagic"
471         LibMagicBase="${LibMagicName}-${LibMagicVersion}"
472
473         echo Build libmagic library ${LibMagicBase}
474         echo configure options:
475         echo --prefix="${LibMagicInstallDir}" ${LibMagicConfigureOptions}
476
477         mkdir -p "${LibMagicBuildDir}" && cd "${LibMagicBuildDir}"
478
479         # ----------------------------------------
480         # Build LibMagic for different architectures
481         # ----------------------------------------
482         FILE_LIST="${LibMagicLibrary}"
483
484         for arch in ${ARCH_LIST} ; do
485                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export CPPFLAGS
486                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYLDFLAGS}"; export LDFLAGS
487                 "${LibMagicSourceDir}/configure"\
488                         --prefix="${LibMagicInstallDir}"\
489                         ${LibMagicConfigureOptions}
490                 make && make install${strip}
491                 for file in ${FILE_LIST} ; do
492                         if [ -f "${LibMagicInstallDir}"/lib/${file} ]; then
493                                 mv "${LibMagicInstallDir}"/lib/${file}\
494                                         "${LibMagicInstallDir}"/lib/${file}-${arch} 
495                         else
496                                 echo Cannot build and install LibMagic for ${arch}.
497                                 exit 1
498                         fi
499                 done
500         done
501         # -------------------------
502         # Create universal binaries
503         # -------------------------
504         for file in ${FILE_LIST} ; do
505                 OBJ_LIST=
506                 for arch in ${ARCH_LIST} ; do
507                         OBJ_LIST="${OBJ_LIST} lib/${file}-${arch}"
508                 done
509                 (
510                         cd "${LibMagicInstallDir}"
511                         lipo -create ${OBJ_LIST} -o lib/${file}
512                         # check for the "missing link"...
513                         test -f lib/libmagic.dylib || (cd lib ; ln -s "${LibMagicLibrary}" libmagic.dylib)
514                 )
515         done
516         # --------
517         # Clean up
518         # --------
519         for arch in ${ARCH_LIST} ; do
520                 rm -f "${LibMagicInstallDir}"/lib/*-${arch}
521         done
522 fi
523
524 if [ -d "${HunSpellSourceDir}" -a ! -f "${HunSpellInstallHdr}" ]; then
525         # we have a private HunSpell source tree at hand...
526         # so let's build and install it
527         if [ -z "${HunSpellVersion}" ]; then
528                 HunSpellVersion=$(grep AC_INIT "${HunSpellSourceDir}"/configure.ac | cut -d, -f2|tr -d " []()")
529         fi
530
531         HunSpellName="Hunspell"
532         HunSpellBase="${HunSpellName}-${HunSpellVersion}"
533
534         echo Build hunspell library ${HunSpellBase}
535         echo configure options:
536         echo --prefix="${HunSpellInstallDir}" ${HunspellConfigureOptions}
537
538         mkdir -p "${HunSpellBuildDir}" && cd "${HunSpellBuildDir}"
539
540         # ----------------------------------------
541         # Build HunSpell for different architectures
542         # ----------------------------------------
543         FILE_LIST="${HunSpellLibrary}"
544
545         for arch in ${ARCH_LIST} ; do
546                 make distclean
547                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export CPPFLAGS
548                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYLDFLAGS}"; export LDFLAGS
549                 "${HunSpellSourceDir}/configure"\
550                         --prefix="${HunSpellInstallDir}"\
551                         ${HunspellConfigureOptions}
552                 make && make install${strip}
553                 for file in ${FILE_LIST} ; do
554                         if [ -f "${HunSpellInstallDir}"/lib/${file} ]; then
555                                 mv "${HunSpellInstallDir}"/lib/${file}\
556                                         "${HunSpellInstallDir}"/lib/${file}-${arch} 
557                         else
558                                 echo Cannot build and install HunSpell for ${arch}.
559                                 exit 1
560                         fi
561                 done
562         done
563         # -------------------------
564         # Create universal binaries
565         # -------------------------
566         for file in ${FILE_LIST} ; do
567                 OBJ_LIST=
568                 for arch in ${ARCH_LIST} ; do
569                         OBJ_LIST="${OBJ_LIST} lib/${file}-${arch}"
570                 done
571                 (
572                         cd "${HunSpellInstallDir}"
573                         lipo -create ${OBJ_LIST} -o lib/${file}
574                         # check for the "missing link"...
575                         test -f lib/libhunspell.dylib || (cd lib ; ln -s "${HunSpellLibrary}" libhunspell.dylib)
576                 )
577         done
578         # --------
579         # Clean up
580         # --------
581         for arch in ${ARCH_LIST} ; do
582                 rm -f "${HunSpellInstallDir}"/lib/*-${arch}
583         done
584 fi
585
586 if [ -d "${ASpellSourceDir}" -a ! -f "${ASpellInstallHdr}" -a "yes" = "${aspell_deployment}" ]; then
587         # we have a private ASpell source tree at hand...
588         # so let's build and install it
589         if [ -z "${ASpellVersion}" ]; then
590                 ASpellVersion=$(grep AC_INIT "${ASpellSourceDir}"/configure.ac | cut -d, -f2|tr -d " []()")
591         fi
592
593         ASpellName="Aspell"
594         ASpellBase="${ASpellName}-${ASpellVersion}"
595
596         echo Build aspell library ${ASpellBase}
597         echo configure options:
598         echo --prefix="${ASpellInstallDir}" ${AspellConfigureOptions}
599
600         # ASpell builds inplace only :(
601         cd "${ASpellSourceDir}"
602
603         # ----------------------------------------
604         # Build ASpell for different architectures
605         # ----------------------------------------
606         FILE_LIST="${ASpellLibrary}"
607
608         for arch in ${ARCH_LIST} ; do
609                 make distclean
610                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYCFLAGS}"; export CPPFLAGS
611                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch} ${MYLDFLAGS}"; export LDFLAGS
612                 CXXFLAGS=-g "${ASpellSourceDir}/configure"\
613                         --prefix="${ASpellInstallDir}"\
614                         ${AspellConfigureOptions}
615                 make && make install${aspellstrip}
616                 for file in ${FILE_LIST} ; do
617                         if [ -f "${ASpellInstallDir}"/lib/${file} ]; then
618                                 mv "${ASpellInstallDir}"/lib/${file}\
619                                         "${ASpellInstallDir}"/lib/${file}-${arch} 
620                         else
621                                 echo Cannot build and install ASpell for ${arch}.
622                                 exit 1
623                         fi
624                 done
625         done
626         # -------------------------
627         # Create universal binaries
628         # -------------------------
629         for file in ${FILE_LIST} ; do
630                 OBJ_LIST=
631                 for arch in ${ARCH_LIST} ; do
632                         OBJ_LIST="${OBJ_LIST} lib/${file}-${arch}"
633                 done
634                 (
635                         cd "${ASpellInstallDir}"
636                         lipo -create ${OBJ_LIST} -o lib/${file}
637                 )
638         done
639         # --------
640         # Clean up
641         # --------
642         for arch in ${ARCH_LIST} ; do
643                 rm -f "${ASpellInstallDir}"/lib/*-${arch}
644         done
645 fi
646
647
648 framework_name() {
649         echo "Frameworks/${1}.framework"
650 }
651
652 LYX_FILE_LIST="lyx lyxclient tex2lyx lyxconvert"
653 BUNDLE_PATH="Contents/MacOS"
654 LYX_BUNDLE_PATH="${LyxAppPrefix}/${BUNDLE_PATH}"
655
656 build_lyx() {
657         # Clear Output
658         if [ -n "${LyxAppZip}" -a -f "${LyxAppZip}" ]; then rm "${LyxAppZip}"; fi
659         if [ -d "${LyxAppPrefix}" ]; then
660                 find "${LyxAppPrefix}" -type d -exec chmod u+w '{}' \;
661                 rm -rf "${LyxAppPrefix}"
662         fi
663
664         # -------------------------------------
665         # Automate configure check
666         # -------------------------------------
667         if [ ! -f "${LyxSourceDir}"/configure -o "${LyxSourceDir}"/configure -ot "${LyxSourceDir}"/configure.ac ]; then
668                 ( cd "${LyxSourceDir}" && sh autogen.sh )
669         else
670                 find "${LyxSourceDir}" -name Makefile.am -print | while read file ; do
671                         dname=$(dirname "$file")
672                         if [ -f "$dname/Makefile.in" -a "$dname/Makefile.in" -ot "$file" ]; then
673                                 ( cd "${LyxSourceDir}" && sh autogen.sh )
674                                 break
675                         fi
676                 done
677         fi
678         # -------------------------------------
679         # Build LyX for different architectures
680         # -------------------------------------
681
682         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_deployment}" ]; then
683                 ConfigureExtraInc="--with-extra-inc=${ASpellInstallDir}/include"
684                 ConfigureExtraLib="--with-extra-lib=${ASpellInstallDir}/lib"
685         fi
686
687         if [ -d "${HunSpellInstallDir}" -a "yes" = "${hunspell_deployment}" ]; then
688                 HunSpellFramework=$(framework_name Hunspell)
689                 HunSpellFramework=$(basename "${HunSpellFramework}")
690                 ConfigureExtraInc="--with-extra-inc=${HunSpellInstallDir}/include"
691                 ConfigureExtraLib="--with-extra-lib=${HunSpellInstallDir}/lib"
692                 # LyXConfigureOptions="${LyXConfigureOptions} --with-hunspell-framework=${HunSpellFramework}"
693         fi
694         LyXConfigureOptions="${LyXConfigureOptions} ${ConfigureExtraInc}"
695         LyXConfigureOptions="${LyXConfigureOptions} ${ConfigureExtraLib}"
696
697         for arch in ${ARCH_LIST} ; do
698
699                 if [ -d "${LyxBuildDir}" ];  then rm -r "${LyxBuildDir}"; fi
700                 mkdir -p "${LyxBuildDir}" && cd "${LyxBuildDir}"
701
702                 CPPFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch}"
703                 LDFLAGS="${SDKROOT:+-isysroot ${SDKROOT}} -arch ${arch}"
704
705                 if [ "$configure_qt_frameworks" = "yes" ]; then
706                         export QT_CORE_CFLAGS="-FQtCore"
707                         export QT_CORE_LIBS="-framework QtCore"
708                         export QT_FRONTEND_CFLAGS="-FQtGui"
709                         export QT_FRONTEND_LIBS="-framework QtGui"
710                         CPPFLAGS="${CPPFLAGS} -I${SDKROOT}/Library/Frameworks/QtCore.framework/Headers"
711                         CPPFLAGS="${CPPFLAGS} -I${SDKROOT}/Library/Frameworks/QtGui.framework/Headers"
712                 fi
713
714                 echo LDFLAGS="${LDFLAGS}"
715                 export LDFLAGS
716                 echo CPPFLAGS="${CPPFLAGS}"
717                 export CPPFLAGS
718                 echo CONFIGURE_OPTIONS="${LyXConfigureOptions}" ${QtInstallDir:+"--with-qt-dir=${QtInstallDir}"}
719                 export PATH="${QtInstallDir}/libexec:$PATH"
720                 "${LyxSourceDir}/configure"\
721                         --prefix="${LyxAppPrefix}" --with-version-suffix="-${LyXVersionSuffix}"\
722                         ${QtInstallDir:+"--with-qt-dir=${QtInstallDir}"} \
723                         ${LyXConfigureOptions}\
724                         --enable-build-type=rel && \
725                 make ${MAKEJOBS} && make install${strip}
726                 for file in ${LYX_FILE_LIST} ; do
727                         if [ -f "${LYX_BUNDLE_PATH}/${file}" ]; then
728                                 mv "${LYX_BUNDLE_PATH}/${file}"\
729                                         "${LYX_BUNDLE_PATH}/${file}-${arch}" 
730                         else
731                                 echo ERROR: Cannot build and install ${file} for ${arch}.
732                                 exit 1
733                         fi
734                 done
735         done
736 }
737
738 content_directory() {
739         target="$1"
740         content=$(dirname "${target}")
741         content=$(dirname "${content}")
742         echo "${content}"
743 }
744
745 installname() {
746                 echo install_name_tool "$@"
747                 install_name_tool "$@" || exit 1
748 }
749
750 private_framework() {
751         fwname="$1" ; shift
752         source="$1" ; shift
753         target="$1" ; shift
754         version=$(echo ${1:-"1.1.1"}.1.1.1 | cut -d. -f1-3) ; shift
755         fwdir=$(framework_name "${fwname}")
756         condir=$(content_directory "${target}")
757         libnm=$(basename "${source}")
758         libid="org.lyx."$(echo "${libnm}" | cut -d. -f1)
759         svrsn=$(echo "${version}" | cut -d. -f1-2)
760         fwvrsn="1"
761         mkdir -p "${condir}/${fwdir}"/Versions/${fwvrsn}/Headers
762         mkdir -p "${condir}/${fwdir}"/Versions/${fwvrsn}/Resources
763         if [ ! -f "${condir}/${fwdir}/Versions/${fwvrsn}/${fwname}" ]; then
764                 cp -p "${source}" "${condir}/${fwdir}/Versions/${fwvrsn}/${fwname}"
765                 for hfile in "$@" ; do
766                         test -f "${hfile}" && cp -p "${hfile}" "${condir}/${fwdir}"/Versions/${fwvrsn}/Headers
767                 done
768                 ln -s ${fwvrsn} "${condir}/${fwdir}/Versions/Current"
769                 ln -s Versions/Current/Headers "${condir}/${fwdir}/Headers"
770                 ln -s Versions/Current/Resources "${condir}/${fwdir}/Resources"
771                 ln -s Versions/Current/"${fwname}" "${condir}/${fwdir}/${fwname}"
772                 installname -id "@executable_path/../${fwdir}/${fwname}" "${condir}/${fwdir}/${fwname}"
773                 if [ -f "${LyxSourceDir}"/development/LyX-Mac-frameworks-template.plist ]; then
774                         cat "${LyxSourceDir}"/development/LyX-Mac-frameworks-template.plist | sed \
775                                 -e "s/@CFBundleExecutable@/${fwname}/" \
776                                 -e "s/@CFBundleIdentifier@/${libid}/" \
777                                 -e "s/@CFBundleShortVersionString@/${svrsn}/" \
778                                 -e "s/@CFBundleVersion@/${version}/" > "${condir}/${fwdir}"/Resources/Info.plist
779                 fi
780         fi
781         installname -change "${source}" "@executable_path/../${fwdir}/${fwname}" "${target}"
782 }
783
784 deploy_qtlibs() {
785         source="${QtInstallDir}"
786         target="$1"
787         version="Versions/${QtFrameworkVersion}/"
788         condir=$(content_directory "${target}")
789         mkdir -p "${condir}/Resources"
790         test -f "${condir}/Resources/qt.conf" || cat - > "${condir}/Resources/qt.conf" <<-EOF
791 [Paths]
792 Plugins = PlugIns
793 Translations = translations
794 EOF
795         if [ ! -d "${condir}/PlugIns" ]; then
796                 mkdir -p "${condir}/PlugIns"
797                 find "${source}/plugins" -name \*.dylib -print | grep -v _debug.dylib | while read libname ; do
798                         echo Copy plugin "${libname}"
799                         dylib=$(basename "${libname}")
800                         dirname=$(dirname "${libname}")
801                         dirname=$(basename "${dirname}")
802                         mkdir -p "${condir}/PlugIns/${dirname}"
803                         cp -p "${libname}" "${condir}/PlugIns/${dirname}"
804                 done
805         fi
806         echo Copy frameworks ${QtLibraries} ...
807         for libnm in ${QtLibraries} ; do
808                 fwdir=$(framework_name "$libnm")
809                 dirname=$(dirname "${fwdir}")
810                 mkdir -p "${condir}/${dirname}"
811                 dirname=$(basename "${fwdir}")
812                 libpath="${source}/lib/${dirname}"
813                 if [ -d "${libpath}" ]; then
814                         test -d "${condir}/${fwdir}" || (
815                                 echo Copy framework "${libpath}"
816                                 cp -pR "${libpath}" "${condir}/${fwdir}"
817                                 rm -f "${condir}/${fwdir}/${libnm}"_debug "${condir}/${fwdir}/${version}${libnm}"_debug
818                                 test -f "${condir}/${fwdir}/${libnm}".prl && mv "${condir}/${fwdir}/${libnm}".prl "${condir}/${fwdir}"/Resources
819                                 test -f "${condir}/${fwdir}/${libnm}"_debug.prl && mv "${condir}/${fwdir}/${libnm}"_debug.prl "${condir}/${fwdir}"/Resources
820                                 installname -id "@executable_path/../${fwdir}/${version}${libnm}" "${condir}/${fwdir}/${version}${libnm}"
821                                 find "${condir}/PlugIns" "${condir}/"$(dirname "${fwdir}") -name Headers -prune -o -type f -print | while read filename ; do
822                                         if [ "${filename}" != "${target}" ]; then
823                                                 otool -L "${filename}" 2>/dev/null | sort -u | while read library ; do
824                                                         # pattern match for: /path/to/qt/lib/QtGui.framework/Versions/4/QtGui (compatibility version 4.6.0, current version 4.6.2)
825                                                         case "${library}" in
826                                                         *@rpath/*"${libnm}"*"("*version*")"*)
827                                                                 # echo rpath based name for ${libnm} is ok.
828                                                                 ;;
829                                                         *"${libnm}"*"("*version*")"*)
830                                                                 installname -change\
831                                                                         "${source}/lib/${dirname}/${version}${libnm}"\
832                                                                         "@executable_path/../${fwdir}/${version}${libnm}"\
833                                                                         "${filename}"
834                                                                 ;;
835                                                         esac
836                                                 done
837                                         fi
838                                 done
839                         )
840                         installname -change\
841                                 "${source}/lib/${dirname}/${version}${libnm}"\
842                                 "@executable_path/../${fwdir}/${version}${libnm}"\
843                                 "${target}"
844                 else
845                         echo Warning: Cannot copy framework for "$libnm" ... missing source "${libpath}"
846                 fi
847         done
848         if [ -d "${source}"/translations ]; then
849                 if [ ! -d "${condir}/translations" ]; then
850                         mkdir -p "${condir}/translations"
851                 fi
852                 echo Copy Qt translations to "${condir}/translations"
853                 cp -p "${source}"/translations/qt_*.qm "${condir}/translations"
854         fi
855 }
856
857 # -------------------------
858 # Create universal binaries
859 # -------------------------
860 convert_universal() {
861         cd "${LyxAppPrefix}"
862         for file in ${LYX_FILE_LIST} ; do
863                 OBJ_LIST=
864                 for arch in ${ARCH_LIST} ; do
865                         if [ -f "${BUNDLE_PATH}/${file}-${arch}" ]; then
866                                 OBJ_LIST="${OBJ_LIST} ${BUNDLE_PATH}/${file}-${arch}"
867                         fi
868                 done
869                 if [ -n "${OBJ_LIST}" ]; then
870                         lipo -create ${OBJ_LIST} -o "${BUNDLE_PATH}/${file}"
871                 fi
872                 if [ -f "${LibMagicInstallDir}/lib/${LibMagicLibrary}" -a "yes" = "${libmagic_deployment}" ]; then
873                         private_framework LibMagic "${LibMagicInstallDir}/lib/${LibMagicLibrary}" "${LYX_BUNDLE_PATH}/${file}" \
874                                 "${LibMagicVersion}" "${LibMagicInstallHdr}"
875                 fi
876                 if [ -f "${ASpellInstallDir}/lib/${ASpellLibrary}" -a "yes" = "${aspell_deployment}" ]; then
877                         private_framework Aspell "${ASpellInstallDir}/lib/${ASpellLibrary}" "${LYX_BUNDLE_PATH}/${file}" \
878                                 "${ASpellVersion}" "${ASpellInstallHdr}"
879                 fi
880                 if [ -f "${HunSpellInstallDir}/lib/${HunSpellLibrary}" -a "yes" = "${hunspell_deployment}" ]; then
881                         private_framework Hunspell "${HunSpellInstallDir}/lib/${HunSpellLibrary}" "${LYX_BUNDLE_PATH}/${file}" \
882                                 "${HunSpellVersion}" "${HunSpellInstallDir}/include/hunspell/"*.hxx "${HunSpellInstallHdr}"
883                 fi
884                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt_deployment}" ]; then
885                         deploy_qtlibs "${LYX_BUNDLE_PATH}/${file}"
886                 fi
887                 otool -L "${BUNDLE_PATH}/${file}" | while read reference ; do
888                         case "${reference}" in
889                         *"${LyxBuildDir}"*"("*")")
890                                 echo ERROR: Bad reference to "${reference}" found!!
891                                 ;;
892                         esac
893                 done
894         done
895         for arch in ${ARCH_LIST} ; do
896                 rm -f "${BUNDLE_PATH}"/*-${arch}
897         done
898 }
899
900 # -------------------------
901 # Create code sign signatures
902 # -------------------------
903 code_sign() {
904         target="$1"
905         condir=$(content_directory "${target}"/lyx)
906         appdir=$(dirname "${condir}")
907         # have to sign frameworks first
908         for fwname in Aspell Hunspell LibMagic ; do
909                 fwitem="${condir}"/$(framework_name "${fwname}")
910                 if [ -d "${fwitem}" ]; then
911                         codesign --verbose --force --sign "${CODESIGN_IDENTITY}" "${fwitem}"
912                 fi
913         done
914         for csitem in \
915                 "${condir}"/Frameworks/Qt*.framework/Versions/${QtFrameworkVersion} \
916                 "${condir}"/PlugIns/*/lib*.dylib \
917                 "${condir}"/Library/Spotlight/* \
918                 "${target}"/inkscape \
919                 "${target}"/lilypond \
920                 "${target}"/lilypond-book \
921                 "${target}"/maxima \
922                 "${target}"/tex2lyx \
923                 "${target}"/lyxeditor \
924                 "${target}"/lyxconvert \
925                 "${target}"/lyxclient
926         do
927                 codesign --verbose --force --sign "${CODESIGN_IDENTITY}" "${csitem}"
928         done
929
930         /usr/bin/codesign --verbose --force --sign "${CODESIGN_IDENTITY}" "${appdir}" || {
931                 echo Warning: codesign failed with certificate named '"'${CODESIGN_IDENTITY}'"'
932         }
933 }
934
935 deduplicate() {
936         find "$@" -type f -print | while read file ; do
937                 echo $(md5 -q "$file") "$file"
938         done | sort | while read hash file ; do
939                 ppath=$(dirname "$pfile")
940                 path=$(dirname "$file")
941                 if [ "$phash" = "$hash" -a "$ppath" = "$path" ]; then
942                         pname=$(basename "$pfile")
943                         name=$(basename "$file")
944                         cmp -s "$pfile" "$file" && (
945                                 rm "$file"
946                                 cd "$path" && ln -s "$pname" "$name" && echo link for "$file" created
947                         )
948                 fi
949                 phash="$hash"
950                 pfile="$file"
951         done
952 }
953
954 copy_dictionaries() {
955         if [ -d "${ASpellInstallDir}" -a "yes" = "${aspell_dictionaries}" ]; then
956                 ASpellResources="${LyxAppPrefix}/Contents/Resources"
957                 # try to reuse macports dictionaries for now
958                 if [ -d /opt/local/lib/aspell-0.60 ]; then ASpellInstallDir=/opt/local ; fi
959                 mkdir -p "${ASpellResources}"
960                 echo Copy Aspell dictionaries from "${ASpellInstallDir}"
961                 mkdir -p "${ASpellResources}"/data "${ASpellResources}"/dicts
962                 cp -p -r "${ASpellInstallDir}/lib/aspell-0.60"/* "${ASpellResources}"/data
963                 cp -p -r "${ASpellInstallDir}/share/aspell"/* "${ASpellResources}"/dicts
964         fi
965         if [ -d "${DictionarySourceDir}" -a "yes" = "${hunspell_dictionaries}" ]; then
966                 HunSpellResources="${LyxAppPrefix}/Contents/Resources"
967                 ( cd "${DictionarySourceDir}" && find dicts -name .svn -prune -o -type f -print | cpio -pmdv "${HunSpellResources}" )
968                 deduplicate "${HunSpellResources}"/dicts
969         fi
970         if [ -d "${DictionarySourceDir}" -a "yes" = "${thesaurus_deployment}" ]; then
971                 MyThesResources="${LyxAppPrefix}/Contents/Resources"
972                 ( cd "${DictionarySourceDir}" && find thes -name .svn -prune -o -type f -print | cpio -pmdv "${MyThesResources}" )
973                 deduplicate "${MyThesResources}"/thes
974         fi
975 }
976
977 set_bundle_display_options() {
978         X_BOUNDS=$2
979         Y_BOUNDS=$3
980         Y_POSITION=$((Y_BOUNDS - 65))
981         Y_BOUNDS=$((Y_BOUNDS + 50))
982         LYX_X_POSITION=$((X_BOUNDS / 4))
983         LYX_Y_POSITION=$Y_POSITION
984         APP_X_POSITION=$((3 * X_BOUNDS / 4))
985         APP_Y_POSITION=$Y_POSITION
986         WITH_DOCUMENTS=$(test -d "${1}/Documents" && echo true || echo false)
987         osascript <<-EOF
988         tell application "Finder"
989         set f to POSIX file ("${1}" as string) as alias
990         tell folder f
991             open
992             tell container window
993                 set toolbar visible to false
994                 set statusbar visible to false
995                 set current view to icon view
996                 delay 1 -- sync
997                 set the bounds to {20, 50, $X_BOUNDS, $Y_BOUNDS}
998             end tell
999             delay 1 -- sync
1000             set icon size of the icon view options of container window to 64
1001             set arrangement of the icon view options of container window to not arranged
1002             if ${WITH_DOCUMENTS} then
1003                set position of item "Documents" to {$LYX_X_POSITION,0}
1004             end if
1005             set position of item "${LyxName}.app" to {$LYX_X_POSITION,$LYX_Y_POSITION}
1006             set position of item "Applications" to {$APP_X_POSITION,$APP_Y_POSITION}
1007             set background picture of the icon view options\
1008                of container window to file "background.png" of folder "Pictures"
1009             set the bounds of the container window to {0, 0, $X_BOUNDS, $Y_BOUNDS}
1010             update without registering applications
1011             delay 5 -- sync
1012             close
1013         end tell
1014         delay 5 -- sync
1015     end tell
1016 EOF
1017 }
1018
1019 # The image was made with with inkscape and tiffutil from dmg-background.svgz
1020 make_image() {
1021         INKSCAPE=/Applications/Inkscape.app/Contents/MacOS/inkscape
1022         (
1023                 cd "${LyxSourceDir}"/development/MacOSX
1024                 test -x ${INKSCAPE} && ${INKSCAPE} --export-type=png -w 560 -o dmg-background.png dmg-background.svgz
1025                 test -x ${INKSCAPE} && ${INKSCAPE} --export-type=png -w 1120 -o dmg-background@2x.png dmg-background.svgz
1026                 tiffutil -cathidpicheck dmg-background.png dmg-background@2x.png -out dmg-background.tiff
1027         )
1028 }
1029
1030 make_dmg() {
1031         cd "${1}"
1032
1033         test -f "${DmgBackground}" || make_image
1034         BGSIZE=$(file "$DmgBackground" | awk -F , '/TIFF/{ print $10 $4 ;}/PNG/{ print $2; }'|sed -e 's/width=//' -e 's/height=//' -e 's/x//')
1035         BG_W=$(echo ${BGSIZE} | awk '{print $1 }')
1036         BG_H=$(echo ${BGSIZE} | awk '{print $2 }')
1037
1038         rm -f "${DMGNAME}.sparseimage" "${DMGNAME}.dmg"
1039
1040         hdiutil create -type SPARSE -size ${DMGSIZE:-"250m"} -fs HFS+ -volname "${LyxBase}" "${DMGNAME}"
1041         # Unmount currently mounted disk image
1042         mount | grep "${LyxBase}" && umount /Volumes/"${LyxBase}"
1043         test -d /Volumes/"${LyxBase}" && rmdir /Volumes/"${LyxBase}"
1044
1045         # Mount the disk image
1046         DEVICES=$(hdiutil attach "${DMGNAME}.sparseimage" | cut -f 1)
1047
1048         # Obtain device information
1049         DEVICE=$(echo $DEVICES | cut -f 1 -d ' ')
1050         VOLUME=$(mount |grep ${DEVICE} | cut -f 3 -d ' ')
1051
1052         # copy in the application bundle
1053         ditto --hfsCompression "${LyxAppPrefix}" "${VOLUME}/${LyxName}.app"
1054
1055         # copy in background image
1056         mkdir -p "${VOLUME}/Pictures"
1057         ditto --hfsCompression "${DmgBackground}" "${VOLUME}/Pictures/background.png"
1058         # symlink applications
1059         ln -s /Applications/ "${VOLUME}"/Applications
1060         test -d "${DocumentationDir}" && ditto --hfsCompression "${DocumentationDir}" "${VOLUME}"
1061         set_bundle_display_options "${VOLUME}" ${BG_W} ${BG_H}
1062         PATH="${XCODE_DEVELOPER}/Tools:$PATH" SetFile -a C "${VOLUME}"
1063         mv "${VOLUME}/Pictures" "${VOLUME}/.Pictures"
1064
1065         # Unmount the disk image
1066         hdiutil detach ${DEVICE}
1067
1068         # Convert the disk image to read-only
1069         hdiutil convert "${DMGNAME}.sparseimage" -format UDBZ -o "${DMGNAME}.dmg"
1070         rm -f "${DMGNAME}.sparseimage"
1071 }
1072
1073 # ------------------------------
1074 # Building distribution packages
1075 # ------------------------------
1076
1077 build_package() {
1078         test -n "${LyxAppZip}" && (
1079                 cd "${LyxAppPrefix}" && zip -r "${LyxAppZip}" .
1080         )
1081
1082         DMGARCH=""
1083         for arch in ${ARCH_LIST} ; do
1084                 DMGARCH="${DMGARCH}-${arch}"
1085         done
1086         QtDmgArchSuffix="qt"${QtMajorVersion}${DMGARCH}${QtAPI}.dmg
1087
1088         test -n "${DMGLocation}" && (
1089                 make_dmg "${DMGLocation}"
1090                 if [ -d "${QtInstallDir}/lib/QtCore.framework/Versions/${QtFrameworkVersion}" -a "yes" = "${qt_deployment}" ]; then
1091                         rm -f "${DMGLocation}/${DMGNAME}+${QtDmgArchSuffix}"
1092                         echo move to "${DMGLocation}/${DMGNAME}+${QtDmgArchSuffix}"
1093                         mv "${DMGLocation}/${DMGNAME}.dmg" "${DMGLocation}/${DMGNAME}+${QtDmgArchSuffix}"
1094                 fi
1095         )
1096 }
1097
1098 # ------------------------------
1099 # main block
1100 # ------------------------------
1101
1102 if [ ${LyxOnlyPackage:-"no"} = "no" ]; then
1103         build_lyx
1104         convert_universal
1105         copy_dictionaries
1106         find "${LyxAppPrefix}" -type d -exec chmod go-w '{}' \;
1107         test -n "${CODESIGN_IDENTITY}" && code_sign "${LYX_BUNDLE_PATH}"
1108 fi
1109 build_package