]> Creatis software - cpPlugins.git/blob - third_party_installers/cpPlugins_Install_CMAKE.sh
07cc1a8194c538dae0ef1e7028b8d01dad4e1453
[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
145 ## Create paths
146 if [ "x$source_file" != "x" ]; then
147     echo -n "==> Cleaning directories... "
148     rm -rf $source_dir
149     rm -rf $build_dir
150     echo "done."
151     echo -n "==> Creating directories... "
152     mkdir -p $source_dir
153     mkdir -p $build_dir
154     echo "done."
155 fi
156
157 ## Extract source code
158 if [ "x$source_file" != "x" ]; then
159     echo -n "==> Extracting sources... "
160     base_path=`abspath $source_file`
161     base_ext=`get_file_extension $base_path`
162     if [ "$base_ext" == "zip" ]; then
163         echo unzip $base_path
164     elif [ "$base_ext" == "tar" ]; then
165         tar xf $base_path -C $source_dir --strip-components=1
166     elif [ "$base_ext" == "tar.gz" ]; then
167         tar xzf $base_path -C $source_dir --strip-components=1
168     elif [ "$base_ext" == "tar.bz2" ]; then
169         tar xjf $base_path -C $source_dir --strip-components=1
170     fi
171     echo "done."
172 fi
173
174 echo "==> Configuring sources... "
175 cd $build_dir
176 $source_dir/bootstrap --prefix=$prefix $qt_options
177 echo "==> Configuring sources... done."
178
179 echo "==> Compiling sources..."
180 cd $build_dir
181 make -j$number_of_threads
182 echo "==> Compiling sources... done."
183
184 echo "==> Installing package..."
185 cd $build_dir
186 make -j install
187 echo "==> Installing package... done."
188
189 ## eof - $RCSfile$