]> Creatis software - cpPlugins.git/blob - dependencies/cpPlugins_Install_ITK.sh
...
[cpPlugins.git] / dependencies / cpPlugins_Install_ITK.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] [-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     *)
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 ## Other configuration variables
135 platform=`uname`
136 number_of_cores=`grep -c ^processor /proc/cpuinfo`
137 number_of_threads=`expr $number_of_cores / 2`
138 if [ "x$source_file" != "x" ]; then
139     source_file=`abspath $source_file`
140 fi
141 source_dir=`abspath $source_dir`
142 build_dir=`abspath $build_dir`
143
144 echo "====================================================================="
145 echo "==> Source file       : $source_file"
146 echo "==> Source dir        : $source_dir"
147 echo "==> Build dir         : $build_dir"
148 echo "==> Build type        : $build_type"
149 echo "==> Prefix            : $prefix"
150 echo "==> cmake             : $cmake_exec"
151 echo "==> Platform          : $platform"
152 echo "==> Number of cores   : $number_of_cores"
153 echo "==> Number of threads : $number_of_threads"
154 echo "====================================================================="
155 read -n1 -r -p "Continue? [Y/N]... " key
156 echo
157 if [ "$key" != 'Y' -a "$key" != 'y' ] ; then
158     exit 1
159 fi
160
161 ## Create paths
162 if [ "x$source_file" != "x" ]; then
163     echo -n "==> Cleaning directories... "
164     rm -rf $source_dir
165     rm -rf $build_dir
166     echo "done."
167     echo -n "==> Creating directories... "
168     mkdir -p $source_dir
169     mkdir -p $build_dir
170     echo "done."
171 fi
172
173 ## Extract source code
174 if [ "x$source_file" != "x" ]; then
175     echo -n "==> Extracting sources... "
176     base_path=`abspath $source_file`
177     base_ext=`get_file_extension $base_path`
178     if [ "$base_ext" == "zip" ]; then
179         echo unzip $base_path
180     elif [ "$base_ext" == "tar" ]; then
181         tar xf $base_path -C $source_dir --strip-components=1
182     elif [ "$base_ext" == "tar.gz" ]; then
183         tar xzf $base_path -C $source_dir --strip-components=1
184     elif [ "$base_ext" == "tar.bz2" ]; then
185         tar xjf $base_path -C $source_dir --strip-components=1
186     fi
187     echo "done."
188 fi
189
190 echo "==> Configuring sources... "
191 cd $build_dir
192 $cmake_exec \
193     -DCMAKE_CXX_FLAGS:STRING=-std=c++11 \
194     -DBUILD_DOCUMENTATION:BOOL=OFF \
195     -DBUILD_EXAMPLES:BOOL=OFF \
196     -DBUILD_SHARED_LIBS:BOOL=ON \
197     -DBUILD_TESTING:BOOL=OFF \
198     -DCMAKE_BUILD_TYPE:STRING=$build_type \
199     -DModule_ITKReview:BOOL=ON \
200     -DModule_ITKVtkGlue:BOOL=OFF \
201     -DModule_ParabolicMorphology:BOOL=ON \
202     -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
203     ${source_dir}
204 echo "==> Configuring sources... done."
205
206 echo "==> Compiling sources..."
207 cd $build_dir
208 make -j$number_of_threads
209 echo "==> Compiling sources... done."
210
211 echo "==> Installing package..."
212 cd $build_dir
213 make -j install
214 echo "==> Installing package... done."
215
216 ## eof - $RCSfile$