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