1#!/bin/sh
2# Copyright 2007-2008 David Johnson
3# Configuration script for use with qt/qmake projects. This configure
4# script is free software; the author gives unlimited permission to
5# copy, distribute and modify it. Portions of this script adapted
6# from the Qt configuration script.
7
8PACKAGE=qbrew
9PROFILE=qbrew.pro
10PREFIX=/usr/local
11QTMINVER=4.3.0
12LOGFILE=config.log
13BUILDDIR=.config
14
15### Messages ########################################################
16
17usage() {
18cat <<EOT
19Usage: $0 [OPTION]...
20
21This script creates the necessary configuration files for building
22
23Main options:
24  --prefix=[path]     Base path for build/install.  Default: /usr/local
25  --bindir=[path]     Directory for binaries.  Default: PREFIX/bin
26  --datadir=[path]    Directory for data.  Default: PREFIX/share
27  --docdir=[path]     Directory for docs.  Default: PREFIX/share/doc
28  --qtdir=[path]      Directory where Qt is installed
29  --spec=[spec]       Spec to use for QMAKESPEC
30  --debug             Enable debug output
31  --help              This help text
32
33EOT
34}
35
36qt_info() {
37cat <<EOF
38Be sure you have a properly configured Qt build environment. This application
39requires a Qt library version of $QTMINVER or greater. Some systems split Qt
40into separate runtime and development packages. If this is your case, make sure
41you have both. The 'qmake' command should be in your path, and you may need to
42use either the QTDIR environment variable or the --qtdir configure option. It
43may be necessary to use the --spec option if qmake cannot determine your build
44system. If your qmake has been installed under a different name, then it may be
45necessary to use the QMAKE variable.
46EOF
47}
48
49### Tests ###########################################################
50
51make_test() {
52    if [ -z "$MAKE" ] ; then
53	for mk in gmake make; do
54            if which $mk >/dev/null 2>&1; then
55                MAKE=`which $mk`
56                break
57            fi
58	done
59	if [ -z "$MAKE" ] ; then
60            echo "ERROR: cannot find 'make' or 'gmake'. Exiting"
61            echo "ERROR: cannot find 'make' or 'gmake'. Exiting" >> $LOGFILE
62            exit 1
63	fi
64    fi
65}
66
67qmake_test() {
68    echo -n "Checking for qmake..."
69
70    # check QTDIR
71    if [ -z "$QMAKE" ] ; then
72        result=$QTDIR/bin/qmake
73        if [ -x "$result" ] ; then
74            QMAKE=$result
75        fi
76    fi
77
78    # check PATH
79    if [ -z "$QMAKE" ] ; then
80        result=`which qmake 2>/dev/null`
81        if [ -x "$result" ] ; then
82            QMAKE=$result
83        fi
84    fi
85
86    # check common rename
87    if [ -z "$QMAKE" ] ; then
88        result=`which qmake-q4 2>/dev/null`
89        if [ -x "$result" ] ; then
90            QMAKE=$result
91        fi
92    fi
93
94    # check pkg-config
95    if [ -z "$QMAKE" ] ; then
96        result=`pkg-config QtCore --variable=exec_prefix 2>/dev/null`
97        if [ ! -z "$result" ] ; then
98            result=$result/bin/qmake
99            if [ -x "$result" ] ; then
100                QMAKE=$result
101            fi
102        fi
103    fi
104
105    if [ -z "$QMAKE" ] ; then
106        echo "no!"
107        echo "ERROR: unable to find the 'qmake' tool"
108        echo "ERROR: unable to find the 'qmake' tool" >> $LOGFILE
109        qt_info
110        exit 1;
111    fi
112
113    echo "yes"
114    echo "found qmake at $QMAKE" >> $LOGFILE
115}
116
117qtversion_test() {
118    echo -n "Checking Qt version..."
119
120    result=`$QMAKE -query QT_VERSION`
121    if [ "$result" '<' "$QTMINVER" ] ; then
122        echo "no!"
123        echo "ERROR: incorrect Qt version found: $result"
124        echo "ERROR: incorrect Qt version found: $result" >> $LOGFILE
125        qt_info
126        exit 1;
127    fi
128    echo "$result"
129    echo "found Qt version $result" >> $LOGFILE
130}
131
132build_test() {
133    echo -n "Checking if a simple Qt program builds..."
134    cwd=`pwd`
135    rm -rf $BUILDDIR
136    mkdir -p $BUILDDIR
137    cd $BUILDDIR
138
139# write it
140cat >config.pro <<EOT
141TARGET = config
142CONFIG += qt $BUILDMODE
143CONFIG -= app_bundle
144QT -= gui
145HEADERS += config.h
146SOURCES += config.cpp
147EOT
148
149cat >config.h <<EOT
150#include <QtCore>
151class Config : public QCoreApplication {
152public:
153Config(int &argc, char **argv);
154};
155EOT
156
157cat >config.cpp <<EOT
158#include "config.h"
159Config::Config(int &argc, char **argv)
160: QCoreApplication(argc,argv) {setApplicationName("config");}
161int main(int argc, char **argv)
162{
163Config app(argc,argv);
164return 0;
165}
166EOT
167
168    # build it
169    $QMAKE config.pro > /dev/null
170    $MAKE >>$LOGFILE 2>&1
171    result=$?
172
173    cd $cwd
174    rm -rf $BUILDDIR
175
176    if [ "$result" != "0" ] ; then
177        echo "no!"
178        echo "ERROR: could not build a simple Qt program. See config.log"
179        echo "ERROR: could not build a simple Qt program" >> $LOGFILE
180        qt_info
181        exit 1;
182    fi
183    echo "yes"
184    echo "built simple Qt program" >> $LOGFILE
185}
186
187### Parse options ###################################################
188
189parse_options() {
190    while [ $# -gt 0 ] ; do
191        case "$1" in
192            --prefix=*)
193                PREFIX="${1#--prefix=}"
194                shift
195                ;;
196
197            --bindir=*)
198                BINDIR="${1#--bindir=}"
199                shift
200                ;;
201
202            --datadir=*)
203                DATADIR="${1#--datadir=}"
204                shift
205                ;;
206
207            --docdir=*)
208                DOCDIR="${1#--docdir=}"
209                shift
210                ;;
211
212            --qtdir=*)
213                QTDIR="${1#--qtdir=}"
214                shift
215                ;;
216
217            --spec=*)
218                QMAKESPEC="${1#--spec=}"
219                shift
220                ;;
221
222            --debug=*)
223                ENABLE_DEBUG="${1#--debug=}"
224                shift
225                ;;
226
227            --debug)
228                ENABLE_DEBUG="yes"
229                shift
230                ;;
231
232            --help) usage; exit ;;
233            *) usage; exit 1 ;;
234        esac
235    done
236}
237
238### Main ############################################################
239
240echo "Configuring $PACKAGE package..."
241
242### initialize
243
244SOURCEPATH=`dirname $0`
245SOURCEPATH=`(cd $SOURCEPATH; /bin/pwd)`
246BUILDDIR=$SOURCEPATH/$BUILDDIR
247LOGFILE=$SOURCEPATH/$LOGFILE
248
249if [ -e $LOGFILE ] ; then rm $LOGFILE ; fi
250echo "configure command: $0 $@" > $LOGFILE
251echo `date` >>$LOGFILE
252
253### parse command options
254
255parse_options $@
256
257QMAKE=${QMAKE:-}
258PREFIX=${PREFIX:-/usr/local}
259BINDIR=${BINDIR:-$PREFIX/bin}
260DATADIR=${DATADIR:-$PREFIX/share/$PACKAGE}
261DOCDIR=${DOCDIR:-$PREFIX/share/doc/$PACKAGE}
262
263if [ "$ENABLE_DEBUG" = "yes" ] ; then
264    BUILDMODE="debug"
265else
266    BUILDMODE="release"
267fi
268
269echo >> $LOGFILE
270echo PREFIX=$PREFIX >> $LOGFILE
271echo BINDIR=$BINDIR >> $LOGFILE
272echo DATADIR=$DATADIR >> $LOGFILE
273echo DOCDIR=$DOCDIR >> $LOGFILE
274echo QTDIR=$QTDIR >> $LOGFILE
275echo QMAKESPEC=$QMAKESPEC >> $LOGFILE
276echo ENABLE_DEBUG=$ENABLE_DEBUG >> $LOGFILE
277echo BUILDMODE=$BUILDMODE >> $LOGFILE
278echo >> $LOGFILE
279
280### tests
281
282make_test
283qmake_test
284qtversion_test
285build_test
286
287### export variables
288
289export BINDIR
290export DATADIR
291export DOCDIR
292if [ ! -z $QTDIR ] ; then
293    export QTDIR
294fi
295if [ ! -z $QMAKESPEC ] ; then
296    export QMAKESPEC
297fi
298
299### create Makefile
300
301echo "Creating Makefile"
302
303$QMAKE -o $SOURCEPATH/Makefile "CONFIG+=$BUILDMODE configure" $PROFILE > /dev/null 2>> $LOGFILE
304
305if [ $? != "0" ] ; then
306    echo "ERROR: could not create Makefile. See config.log"
307    echo "ERROR: could not create Makefile." >> $LOGFILE
308    echo "Command: $QMAKE -o $SOURCEPATH/Makefile \"CONFIG+=$BUILDMODE\" $PROFILE" >> $LOGFILE
309    exit 1
310fi
311
312### finished
313
314echo
315echo "$PACKAGE configured successfully"
316echo "Relax, don't worry, have a homebrew!"
317echo