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