Can't stress how much I hate bash... This should fix a bug with version comparison...
[blender.git] / build_files / build_environment / install_deps.sh
1 #!/bin/bash
2
3 # Parse command line!
4 ARGS=$( \
5 getopt \
6 -o s:i:t:h \
7 --long source:,install:,threads:,help,with-osl,all-static,force-all,force-python,\
8 force-boost,force-ocio,force-oiio,force-llvm,force-osl,force-ffmpeg,\
9 skip-python,skip-boost,skip-ocio,skip-oiio,skip-llvm,skip-osl,skip-ffmpeg \
10 -- "$@" \
11 )
12
13 DISTRO=""
14 SRC="$HOME/src/blender-deps"
15 INST="/opt/lib"
16 CWD=$PWD
17
18 # Do not yet enable osl, use --build-osl option to try it.
19 BUILD_OSL=false
20
21 # Try to link everything statically. Use this to produce protable versions of blender.
22 ALL_STATIC=false
23
24 THREADS=`cat /proc/cpuinfo | grep cores | uniq | sed -e "s/.*: *\(.*\)/\\1/"`
25 if [ -z "$THREADS" ]; then
26   THREADS=1
27 fi
28
29 COMMON_INFO="\"Source code of dependencies needed to be compiled will be downloaded and extracted into '\$SRC'.
30 Built libs of dependencies needed to be compiled will be installed into '\$INST'.
31 Please edit \\\$SRC and/or \\\$INST variables at the beginning of this script,
32 or use --source/--install options, if you want to use other paths!
33
34 Number of threads for building: \$THREADS (automatically detected, use --threads=<nbr> to override it).
35 Building OSL: \$BUILD_OSL (use --with-osl option to enable it).
36 All static linking: \$ALL_STATIC (use --all-static option to enable it).
37
38 Use --help to show all available options!\""
39
40 ARGUMENTS_INFO="\"COMMAND LINE ARGUMENTS:
41     -h, --help
42         Show this message and exit.
43
44     -s <path>, --source=<path>
45         Use a specific path where to store downloaded libraries sources (defaults to '\$SRC').
46
47     -i <path>, --install=<path>
48         Use a specific path where to install built libraries (defaults to '\$INST').
49
50     -t n, --threads=n
51         Use a specific number of threads when building the libraries (auto-detected as '\$THREADS').
52
53     --with-osl
54         Try to install or build the OpenShadingLanguage libraries (and their dependencies).
55         Still experimental!
56
57     --all-static
58         Build libraries as statically as possible, to create static builds of Blender.
59
60     --force-all
61         Force the rebuild of all built libraries.
62
63     --force-python
64         Force the rebuild of Python.
65
66     --force-boost
67         Force the rebuild of Boost.
68
69     --force-ocio
70         Force the rebuild of OpenColorIO.
71
72     --force-oiio
73         Force the rebuild of OpenImageIO.
74
75     --force-llvm
76         Force the rebuild of LLVM.
77
78     --force-osl
79         Force the rebuild of OpenShadingLanguage.
80
81     --force-ffmpeg
82         Force the rebuild of FFMpeg.
83
84     Note about the --force-foo options:
85         * They obviously only have an effect if those libraries are built by this script
86           (i.e. if there is no available and satisfactory package)!
87         * If the “force-rebuilt” library is a dependency of others, it will force the rebuild
88           of those libraries too (e.g. --force-boost will also rebuild oiio and osl...).
89         * Do not forget --with-osl if you built it and still want it!
90
91     --skip-python
92         Unconditionally skip Python installation/building.
93
94     --skip-boost
95         Unconditionally skip Boost installation/building.
96
97     --skip-ocio
98         Unconditionally skip OpenColorIO installation/building.
99
100     --skip-oiio
101         Unconditionally skip OpenImageIO installation/building.
102
103     --skip-llvm
104         Unconditionally skip LLVM installation/building.
105
106     --skip-osl
107         Unconditionally skip OpenShadingLanguage installation/building.
108
109     --skip-ffmpeg
110         Unconditionally skip FFMpeg installation/building.\""
111
112 PYTHON_VERSION="3.3.0"
113 PYTHON_VERSION_MIN="3.3"
114 PYTHON_SOURCE="http://python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.bz2"
115 PYTHON_FORCE_REBUILD=false
116 PYTHON_SKIP=false
117
118 BOOST_VERSION="1.51.0"
119 _boost_version_nodots=`echo "$BOOST_VERSION" | sed -r 's/\./_/g'`
120 BOOST_SOURCE="http://sourceforge.net/projects/boost/files/boost/$BOOST_VERSION/boost_$_boost_version_nodots.tar.bz2/download"
121 BOOST_VERSION_MIN="1.49"
122 BOOST_FORCE_REBUILD=false
123 BOOST_SKIP=false
124
125 OCIO_VERSION="1.0.7"
126 OCIO_SOURCE="https://github.com/imageworks/OpenColorIO/tarball/v$OCIO_VERSION"
127 OCIO_VERSION_MIN="1.0"
128 OCIO_FORCE_REBUILD=false
129 OCIO_SKIP=false
130
131 OIIO_VERSION="1.1.1"
132 OIIO_SOURCE="https://github.com/OpenImageIO/oiio/tarball/Release-$OIIO_VERSION"
133 OIIO_VERSION_MIN="1.1"
134 OIIO_FORCE_REBUILD=false
135 OIIO_SKIP=false
136
137 LLVM_VERSION="3.1"
138 LLVM_VERSION_MIN="3.0"
139 LLVM_VERSION_FOUND=""
140 LLVM_SOURCE="http://llvm.org/releases/$LLVM_VERSION/llvm-$LLVM_VERSION.src.tar.gz"
141 LLVM_CLANG_SOURCE="http://llvm.org/releases/$LLVM_VERSION/clang-$LLVM_VERSION.src.tar.gz"
142 LLVM_FORCE_REBUILD=false
143 LLVM_SKIP=false
144
145 # OSL needs to be compiled for now!
146 OSL_VERSION="1.2.0"
147 OSL_SOURCE="https://github.com/mont29/OpenShadingLanguage/archive/blender-fixes.tar.gz"
148 OSL_FORCE_REBUILD=false
149 OSL_SKIP=false
150
151 FFMPEG_VERSION="1.0"
152 FFMPEG_SOURCE="http://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.bz2"
153 FFMPEG_VERSION_MIN="0.7.6"
154 FFMPEG_FORCE_REBUILD=false
155 FFMPEG_SKIP=false
156 _ffmpeg_list_sep=";"
157
158 # FFMPEG optional libs.
159 VORBIS_USE=false
160 VORBIS_DEV=""
161 SCHRO_USE=false
162 SCRHO_DEV=""
163 THEORA_USE=false
164 THEORA_DEV=""
165 XVID_USE=false
166 XVID_DEV=""
167 X264_USE=false
168 X264_DEV=""
169 X264_VERSION_MIN=0.118
170 VPX_USE=false
171 VPX_VERSION_MIN=0.9.7
172 VPX_DEV=""
173 MP3LAME_USE=false
174 MP3LAME_DEV=""
175 OPENJPEG_USE=false
176 OPENJPEG_DEV=""
177
178 # Switch to english language, else some things (like check_package_DEB()) won't work!
179 LANG_BACK=$LANG
180 LANG=""
181 export LANG
182
183
184 _echo() {
185   if [ "X$1" = "X-n" ]; then
186      shift; printf "%s" "$@"
187   else
188      printf "%s\n" "$@"
189   fi
190 }
191
192 ERROR() {
193   _echo "$@"
194 }
195
196 INFO() {
197   _echo "$@"
198 }
199
200 # Finish parsing the commandline args.
201 eval set -- "$ARGS"
202 while true; do
203   case $1 in
204     -s|--source)
205       SRC="$2"; shift; shift; continue
206     ;;
207     -i|--install)
208       INST="$2"; shift; shift; continue
209     ;;
210     -t|--threads)
211       THREADS="$2"; shift; shift; continue
212     ;;
213     -h|--help)
214       INFO ""
215       INFO "USAGE:"
216       INFO ""
217       INFO "`eval _echo "$COMMON_INFO"`"
218       INFO ""
219       INFO "`eval _echo "$ARGUMENTS_INFO"`"
220       INFO ""
221       exit 0
222     ;;
223     --with-osl)
224       BUILD_OSL=true; shift; continue
225     ;;
226     --all-static)
227       ALL_STATIC=true; shift; continue
228     ;;
229     --force-all)
230       PYTHON_FORCE_REBUILD=true
231       BOOST_FORCE_REBUILD=true
232       OCIO_FORCE_REBUILD=true
233       OIIO_FORCE_REBUILD=true
234       LLVM_FORCE_REBUILD=true
235       OSL_FORCE_REBUILD=true
236       FFMPEG_FORCE_REBUILD=true
237       shift; continue
238     ;;
239     --force-python)
240       PYTHON_FORCE_REBUILD=true; shift; continue
241     ;;
242     --force-boost)
243       BOOST_FORCE_REBUILD=true; shift; continue
244     ;;
245     --force-ocio)
246       OCIO_FORCE_REBUILD=true; shift; continue
247     ;;
248     --force-oiio)
249       OIIO_FORCE_REBUILD=true; shift; continue
250     ;;
251     --force-llvm)
252       LLVM_FORCE_REBUILD=true; shift; continue
253     ;;
254     --force-osl)
255       OSL_FORCE_REBUILD=true; shift; continue
256     ;;
257     --force-ffmpeg)
258       FFMPEG_FORCE_REBUILD=true; shift; continue
259     ;;
260     --skip-python)
261       PYTHON_SKIP=true; shift; continue
262     ;;
263     --skip-boost)
264       BOOST_SKIP=true; shift; continue
265     ;;
266     --skip-ocio)
267       OCIO_SKIP=true; shift; continue
268     ;;
269     --skip-oiio)
270       OIIO_SKIP=true; shift; continue
271     ;;
272     --skip-llvm)
273       LLVM_SKIP=true; shift; continue
274     ;;
275     --skip-osl)
276       OSL_SKIP=true; shift; continue
277     ;;
278     --skip-ffmpeg)
279       FFMPEG_SKIP=true; shift; continue
280     ;;
281     --)
282       # no more arguments to parse
283       break
284     ;;
285     *)
286       INFO ""
287       INFO "Wrong parameter! Usage:"
288       INFO ""
289       INFO "`eval _echo "$COMMON_INFO"`"
290       INFO ""
291       exit 1
292     ;;
293   esac
294 done
295
296 # Return 0 if $1 = $2 (i.e. 1.01.0 = 1.1, but 1.1.1 != 1.1), else 1.
297 # $1 and $2 should be version numbers made of numbers only.
298 version_eq() {
299   backIFS=$IFS
300   IFS='.'
301
302   # Split both version numbers into their numeric elements.
303   arr1=( $1 )
304   arr2=( $2 )
305
306   ret=1
307
308   count1=${#arr1[@]}
309   count2=${#arr2[@]}
310   if [ $count2 -ge $count1 ]; then
311     _t=$count1
312     count1=$count2
313     count2=$_t
314     arr1=( $2 )
315     arr2=( $1 )
316   fi
317
318   ret=0
319   for (( i=0; $i < $count2; i++ ))
320   do
321     if [ $(( 10#${arr1[$i]} )) -ne $(( 10#${arr2[$i]} )) ]; then
322       ret=1
323       break
324     fi
325   done
326
327   for (( i=$count2; $i < $count1; i++ ))
328   do
329     if [ $(( 10#${arr1[$i]} )) -ne 0 ]; then
330       ret=1
331       break
332     fi
333   done
334
335   IFS=$backIFS
336   return $ret
337 }
338
339 # Return 0 if $1 >= $2, else 1.
340 # $1 and $2 should be version numbers made of numbers only.
341 version_ge() {
342   version_eq $1 $2
343   if [ $? -eq 1 -a $(_echo "$1" "$2" | sort --version-sort | head --lines=1) = "$1" ]; then
344     return 1
345   else
346     return 0
347   fi
348 }
349
350 # Return 0 if $1 is into $2 (e.g. 3.3.2 is into 3.3, but not 3.3.0 or 3.3.5), else 1.
351 # $1 and $2 should be version numbers made of numbers only.
352 # $1 should be at least as long as $2!
353 version_match() {
354   backIFS=$IFS
355   IFS='.'
356
357   # Split both version numbers into their numeric elements.
358   arr1=( $1 )
359   arr2=( $2 )
360
361   ret=1
362
363   count1=${#arr1[@]}
364   count2=${#arr2[@]}
365   if [ $count1 -ge $count2 ]; then
366     ret=0
367     for (( i=0; $i < $count2; i++ ))
368     do
369       if [ $(( 10#${arr1[$i]} )) -ne $(( 10#${arr2[$i]} )) ]; then
370         ret=1
371         break
372       fi
373     done
374   fi
375
376   IFS=$backIFS
377   return $ret
378 }
379
380 detect_distro() {
381   if [ -f /etc/debian_version ]; then
382     DISTRO="DEB"
383   elif [ -f /etc/redhat-release ]; then
384     DISTRO="RPM"
385   elif [ -f /etc/SuSE-release ]; then
386     DISTRO="SUSE"
387   fi
388 }
389
390 prepare_opt() {
391   INFO "Ensuring $INST exists and is writable by us"
392   if [ ! -d  $INST ]; then
393     sudo mkdir -p $INST
394   fi
395
396   if [ ! -w $INST ]; then
397     sudo chown $USER $INST
398     sudo chmod 775 $INST
399   fi
400 }
401
402 # Check whether the current package needs to be recompiled, based on a dummy file containing a magic number in its name...
403 magic_compile_check() {
404   if [ -f $INST/.$1-magiccheck-$2 ]; then
405     return 0
406   else
407     return 1
408   fi
409 }
410
411 magic_compile_set() {
412   rm -f $INST/.$1-magiccheck-*
413   touch $INST/.$1-magiccheck-$2
414 }
415
416 compile_Python() {
417   # To be changed each time we make edits that would modify the compiled result!
418   py_magic=0
419
420   _src=$SRC/Python-$PYTHON_VERSION
421   _inst=$INST/python-$PYTHON_VERSION
422
423   # Clean install if needed!
424   magic_compile_check python-$PYTHON_VERSION $py_magic
425   if [ $? -eq 1 -o $PYTHON_FORCE_REBUILD == true ]; then
426     rm -rf $_inst
427   fi
428
429   if [ ! -d $_inst ]; then
430     INFO "Building Python-$PYTHON_VERSION"
431
432     prepare_opt
433
434     if [ ! -d $_src ]; then
435       mkdir -p $SRC
436       wget -c $PYTHON_SOURCE -O $_src.tar.bz2
437
438       INFO "Unpacking Python-$PYTHON_VERSION"
439       tar -C $SRC -xf $_src.tar.bz2
440     fi
441
442     cd $_src
443
444     ./configure --prefix=$_inst --enable-ipv6 \
445         --enable-loadable-sqlite-extensions --with-dbmliborder=bdb \
446         --with-computed-gotos --with-pymalloc
447
448     make -j$THREADS && make install
449     make clean
450
451     if [ -d $_inst ]; then
452       rm -f $INST/python-3.3
453       ln -s python-$PYTHON_VERSION $INST/python-3.3
454     else
455       ERROR "Python--$PYTHON_VERSION failed to compile, exiting"
456       exit 1
457     fi
458
459     magic_compile_set python-$PYTHON_VERSION $py_magic
460
461     cd $CWD
462     INFO "Done compiling Python-$PYTHON_VERSION!"
463   else
464     INFO "Own Python-$PYTHON_VERSION is up to date, nothing to do!"
465     INFO "If you want to force rebuild of this lib, use the --force-python option."
466   fi
467 }
468
469 compile_Boost() {
470   # To be changed each time we make edits that would modify the compiled result!
471   boost_magic=7
472
473   _src=$SRC/boost-$BOOST_VERSION
474   _inst=$INST/boost-$BOOST_VERSION
475
476   # Clean install if needed!
477   magic_compile_check boost-$BOOST_VERSION $boost_magic
478   if [ $? -eq 1 -o $BOOST_FORCE_REBUILD == true ]; then
479     rm -rf $_inst
480   fi
481
482   if [ ! -d $_inst ]; then
483     INFO "Building Boost-$BOOST_VERSION"
484
485     prepare_opt
486
487     if [ ! -d $_src ]; then
488       INFO "Downloading Boost-$BOOST_VERSION"
489       mkdir -p $SRC
490       wget -c $BOOST_SOURCE -O $_src.tar.bz2
491       tar -C $SRC --transform "s,(.*/?)boost_1_[^/]+(.*),\1boost-$BOOST_VERSION\2,x" -xf $_src.tar.bz2
492     fi
493
494     cd $_src
495     if [ ! -f $_src/b2 ]; then
496       ./bootstrap.sh
497     fi
498     ./b2 -j$THREADS -a --with-system --with-filesystem --with-thread --with-regex --with-locale --with-date_time \
499          --prefix=$_inst --disable-icu boost.locale.icu=off install
500     ./b2 --clean
501
502     if [ -d $_inst ]; then
503       rm -f $INST/boost
504       ln -s boost-$BOOST_VERSION $INST/boost
505     else
506       ERROR "Boost-$BOOST_VERSION failed to compile, exiting"
507       exit 1
508     fi
509
510     magic_compile_set boost-$BOOST_VERSION $boost_magic
511
512     # Rebuild dependecies as well!
513     OIIO_FORCE_REBUILD=true
514     OSL_FORCE_REBUILD=true
515
516     cd $CWD
517     INFO "Done compiling Boost-$BOOST_VERSION!"
518   else
519     INFO "Own Boost-$BOOST_VERSION is up to date, nothing to do!"
520     INFO "If you want to force rebuild of this lib, use the --force-boost option."
521   fi
522 }
523
524 compile_OCIO() {
525   # To be changed each time we make edits that would modify the compiled result!
526   ocio_magic=1
527
528   _src=$SRC/OpenColorIO-$OCIO_VERSION
529   _inst=$INST/ocio-$OCIO_VERSION
530
531   # Clean install if needed!
532   magic_compile_check ocio-$OCIO_VERSION $ocio_magic
533   if [ $? -eq 1 -o $OCIO_FORCE_REBUILD == true ]; then
534     rm -rf $_inst
535   fi
536
537   if [ ! -d $_inst ]; then
538     INFO "Building OpenColorIO-$OCIO_VERSION"
539
540     prepare_opt
541
542     if [ ! -d $_src ]; then
543       INFO "Downloading OpenColorIO-$OCIO_VERSION"
544       mkdir -p $SRC
545       wget -c $OCIO_SOURCE -O $_src.tar.gz
546
547       INFO "Unpacking OpenColorIO-$OCIO_VERSION"
548       tar -C $SRC --transform "s,(.*/?)imageworks-OpenColorIO[^/]*(.*),\1OpenColorIO-$OCIO_VERSION\2,x" \
549           -xf $_src.tar.gz
550     fi
551
552     cd $_src
553     # Always refresh the whole build!
554     if [ -d build ]; then
555       rm -rf build
556     fi    
557     mkdir build
558     cd build
559
560     if file /bin/cp | grep -q '32-bit'; then
561       cflags="-fPIC -m32 -march=i686"
562     else
563       cflags="-fPIC"
564     fi
565
566     cmake -D CMAKE_BUILD_TYPE=Release \
567           -D CMAKE_PREFIX_PATH=$_inst \
568           -D CMAKE_INSTALL_PREFIX=$_inst \
569           -D CMAKE_CXX_FLAGS="$cflags" \
570           -D CMAKE_EXE_LINKER_FLAGS="-lgcc_s -lgcc" \
571           -D OCIO_BUILD_APPS=OFF \
572           -D OCIO_BUILD_PYGLUE=OFF \
573           ..
574
575     make -j$THREADS && make install
576
577     # Force linking against static libs
578     rm -f $_inst/lib/*.so*
579
580     # Additional depencencies
581     cp ext/dist/lib/libtinyxml.a $_inst/lib
582     cp ext/dist/lib/libyaml-cpp.a $_inst/lib
583
584     make clean
585
586     if [ -d $_inst ]; then
587       rm -f $INST/ocio
588       ln -s ocio-$OCIO_VERSION $INST/ocio
589     else
590       ERROR "OpenColorIO-$OCIO_VERSION failed to compile, exiting"
591       exit 1
592     fi
593
594     magic_compile_set ocio-$OCIO_VERSION $ocio_magic
595
596     cd $CWD
597     INFO "Done compiling OpenColorIO-$OCIO_VERSION!"
598   else
599     INFO "Own OpenColorIO-$OCIO_VERSION is up to date, nothing to do!"
600     INFO "If you want to force rebuild of this lib, use the --force-ocio option."
601   fi
602 }
603
604 compile_OIIO() {
605   # To be changed each time we make edits that would modify the compiled result!
606   oiio_magic=6
607
608   _src=$SRC/OpenImageIO-$OIIO_VERSION
609   _inst=$INST/oiio-$OIIO_VERSION
610
611   # Clean install if needed!
612   magic_compile_check oiio-$OIIO_VERSION $oiio_magic
613   if [ $? -eq 1 -o $OIIO_FORCE_REBUILD == true ]; then
614     rm -rf $_inst
615   fi
616
617   if [ ! -d $_inst ]; then
618     INFO "Building OpenImageIO-$OIIO_VERSION"
619
620     prepare_opt
621
622     if [ ! -d $_src ]; then
623       mkdir -p $SRC
624       wget -c $OIIO_SOURCE -O "$_src.tar.gz"
625
626       INFO "Unpacking OpenImageIO-$OIIO_VERSION"
627       tar -C $SRC --transform "s,(.*/?)OpenImageIO-oiio[^/]*(.*),\1OpenImageIO-$OIIO_VERSION\2,x" \
628           -xf $_src.tar.gz
629
630       cd $_src
631
632       # XXX Ugly patching hack!
633       cat << EOF | patch -p1
634 diff --git a/src/libutil/SHA1.cpp b/src/libutil/SHA1.cpp
635 index b9e6c8b..c761185 100644
636 --- a/src/libutil/SHA1.cpp
637 +++ b/src/libutil/SHA1.cpp
638 @@ -8,9 +8,9 @@
639  
640  // If compiling with MFC, you might want to add #include "StdAfx.h"
641  
642 +#include "SHA1.h"
643  #include "hash.h"
644  #include "dassert.h"
645 -#include "SHA1.h"
646  
647  #ifdef SHA1_UTILITY_FUNCTIONS
648  #define SHA1_MAX_FILE_BUFFER 8000
649 EOF
650
651       cd $CWD
652
653     fi
654
655     cd $_src
656     # Always refresh the whole build!
657     if [ -d build ]; then
658       rm -rf build
659     fi    
660     mkdir build
661     cd build
662
663     cmake_d="-D CMAKE_BUILD_TYPE=Release"
664     cmake_d="$cmake_d -D CMAKE_PREFIX_PATH=$_inst"
665     cmake_d="$cmake_d -D CMAKE_INSTALL_PREFIX=$_inst"
666     cmake_d="$cmake_d -D BUILDSTATIC=ON"
667
668     # linking statically could give issues on Debian/Ubuntu (and probably other distros
669     # which doesn't like static linking) when linking shared oiio library due to missing
670     # text symbols (static libs should be compiled with -fPIC)
671     # cmake_d="$cmake_d -D LINKSTATIC=ON"
672
673     if [ -d $INST/boost ]; then
674       cmake_d="$cmake_d -D BOOST_ROOT=$INST/boost -D Boost_NO_SYSTEM_PATHS=ON"
675       if $ALL_STATIC; then
676         cmake_d="$cmake_d -D Boost_USE_STATIC_LIBS=ON"
677       fi
678     fi
679
680     # Looks like we do not need ocio in oiio for now...
681 #    if [ -d $INST/ocio ]; then
682 #      cmake_d="$cmake_d -D OCIO_PATH=$INST/ocio"
683 #    fi
684
685     if file /bin/cp | grep -q '32-bit'; then
686       cflags="-fPIC -m32 -march=i686"
687     else
688       cflags="-fPIC"
689     fi
690
691     cmake $cmake_d -D CMAKE_CXX_FLAGS="$cflags" -D CMAKE_EXE_LINKER_FLAGS="-lgcc_s -lgcc" ../src
692
693     make -j$THREADS && make install
694     make clean
695
696     if [ -d $_inst ]; then
697       rm -f $INST/oiio
698       ln -s oiio-$OIIO_VERSION $INST/oiio
699     else
700       ERROR "OpenImageIO-$OIIO_VERSION failed to compile, exiting"
701       exit 1
702     fi
703
704     magic_compile_set oiio-$OIIO_VERSION $oiio_magic
705
706     # Rebuild dependecies as well!
707     OSL_FORCE_REBUILD=true
708
709     cd $CWD
710     INFO "Done compiling OpenImageIO-$OIIO_VERSION!"
711   else
712     INFO "Own OpenImageIO-$OIIO_VERSION is up to date, nothing to do!"
713     INFO "If you want to force rebuild of this lib, use the --force-oiio option."
714   fi
715 }
716
717 compile_LLVM() {
718   # To be changed each time we make edits that would modify the compiled result!
719   llvm_magic=1
720
721   _src=$SRC/LLVM-$LLVM_VERSION
722   _inst=$INST/llvm-$LLVM_VERSION
723   _src_clang=$SRC/CLANG-$LLVM_VERSION
724
725   # Clean install if needed!
726   magic_compile_check llvm-$LLVM_VERSION $llvm_magic
727   if [ $? -eq 1 -o $LLVM_FORCE_REBUILD == true ]; then
728     rm -rf $_inst
729     rm -rf $_inst_clang
730   fi
731
732   if [ ! -d $_inst ]; then
733     INFO "Building LLVM-$LLVM_VERSION (CLANG included!)"
734
735     prepare_opt
736
737     if [ ! -d $_src -o true ]; then
738       mkdir -p $SRC
739       wget -c $LLVM_SOURCE -O "$_src.tar.gz"
740       wget -c $LLVM_CLANG_SOURCE -O "$_src_clang.tar.gz"
741
742       INFO "Unpacking LLVM-$LLVM_VERSION"
743       tar -C $SRC --transform "s,([^/]*/?)llvm-[^/]*(.*),\1LLVM-$LLVM_VERSION\2,x" \
744           -xf $_src.tar.gz
745       INFO "Unpacking CLANG-$LLVM_VERSION to $_src/tools/clang"
746       tar -C $_src/tools \
747           --transform "s,([^/]*/?)clang-[^/]*(.*),\1clang\2,x" \
748           -xf $_src_clang.tar.gz
749
750       cd $_src
751
752       # XXX Ugly patching hack!
753       cat << EOF | patch -p1
754 --- a/CMakeLists.txt
755 +++ b/CMakeLists.txt
756 @@ -13,7 +13,7 @@
757  set(LLVM_VERSION_MAJOR 3)
758  set(LLVM_VERSION_MINOR 1)
759  
760 -set(PACKAGE_VERSION "\${LLVM_VERSION_MAJOR}.\${LLVM_VERSION_MINOR}svn")
761 +set(PACKAGE_VERSION "\${LLVM_VERSION_MAJOR}.\${LLVM_VERSION_MINOR}")
762  
763  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
764  
765 EOF
766
767       cd $CWD
768
769     fi
770
771     cd $_src
772
773     # Always refresh the whole build!
774     if [ -d build ]; then
775       rm -rf build
776     fi    
777     mkdir build
778     cd build
779
780     cmake_d="-D CMAKE_BUILD_TYPE=Release"
781     cmake_d="$cmake_d -D CMAKE_INSTALL_PREFIX=$_inst"
782     cmake_d="$cmake_d -D LLVM_ENABLE_FFI=ON"
783
784     if [ -d $_FFI_INCLUDE_DIR ]; then
785       cmake_d="$cmake_d -D FFI_INCLUDE_DIR=$_FFI_INCLUDE_DIR"
786     fi
787
788     cmake $cmake_d ..
789
790     make -j$THREADS && make install
791     make clean
792
793     if [ -d $_inst ]; then
794       rm -f $INST/llvm
795       ln -s llvm-$LLVM_VERSION $INST/llvm
796     else
797       ERROR "LLVM-$LLVM_VERSION failed to compile, exiting"
798       exit 1
799     fi
800
801     magic_compile_set llvm-$LLVM_VERSION $llvm_magic
802
803     # Rebuild dependecies as well!
804     OSL_FORCE_REBUILD=true
805
806     cd $CWD
807     INFO "Done compiling LLVM-$LLVM_VERSION (CLANG included)!"
808   else
809     INFO "Own LLVM-$LLVM_VERSION (CLANG included) is up to date, nothing to do!"
810     INFO "If you want to force rebuild of this lib, use the --force-llvm option."
811   fi
812 }
813
814 compile_OSL() {
815   # To be changed each time we make edits that would modify the compiled result!
816   osl_magic=7
817
818   _src=$SRC/OpenShadingLanguage-$OSL_VERSION
819   _inst=$INST/osl-$OSL_VERSION
820
821   # Clean install if needed!
822   magic_compile_check osl-$OSL_VERSION $osl_magic
823   if [ $? -eq 1 -o $OSL_FORCE_REBUILD == true ]; then
824     rm -rf $_inst
825   fi
826
827   if [ ! -d $_inst ]; then
828     INFO "Building OpenShadingLanguage-$OSL_VERSION"
829
830     prepare_opt
831
832     if [ ! -d $_src ]; then
833       mkdir -p $SRC
834
835       # XXX Using git on my own repo for now, looks like archives are not updated immediately... :/
836 #      wget -c $OSL_SOURCE -O "$_src.tar.gz"
837
838 #      INFO "Unpacking OpenShadingLanguage-$OSL_VERSION"
839 #      tar -C $SRC --transform "s,(.*/?)OpenShadingLanguage-[^/]*(.*),\1OpenShadingLanguage-$OSL_VERSION\2,x" \
840 #          -xf $_src.tar.gz
841       git clone https://github.com/mont29/OpenShadingLanguage.git $_src
842       cd $_src
843       git checkout blender-fixes
844       cd $CWD
845     fi
846
847     cd $_src
848     # XXX For now, always update from latest repo...
849     git pull origin
850
851     # Always refresh the whole build!
852     if [ -d build ]; then
853       rm -rf build
854     fi    
855     mkdir build
856     cd build
857
858     cmake_d="-D CMAKE_BUILD_TYPE=Release"
859     cmake_d="$cmake_d -D CMAKE_INSTALL_PREFIX=$_inst"
860     cmake_d="$cmake_d -D BUILDSTATIC=ON"
861     cmake_d="$cmake_d -D BUILD_TESTING=OFF"
862
863     if [ -d $INST/boost ]; then
864       cmake_d="$cmake_d -D BOOST_ROOT=$INST/boost -D Boost_NO_SYSTEM_PATHS=ON"
865       if $ALL_STATIC; then
866         cmake_d="$cmake_d -D Boost_USE_STATIC_LIBS=ON"        
867       fi
868     fi
869
870     if [ -d $INST/oiio ]; then
871       cmake_d="$cmake_d -D OPENIMAGEIOHOME=$INST/oiio"
872     fi
873
874     if [ ! -z $LLVM_VERSION_FOUND ]; then
875       cmake_d="$cmake_d -D LLVM_VERSION=$LLVM_VERSION_FOUND"
876       if [ -d $INST/llvm ]; then
877         cmake_d="$cmake_d -D LLVM_DIRECTORY=$INST/llvm"
878         cmake_d="$cmake_d -D LLVM_STATIC=ON"
879       fi
880     fi
881
882     cmake $cmake_d ../src
883
884     make -j$THREADS && make install
885     make clean
886
887     if [ -d $_inst ]; then
888       rm -f $INST/osl
889       ln -s osl-$OSL_VERSION $INST/osl
890     else
891       ERROR "OpenShadingLanguage-$OSL_VERSION failed to compile, exiting"
892       exit 1
893     fi
894
895     magic_compile_set osl-$OSL_VERSION $osl_magic
896
897     cd $CWD
898     INFO "Done compiling OpenShadingLanguage-$OSL_VERSION!"
899   else
900     INFO "Own OpenShadingLanguage-$OSL_VERSION is up to date, nothing to do!"
901     INFO "If you want to force rebuild of this lib, use the --force-osl option."
902   fi
903 }
904
905 compile_FFmpeg() {
906   # To be changed each time we make edits that would modify the compiled result!
907   ffmpeg_magic=3
908
909   _src=$SRC/ffmpeg-$FFMPEG_VERSION
910   _inst=$INST/ffmpeg-$FFMPEG_VERSION
911
912   # Clean install if needed!
913   magic_compile_check ffmpeg-$FFMPEG_VERSION $ffmpeg_magic
914   if [ $? -eq 1 -o $FFMPEG_FORCE_REBUILD == true ]; then
915     rm -rf $_inst
916   fi
917
918   if [ ! -d $_inst ]; then
919     INFO "Building ffmpeg-$FFMPEG_VERSION"
920
921     prepare_opt
922
923     if [ ! -d $_src ]; then
924       INFO "Downloading ffmpeg-$FFMPEG_VERSION"
925       mkdir -p $SRC
926       wget -c $FFMPEG_SOURCE -O "$_src.tar.bz2"
927
928       INFO "Unpacking ffmpeg-$FFMPEG_VERSION"
929       tar -C $SRC -xf $_src.tar.bz2
930     fi
931
932     cd $_src
933
934     extra=""
935
936     if $VORBIS_USE; then
937       extra="$extra --enable-libvorbis"
938     fi
939
940     if $THEORA_USE; then
941       extra="$extra --enable-libtheora"
942     fi
943
944     # XXX At least under Debian, static schro gives problem at blender linking time... :/
945     if $SCHRO_USE && ! $ALL_STATIC; then
946       extra="$extra --enable-libschroedinger"
947     fi
948
949     if $XVID_USE; then
950       extra="$extra --enable-libxvid"
951     fi
952
953     if $X264_USE; then
954       extra="$extra --enable-libx264"
955     fi
956
957     if $VPX_USE; then
958       extra="$extra --enable-libvpx"
959     fi
960
961     if $MP3LAME_USE; then
962       extra="$extra --enable-libmp3lame"
963     fi
964
965     if $OPENJPEG_USE; then
966       extra="$extra --enable-libopenjpeg"
967     fi
968
969     ./configure --cc="gcc -Wl,--as-needed" \
970         --extra-ldflags="-pthread -static-libgcc" \
971         --prefix=$_inst --enable-static \
972         --disable-ffplay --disable-ffserver --disable-doc \
973         --enable-gray \
974         --enable-avfilter --disable-vdpau \
975         --disable-bzlib --disable-libgsm --disable-libspeex \
976         --enable-pthreads --enable-zlib --enable-stripping --enable-runtime-cpudetect \
977         --disable-vaapi  --disable-libfaac --disable-nonfree --enable-gpl \
978         --disable-postproc --disable-x11grab  --disable-librtmp  --disable-libopencore-amrnb \
979         --disable-libopencore-amrwb --disable-libdc1394 --disable-version3  --disable-outdev=sdl \
980         --disable-outdev=alsa --disable-indev=sdl --disable-indev=alsa --disable-indev=jack \
981         --disable-indev=lavfi $extra
982
983     make -j$THREADS && make install
984     make clean
985
986     if [ -d $_inst ]; then
987       rm -f $INST/ffmpeg
988       ln -s ffmpeg-$FFMPEG_VERSION $INST/ffmpeg
989     else
990       ERROR "FFmpeg-$FFMPEG_VERSION failed to compile, exiting"
991       exit 1
992     fi
993
994     magic_compile_set ffmpeg-$FFMPEG_VERSION $ffmpeg_magic
995
996     cd $CWD
997     INFO "Done compiling ffmpeg-$FFMPEG_VERSION!"
998   else
999     INFO "Own ffmpeg-$FFMPEG_VERSION is up to date, nothing to do!"
1000     INFO "If you want to force rebuild of this lib, use the --force-ffmpeg option."
1001   fi
1002 }
1003
1004 get_package_version_DEB() {
1005     dpkg-query -W -f '${Version}' $1 | sed -r 's/.*:\s*([0-9]+:)(([0-9]+\.?)+).*/\2/'
1006 }
1007
1008 check_package_DEB() {
1009   r=`apt-cache policy $1 | grep -c 'Candidate:'`
1010
1011   if [ $r -ge 1 ]; then
1012     return 0
1013   else
1014     return 1
1015   fi
1016 }
1017
1018 check_package_version_match_DEB() {
1019   v=`apt-cache policy $1 | grep 'Candidate:' | sed -r 's/.*:\s*([0-9]+:)(([0-9]+\.?)+).*/\2/'`
1020
1021   if [ -z "$v" ]; then
1022     return 1
1023   fi
1024
1025   version_match $v $2
1026   return $?
1027 }
1028
1029 check_package_version_ge_DEB() {
1030   v=`apt-cache policy $1 | grep 'Candidate:' | sed -r 's/.*:\s*([0-9]+:)?(([0-9]+\.?)+).*/\2/'`
1031
1032   if [ -z "$v" ]; then
1033     return 1
1034   fi
1035
1036   version_ge $v $2
1037   return $?
1038 }
1039
1040 install_packages_DEB() {
1041   sudo apt-get install -y $@
1042   if [ $? -ge 1 ]; then
1043     ERROR "apt-get failed to install requested packages, exiting."
1044     exit 1
1045   fi
1046 }
1047
1048 install_DEB() {
1049   INFO ""
1050   INFO "Installing dependencies for DEB-based distribution"
1051   INFO ""
1052   INFO "`eval _echo "$COMMON_INFO"`"
1053   INFO ""
1054
1055   if [ ! -z "`cat /etc/debian_version | grep ^6`"  ]; then
1056     if [ -z "`cat /etc/apt/sources.list | grep backports.debian.org`"  ]; then
1057       INFO "Looks like you're using Debian Squeeze which does have broken CMake"
1058       INFO "It is highly recommended to install cmake from backports, otherwise"
1059       INFO "compilation of some libraries could fail"
1060       INFO ""
1061       INFO "You could install newer CMake from debian-backports repository"
1062       INFO "Add this this line to your /etc/apt/sources.lixt:"
1063       INFO ""
1064       INFO "deb http://backports.debian.org/debian-backports squeeze-backports main"
1065       INFO ""
1066       INFO "and then run:"
1067       INFO ""
1068       INFO "sudo apt-get update && sudo apt-get install cmake=2.8.7-4~bpo60+1 sudo apt-get install cmake=2.8.7-4~bpo60+1"
1069       INFO ""
1070       INFO "(you could also add this reporisotry using GUI like synaptic)"
1071       INFO ""
1072       INFO "Hit Enter to continue running the script, or hit Ctrl-C to abort the script"
1073
1074       read
1075       INFO ""
1076     fi
1077   fi
1078
1079   sudo apt-get update
1080 # XXX Why in hell? Let's let this stuff to the user's responsability!!!
1081 #  sudo apt-get -y upgrade
1082
1083   # These libs should always be available in debian/ubuntu official repository...
1084   OPENJPEG_DEV="libopenjpeg-dev"
1085   SCHRO_DEV="libschroedinger-dev"
1086   VORBIS_DEV="libvorbis-dev"
1087   THEORA_DEV="libtheora-dev"
1088
1089   INFO ""
1090   install_packages_DEB gawk cmake scons gcc g++ libjpeg-dev libpng-dev libtiff-dev \
1091     libfreetype6-dev libx11-dev libxi-dev wget libsqlite3-dev libbz2-dev libncurses5-dev \
1092     libssl-dev liblzma-dev libreadline-dev $OPENJPEG_DEV libopenexr-dev libopenal-dev \
1093     libglew-dev yasm $SCHRO_DEV $THEORA_DEV $VORBIS_DEV libsdl1.2-dev \
1094     libfftw3-dev libjack0 libjack-dev python-dev patch bzip2
1095
1096   OPENJPEG_USE=true
1097   SCHRO_USE=true
1098   VORBIS_USE=true
1099   THEORA_USE=true
1100
1101   INFO ""
1102   # Grmpf, debian is libxvidcore-dev and ubuntu libxvidcore4-dev!
1103   XVID_DEV="libxvidcore-dev"
1104   check_package_DEB $XVID_DEV
1105   if [ $? -eq 0 ]; then
1106     install_packages_DEB $XVID_DEV
1107     XVID_USE=true
1108   else
1109     XVID_DEV="libxvidcore4-dev"
1110     check_package_DEB $XVID_DEV
1111     if [ $? -eq 0 ]; then
1112       install_packages_DEB $XVID_DEV
1113       XVID_USE=true
1114     fi
1115   fi
1116
1117   INFO ""
1118   MP3LAME_DEV="libmp3lame-dev"
1119   check_package_DEB $MP3LAME_DEV
1120   if [ $? -eq 0 ]; then
1121     install_packages_DEB $MP3LAME_DEV
1122     MP3LAME_USE=true
1123   fi
1124
1125   INFO ""
1126   X264_DEV="libx264-dev"
1127   check_package_version_ge_DEB $X264_DEV $X264_VERSION_MIN
1128   if [ $? -eq 0 ]; then
1129     install_packages_DEB $X264_DEV
1130     X264_USE=true
1131   fi
1132
1133   INFO ""
1134   VPX_DEV="libvpx-dev"
1135   check_package_version_ge_DEB $VPX_DEV $VPX_VERSION_MIN
1136   if [ $? -eq 0 ]; then
1137     install_packages_DEB $VPX_DEV
1138     VPX_USE=true
1139   fi
1140
1141   INFO ""
1142   check_package_DEB libspnav-dev
1143   if [ $? -eq 0 ]; then
1144     install_packages_DEB libspnav-dev
1145   fi
1146
1147   INFO ""
1148   if $PYTHON_SKIP; then
1149     INFO "WARNING! Skipping Python installation, as requested..."
1150   else
1151     check_package_DEB python3.3-dev
1152     if [ $? -eq 0 ]; then
1153       install_packages_DEB python3.3-dev
1154     else
1155       compile_Python
1156     fi
1157   fi
1158
1159   INFO ""
1160   if $BOOST_SKIP; then
1161     INFO "WARNING! Skipping Boost installation, as requested..."
1162   else
1163     check_package_version_ge_DEB libboost-dev $BOOST_VERSION_MIN
1164     if [ $? -eq 0 ]; then
1165       install_packages_DEB libboost-dev
1166
1167       boost_version=$(echo `get_package_version_DEB libboost-dev` | sed -r 's/^([0-9]+\.[0-9]+).*/\1/')
1168
1169       check_package_DEB libboost-locale$boost_version-dev
1170       if [ $? -eq 0 ]; then
1171         install_packages_DEB libboost-locale$boost_version-dev libboost-filesystem$boost_version-dev \
1172                              libboost-regex$boost_version-dev libboost-system$boost_version-dev \
1173                              libboost-thread$boost_version-dev
1174       else
1175         compile_Boost
1176       fi
1177     else
1178       compile_Boost
1179     fi
1180   fi
1181
1182   INFO ""
1183   if $OCIO_SKIP; then
1184     INFO "WARNING! Skipping OpenColorIO installation, as requested..."
1185   else
1186     check_package_version_ge_DEB libopencolorio-dev $OCIO_VERSION_MIN
1187     if [ $? -eq 0 ]; then
1188       install_packages_DEB libopencolorio-dev
1189     else
1190       compile_OCIO
1191     fi
1192   fi
1193
1194   INFO ""
1195   if $OIIO_SKIP; then
1196     INFO "WARNING! Skipping OpenImageIO installation, as requested..."
1197   else
1198     check_package_version_ge_DEB libopenimageio-dev $OIIO_VERSION_MIN
1199     if [ $? -eq 0 ]; then
1200       install_packages_DEB libopenimageio-dev
1201     else
1202       compile_OIIO
1203     fi
1204   fi
1205
1206   if $BUILD_OSL; then
1207     have_llvm=false
1208
1209     if $LLVM_SKIP; then
1210       INFO "WARNING! Skipping LLVM installation, as requested (this also implies skipping OSL!)..."
1211     else
1212       INFO ""
1213       check_package_DEB llvm-$LLVM_VERSION-dev
1214       if [ $? -eq 0 ]; then
1215         install_packages_DEB llvm-$LLVM_VERSION-dev
1216         have_llvm=true
1217         LLVM_VERSION_FOUND=$LLVM_VERSION
1218       else
1219         check_package_DEB llvm-$LLVM_VERSION_MIN-dev
1220         if [ $? -eq 0 ]; then
1221           install_packages_DEB llvm-$LLVM_VERSION_MIN-dev
1222           have_llvm=true
1223           LLVM_VERSION_FOUND=$LLVM_VERSION_MIN
1224         else
1225           install_packages_DEB libffi-dev
1226           INFO ""
1227           compile_LLVM
1228           have_llvm=true
1229           LLVM_VERSION_FOUND=$LLVM_VERSION
1230         fi
1231       fi
1232     fi
1233
1234     if $OSL_SKIP; then
1235       INFO ""
1236       INFO "WARNING! Skipping OpenShadingLanguage installation, as requested..."
1237     else
1238       if $have_llvm; then
1239         INFO ""
1240         install_packages_DEB clang flex bison libtbb-dev git
1241         # No package currently!
1242         INFO ""
1243         compile_OSL
1244       fi
1245     fi
1246   fi
1247
1248
1249   INFO ""
1250   if $FFMPEG_SKIP; then
1251     INFO "WARNING! Skipping FFMpeg installation, as requested..."
1252   else
1253 #    XXX Debian features libav packages as ffmpeg, those are not really compatible with blender code currently :/
1254 #        So for now, always build our own ffmpeg.
1255 #    check_package_DEB ffmpeg
1256 #    if [ $? -eq 0 ]; then
1257 #      install_packages_DEB ffmpeg
1258 #      ffmpeg_version=`get_package_version_DEB ffmpeg`
1259 #      INFO "ffmpeg version: $ffmpeg_version"
1260 #      if [ ! -z "$ffmpeg_version" ]; then
1261 #        if  dpkg --compare-versions $ffmpeg_version gt 0.7.2; then
1262 #          install_packages_DEB libavfilter-dev libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev
1263 #        else
1264 #          compile_FFmpeg
1265 #        fi
1266 #      fi
1267 #    fi
1268     compile_FFmpeg
1269   fi
1270 }
1271
1272 get_package_version_RPM() {
1273   yum info $1 | grep Version | tail -n 1 | sed -r 's/.*:\s+(([0-9]+\.?)+).*/\1/'
1274 }
1275
1276 check_package_RPM() {
1277   r=`yum info $1 | grep -c 'Summary'`
1278
1279   if [ $r -ge 1 ]; then
1280     return 0
1281   else
1282     return 1
1283   fi
1284 }
1285
1286 check_package_version_match_RPM() {
1287   v=`get_package_version_RPM $1`
1288
1289   if [ -z "$v" ]; then
1290     return 1
1291   fi
1292
1293   version_match $v $2
1294   return $?
1295 }
1296
1297 check_package_version_ge_RPM() {
1298   v=`get_package_version_RPM $1`
1299
1300   if [ -z "$v" ]; then
1301     return 1
1302   fi
1303
1304   version_ge $v $2
1305   return $?
1306 }
1307
1308 install_packages_RPM() {
1309   sudo yum install -y $@
1310   if [ $? -ge 1 ]; then
1311     ERROR "yum failed to install requested packages, exiting."
1312     exit 1
1313   fi
1314 }
1315
1316 install_RPM() {
1317   INFO ""
1318   INFO "Installing dependencies for RPM-based distribution"
1319   INFO ""
1320   INFO "`eval _echo "$COMMON_INFO"`"
1321   INFO ""
1322
1323   sudo yum -y update
1324
1325   # These libs should always be available in debian/ubuntu official repository...
1326   OPENJPEG_DEV="openjpeg-devel"
1327   SCHRO_DEV="schroedinger-devel"
1328   VORBIS_DEV="libvorbis-devel"
1329   THEORA_DEV="libtheora-devel"
1330
1331   INFO ""
1332   install_packages_RPM gawk gcc gcc-c++ cmake scons libpng-devel libtiff-devel \
1333     freetype-devel libX11-devel libXi-devel wget libsqlite3x-devel ncurses-devel \
1334     readline-devel $OPENJPEG_DEV openexr-devel openal-soft-devel \
1335     glew-devel yasm $SCHRO_DEV $THEORA_DEV $VORBIS_DEV SDL-devel \
1336     fftw-devel lame-libs jack-audio-connection-kit-devel libspnav-devel \
1337     libjpeg-devel patch python-devel
1338
1339   OPENJPEG_USE=true
1340   SCHRO_USE=true
1341   VORBIS_USE=true
1342   THEORA_USE=true
1343
1344   INFO ""
1345   X264_DEV="x264-devel"
1346   check_package_version_ge_RPM $X264_DEV $X264_VERSION_MIN
1347   if [ $? -eq 0 ]; then
1348     install_packages_RPM $X264_DEV
1349     X264_USE=true
1350   fi
1351
1352   INFO ""
1353   XVID_DEV="xvidcore-devel"
1354   check_package_RPM $XVID_DEV
1355   if [ $? -eq 0 ]; then
1356     install_packages_RPM $XVID_DEV
1357     XVID_USE=true
1358   fi
1359
1360   INFO ""
1361   VPX_DEV="libvpx-devel"
1362   check_package_version_ge_RPM $VPX_DEV $VPX_VERSION_MIN
1363   if [ $? -eq 0 ]; then
1364     install_packages_RPM $VPX_DEV
1365     VPX_USE=true
1366   fi
1367
1368   INFO ""
1369   MP3LAME_DEV="lame-devel"
1370   check_package_RPM $MP3LAME_DEV
1371   if [ $? -eq 0 ]; then
1372     install_packages_RPM $MP3LAME_DEV
1373     MP3LAME_USE=true
1374   fi
1375
1376   INFO ""
1377   if $PYTHON_SKIP; then
1378     INFO "WARNING! Skipping Python installation, as requested..."
1379   else
1380     check_package_version_match_RPM python3-devel $PYTHON_VERSION_MIN
1381     if [ $? -eq 0 ]; then
1382       install_packages_RPM python3-devel
1383     else
1384       compile_Python
1385     fi
1386   fi
1387
1388   INFO ""
1389   if $BOOST_SKIP; then
1390     INFO "WARNING! Skipping Boost installation, as requested..."
1391   else
1392     check_package_version_ge_RPM boost-devel $BOOST_VERSION_MIN
1393     if [ $? -eq 0 ]; then
1394       install_packages_RPM boost-devel
1395     else
1396       compile_Boost
1397     fi
1398   fi
1399
1400   INFO ""
1401   if $OCIO_SKIP; then
1402     INFO "WARNING! Skipping OpenColorIO installation, as requested..."
1403   else
1404     check_package_version_ge_RPM OpenColorIO-devel $OCIO_VERSION_MIN
1405     if [ $? -eq 0 ]; then
1406       install_packages_RPM OpenColorIO-devel
1407     else
1408       compile_OCIO
1409     fi
1410   fi
1411
1412   INFO ""
1413   if $OIIO_SKIP; then
1414     INFO "WARNING! Skipping OpenImageIO installation, as requested..."
1415   else
1416     check_package_version_ge_RPM OpenImageIO-devel $OIIO_VERSION_MIN
1417     if [ $? -eq 0 ]; then
1418       install_packages_RPM OpenImageIO-devel
1419     else
1420       compile_OIIO
1421     fi
1422   fi
1423
1424   if $BUILD_OSL; then
1425     have_llvm=false
1426
1427     INFO ""
1428     if $LLVM_SKIP; then
1429       INFO "WARNING! Skipping LLVM installation, as requested (this also implies skipping OSL!)..."
1430     else
1431       check_package_RPM llvm-$LLVM_VERSION-devel
1432       if [ $? -eq 0 ]; then
1433         install_packages_RPM llvm-$LLVM_VERSION-devel
1434         have_llvm=true
1435         LLVM_VERSION_FOUND=$LLVM_VERSION
1436       else
1437 #        check_package_RPM llvm-$LLVM_VERSION_MIN-devel
1438 #        if [ $? -eq 0 ]; then
1439 #          install_packages_RPM llvm-$LLVM_VERSION_MIN-devel
1440 #          have_llvm=true
1441 #          LLVM_VERSION_FOUND=$LLVM_VERSION_MIN
1442 #        else
1443 #          check_package_version_ge_RPM llvm-devel $LLVM_VERSION_MIN
1444 #          if [ $? -eq 0 ]; then
1445 #            install_packages_RPM llvm-devel
1446 #            have_llvm=true
1447 #            LLVM_VERSION_FOUND=`get_package_version_RPM llvm-devel`
1448 #          fi
1449 #        fi
1450       install_packages_RPM libffi-devel
1451       # XXX Stupid fedora puts ffi header into a darn stupid dir!
1452       _FFI_INCLUDE_DIR=`rpm -ql libffi-devel | grep -e ".*/ffi.h" | sed -r 's/(.*)\/ffi.h/\1/'`
1453       INFO ""
1454       compile_LLVM
1455       have_llvm=true
1456       LLVM_VERSION_FOUND=$LLVM_VERSION
1457       fi
1458     fi
1459
1460     if $OSL_SKIP; then
1461       INFO ""
1462       INFO "WARNING! Skipping OpenShadingLanguage installation, as requested..."
1463     else
1464       if $have_llvm; then
1465         INFO ""
1466         install_packages_RPM flex bison clang tbb-devel git
1467         # No package currently!
1468         INFO ""
1469         compile_OSL
1470       fi
1471     fi
1472   fi
1473
1474   INFO ""
1475   if $FFMPEG_SKIP; then
1476     INFO "WARNING! Skipping FFMpeg installation, as requested..."
1477   else
1478     # Always for now, not sure which packages should be installed
1479     compile_FFmpeg
1480   fi
1481 }
1482
1483 get_package_version_SUSE() {
1484   zypper info $1 | grep Version | tail -n 1 | sed -r 's/.*:\s+(([0-9]+\.?)+).*/\1/'
1485 }
1486
1487 check_package_SUSE() {
1488   r=`zypper info $1 | grep -c 'Summary'`
1489
1490   if [ $r -ge 1 ]; then
1491     return 0
1492   else
1493     return 1
1494   fi
1495 }
1496
1497 check_package_version_match_SUSE() {
1498   v=`get_package_version_SUSE $1`
1499
1500   if [ -z "$v" ]; then
1501     return 1
1502   fi
1503
1504   version_match $v $2
1505   return $?
1506 }
1507
1508 check_package_version_ge_SUSE() {
1509   v=`get_package_version_SUSE $1`
1510
1511   if [ -z "$v" ]; then
1512     return 1
1513   fi
1514
1515   version_ge $v $2
1516   return $?
1517 }
1518
1519 install_packages_SUSE() {
1520   sudo zypper --non-interactive install --auto-agree-with-licenses $@
1521   if [ $? -ge 1 ]; then
1522     ERROR "zypper failed to install requested packages, exiting."
1523     exit 1
1524   fi
1525 }
1526
1527
1528 install_SUSE() {
1529   INFO ""
1530   INFO "Installing dependencies for SuSE-based distribution"
1531   INFO ""
1532   INFO "`eval _echo "$COMMON_INFO"`"
1533   INFO ""
1534
1535   sudo zypper --non-interactive update --auto-agree-with-licenses
1536
1537   # These libs should always be available in debian/ubuntu official repository...
1538   OPENJPEG_DEV="openjpeg-devel"
1539   SCHRO_DEV="schroedinger-devel"
1540   VORBIS_DEV="libvorbis-devel"
1541   THEORA_DEV="libtheora-devel"
1542
1543   INFO ""
1544   install_packages_SUSE gawk gcc gcc-c++ cmake scons libpng12-devel libtiff-devel \
1545     freetype-devel libX11-devel libXi-devel wget sqlite3-devel ncurses-devel \
1546     readline-devel $OPENJPEG_DEV libopenexr-devel openal-soft-devel \
1547     glew-devel yasm $SCHRO_DEV $THEORA_DEV $VORBIS_DEV libSDL-devel \
1548     fftw3-devel libjack-devel libspnav-devel \
1549     libjpeg62-devel patch python-devel
1550
1551   OPENJPEG_USE=true
1552   SCHRO_USE=true
1553   VORBIS_USE=true
1554   THEORA_USE=true
1555
1556   INFO ""
1557   X264_DEV="x264-devel"
1558   check_package_version_ge_SUSE $X264_DEV $X264_VERSION_MIN
1559   if [ $? -eq 0 ]; then
1560     install_packages_SUSE $X264_DEV
1561     X264_USE=true
1562   fi
1563
1564   INFO ""
1565   XVID_DEV="xvidcore-devel"
1566   check_package_SUSE $XVID_DEV
1567   if [ $? -eq 0 ]; then
1568     install_packages_SUSE $XVID_DEV
1569     XVID_USE=true
1570   fi
1571
1572   INFO ""
1573   VPX_DEV="libvpx-devel"
1574   check_package_version_ge_SUSE $VPX_DEV $VPX_VERSION_MIN
1575   if [ $? -eq 0 ]; then
1576     install_packages_SUSE $VPX_DEV
1577     VPX_USE=true
1578   fi
1579
1580   INFO ""
1581   # No mp3 in suse, it seems.
1582   MP3LAME_DEV="lame-devel"
1583   check_package_SUSE $MP3LAME_DEV
1584   if [ $? -eq 0 ]; then
1585     install_packages_SUSE $MP3LAME_DEV
1586     MP3LAME_USE=true
1587   fi
1588
1589   INFO ""
1590   if $PYTHON_SKIP; then
1591     INFO "WARNING! Skipping Python installation, as requested..."
1592   else
1593     check_package_version_match_SUSE python3-devel 3.3.
1594     if [ $? -eq 0 ]; then
1595       install_packages_SUSE python3-devel
1596     else
1597       compile_Python
1598     fi
1599   fi
1600
1601   INFO ""
1602   if $BOOST_SKIP; then
1603     INFO "WARNING! Skipping Boost installation, as requested..."
1604   else
1605     # No boost_locale currently available, so let's build own boost.
1606     compile_Boost
1607   fi
1608
1609   INFO ""
1610   if $OCIO_SKIP; then
1611     INFO "WARNING! Skipping OpenColorIO installation, as requested..."
1612   else
1613     # No ocio currently available, so let's build own boost.
1614     compile_OCIO
1615   fi
1616
1617   INFO ""
1618   if $OIIO_SKIP; then
1619     INFO "WARNING! Skipping OpenImageIO installation, as requested..."
1620   else
1621     # No oiio currently available, so let's build own boost.
1622     compile_OIIO
1623   fi
1624
1625   if $BUILD_OSL; then
1626     have_llvm=false
1627
1628     INFO ""
1629     if $LLVM_SKIP; then
1630       INFO "WARNING! Skipping LLVM installation, as requested (this also implies skipping OSL!)..."
1631     else
1632       # Suse llvm package *_$SUCKS$_* (tm) !!!
1633 #      check_package_version_ge_SUSE llvm-devel $LLVM_VERSION_MIN
1634 #      if [ $? -eq 0 ]; then
1635 #        install_packages_SUSE llvm-devel
1636 #        have_llvm=true
1637 #        LLVM_VERSION_FOUND=`get_package_version_SUSE llvm-devel`
1638 #      fi
1639
1640       install_packages_SUSE libffi47-devel
1641       INFO ""
1642       compile_LLVM
1643       have_llvm=true
1644       LLVM_VERSION_FOUND=$LLVM_VERSION
1645     fi
1646
1647     if $OSL_SKIP; then
1648       INFO ""
1649       INFO "WARNING! Skipping OpenShaderLanguage installation, as requested..."
1650     else
1651       if $have_llvm; then
1652         INFO ""
1653         # XXX No tbb lib!
1654         install_packages_SUSE flex bison git
1655         # No package currently!
1656         INFO ""
1657         compile_OSL
1658       fi
1659     fi
1660   fi
1661
1662   INFO ""
1663   if $FFMPEG_SKIP; then
1664     INFO "WARNING! Skipping FFMpeg installation, as requested..."
1665   else
1666     # No ffmpeg currently available, so let's build own boost.
1667     compile_FFmpeg
1668   fi
1669 }
1670
1671 print_info_ffmpeglink_DEB() {
1672   if $ALL_STATIC; then
1673     dpkg -L $_packages | grep -e ".*\/lib[^\/]\+\.a" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", $0); nlines++ }'
1674   else
1675     dpkg -L $_packages | grep -e ".*\/lib[^\/]\+\.so" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", gensub(/.*lib([^\/]+)\.so/, "\\1", "g", $0)); nlines++ }'
1676   fi
1677 }
1678
1679 print_info_ffmpeglink_RPM() {
1680   if $ALL_STATIC; then
1681     rpm -ql $_packages | grep -e ".*\/lib[^\/]\+\.a" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", $0); nlines++ }'
1682   else
1683     rpm -ql $_packages | grep -e ".*\/lib[^\/]\+\.so" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", gensub(/.*lib([^\/]+)\.so/, "\\1", "g", $0)); nlines++ }'
1684   fi
1685 }
1686
1687 print_info_ffmpeglink_SUSE() {
1688   if $ALL_STATIC; then
1689     rpm -ql $_packages | grep -e ".*\/lib[^\/]\+\.a" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", $0); nlines++ }'
1690   else
1691     rpm -ql $_packages | grep -e ".*\/lib[^\/]\+\.so" | gawk '{ printf(nlines ? "'"$_ffmpeg_list_sep"'%s" : "%s", gensub(/.*lib([^\/]+)\.so/, "\\1", "g", $0)); nlines++ }'
1692   fi
1693 }
1694
1695 print_info_ffmpeglink() {
1696   # This func must only print a ';'-separated list of libs...
1697   if [ -z "$DISTRO" ]; then
1698     ERROR "Failed to detect distribution type"
1699     exit 1
1700   fi
1701
1702   # Create list of packages from which to get libs names...
1703   _packages=""
1704
1705   if $THEORA_USE; then
1706     _packages="$_packages $THEORA_DEV"
1707   fi
1708
1709   if $VORBIS_USE; then
1710     _packages="$_packages $VORBIS_DEV"
1711   fi
1712
1713   if $XVID_USE; then
1714     _packages="$_packages $XVID_DEV"
1715   fi
1716
1717   if $VPX_USE; then
1718     _packages="$_packages $VPX_DEV"
1719   fi
1720
1721   if $MP3LAME_USE; then
1722     _packages="$_packages $MP3LAME_DEV"
1723   fi
1724
1725   if $X264_USE; then
1726     _packages="$_packages $X264_DEV"
1727   fi
1728
1729   if $OPENJPEG_USE; then
1730     _packages="$_packages $OPENJPEG_DEV"
1731   fi
1732
1733   # XXX At least under Debian, static schro gives problem at blender linking time... :/
1734   if $SCHRO_USE && ! $ALL_STATIC; then
1735     _packages="$_packages $SCHRO_DEV"
1736   fi
1737
1738   if [ "$DISTRO" = "DEB" ]; then
1739     print_info_ffmpeglink_DEB
1740   elif [ "$DISTRO" = "RPM" ]; then
1741     print_info_ffmpeglink_RPM
1742   elif [ "$DISTRO" = "SUSE" ]; then
1743     print_info_ffmpeglink_SUSE
1744   # XXX TODO!
1745   else INFO "<Could not determine additional link libraries needed for ffmpeg, replace this by valid list of libs...>"
1746   fi
1747 }
1748
1749 print_info() {
1750   INFO ""
1751   INFO "If you're using CMake add this to your configuration flags:"
1752
1753   if $ALL_STATIC; then
1754     INFO "  -D WITH_STATIC_LIBS=ON"
1755   fi
1756
1757   if [ -d $INST/boost ]; then
1758     INFO "  -D BOOST_ROOT=$INST/boost"
1759     INFO "  -D Boost_NO_SYSTEM_PATHS=ON"
1760   elif $ALL_STATIC; then
1761     INFO "  -D Boost_USE_ICU=ON"
1762   fi
1763
1764   if [ -d $INST/osl -a $BUILD_OSL == true ]; then
1765     INFO "  -D CYCLES_OSL=$INST/osl"
1766     INFO "  -D WITH_CYCLES_OSL=ON"
1767     INFO "  -D LLVM_VERSION=$LLVM_VERSION_FOUND"
1768     if [ -d $INST/llvm ]; then
1769       INFO "  -D LLVM_DIRECTORY=$INST/llvm"
1770       INFO "  -D LLVM_STATIC=ON"
1771     fi
1772   fi
1773
1774   if [ -d $INST/ffmpeg ]; then
1775     INFO "  -D WITH_CODEC_FFMPEG=ON"
1776     INFO "  -D FFMPEG=$INST/ffmpeg"
1777     INFO "  -D FFMPEG_LIBRARIES='avformat;avcodec;avutil;avdevice;swscale;rt;`print_info_ffmpeglink`'"
1778   fi
1779
1780   INFO ""
1781   INFO "If you're using SCons add this to your user-config:"
1782
1783   if [ -d $INST/python-3.3 ]; then
1784     INFO "BF_PYTHON = '$INST/python-3.3'"
1785     INFO "BF_PYTHON_ABI_FLAGS = 'm'"
1786   fi
1787
1788   if [ -d $INST/ocio ]; then
1789     INFO "BF_OCIO = '$INST/ocio'"
1790   fi
1791
1792   if [ -d $INST/oiio ]; then
1793     INFO "BF_OIIO = '$INST/oiio'"
1794   fi
1795
1796   if [ -d $INST/boost ]; then
1797     INFO "BF_BOOST = '$INST/boost'"
1798   fi
1799
1800   if [ -d $INST/ffmpeg ]; then
1801     INFO "BF_FFMPEG = '$INST/ffmpeg'"
1802     _ffmpeg_list_sep=" "
1803     INFO "BF_FFMPEG_LIB = 'avformat avcodec swscale avutil avdevice `print_info_ffmpeglink`'"
1804   fi
1805
1806   INFO ""
1807   INFO ""
1808   INFO "WARNING: If this script had to build boost into $INST, and you are dynamically linking "
1809   INFO "         blender against it, you will have to run those commands as root user:"
1810   INFO ""
1811   INFO "    echo \"$INST/boost/lib\" > /etc/ld.so.conf.d/boost.conf"
1812   INFO "    ldconfig"
1813   INFO ""
1814 }
1815
1816 # Detect distributive type used on this machine
1817 detect_distro
1818
1819 if [ -z "$DISTRO" ]; then
1820   ERROR "Failed to detect distribution type"
1821   exit 1
1822 elif [ "$DISTRO" = "DEB" ]; then
1823   install_DEB
1824 elif [ "$DISTRO" = "RPM" ]; then
1825   install_RPM
1826 elif [ "$DISTRO" = "SUSE" ]; then
1827   install_SUSE
1828 fi
1829
1830 print_info
1831
1832 # Switch back to user language.
1833 LANG=LANG_BACK
1834 export LANG