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