summaryrefslogtreecommitdiff
path: root/FindQt.sh
blob: 71a7b10a126ddbf26fded74e2dcf62206f3c1aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash

# Copyright (c) 2013, Ford Motor Company
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the
# distribution.
#
# Neither the name of the Ford Motor Company nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

usage() {
  echo "Usage: $0 [-v <version>] [-b] [<file name>]"
  echo
  echo "       Look for file in Qt directory."
  echo "       If file name isn't set then look for bin directory."
  echo
  echo "       -v <version>  look for Qt specific version x.x.x"
  echo "       -b look for binary file"
  echo
}

version2int() {
  IFS="."
  local ver=0
  for i in $1; do
    ver=$(( $ver * 32 + $i ))
  done
  echo $ver
}

version_match() {
  v1=$(version2int $1)
  v2=$(version2int $2)
  # It's bash way to say "if ((version1 < version2) and (major1 == major2))
  if [[ ( $v1 -le $v2 ) && ( $(( ($v1 / 1024) - ($v2 / 1024) )) == 0 ) ]]; then
    return 0;
  else
    return 1;
  fi
}

qmake_data() {
  $1 --version 2>/dev/null | grep "Using Qt version" | sed "s/.*Qt version \\([0-9\\.]*\\) in \\(.*\\)$/\\$2/"
}

find_binary() {
  local qt_binary="$1/$2" # check specified binary
  if [[ -x $qt_binary && ! -d $qt_binary ]]; then # to be executable and not to be directory
    echo -n $qt_binary # output without newline
    return 0
  fi
  return 1
}

find_file() {
  local qt_file=$(find $1 -name "$2" -type f -print0 -quit 2>/dev/null) # check specified binary
  if [[ -n $qt_file && ! -d $qt_file ]]; then # if found
    echo -n $qt_file #output without newline
    return 0
  fi
  return 1
}

type=file
version="0.0.0"
while getopts :v:b option; do
  case "$option" in
    v) version=$OPTARG ;;
    b) type=binary ;;
    *) usage; exit 1; ;;
  esac
done

shift $(( OPTIND - 1 ))
if [[ -z $1 ]]; then
  type=bindir
else
  file_name=$1
fi

## First attempt - using locate
if command -v locate > /dev/null; then
  for searchloc in $CUSTOM_QT_DIR ~ /opt /usr/local; do
    qmake_list=$(locate $searchloc/*/bin/qmake)
    for qmake in $qmake_list; do
      if [[ ! -x $qmake || -d $qmake ]]; then
        continue
      fi
      # called with "qmake 1" return qmake version
      qt_version=$(qmake_data $qmake 1)
      if ! version_match $version $qt_version; then
        continue
      fi

      case $type in
        binary)
          qt_dir=$(dirname $qmake 2> /dev/null)
          if find_binary $qt_dir $file_name; then
            exit 0
          fi
          ;;
        file)
          # called with "qmake 2" return Qt installation dir
          qt_installdir=$(qmake_data $qmake 2)
          if find_file $qt_installdir $file_name; then
            exit 0
          fi
          ;;
        bindir)
          echo -n $(dirname $qmake 2>/dev/null)
          exit 0
          ;;
      esac
    done
  done
fi

## Second attempt - using find
export -f find_file
export -f qmake_data
export -f version_match
export -f version2int

qmake=$(find -L $CUSTOM_QT_DIR ~ /opt /usr/local -name '.*' -prune \
        -o -name qmake -type f \
        -executable \
        -exec /bin/bash -c "version_qt=\$(qmake_data {} 1);version_match $version \$version_qt" {} \; -print -quit > /dev/null)
if ! [ $? ]; then
   exit 1;
fi

case $type in
  binary)
    qt_dir=$(dirname $qmake 2>/dev/null)
    if find_binary $qt_dir $file_name; then
      exit 0
    fi
    ;;
  file)
    # called with "qmake 2" return Qt installation dir
    qt_installdir=$(qmake_data $qmake 2)
    if find_file $qt_installdir $file_name; then
      exit 0
    fi
    ;;
  bindir)
    echo -n $(dirname $qmake 2>/dev/null)
    exit 0
    ;;
esac

exit 1