chiark / gitweb /
518e0be35d08b9a92104f4957a7e637d3c851ef2
[cura.git] / package.sh
1 #!/usr/bin/env bash
2
3 # This script is to package the Cura package for Windows/Linux and Mac OS X
4 # This script should run under Linux and Mac OS X, as well as Windows with Cygwin.
5
6 #############################
7 # CONFIGURATION
8 #############################
9
10 ##Select the build target
11 BUILD_TARGET=${1:-none}
12 #BUILD_TARGET=win32
13 #BUILD_TARGET=darwin
14 #BUILD_TARGET=debian_i386
15 #BUILD_TARGET=debian_amd64
16 #BUILD_TARGET=freebsd
17
18 ##Do we need to create the final archive
19 ARCHIVE_FOR_DISTRIBUTION=1
20 ##Which version name are we appending to the final archive
21 export BUILD_NAME=14.09
22 TARGET_DIR=Cura-${BUILD_NAME}-${BUILD_TARGET}
23
24 ##Revision
25 export REVISION=1.17
26
27 ##Git commit
28 GIT_HASH=$(git rev-parse --short=4 HEAD)
29
30 ##Which versions of external programs to use
31 WIN_PORTABLE_PY_VERSION=2.7.2.1
32
33 ##Which CuraEngine to use
34 if [ -z ${CURA_ENGINE_REPO} ] ; then
35         CURA_ENGINE_REPO="https://github.com/alephobjects/CuraEngine"
36 fi
37
38 #############################
39 # Support functions
40 #############################
41 function checkTool
42 {
43         if [ -z "`which $1`" ]; then
44                 echo "The $1 command must be somewhere in your \$PATH."
45                 echo "Fix your \$PATH or install $2"
46                 exit 1
47         fi
48 }
49
50 function downloadURL
51 {
52         filename=`basename "$1"`
53         echo "Checking for $filename"
54         if [ ! -f "$filename" ]; then
55                 echo "Downloading $1"
56                 curl -L -O "$1"
57                 if [ $? != 0 ]; then
58                         echo "Failed to download $1"
59                         exit 1
60                 fi
61         fi
62 }
63
64 function extract
65 {
66         echo "Extracting $*"
67         echo "7z x -y $*" >> log.txt
68         7z x -y $* >> log.txt
69         if [ $? != 0 ]; then
70         echo "Failed to extract $*"
71         exit 1
72         fi
73 }
74
75 #############################
76 # Actual build script
77 #############################
78 if [ "$BUILD_TARGET" = "none" ]; then
79         echo "You need to specify a build target with:"
80         echo "$0 win32"
81         echo "$0 debian_i368"
82         echo "$0 debian_amd64"
83         echo "$0 darwin"
84         echo "$0 freebsd"
85         exit 0
86 fi
87
88 # Change working directory to the directory the script is in
89 # http://stackoverflow.com/a/246128
90 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
91 cd $SCRIPT_DIR
92
93 checkTool git "git: http://git-scm.com/"
94 checkTool curl "curl: http://curl.haxx.se/"
95 if [ $BUILD_TARGET = "win32" ]; then
96         #Check if we have 7zip, needed to extract and packup a bunch of packages for windows.
97         checkTool 7z "7zip: http://www.7-zip.org/"
98         checkTool mingw32-make "mingw: http://www.mingw.org/"
99 fi
100 #For building under MacOS we need gnutar instead of tar
101 if [ -z `which gnutar` ]; then
102         TAR=tar
103 else
104         TAR=gnutar
105 fi
106
107
108 #############################
109 # Darwin
110 #############################
111
112 if [ "$BUILD_TARGET" = "darwin" ]; then
113     TARGET_DIR=Cura-${BUILD_NAME}-MacOS
114
115         rm -rf scripts/darwin/build
116         rm -rf scripts/darwin/dist
117
118         python build_app.py py2app
119         rc=$?
120         if [[ $rc != 0 ]]; then
121                 echo "Cannot build app."
122                 exit 1
123         fi
124
125     #Add cura version file (should read the version from the bundle with pyobjc, but will figure that out later)
126     echo $BUILD_NAME > scripts/darwin/dist/Cura.app/Contents/Resources/version
127         rm -rf CuraEngine
128         git clone ${CURA_ENGINE_REPO}
129     if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
130         make -C CuraEngine VERSION=${BUILD_NAME}
131     if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
132         cp CuraEngine/build/CuraEngine scripts/darwin/dist/Cura.app/Contents/Resources/CuraEngine
133
134         cd scripts/darwin
135
136         # Install QuickLook plugin
137         mkdir -p dist/Cura.app/Contents/Library/QuickLook
138         cp -a STLQuickLook.qlgenerator dist/Cura.app/Contents/Library/QuickLook/
139
140         # Archive app
141         cd dist
142         $TAR cfp - Cura.app | gzip --best -c > ../../../${TARGET_DIR}.tar.gz
143         cd ..
144
145         # Create sparse image for distribution
146         hdiutil detach /Volumes/Cura\ -\ Lulzbot/
147         rm -rf Cura.dmg.sparseimage
148         hdiutil convert DmgTemplateCompressed.dmg -format UDSP -o Cura.dmg
149         hdiutil resize -size 500m Cura.dmg.sparseimage
150         hdiutil attach Cura.dmg.sparseimage
151         cp -a dist/Cura.app /Volumes/Cura\ -\ Lulzbot/Cura/
152         hdiutil detach /Volumes/Cura\ -\ Lulzbot
153         hdiutil convert Cura.dmg.sparseimage -format UDZO -imagekey zlib-level=9 -ov -o ../../${TARGET_DIR}.dmg
154         exit
155 fi
156
157 #############################
158 # FreeBSD part by CeDeROM
159 #############################
160
161 if [ "$BUILD_TARGET" = "freebsd" ]; then
162         export CXX="c++"
163         rm -rf Power
164         if [ ! -d "Power" ]; then
165                 git clone https://github.com/GreatFruitOmsk/Power
166         else
167                 cd Power
168                 git pull
169                 cd ..
170         fi
171         rm -rf CuraEngine
172         git clone ${CURA_ENGINE_REPO}
173     if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
174         gmake -j4 -C CuraEngine VERSION=${BUILD_NAME}
175     if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
176         rm -rf scripts/freebsd/dist
177         mkdir -p scripts/freebsd/dist/share/cura
178         mkdir -p scripts/freebsd/dist/share/applications
179         mkdir -p scripts/freebsd/dist/bin
180         cp -a Cura scripts/freebsd/dist/share/cura/
181         cp -a resources scripts/freebsd/dist/share/cura/
182         cp -a plugins scripts/freebsd/dist/share/cura/
183         cp -a CuraEngine/build/CuraEngine scripts/freebsd/dist/share/cura/
184         cp scripts/freebsd/cura.py scripts/freebsd/dist/share/cura/
185         cp scripts/freebsd/cura.desktop scripts/freebsd/dist/share/applications/
186         cp scripts/freebsd/cura scripts/freebsd/dist/bin/
187         cp -a Power/power scripts/freebsd/dist/share/cura/
188         echo $BUILD_NAME > scripts/freebsd/dist/share/cura/Cura/version
189         #Create file list (pkg-plist)
190         cd scripts/freebsd/dist
191         find * -type f > ../pkg-plist
192         DIRLVL=20; while [ $DIRLVL -ge 0 ]; do
193                 DIRS=`find share/cura -type d -depth $DIRLVL`
194                 for DIR in $DIRS; do
195                         echo "@dirrm $DIR" >> ../pkg-plist
196                 done
197                 DIRLVL=`expr $DIRLVL - 1`
198         done
199         cd ..
200         # Create archive or package if root
201         if [ `whoami` == "root" ]; then
202             echo "Are you root? Use the Port Luke! :-)"
203         else
204             echo "You are not root, building simple package archive..."
205             pwd
206             $TAR czf ../../${TARGET_DIR}.tar.gz dist/**
207         fi
208         exit
209 fi
210
211 #############################
212 # Debian 32bit .deb
213 #############################
214
215 if [ "$BUILD_TARGET" = "debian_i386" ]; then
216     export CXX="g++ -m32"
217         if [ ! -d "Power" ]; then
218                 git clone https://github.com/GreatFruitOmsk/Power
219         else
220                 cd Power
221                 git pull
222                 cd ..
223         fi
224         rm -rf CuraEngine
225         git clone ${CURA_ENGINE_REPO}
226     if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
227         make -C CuraEngine VERSION=${BUILD_NAME}
228     if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
229         rm -rf scripts/linux/${BUILD_TARGET}/usr/share/cura
230         mkdir -p scripts/linux/${BUILD_TARGET}/usr/share/cura
231         cp -a Cura scripts/linux/${BUILD_TARGET}/usr/share/cura/
232         cp -a resources scripts/linux/${BUILD_TARGET}/usr/share/cura/
233         cp -a plugins scripts/linux/${BUILD_TARGET}/usr/share/cura/
234         cp -a CuraEngine/build/CuraEngine scripts/linux/${BUILD_TARGET}/usr/share/cura/
235         cp scripts/linux/cura.py scripts/linux/${BUILD_TARGET}/usr/share/cura/
236         cp -a Power/power scripts/linux/${BUILD_TARGET}/usr/share/cura/
237         echo $BUILD_NAME > scripts/linux/${BUILD_TARGET}/usr/share/cura/Cura/version
238         sudo chown root:root scripts/linux/${BUILD_TARGET} -R
239         sudo chmod 755 scripts/linux/${BUILD_TARGET}/usr -R
240         sudo chmod 755 scripts/linux/${BUILD_TARGET}/DEBIAN -R
241         cd scripts/linux
242         dpkg-deb --build ${BUILD_TARGET} $(dirname ${TARGET_DIR})/cura_${BUILD_NAME}-${REVISION}-${GIT_HASH}_i386.deb
243         sudo chown `id -un`:`id -gn` ${BUILD_TARGET} -R
244         exit
245 fi
246
247 #############################
248 # Debian 64bit .deb
249 #############################
250
251 if [ "$BUILD_TARGET" = "debian_amd64" ]; then
252     export CXX="g++ -m64"
253         if [ ! -d "Power" ]; then
254                 git clone https://github.com/GreatFruitOmsk/Power
255         else
256                 cd Power
257                 git pull
258                 cd ..
259         fi
260         rm -rf CuraEngine
261         git clone ${CURA_ENGINE_REPO}
262     if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
263         make -C CuraEngine
264     if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
265         rm -rf scripts/linux/${BUILD_TARGET}/usr/share/cura
266         mkdir -p scripts/linux/${BUILD_TARGET}/usr/share/cura
267         cp -a Cura scripts/linux/${BUILD_TARGET}/usr/share/cura/
268         cp -a resources scripts/linux/${BUILD_TARGET}/usr/share/cura/
269         cp -a plugins scripts/linux/${BUILD_TARGET}/usr/share/cura/
270         cp -a CuraEngine/build/CuraEngine scripts/linux/${BUILD_TARGET}/usr/share/cura/
271         cp scripts/linux/cura.py scripts/linux/${BUILD_TARGET}/usr/share/cura/
272         cp -a Power/power scripts/linux/${BUILD_TARGET}/usr/share/cura/
273         echo $BUILD_NAME > scripts/linux/${BUILD_TARGET}/usr/share/cura/Cura/version
274         sudo chown root:root scripts/linux/${BUILD_TARGET} -R
275         sudo chmod 755 scripts/linux/${BUILD_TARGET}/usr -R
276         sudo chmod 755 scripts/linux/${BUILD_TARGET}/DEBIAN -R
277         cd scripts/linux
278         dpkg-deb --build ${BUILD_TARGET} $(dirname ${TARGET_DIR})/cura_${BUILD_NAME}-${REVISION}-${GIT_HASH}_amd64.deb
279         sudo chown `id -un`:`id -gn` ${BUILD_TARGET} -R
280         exit
281 fi
282
283 #############################
284 # Rest
285 #############################
286
287 #############################
288 # Download all needed files.
289 #############################
290
291 if [ $BUILD_TARGET = "win32" ]; then
292         #Get portable python for windows and extract it. (Linux and Mac need to install python themselfs)
293         downloadURL http://ftp.nluug.nl/languages/python/portablepython/v2.7/PortablePython_${WIN_PORTABLE_PY_VERSION}.exe
294         downloadURL http://sourceforge.net/projects/pyserial/files/pyserial/2.5/pyserial-2.5.win32.exe
295         downloadURL http://sourceforge.net/projects/pyopengl/files/PyOpenGL/3.0.1/PyOpenGL-3.0.1.win32.exe
296         downloadURL http://sourceforge.net/projects/numpy/files/NumPy/1.6.2/numpy-1.6.2-win32-superpack-python2.7.exe
297         downloadURL http://videocapture.sourceforge.net/VideoCapture-0.9-5.zip
298         #downloadURL http://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-20120927-git-13f0cd6-win32-static.7z
299         downloadURL http://sourceforge.net/projects/comtypes/files/comtypes/0.6.2/comtypes-0.6.2.win32.exe
300         downloadURL http://www.uwe-sieber.de/files/ejectmedia.zip
301         #Get the power module for python
302         rm -rf Power
303         git clone https://github.com/GreatFruitOmsk/Power
304         rm -rf CuraEngine
305         git clone ${CURA_ENGINE_REPO}
306     if [ $? != 0 ]; then echo "Failed to clone CuraEngine"; exit 1; fi
307 fi
308
309 #############################
310 # Build the packages
311 #############################
312 rm -rf ${TARGET_DIR}
313 mkdir -p ${TARGET_DIR}
314
315 rm -f log.txt
316 if [ $BUILD_TARGET = "win32" ]; then
317         #For windows extract portable python to include it.
318         extract PortablePython_${WIN_PORTABLE_PY_VERSION}.exe \$_OUTDIR/App
319         extract PortablePython_${WIN_PORTABLE_PY_VERSION}.exe \$_OUTDIR/Lib/site-packages
320         extract pyserial-2.5.win32.exe PURELIB
321         extract PyOpenGL-3.0.1.win32.exe PURELIB
322         extract numpy-1.6.2-win32-superpack-python2.7.exe numpy-1.6.2-sse2.exe
323         extract numpy-1.6.2-sse2.exe PLATLIB
324         extract VideoCapture-0.9-5.zip VideoCapture-0.9-5/Python27/DLLs/vidcap.pyd
325         #extract ffmpeg-20120927-git-13f0cd6-win32-static.7z ffmpeg-20120927-git-13f0cd6-win32-static/bin/ffmpeg.exe
326         #extract ffmpeg-20120927-git-13f0cd6-win32-static.7z ffmpeg-20120927-git-13f0cd6-win32-static/licenses
327         extract comtypes-0.6.2.win32.exe
328         extract ejectmedia.zip Win32
329
330         mkdir -p ${TARGET_DIR}/python
331         mkdir -p ${TARGET_DIR}/Cura/
332         mv \$_OUTDIR/App/* ${TARGET_DIR}/python
333         mv \$_OUTDIR/Lib/site-packages/wx* ${TARGET_DIR}/python/Lib/site-packages/
334         mv PURELIB/serial ${TARGET_DIR}/python/Lib
335         mv PURELIB/OpenGL ${TARGET_DIR}/python/Lib
336         mv PURELIB/comtypes ${TARGET_DIR}/python/Lib
337         mv PLATLIB/numpy ${TARGET_DIR}/python/Lib
338         mv Power/power ${TARGET_DIR}/python/Lib
339         mv VideoCapture-0.9-5/Python27/DLLs/vidcap.pyd ${TARGET_DIR}/python/DLLs
340         #mv ffmpeg-20120927-git-13f0cd6-win32-static/bin/ffmpeg.exe ${TARGET_DIR}/Cura/
341         #mv ffmpeg-20120927-git-13f0cd6-win32-static/licenses ${TARGET_DIR}/Cura/ffmpeg-licenses/
342         mv Win32/EjectMedia.exe ${TARGET_DIR}/Cura/
343         cp -a scripts/win32/nsisPlugins/libgcc_s_dw2-1.dll ${TARGET_DIR}
344         cp -a scripts/win32/nsisPlugins/libstdc++-6.dll ${TARGET_DIR}
345         
346         rm -rf Power/
347         rm -rf \$_OUTDIR
348         rm -rf PURELIB
349         rm -rf PLATLIB
350         rm -rf VideoCapture-0.9-5
351         rm -rf numpy-1.6.2-sse2.exe
352         #rm -rf ffmpeg-20120927-git-13f0cd6-win32-static
353
354         #Clean up portable python a bit, to keep the package size down.
355         rm -rf ${TARGET_DIR}/python/PyScripter.*
356         rm -rf ${TARGET_DIR}/python/Doc
357         rm -rf ${TARGET_DIR}/python/locale
358         rm -rf ${TARGET_DIR}/python/tcl
359         rm -rf ${TARGET_DIR}/python/Lib/test
360         rm -rf ${TARGET_DIR}/python/Lib/distutils
361         rm -rf ${TARGET_DIR}/python/Lib/site-packages/wx-2.8-msw-unicode/wx/tools
362         rm -rf ${TARGET_DIR}/python/Lib/site-packages/wx-2.8-msw-unicode/wx/locale
363         #Remove the gle files because they require MSVCR71.dll, which is not included. We also don't need gle, so it's safe to remove it.
364         rm -rf ${TARGET_DIR}/python/Lib/OpenGL/DLLS/gle*
365
366     #Build the C++ engine
367         mingw32-make -C CuraEngine VERSION=${BUILD_NAME}
368     if [ $? != 0 ]; then echo "Failed to build CuraEngine"; exit 1; fi
369 fi
370
371 #add Cura
372 mkdir -p ${TARGET_DIR}/Cura ${TARGET_DIR}/resources ${TARGET_DIR}/plugins
373 cp -a Cura/* ${TARGET_DIR}/Cura
374 cp -a resources/* ${TARGET_DIR}/resources
375 cp -a plugins/* ${TARGET_DIR}/plugins
376 #Add cura version file
377 echo $BUILD_NAME > ${TARGET_DIR}/Cura/version
378
379 #add script files
380 if [ $BUILD_TARGET = "win32" ]; then
381     cp -a scripts/${BUILD_TARGET}/*.bat $TARGET_DIR/
382     cp CuraEngine/build/CuraEngine.exe $TARGET_DIR
383 else
384     cp -a scripts/${BUILD_TARGET}/*.sh $TARGET_DIR/
385 fi
386
387 #package the result
388 if (( ${ARCHIVE_FOR_DISTRIBUTION} )); then
389         if [ $BUILD_TARGET = "win32" ]; then
390                 #rm ${TARGET_DIR}.zip
391                 #cd ${TARGET_DIR}
392                 #7z a ../${TARGET_DIR}.zip *
393                 #cd ..
394
395                 if [ ! -z `which wine` ]; then
396                         #if we have wine, try to run our nsis script.
397                         rm -rf scripts/win32/dist
398                         ln -sf `pwd`/${TARGET_DIR} scripts/win32/dist
399                         wine ~/.wine/drive_c/Program\ Files/NSIS/makensis.exe /DVERSION=${BUILD_NAME} scripts/win32/installer.nsi
400             if [ $? != 0 ]; then echo "Failed to package NSIS installer"; exit 1; fi
401                         mv scripts/win32/Cura_${BUILD_NAME}-${REVISION}-${GIT_HASH}.exe ./
402                 fi
403                 if [ -f '/c/Program Files (x86)/NSIS/makensis.exe' ]; then
404                         rm -rf scripts/win32/dist
405                         mv `pwd`/${TARGET_DIR} scripts/win32/dist
406                         '/c/Program Files (x86)/NSIS/makensis.exe' -DVERSION=${BUILD_NAME} 'scripts/win32/installer.nsi' >> log.txt
407             if [ $? != 0 ]; then echo "Failed to package NSIS installer"; exit 1; fi
408                         mv scripts/win32/Cura_${BUILD_NAME}.exe ./
409                 fi
410         else
411                 echo "Archiving to ${TARGET_DIR}.tar.gz"
412                 $TAR cfp - ${TARGET_DIR} | gzip --best -c > ${TARGET_DIR}.tar.gz
413         fi
414 else
415         echo "Installed into ${TARGET_DIR}"
416 fi