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