]> Creatis software - cpPlugins.git/blob - third_party_installers/cpPlugins_Install_ITK.sh
Code cleaning
[cpPlugins.git] / third_party_installers / cpPlugins_Install_ITK.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] [-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     *)
74     ;;
75     esac
76 done
77
78 ## Check command line arguments
79 if [ "x$source_dir" == "x" ]; then
80     if [ "x$source_file" != "x" ]; then
81         base_path=`abspath $source_file`
82         base_ext=`get_file_extension $base_path`
83         base_dir=`dirname $base_path`
84         if [ "x$base_ext" != "x" ]; then
85             source_dir="$base_dir"/`basename $base_path .$base_ext`
86         else
87             echo "Error: Input compressed file extension not recognized."
88             exit 1
89         fi
90     else
91         print_help
92         exit 1
93     fi
94 fi
95 if [ "x$build_dir" == "x" ]; then
96     if [ "x$source_dir" != "x" ]; then
97         base_dir=$source_dir
98         if [ "${source_dir:$((${#str}-1)):1}" == "/" ]; then
99             base_dir=`echo $source_dir | rev | cut -c 2- | rev`
100         fi
101         build_dir="$base_dir-build"
102     else
103         print_help
104         exit 1
105     fi
106 fi
107
108 ## Locate cmake executable
109 if [ "x$cmake_exec" == "x" ]; then
110     cmake_locations=("/usr/bin" "/usr/local/bin" "${HOME}/local/bin")
111     cmake_ver=""
112     for loc in ${cmake_locations[@]}; do
113         cmake_file="$loc/cmake"
114         if [ -x $cmake_file ]; then
115             str=`$cmake_file --version | grep version`
116             version=${str:14}
117             if [ "$cmake_ver" \< "$version" ]; then
118                 cmake_ver=$version
119                 cmake_exec=$cmake_file
120             fi
121         fi
122     done
123 fi
124 if [ ! -x $cmake_exec ]; then
125     echo "ERROR: no valid cmake found."
126     exit 1
127 fi
128 cmake_exec=`abspath $cmake_exec`
129
130 ## Other configuration variables
131 platform=`uname`
132 number_of_cores=`grep -c ^processor /proc/cpuinfo`
133 number_of_threads=`expr $number_of_cores / 2`
134 if [ "x$source_file" != "x" ]; then
135     source_file=`abspath $source_file`
136 fi
137 source_dir=`abspath $source_dir`
138 build_dir=`abspath $build_dir`
139
140 echo "====================================================================="
141 echo "==> Source file       : $source_file"
142 echo "==> Source dir        : $source_dir"
143 echo "==> Build dir         : $build_dir"
144 echo "==> Build type        : $build_type"
145 echo "==> Prefix            : $prefix"
146 echo "==> cmake             : $cmake_exec"
147 echo "==> Platform          : $platform"
148 echo "==> Number of cores   : $number_of_cores"
149 echo "==> Number of threads : $number_of_threads"
150 echo "====================================================================="
151 read -n1 -r -p "Continue? [Y/N]... " key
152 echo
153 if [ "$key" != 'Y' -a "$key" != 'y' ] ; then
154     exit 1
155 fi
156
157 ## Create paths
158 if [ "x$source_file" != "x" ]; then
159     echo -n "==> Cleaning directories... "
160     rm -rf $source_dir
161     rm -rf $build_dir
162     echo "done."
163     echo -n "==> Creating directories... "
164     mkdir -p $source_dir
165     mkdir -p $build_dir
166     echo "done."
167 fi
168
169 ## Extract source code
170 if [ "x$source_file" != "x" ]; then
171     echo -n "==> Extracting sources... "
172     base_path=`abspath $source_file`
173     base_ext=`get_file_extension $base_path`
174     if [ "$base_ext" == "zip" ]; then
175         echo unzip $base_path
176     elif [ "$base_ext" == "tar" ]; then
177         tar xf $base_path -C $source_dir --strip-components=1
178     elif [ "$base_ext" == "tar.gz" ]; then
179         tar xzf $base_path -C $source_dir --strip-components=1
180     elif [ "$base_ext" == "tar.bz2" ]; then
181         tar xjf $base_path -C $source_dir --strip-components=1
182     fi
183     echo "done."
184 fi
185
186 echo "==> Configuring sources... "
187 cd $build_dir
188 $cmake_exec \
189     -DCMAKE_CXX_FLAGS:STRING=-std=c++11 \
190     -DBUILD_DOCUMENTATION:BOOL=OFF \
191     -DBUILD_EXAMPLES:BOOL=OFF \
192     -DBUILD_SHARED_LIBS:BOOL=ON \
193     -DBUILD_TESTING:BOOL=OFF \
194     -DCMAKE_BUILD_TYPE:STRING=$build_type \
195     -DModule_ITKReview:BOOL=ON \
196     -DModule_ITKVtkGlue:BOOL=OFF \
197     -DModule_ParabolicMorphology:BOOL=ON \
198     -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
199     ${source_dir}
200 echo "==> Configuring sources... done."
201
202 echo "==> Compiling sources..."
203 cd $build_dir
204 make -j$number_of_threads
205 echo "==> Compiling sources... done."
206
207 echo "==> Installing package..."
208 cd $build_dir
209 make -j install
210 echo "==> Installing package... done."
211
212 ## eof - $RCSfile$