a89df97f4e191e000cb2922932bd50f2548ee339
[blender.git] / build_files / build_environment / install_deps.sh
1 #!/bin/bash
2
3 DISTRO=""
4 SRC="$HOME/src/blender-deps"
5 CWD=$PWD
6
7 THREADS=`cat /proc/cpuinfo | grep cores | uniq | sed -e "s/.*: *\(.*\)/\\1/"`
8
9 PYTHON_VERSION="3.3.0"
10 BOOST_VERSION="1_51_0"
11 OIIO_VERSION="1.1.0"
12 OCIO_VERSION="1.0.7"
13 FFMPEG_VERSION="1.0"
14
15 HASXVID=false
16 HASVPX=false
17 HASMP3LAME=false
18 HASX264=false
19 HASOPENJPEG=false
20 HASSCHRO=false
21
22 # Switch to english language, else some things (like check_package_DEB()) won't work!
23 LANG_BACK=$LANG
24 LANG=""
25 export LANG
26
27 ERROR() {
28   echo "${@}"
29 }
30
31 INFO() {
32   echo "${@}"
33 }
34
35 detect_distro() {
36   if [ -f /etc/debian_version ]; then
37     DISTRO="DEB"
38   elif [ -f /etc/redhat-release ]; then
39     DISTRO="RPM"
40   elif [ -f /etc/SuSE-release ]; then
41     DISTRO="SUSE"
42   fi
43 }
44
45 prepare_opt() {
46   INFO "Ensuring /opt/lib exists and writable by us"
47   sudo mkdir -p /opt/lib
48   sudo chown $USER /opt/lib
49   sudo chmod 775 /opt/lib
50 }
51
52 compile_Python() {
53   if [ ! -d /opt/lib/python-$PYTHON_VERSION ]; then
54     INFO "Building Python-$PYTHON_VERSION"
55
56     prepare_opt
57
58     if [ ! -d $SRC/Python-$PYTHON_VERSION ]; then
59       mkdir -p $SRC
60       wget -c http://python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.bz2 -P $SRC
61
62       INFO "Unpacking Python-$PYTHON_VERSION"
63       tar -C $SRC -xf $SRC/Python-$PYTHON_VERSION.tar.bz2
64     fi
65
66     cd $SRC/Python-$PYTHON_VERSION
67
68     ./configure --prefix=/opt/lib/python-$PYTHON_VERSION --enable-ipv6 \
69         --enable-loadable-sqlite-extensions --with-dbmliborder=bdb \
70         --with-computed-gotos --with-pymalloc
71
72     make -j$THREADS
73     make install
74     make clean
75
76     rm -f /opt/lib/python-3.3
77     ln -s python-$PYTHON_VERSION /opt/lib/python-3.3
78
79     cd $CWD
80   fi
81 }
82
83 compile_Boost() {
84   INFO "Building boost"
85
86   version_dots=`echo "$BOOST_VERSION" | sed -r 's/_/./g'`
87
88   if [ ! -d /opt/lib/boost-$version_dots ]; then
89     INFO "Building Boost-$version_dots"
90
91     prepare_opt
92
93     if [ ! -d $SRC/boost_$BOOST_VERSION ]; then
94       INFO "Downloading Boost-$version_dots"
95       mkdir -p $SRC
96       wget -c http://sourceforge.net/projects/boost/files/boost/$version_dots/boost_$BOOST_VERSION.tar.bz2/download \
97         -O $SRC/boost_$BOOST_VERSION.tar.bz2
98       tar -C $SRC -xf $SRC/boost_$BOOST_VERSION.tar.bz2
99     fi
100
101     cd $SRC/boost_$BOOST_VERSION
102     ./bootstrap.sh --with-libraries=system,filesystem,thread,regex,locale --prefix=/opt/lib/boost-$version_dots
103     ./b2 install
104     ./b2 --clean
105
106     rm -f /opt/lib/boost
107     ln -s boost-$version_dots /opt/lib/boost
108
109     cd $CWD
110   fi
111 }
112
113 compile_OCIO() {
114   if [ ! -d /opt/lib/ocio-$OCIO_VERSION ]; then
115     INFO "Building OpenColorIO-$OCIO_VERSION"
116
117     prepare_opt
118
119     if [ ! -d $SRC/OpenColorIO-$OCIO_VERSION ]; then
120         INFO "Downloading OpenColorIO-$OCIO_VERSION"
121         mkdir -p $SRC
122         wget -c http://github.com/imageworks/OpenColorIO/tarball/v$OCIO_VERSION \
123           -O $SRC/OpenColorIO-$OCIO_VERSION.tar.gz
124
125         INFO "Unpacking OpenColorIO-$OCIO_VERSION"
126         tar -C "$SRC" -xf $SRC/OpenColorIO-$OCIO_VERSION.tar.gz
127         mv $SRC/imageworks-OpenColorIO* $SRC/OpenColorIO-$OCIO_VERSION
128     fi
129
130     cd $SRC/OpenColorIO-$OCIO_VERSION
131     mkdir build
132     cd build
133
134     if file /bin/cp | grep -q '32-bit'; then
135       cflags="-fPIC -m32 -march=i686"
136     else
137       cflags="-fPIC"
138     fi
139
140     cmake -D CMAKE_BUILD_TYPE=Release \
141         -D CMAKE_PREFIX_PATH=/opt/lib/ocio-$OCIO_VERSION \
142         -D CMAKE_INSTALL_PREFIX=/opt/lib/ocio-$OCIO_VERSION \
143         -D CMAKE_CXX_FLAGS="$cflags" \
144         -D CMAKE_EXE_LINKER_FLAGS="-lgcc_s -lgcc" \
145         ..
146
147     make -j$THREADS
148     make install
149
150     # Force linking against sttaic libs
151     rm -f /opt/lib/ocio-$OCIO_VERSION/lib/*.so*
152
153     # Additional depencencies
154     cp ext/dist/lib/libtinyxml.a /opt/lib/ocio-$OCIO_VERSION/lib
155     cp ext/dist/lib/libyaml-cpp.a /opt/lib/ocio-$OCIO_VERSION/lib
156
157     make clean
158
159     rm -f /opt/lib/ocio
160     ln -s ocio-$OCIO_VERSION /opt/lib/ocio
161
162     cd $CWD
163   fi
164 }
165
166 compile_OIIO() {
167   if [ ! -d /opt/lib/oiio-$OIIO_VERSION ]; then
168     INFO "Building OpenImageIO-$OIIO_VERSION"
169
170     prepare_opt
171
172     if [ ! -d $SRC/OpenImageIO-$OIIO_VERSION ]; then
173       wget -c https://github.com/OpenImageIO/oiio/tarball/Release-$OIIO_VERSION \
174           -O "$SRC/OpenImageIO-$OIIO_VERSION.tar.gz"
175
176       INFO "Unpacking OpenImageIO-$OIIO_VERSION"
177       tar -C $SRC -xf $SRC/OpenImageIO-$OIIO_VERSION.tar.gz
178       mv $SRC/OpenImageIO-oiio* $SRC/OpenImageIO-$OIIO_VERSION
179     fi
180
181     cd $SRC/OpenImageIO-$OIIO_VERSION
182     mkdir build
183     cd build
184
185     if [ -d /opt/lib/boost ]; then
186       boost_root="/opt/lib/boost"
187     else
188       boost_root="/usr"
189     fi
190
191     if file /bin/cp | grep -q '32-bit'; then
192       cflags="-fPIC -m32 -march=i686"
193     else
194       cflags="-fPIC"
195     fi
196
197     cmake -D CMAKE_BUILD_TYPE=Release \
198         -D CMAKE_PREFIX_PATH=/opt/lib/oiio-$OIIO_VERSION \
199         -D CMAKE_INSTALL_PREFIX=/opt/lib/oiio-$OIIO_VERSION \
200         -D BUILDSTATIC=ON \
201         -D CMAKE_CXX_FLAGS="$cflags" \
202         -D CMAKE_EXE_LINKER_FLAGS="-lgcc_s -lgcc" \
203         -D BOOST_ROOT="$boost_root" \
204         ../src
205
206     make -j$THREADS
207     make install
208     make clean
209
210     rm -f /opt/lib/oiio
211     ln -s oiio-$OIIO_VERSION /opt/lib/oiio
212
213     cd $CWD
214   fi
215 }
216
217 compile_FFmpeg() {
218   if [ ! -d /opt/lib/ffmpeg-$FFMPEG_VERSION ]; then
219     INFO "Building FFmpeg-$FFMPEG_VERSION"
220
221     prepare_opt
222
223     if [ ! -d $SRC/ffmpeg-$FFMPEG_VERSION ]; then
224       INFO "Downloading FFmpeg-$FFMPEG_VERSION"
225       wget -c http://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.bz2 -P $SRC
226
227       INFO "Unpacking FFmpeg-$FFMPEG_VERSION"
228       tar -C $SRC -xf $SRC/ffmpeg-$FFMPEG_VERSION.tar.bz2
229     fi
230
231     cd $SRC/ffmpeg-$FFMPEG_VERSION
232
233     extra=""
234
235     if $HASXVID; then
236       extra="$extra --enable-libxvid"
237     fi
238
239     if $HASVPX; then
240       extra="$extra --enable-libvpx"
241     fi
242
243     if $HASMP3LAME; then
244       extra="$extra --enable-libmp3lame"
245     fi
246
247     if $HASX264; then
248       extra="$extra --enable-libx264"
249     fi
250
251     if $HASOPENJPEG; then
252       extra="$extra --enable-libopenjpeg"
253     fi
254
255     if $HASSCHRO; then
256       extra="$extra --enable-libschroedinger"
257     fi
258
259     ./configure --cc="gcc -Wl,--as-needed" --extra-ldflags="-pthread -static-libgcc" \
260         --prefix=/opt/lib/ffmpeg-$FFMPEG_VERSION --enable-static --enable-avfilter --disable-vdpau \
261         --disable-bzlib --disable-libgsm --disable-libspeex --enable-libtheora \
262         --enable-libvorbis --enable-pthreads --enable-zlib --enable-stripping --enable-runtime-cpudetect \
263         --disable-vaapi  --disable-libfaac --disable-nonfree --enable-gpl \
264         --disable-postproc --disable-x11grab  --disable-librtmp  --disable-libopencore-amrnb \
265         --disable-libopencore-amrwb --disable-libdc1394 --disable-version3  --disable-outdev=sdl \
266         --disable-outdev=alsa --disable-indev=sdl --disable-indev=alsa --disable-indev=jack \
267         --disable-indev=lavfi $extra
268
269     make -j$THREADS
270     make install
271     make clean
272
273     rm -f /opt/lib/ffmpeg
274     ln -s ffmpeg-$FFMPEG_VERSION /opt/lib/ffmpeg
275
276     cd $CWD
277   fi
278 }
279
280 deb_version() {
281     dpkg-query -W -f '${Version}' $1 | sed -r 's/^([0-9]\.[0-9]+).*/\1/'
282 }
283
284 check_package_DEB() {
285   r=`apt-cache policy $1 | grep -c 'Candidate:'`
286
287   if [ $r -ge 1 ]; then
288     return 0
289   else
290     return 1
291   fi
292 }
293
294 install_DEB() {
295   INFO "Installing dependencies for DEB-based distributive"
296
297   sudo apt-get update
298   sudo apt-get -y upgrade
299
300   sudo apt-get install -y cmake scons gcc g++ libjpeg-dev libpng-dev libtiff-dev \
301     libfreetype6-dev libx11-dev libxi-dev wget libsqlite3-dev libbz2-dev libncurses5-dev \
302     libssl-dev liblzma-dev libreadline-dev libopenjpeg-dev libopenexr-dev libopenal-dev \
303     libglew-dev yasm libschroedinger-dev libtheora-dev libvorbis-dev libsdl1.2-dev \
304     libfftw3-dev libjack-dev python-dev patch
305
306   HASOPENJPEG=true
307   HASSCHRO=true
308
309   check_package_DEB libxvidcore-dev
310   if [ $? -eq 0 ]; then
311     sudo apt-get install -y libxvidcore-dev
312     HASXVID=true
313   fi
314
315   check_package_DEB libxvidcore4-dev
316   if [ $? -eq 0 ]; then
317     sudo apt-get install -y libxvidcore4-dev
318     HASXVID=true
319   fi
320
321   check_package_DEB libmp3lame-dev
322   if [ $? -eq 0 ]; then
323     sudo apt-get install -y libmp3lame-dev
324     HASMP3LAME=true
325   fi
326
327   check_package_DEB libx264-dev
328   if [ $? -eq 0 ]; then
329     sudo apt-get install -y libx264-dev
330     HASX264=true
331   fi
332
333   check_package_DEB libvpx-dev
334   if [ $? -eq 0 ]; then
335     sudo apt-get install -y libvpx-dev
336     vpx_version=`deb_version libvpx-dev`
337     if [ ! -z "$vpx_version" ]; then
338       if  dpkg --compare-versions $vpx_version gt 0.9.7; then
339         HASVPX=true
340       fi
341     fi
342   fi
343
344   check_package_DEB libspnav-dev
345   if [ $? -eq 0 ]; then
346     sudo apt-get install -y libspnav-dev
347   fi
348
349   check_package_DEB python3.3-dev
350   if [ $? -eq 0 ]; then
351     sudo apt-get install -y python3.3-dev
352   else
353     compile_Python
354   fi
355
356   check_package_DEB libboost-dev
357   if [ $? -eq 0 ]; then
358     sudo apt-get install -y libboost-dev
359
360     boost_version=`deb_version libboost-dev`
361
362     check_package_DEB libboost-locale$boost_version-dev
363     if [ $? -eq 0 ]; then
364       sudo apt-get install -y libboost-locale$boost_version-dev libboost-filesystem$boost_version-dev \
365         libboost-regex$boost_version-dev libboost-system$boost_version-dev libboost-thread$boost_version-dev
366     else
367       compile_Boost
368     fi
369   else
370     compile_Boost
371   fi
372
373   check_package_DEB libopencolorio-dev
374   if [ $? -eq 0 ]; then
375     sudo apt-get install -y libopencolorio-dev
376   else
377     compile_OCIO
378   fi
379
380   check_package_DEB libopenimageio-dev
381   if [ $? -eq 0 ]; then
382     sudo apt-get install -y libopenimageio-dev
383   else
384     compile_OIIO
385   fi
386
387 #  XXX Debian features libav packages as ffmpeg, those are not really compatible with blender code currently :/
388 #      So for now, always build our own ffmpeg.
389 #  check_package_DEB ffmpeg
390 #  if [ $? -eq 0 ]; then
391 #    sudo apt-get install -y ffmpeg
392 #    ffmpeg_version=`deb_version ffmpeg`
393 #    INFO "ffmpeg version: $ffmpeg_version"
394 #    if [ ! -z "$ffmpeg_version" ]; then
395 #      if  dpkg --compare-versions $ffmpeg_version gt 0.7.2; then
396 #        sudo apt-get install -y libavfilter-dev libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev
397 #      else
398 #        compile_FFmpeg
399 #      fi
400 #    fi
401 #  fi
402   compile_FFmpeg
403 }
404
405 check_package_RPM() {
406   r=`yum info $1 | grep -c 'Summary'`
407
408   if [ $r -ge 1 ]; then
409     return 0
410   else
411     return 1
412   fi
413 }
414
415 check_package_version_RPM() {
416   v=`yum info $1 | grep Version | tail -n 1 | sed -r 's/.*:\s+(([0-9]+\.?)+).*/\1/'`
417
418   # for now major and minor versions only (as if x.y, not x.y.z)
419   r=`echo $v | grep -c $2`
420
421   if [ $r -ge 1 ]; then
422     return 0
423   else
424     return 1
425   fi
426 }
427
428 install_RPM() {
429   INFO "Installing dependencies for RPM-based distributive"
430
431   sudo yum -y update
432
433   sudo yum -y install gcc gcc-c++ cmake scons libpng-devel libtiff-devel \
434     freetype-devel libX11-devel libXi-devel wget libsqlite3x-devel ncurses-devel \
435     readline-devel openjpeg-devel openexr-devel openal-soft-devel \
436     glew-devel yasm schroedinger-devel libtheora-devel libvorbis-devel SDL-devel \
437     fftw-devel lame-libs jack-audio-connection-kit-devel x264-devel libspnav-devel \
438     libjpeg-devel patch python-devel
439
440   HASOPENJPEG=true
441   HASSCHRO=true
442
443   check_package_version_RPM python-devel 3.3.
444   if [ $? -eq 0 ]; then
445     sudo yum install -y python-devel
446   else
447     compile_Python
448   fi
449
450   check_package_RPM boost-devel
451   if [ $? -eq 0 ]; then
452     sudo yum install -y boost-devel
453   else
454     compile_Boost
455   fi
456
457   check_package_RPM OpenColorIO-devel
458   if [ $? -eq 0 ]; then
459     sudo yum install -y OpenColorIO-devel
460   else
461     compile_OCIO
462   fi
463
464   check_package_RPM OpenImageIO-devel
465   if [ $? -eq 0 ]; then
466     sudo yum install -y OpenImageIO-devel
467   else
468     compile_OIIO
469   fi
470
471   # Always for now, not sure which packages should be installed
472   compile_FFmpeg
473 }
474
475 check_package_SUSE() {
476   r=`zypper info $1 | grep -c 'Summary'`
477
478   if [ $r -ge 1 ]; then
479     return 0
480   else
481     return 1
482   fi
483 }
484
485 check_package_version_SUSE() {
486   v=`zypper info $1 | grep Version | tail -n 1 | sed -r 's/.*:\s+(([0-9]+\.?)+).*/\1/'`
487
488   # for now major and minor versions only (as if x.y, not x.y.z)
489   r=`echo $v | grep -c $2`
490
491   if [ $r -ge 1 ]; then
492     return 0
493   else
494     return 1
495   fi
496 }
497
498 install_SUSE() {
499   INFO "Installing dependencies for SuSE-based distributive"
500
501   sudo zypper --non-interactive update --auto-agree-with-licenses
502
503   sudo zypper --non-interactive install --auto-agree-with-licenses \
504     gcc gcc-c++ libSDL-devel openal-soft-devel libpng12-devel libjpeg62-devel \
505     libtiff-devel OpenEXR-devel yasm libtheora-devel libvorbis-devel cmake \
506     scons patch
507
508   check_package_version_SUSE python3-devel 3.3.
509   if [ $? -eq 0 ]; then
510     sudo zypper --non-interactive install --auto-agree-with-licenses python3-devel
511   else
512     compile_Python
513   fi
514
515   # can not see boost_locale in repo, so let's build own boost
516   compile_Boost
517
518   # this libraries are also missing in the repo
519   compile_OCIO
520   compile_OIIO
521   compile_FFmpeg
522 }
523
524 print_info() {
525   INFO ""
526   INFO "If you're using CMake add this to your configuration flags:"
527
528   if [ -d /opt/lib/boost ]; then
529     INFO "  -D BOOST_ROOT=/opt/lib/boost"
530     INFO "  -D Boost_NO_SYSTEM_PATHS=ON"
531   fi
532
533   if [ -d /opt/lib/ffmpeg ]; then
534     INFO "  -D FFMPEG=/opt/lib/ffmpeg"
535   fi
536
537   INFO ""
538   INFO "If you're using SCons add this to your user-config:"
539
540   if [ -d /opt/lib/python3.3 ]; then
541     INFO "BF_PYTHON='/opt/lib/puthon-3.3'"
542     INFO "BF_PYTHON_ABI_FLAGS='m'"
543   fi
544
545   if [ -d /opt/lib/ocio ]; then
546     INFO "BF_OCIO='/opt/lib/ocio'"
547   fi
548
549   if [ -d /opt/lib/oiio ]; then
550     INFO "BF_OCIO='/opt/lib/oiio'"
551   fi
552
553   if [ -d /opt/lib/boost ]; then
554     INFO "BF_BOOST='/opt/lib/boost'"
555   fi
556
557   if [ -d /opt/lib/ffmpeg ]; then
558     INFO "BF_FFMPEG='/opt/lib/ffmpeg'"
559   fi
560 }
561
562 # Detect distributive type used on this machine
563 detect_distro
564
565 if [ -z "$DISTRO" ]; then
566   ERROR "Failed to detect distribytive type"
567   exit 1
568 elif [ "$DISTRO" = "DEB" ]; then
569   install_DEB
570 elif [ "$DISTRO" = "RPM" ]; then
571   install_RPM
572 elif [ "$DISTRO" = "SUSE" ]; then
573   install_SUSE
574 fi
575
576 print_info
577
578 # Switch back to user language.
579 LANG=LANG_BACK
580 export LANG