make_dmg.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/bin/bash
  2. set -euo pipefail
  3. # Assembles an installation bundle from a built copy of Slic3r.
  4. # Requires PAR::Packer to be installed for the version of
  5. # perl copied.
  6. # Adapted from script written by bubnikv for Prusa3D.
  7. # Run from slic3r repo root directory.
  8. # While we might have a pp executable in our path, it might not be
  9. # using the perl binary we have in path, so make sure they belong
  10. # to the same Perl instance:
  11. source $(dirname $0)/../common/util.sh
  12. if [ $# -lt 1 ]; then
  13. set_source_dir
  14. else
  15. set_source_dir $1
  16. fi
  17. set_version
  18. get_commit
  19. set_build_id
  20. set_branch
  21. set_app_name
  22. set_pr_id
  23. if !(perl -Mlocal::lib=local-lib -MPAR::Packer -e1 2> /dev/null); then
  24. echo "The PAR::Packer module was not found; installing..."
  25. cpanm --local-lib local-lib PAR::Packer
  26. fi
  27. WD=$(dirname $0)
  28. appname=Slic3r
  29. # Determine if this is a tagged (release) commit.
  30. # Change the build id accordingly.
  31. if [ $(git describe --exact-match &>/dev/null) ]; then
  32. echo "This is a tagged build"
  33. SLIC3R_BUILD_ID=$(git describe)
  34. else
  35. # Get the current branch
  36. if [ ! -z ${GIT_BRANCH+x} ]; then
  37. echo "Setting to GIT_BRANCH"
  38. current_branch=$(echo $GIT_BRANCH | cut -d / -f 2)
  39. elif [ ! -z ${TRAVIS_BRANCH+x} ]; then
  40. echo "Setting to TRAVIS_BRANCH"
  41. current_branch=$TRAVIS_BRANCH
  42. elif [ ! -z ${APPVEYOR_REPO_BRANCH+x} ]; then
  43. echo "Setting to APPVEYOR_REPO_BRANCH"
  44. current_branch=$APPVEYOR_REPO_BRANCH
  45. else
  46. current_branch=$(git symbolic-ref HEAD | sed 's!refs\/heads\/!!')
  47. fi
  48. if [ "$current_branch" == "master" ]; then
  49. echo "This is a build of the master branch"
  50. SLIC3R_VERSION=$(grep "VERSION" xs/src/libslic3r/libslic3r.h | awk -F\" '{print $2}')
  51. SLIC3R_BUILD_ID=${SLIC3R_VERSION}-$(git rev-parse --short HEAD)
  52. else
  53. echo "This is a build of a non-master branch"
  54. appname=Slic3r-${current_branch}
  55. SLIC3R_BUILD_ID=${current_branch}-$(git rev-parse --short HEAD)
  56. fi
  57. fi
  58. if [ "$current_branch" == "master" ]; then
  59. if [ ! -z ${PR_ID+x} ]; then
  60. dmgfile=slic3r-${SLIC3R_BUILD_ID}-osx-PR${PR_ID}.dmg
  61. else
  62. dmgfile=slic3r-${SLIC3R_BUILD_ID}-osx.dmg
  63. fi
  64. else
  65. dmgfile=slic3r-${SLIC3R_BUILD_ID}-osx-${current_branch}.dmg
  66. fi
  67. echo "DMG filename: ${dmgfile}"
  68. rm -rf $WD/_tmp
  69. mkdir -p $WD/_tmp
  70. # OSX Application folder shenanigans.
  71. appfolder="$WD/${appname}.app"
  72. macosfolder=$appfolder/Contents/MacOS
  73. resourcefolder=$appfolder/Contents/Resources
  74. plistfile=$appfolder/Contents/Info.plist
  75. PkgInfoContents="APPL????"
  76. source $WD/plist.sh
  77. # Our slic3r dir and location of perl
  78. eval $(perl -Mlocal::lib=local-lib)
  79. PERL_BIN=$(which perl)
  80. PP_BIN=$(which pp)
  81. SLIC3R_DIR=$(perl -MCwd=realpath -e "print realpath '${WD}/../../'")
  82. if [[ -d "${appfolder}" ]]; then
  83. echo "Deleting old working folder: ${appfolder}"
  84. rm -rf ${appfolder}
  85. fi
  86. if [[ -e "${dmgfile}" ]]; then
  87. echo "Deleting old dmg: ${dmgfile}"
  88. rm -rf ${dmgfile}
  89. fi
  90. echo "Creating new app folder: $appfolder"
  91. mkdir -p $appfolder
  92. mkdir -p $macosfolder
  93. mkdir -p $resourcefolder
  94. echo "Copying resources..."
  95. cp -rf $SLIC3R_DIR/var $resourcefolder/
  96. mv $resourcefolder/var/Slic3r.icns $resourcefolder
  97. mv $resourcefolder/var/stl.icns $resourcefolder
  98. mv $resourcefolder/var/gcode.icns $resourcefolder
  99. echo "Copying Slic3r..."
  100. cp $SLIC3R_DIR/slic3r.pl $macosfolder/slic3r.pl
  101. cp -fRP $SLIC3R_DIR/local-lib $macosfolder/local-lib
  102. cp -fRP $SLIC3R_DIR/lib/* $macosfolder/local-lib/lib/perl5/
  103. echo "Relocating Wx dylib paths..."
  104. mkdir $macosfolder/dylibs
  105. function relocate_dylibs {
  106. local bundle=$1
  107. chmod +w $bundle
  108. local dylib
  109. for dylib in $(otool -l $bundle | grep .dylib | grep -v /usr/lib | awk '{print $2}' | grep -v '^@'); do
  110. local dylib_dest="$macosfolder/dylibs/$(basename $dylib)"
  111. if [ ! -e $dylib_dest ]; then
  112. echo " relocating $dylib"
  113. cp $dylib $macosfolder/dylibs/
  114. relocate_dylibs $dylib_dest
  115. fi
  116. install_name_tool -change "$dylib" "@executable_path/dylibs/$(basename $dylib)" $bundle
  117. done
  118. }
  119. for bundle in $(find $macosfolder/local-lib/ \( -name '*.bundle' -or -name '*.dylib' \) -type f); do
  120. relocate_dylibs "$bundle"
  121. done
  122. echo "Copying startup script..."
  123. cp -f $WD/startup_script.sh $macosfolder/$appname
  124. chmod +x $macosfolder/$appname
  125. echo "Copying perl from $PERL_BIN"
  126. cp -f $PERL_BIN $macosfolder/perl-local
  127. chmod +w $macosfolder/perl-local
  128. relocate_dylibs $macosfolder/perl-local
  129. echo "Copying core modules"
  130. # Edit package/common/coreperl to add/remove core Perl modules added to this package, one per line.
  131. ${PP_BIN} \
  132. -M $(grep -v "^#" ${WD}/../common/coreperl | xargs | awk 'BEGIN { OFS=" -M "}; {$1=$1; print $0}') \
  133. -B -p -e "print 123" -o $WD/_tmp/bundle.par
  134. unzip -o $WD/_tmp/bundle.par -d $WD/_tmp/
  135. cp -rf $WD/_tmp/lib/* $macosfolder/local-lib/lib/perl5/
  136. echo "Cleaning bundle"
  137. rm -rf $macosfolder/local-lib/bin
  138. rm -rf $macosfolder/local-lib/man
  139. rm -f $macosfolder/local-lib/lib/perl5/Algorithm/*.pl
  140. rm -rf $macosfolder/local-lib/lib/perl5/unicore
  141. rm -rf $macosfolder/local-lib/lib/perl5/App
  142. rm -rf $macosfolder/local-lib/lib/perl5/Devel/CheckLib.pm
  143. rm -rf $macosfolder/local-lib/lib/perl5/ExtUtils
  144. rm -rf $macosfolder/local-lib/lib/perl5/Module/Build*
  145. rm -rf $macosfolder/local-lib/lib/perl5/TAP
  146. rm -rf $macosfolder/local-lib/lib/perl5/Test*
  147. find -d $macosfolder/local-lib -name '*.pod' -delete
  148. find -d $macosfolder/local-lib -name .packlist -delete
  149. find -d $macosfolder/local-lib -name .meta -exec rm -rf "{}" \;
  150. find -d $macosfolder/local-lib -name '*.h' -delete
  151. find -d $macosfolder/local-lib -name wxPerl.app -exec rm -rf "{}" \;
  152. find -d $macosfolder/local-lib -type d -path '*/Wx/*' \( -name WebView \
  153. -or -name DocView -or -name STC -or -name IPC \
  154. -or -name Calendar -or -name DataView \
  155. -or -name DateTime -or -name Media -or -name PerlTest \
  156. -or -name Ribbon \) -exec rm -rf "{}" \;
  157. find -d $macosfolder/local-lib -name libwx_osx_cocoau_ribbon-3.* -delete
  158. find -d $macosfolder/local-lib -name libwx_osx_cocoau_stc-3.* -delete
  159. find -d $macosfolder/local-lib -name libwx_osx_cocoau_webview-3.* -delete
  160. rm -rf $macosfolder/local-lib/lib/perl5/darwin-thread-multi-2level/Alien/wxWidgets/osx_cocoa_3_0_2_uni/include
  161. find -d $macosfolder/local-lib -type d -empty -delete
  162. make_plist
  163. echo $PkgInfoContents >$appfolder/Contents/PkgInfo
  164. KEYCHAIN_FILE_=${KEYCHAIN_FILE:-}
  165. KEYCHAIN_BASE64_=${KEYCHAIN_BASE64:-}
  166. KEYCHAIN_PASSWORD_=${KEYCHAIN_PASSWORD:-travis}
  167. KEYCHAIN_IDENTITY_=${KEYCHAIN_IDENTITY:-Developer ID Application: Alessandro Ranellucci (975MZ9YJL7)}
  168. # In case we were supplied a base64-encoded .p12 file instead of the path
  169. # to an existing keychain, create a temporary one
  170. if [[ -z "$KEYCHAIN_FILE_" && ! -z "$KEYCHAIN_BASE64_" ]]; then
  171. KEYCHAIN_FILE_=$WD/_tmp/build.keychain
  172. echo "Creating temporary keychain at ${KEYCHAIN_FILE_}"
  173. echo "$KEYCHAIN_BASE64_" | base64 --decode > "${KEYCHAIN_FILE_}.p12"
  174. security delete-keychain "$KEYCHAIN_FILE_" || true
  175. security create-keychain -p "${KEYCHAIN_PASSWORD_}" "$KEYCHAIN_FILE_"
  176. security set-keychain-settings -t 3600 -u "$KEYCHAIN_FILE_"
  177. security import "${KEYCHAIN_FILE_}.p12" -k "$KEYCHAIN_FILE_" -P "${KEYCHAIN_PASSWORD_}" -T /usr/bin/codesign
  178. security set-key-partition-list -S apple-tool:,apple: -s -k "${KEYCHAIN_PASSWORD_}" "$KEYCHAIN_FILE_"
  179. fi
  180. if [ ! -z $KEYCHAIN_FILE_ ]; then
  181. echo "Signing app..."
  182. chmod -R +w $macosfolder/*
  183. xattr -cr $appfolder
  184. security list-keychains -s "${KEYCHAIN_FILE_}"
  185. security default-keychain -s "${KEYCHAIN_FILE_}"
  186. security unlock-keychain -p "${KEYCHAIN_PASSWORD_}" "${KEYCHAIN_FILE_}"
  187. codesign --sign "${KEYCHAIN_IDENTITY_}" --deep "$appfolder"
  188. else
  189. echo "No KEYCHAIN_FILE or KEYCHAIN_BASE64 env variable; skipping codesign"
  190. fi
  191. echo "Creating dmg file...."
  192. hdiutil create -fs HFS+ -srcfolder "$appfolder" -volname "$appname" "$WD/_tmp/$dmgfile"
  193. # Compress the DMG image
  194. hdiutil convert "$WD/_tmp/$dmgfile" -format UDZO -imagekey zlib-level=9 -o "$dmgfile"
  195. if [ ! -z $KEYCHAIN_FILE_ ]; then
  196. echo "Signing app dmg..."
  197. chmod +w $dmgfile
  198. security list-keychains -s "${KEYCHAIN_FILE_}"
  199. security default-keychain -s "${KEYCHAIN_FILE_}"
  200. security unlock-keychain -p "${KEYCHAIN_PASSWORD_}" "${KEYCHAIN_FILE_}"
  201. codesign --sign "${KEYCHAIN_IDENTITY_}" "$dmgfile"
  202. fi
  203. rm -rf $WD/_tmp