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