]> Creatis software - cpPlugins.git/blob - dependencies/cpPlugins_Install_CMAKE.sh
...
[cpPlugins.git] / dependencies / cpPlugins_Install_CMAKE.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]"
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 for i in "$@"; do
51     case $i in
52     -f=*|--file=*)
53     source_file="${i#*=}"
54     shift
55     ;;
56     -c=*|--source_dir=*)
57     source_dir="${i#*=}"
58     shift
59     ;;
60     -b=*|--build_dir=*)
61     build_dir="${i#*=}"
62     shift
63     ;;
64     -p=*|--prefix=*)
65     prefix="${i#*=}"
66     shift
67     ;;
68     -q=*|--qmake=*)
69     qmake_exec="${i#*=}"
70     shift
71     ;;
72     *)
73     ;;
74     esac
75 done
76
77 ## Check command line arguments
78 if [ "x$source_dir" == "x" ]; then
79     if [ "x$source_file" != "x" ]; then
80         base_path=`abspath $source_file`
81         base_ext=`get_file_extension $base_path`
82         base_dir=`dirname $base_path`
83         if [ "x$base_ext" != "x" ]; then
84             source_dir="$base_dir"/`basename $base_path .$base_ext`
85         else
86             echo "Error: Input compressed file extension not recognized."
87             exit 1
88         fi
89     else
90         print_help
91         exit 1
92     fi
93 fi
94 if [ "x$build_dir" == "x" ]; then
95     if [ "x$source_dir" != "x" ]; then
96         base_dir=$source_dir
97         if [ "${source_dir:$((${#str}-1)):1}" == "/" ]; then
98             base_dir=`echo $source_dir | rev | cut -c 2- | rev`
99         fi
100         build_dir="$base_dir-build"
101     else
102         print_help
103         exit 1
104     fi
105 fi
106
107 # Locate qmake executable
108 if [ "x$qmake_exec" == "x" ]; then
109     qmake_locations=("/usr/bin" "/usr/local/bin" "${HOME}/local/bin")
110     for loc in ${qmake_locations[@]}; do
111         qmake_file="$loc/qmake"
112         if [ -x $qmake_file ]; then
113             version=`$qmake_file --version | grep Using\ Qt\ version | cut -d ' ' -f 4`
114             if [ "${version:0:3}" == "4.8" ]; then
115                 qmake_exec=$qmake_file
116             fi
117         fi
118     done
119 fi
120 qmake_exec=`abspath $qmake_exec`
121 if [ -x $qmake_exec ]; then
122     qt_options="--qt-gui --qt-qmake=$qmake_exec"
123 else
124     qt_options="--no-qt-gui"
125 fi
126
127 ## Other configuration variables
128 platform=`uname`
129 number_of_cores=`grep -c ^processor /proc/cpuinfo`
130 number_of_threads=`expr $number_of_cores / 2`
131 if [ "x$source_file" != "x" ]; then
132     source_file=`abspath $source_file`
133 fi
134 source_dir=`abspath $source_dir`
135 build_dir=`abspath $build_dir`
136
137 echo "====================================================================="
138 echo "==> Source file       : $source_file"
139 echo "==> Source dir        : $source_dir"
140 echo "==> Build dir         : $build_dir"
141 echo "==> Prefix            : $prefix"
142 echo "==> qmake             : $qmake_exec"
143 echo "==> Qt options        : $qt_options"
144 echo "==> Platform          : $platform"
145 echo "==> Number of cores   : $number_of_cores"
146 echo "==> Number of threads : $number_of_threads"
147 echo "====================================================================="
148 read -n1 -r -p "Continue? [Y/N]... " key
149 echo
150 if [ "$key" != 'Y' -a "$key" != 'y' ] ; then
151     exit 1
152 fi
153
154 ## Create paths
155 if [ "x$source_file" != "x" ]; then
156     echo -n "==> Cleaning directories... "
157     rm -rf $source_dir
158     rm -rf $build_dir
159     echo "done."
160     echo -n "==> Creating directories... "
161     mkdir -p $source_dir
162     mkdir -p $build_dir
163     echo "done."
164 fi
165
166 ## Extract source code
167 if [ "x$source_file" != "x" ]; then
168     echo -n "==> Extracting sources... "
169     base_path=`abspath $source_file`
170     base_ext=`get_file_extension $base_path`
171     if [ "$base_ext" == "zip" ]; then
172         echo unzip $base_path
173     elif [ "$base_ext" == "tar" ]; then
174         tar xf $base_path -C $source_dir --strip-components=1
175     elif [ "$base_ext" == "tar.gz" ]; then
176         tar xzf $base_path -C $source_dir --strip-components=1
177     elif [ "$base_ext" == "tar.bz2" ]; then
178         tar xjf $base_path -C $source_dir --strip-components=1
179     fi
180     echo "done."
181 fi
182
183 echo "==> Configuring sources... "
184 cd $build_dir
185 $source_dir/bootstrap --prefix=$prefix $qt_options
186 echo "==> Configuring sources... done."
187
188 echo "==> Compiling sources..."
189 cd $build_dir
190 make -j$number_of_threads
191 echo "==> Compiling sources... done."
192
193 echo "==> Installing package..."
194 cd $build_dir
195 make -j install
196 echo "==> Installing package... done."
197
198 ## eof - $RCSfile$