use CMAKE_INSTALL_LIBDIR instead of "/usr/lib"
[profile/ivi/smartdevicelink.git] / FindQt.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2013, Ford Motor Company
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # Redistributions of source code must retain the above copyright notice, this
10 # list of conditions and the following disclaimer.
11 #
12 # Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # Neither the name of the Ford Motor Company nor the names of its contributors
18 # may be used to endorse or promote products derived from this software
19 # without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32
33 usage() {
34   echo "Usage: $0 [-v <version>] [-b] [<file name>]"
35   echo
36   echo "       Look for file in Qt directory."
37   echo "       If file name isn't set then look for bin directory."
38   echo
39   echo "       -v <version>  look for Qt specific version x.x.x"
40   echo "       -b look for binary file"
41   echo
42 }
43
44 version2int() {
45   IFS="."
46   local ver=0
47   for i in $1; do
48     ver=$(( $ver * 32 + $i ))
49   done
50   echo $ver
51 }
52
53 version_match() {
54   v1=$(version2int $1)
55   v2=$(version2int $2)
56   # It's bash way to say "if ((version1 < version2) and (major1 == major2))
57   if [[ ( $v1 -le $v2 ) && ( $(( ($v1 / 1024) - ($v2 / 1024) )) == 0 ) ]]; then
58     return 0;
59   else
60     return 1;
61   fi
62 }
63
64 qmake_data() {
65   $1 --version 2>/dev/null | grep "Using Qt version" | sed "s/.*Qt version \\([0-9\\.]*\\) in \\(.*\\)$/\\$2/"
66 }
67
68 find_binary() {
69   local qt_binary="$1/$2" # check specified binary
70   if [[ -x $qt_binary && ! -d $qt_binary ]]; then # to be executable and not to be directory
71     echo -n $qt_binary # output without newline
72     return 0
73   fi
74   return 1
75 }
76
77 find_file() {
78   local qt_file=$(find $1 -name "$2" -type f -print0 -quit 2>/dev/null) # check specified binary
79   if [[ -n $qt_file && ! -d $qt_file ]]; then # if found
80     echo -n $qt_file #output without newline
81     return 0
82   fi
83   return 1
84 }
85
86 type=file
87 version="0.0.0"
88 while getopts :v:b option; do
89   case "$option" in
90     v) version=$OPTARG ;;
91     b) type=binary ;;
92     *) usage; exit 1; ;;
93   esac
94 done
95
96 shift $(( OPTIND - 1 ))
97 if [[ -z $1 ]]; then
98   type=bindir
99 else
100   file_name=$1
101 fi
102
103 ## First attempt - using locate
104 if command -v locate > /dev/null; then
105   for searchloc in $CUSTOM_QT_DIR ~ /opt /usr/local; do
106     qmake_list=$(locate $searchloc/*/bin/qmake)
107     for qmake in $qmake_list; do
108       if [[ ! -x $qmake || -d $qmake ]]; then
109         continue
110       fi
111       # called with "qmake 1" return qmake version
112       qt_version=$(qmake_data $qmake 1)
113       if ! version_match $version $qt_version; then
114         continue
115       fi
116
117       case $type in
118         binary)
119           qt_dir=$(dirname $qmake 2> /dev/null)
120           if find_binary $qt_dir $file_name; then
121             exit 0
122           fi
123           ;;
124         file)
125           # called with "qmake 2" return Qt installation dir
126           qt_installdir=$(qmake_data $qmake 2)
127           if find_file $qt_installdir $file_name; then
128             exit 0
129           fi
130           ;;
131         bindir)
132           echo -n $(dirname $qmake 2>/dev/null)
133           exit 0
134           ;;
135       esac
136     done
137   done
138 fi
139
140 ## Second attempt - using find
141 export -f find_file
142 export -f qmake_data
143 export -f version_match
144 export -f version2int
145
146 qmake=$(find -L $CUSTOM_QT_DIR ~ /opt /usr/local -name '.*' -prune \
147         -o -name qmake -type f \
148         -executable \
149         -exec /bin/bash -c "version_qt=\$(qmake_data {} 1);version_match $version \$version_qt" {} \; -print -quit > /dev/null)
150 if ! [ $? ]; then
151    exit 1;
152 fi
153
154 case $type in
155   binary)
156     qt_dir=$(dirname $qmake 2>/dev/null)
157     if find_binary $qt_dir $file_name; then
158       exit 0
159     fi
160     ;;
161   file)
162     # called with "qmake 2" return Qt installation dir
163     qt_installdir=$(qmake_data $qmake 2)
164     if find_file $qt_installdir $file_name; then
165       exit 0
166     fi
167     ;;
168   bindir)
169     echo -n $(dirname $qmake 2>/dev/null)
170     exit 0
171     ;;
172 esac
173
174 exit 1