]> Creatis software - cpPlugins.git/blob - third_party_installers/cpPlugins_Install_VTK.sh
...
[cpPlugins.git] / third_party_installers / cpPlugins_Install_VTK.sh
1 #!/bin/bash
2
3 function abspath()
4 {
5     pushd . > /dev/null
6     if [ -d "$1" ]; then
7         cd "$1"
8         dirs -l +0
9     else
10         cd "`dirname \"$1\"`"
11         cur_dir=`dirs -l +0`
12         if [ "$cur_dir" == "/" ]; then
13             echo "$cur_dir`basename \"$1\"`"
14         else
15             echo "$cur_dir/`basename \"$1\"`"
16         fi
17     fi
18     popd > /dev/null
19 }
20
21 function get_file_extension
22 {
23     valid_extensions=("zip" "tar" "tar.gz" "tar.bz2")
24     actual_file=`abspath $1`
25     actual_ext=""
26     for ext in ${valid_extensions[@]}; do
27         test_str=`dirname $actual_file`/`basename $actual_file $ext`
28         if [ $test_str != $actual_file ]; then
29             actual_ext=$ext
30         fi
31     done
32     echo "$actual_ext"
33 }
34
35 function print_help()
36 {
37     echo "Usage: `basename $0` -f=compressed_code -c=source_dir -b=build_dir [-p=instalation_prefix] [-q=qmake_executable] [-m=cmake_executable] [-t=build_type[MinSizeRel/Debug/Release]]"
38 }
39
40 ## Analyze command-line arguments
41 if [ $# -eq 0 ]; then
42         print_help
43         exit 1
44 fi
45 prefix="${HOME}/local"
46 build_type="MinSizeRel"
47 for i in "$@"; do
48     case $i in
49     -f=*|--file=*)
50     source_file="${i#*=}"
51     shift
52     ;;
53     -c=*|--source_dir=*)
54     source_dir="${i#*=}"
55     shift
56     ;;
57     -b=*|--build_dir=*)
58     build_dir="${i#*=}"
59     shift
60     ;;
61     -t=*|--build_type=*)
62     build_type="${i#*=}"
63     shift
64     ;;
65     -p=*|--prefix=*)
66     prefix="${i#*=}"
67     shift
68     ;;
69     -m=*|--cmake=*)
70     cmake_exec="${i#*=}"
71     shift
72     ;;
73     -q=*|--qmake=*)
74     qmake_exec="${i#*=}"
75     shift
76     ;;
77     *)
78     ;;
79     esac
80 done
81
82 ## Check command line arguments
83 if [ "x$source_dir" == "x" ]; then
84     if [ "x$source_file" != "x" ]; then
85         base_path=`abspath $source_file`
86         base_ext=`get_file_extension $base_path`
87         base_dir=`dirname $base_path`
88         if [ "x$base_ext" != "x" ]; then
89             source_dir="$base_dir"/`basename $base_path .$base_ext`
90         else
91             echo "Error: Input compressed file extension not recognized."
92             exit 1
93         fi
94     else
95         print_help
96         exit 1
97     fi
98 fi
99 if [ "x$build_dir" == "x" ]; then
100     if [ "x$source_dir" != "x" ]; then
101         base_dir=$source_dir
102         if [ "${source_dir:$((${#str}-1)):1}" == "/" ]; then
103             base_dir=`echo $source_dir | rev | cut -c 2- | rev`
104         fi
105         build_dir="$base_dir-build"
106     else
107         print_help
108         exit 1
109     fi
110 fi
111
112 ## Locate cmake executable
113 if [ "x$cmake_exec" == "x" ]; then
114     cmake_locations=("/usr/bin" "/usr/local/bin" "${HOME}/local/bin")
115     cmake_ver=""
116     for loc in ${cmake_locations[@]}; do
117         cmake_file="$loc/cmake"
118         if [ -x $cmake_file ]; then
119             str=`$cmake_file --version | grep version`
120             version=${str:14}
121             if [ "$cmake_ver" \< "$version" ]; then
122                 cmake_ver=$version
123                 cmake_exec=$cmake_file
124             fi
125         fi
126     done
127 fi
128 if [ ! -x $cmake_exec ]; then
129     echo "ERROR: no valid cmake found."
130     exit 1
131 fi
132 cmake_exec=`abspath $cmake_exec`
133
134 # Locate qmake executable
135 if [ "x$qmake_exec" == "x" ]; then
136     qmake_locations=("/usr/bin" "/usr/local/bin" "${HOME}/local/bin")
137     for loc in ${qmake_locations[@]}; do
138         qmake_file="$loc/qmake"
139         if [ -x $qmake_file ]; then
140             version=`$qmake_file --version | grep Using\ Qt\ version | cut -d ' ' -f 4`
141             if [ "${version:0:3}" == "4.8" ]; then
142                 qmake_exec=$qmake_file
143             fi
144         fi
145     done
146 fi
147 qmake_exec=`abspath $qmake_exec`
148 if [ -x $qmake_exec ]; then
149     use_qt="1"
150 else
151     use_qt="0"
152 fi
153
154 ## Other configuration variables
155 platform=`uname`
156 number_of_cores=`grep -c ^processor /proc/cpuinfo`
157 number_of_threads=`expr $number_of_cores / 2`
158 if [ "x$source_file" != "x" ]; then
159     source_file=`abspath $source_file`
160 fi
161 source_dir=`abspath $source_dir`
162 build_dir=`abspath $build_dir`
163
164 echo "====================================================================="
165 echo "==> Source file       : $source_file"
166 echo "==> Source dir        : $source_dir"
167 echo "==> Build dir         : $build_dir"
168 echo "==> Build type        : $build_type"
169 echo "==> Prefix            : $prefix"
170 echo "==> cmake             : $cmake_exec"
171 echo "==> qmake             : $qmake_exec"
172 echo "==> Use Qt            : $use_qt"
173 echo "==> Platform          : $platform"
174 echo "==> Number of cores   : $number_of_cores"
175 echo "==> Number of threads : $number_of_threads"
176 echo "====================================================================="
177 read -n1 -r -p "Continue? [Y/N]... " key
178 echo
179 if [ "$key" != 'Y' -a "$key" != 'y' ] ; then
180     exit 1
181 fi
182
183 ## Create paths
184 if [ "x$source_file" != "x" ]; then
185     echo -n "==> Cleaning directories... "
186     rm -rf $source_dir
187     rm -rf $build_dir
188     echo "done."
189     echo -n "==> Creating directories... "
190     mkdir -p $source_dir
191     mkdir -p $build_dir
192     echo "done."
193 fi
194
195 ## Extract source code
196 if [ "x$source_file" != "x" ]; then
197     echo -n "==> Extracting sources... "
198     base_path=`abspath $source_file`
199     base_ext=`get_file_extension $base_path`
200     if [ "$base_ext" == "zip" ]; then
201         echo unzip $base_path
202     elif [ "$base_ext" == "tar" ]; then
203         tar xf $base_path -C $source_dir --strip-components=1
204     elif [ "$base_ext" == "tar.gz" ]; then
205         tar xzf $base_path -C $source_dir --strip-components=1
206     elif [ "$base_ext" == "tar.bz2" ]; then
207         tar xjf $base_path -C $source_dir --strip-components=1
208     fi
209     echo "done."
210 fi
211
212 echo "==> Configuring sources... "
213 cd $build_dir
214 if [ "$use_qt" == "1" ]; then
215     $cmake_exec \
216         -DCMAKE_CXX_FLAGS:STRING=-std=c++11 \
217         -DBUILD_DOCUMENTATION:BOOL=OFF \
218         -DBUILD_EXAMPLES:BOOL=OFF \
219         -DBUILD_SHARED_LIBS:BOOL=ON \
220         -DBUILD_TESTING:BOOL=OFF \
221         -DQT_QMAKE_EXECUTABLE:PATH=$qmake_exec \
222         -DCMAKE_BUILD_TYPE:STRING=$build_type \
223         -DModule_vtkGUISupportQt:BOOL=ON \
224         -DModule_vtkGUISupportQtOpenGL:BOOL=ON \
225         -DModule_vtkGUISupportQtSQL:BOOL=OFF \
226         -DModule_vtkGUISupportQtWebkit:BOOL=OFF \
227         -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
228         ${source_dir}
229 else
230     $cmake_exec \
231         -DCMAKE_CXX_FLAGS:STRING=-std=c++11 \
232         -DBUILD_DOCUMENTATION:BOOL=OFF \
233         -DBUILD_EXAMPLES:BOOL=OFF \
234         -DBUILD_SHARED_LIBS:BOOL=ON \
235         -DBUILD_TESTING:BOOL=OFF \
236         -DCMAKE_BUILD_TYPE:STRING=$build_type \
237         -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
238         ${source_dir}
239 fi
240 echo "==> Configuring sources... done."
241
242 echo "==> Compiling sources..."
243 cd $build_dir
244 make -j$number_of_threads
245 echo "==> Compiling sources... done."
246
247 echo "==> Installing package..."
248 cd $build_dir
249 make -j install
250 echo "==> Installing package... done."
251
252 ## eof - $RCSfile$