summaryrefslogtreecommitdiff
path: root/util/msrtool/configure
blob: 88c38af6bd88176ba27f89fe025424bb7e92ccce (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
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
#
# This file is part of msrtool.
#
# Copyright (c) 2008, 2009 Peter Stuge <peter@stuge.se>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

# If this is left unset, try to set the version string from the highest
# revision number of the checked out files.
VERSION=""

REV="`svnversion -c . 2>/dev/null | sed 's,.*:,,' 2>/dev/null`"
VERSION="${VERSION:-$REV}"

findprog() {
	NPARMS=$#
	WHAT="${1}"
	shift
	FROMENV="${1}"
	shift
	printf "searching for ${WHAT} (${*})..." 1>&2
	test -n "${FROMENV}" && {
		echo "  using environment: ${FROMENV}" 1>&2
		echo "${FROMENV}"
		exit 0
	}
	i=2
	while test $i -lt $NPARMS; do
		test -z "${1}" && {
			shift
			i=$(($i+1))
			continue
		}
		FILE="`which "${1}" 2>/dev/null`"
		test $? -eq 0 && {
			echo "${1}"
			break
		}
		shift
		i=$(($i+1))
	done
	test -z "${1}" && {
		echo "  not found!" 1>&2
		echo 1>&2
		echo "This is a fatal error, configure is exiting!" 1>&2
		exit 1
	}
	echo "  using ${FILE} in PATH" 1>&2
	exit 0
}

trycompile() {
	NPARMS=$#
	WHAT="${1}"
	shift
	printf "finding CFLAGS for ${WHAT}... " 1>&2
	OUT="${OUT}\n${CC} ${CFLAGS} -o .config.o -c .config.c"
	OUT="${OUT}\n`echo "${CC} ${CFLAGS} -o .config.o -c .config.c"|sh 2>&1`"
	test $? -eq 0 && {
		echo " using: ${CFLAGS}" 1>&2
		echo "${CFLAGS}"
		exit 0
	}
	i=1
	while test $i -lt $NPARMS; do
		OUT="${OUT}\n${CC} ${CFLAGS} ${1} -o .config.o -c .config.c 2>&1"
		OUT="${OUT}\n`echo "${CC} ${CFLAGS} ${1} -o .config.o -c .config.c"|sh 2>&1`"
		test $? -eq 0 && {
			echo " using: ${CFLAGS} ${1}" 1>&2
			echo "${CFLAGS} ${1}"
			exit 0
		} 
		shift
		i=$(($i+1))
	done
	echo "failed!" 1>&2
	echo 1>&2
	printf "The following compiler commands were executed:" 1>&2
	echo -e "${OUT}\n" 1>&2
	echo "This is a fatal error, configure is exiting!" 1>&2
	echo 1>&2
	echo "You can try to fix this by manually setting CFLAGS in the environment before" 1>&2
	echo "running configure. E.g.:" 1>&2
	echo "CFLAGS=-I/opt/somedir/include ./configure" 1>&2
	echo 1>&2
	exit 1
}

trylink() {
	NPARMS=$#
	WHAT="${1}"
	shift
	printf "finding LDFLAGS for ${WHAT}... " 1>&2
	OUT="${OUT}\n${CC} -o .config .config.o ${LDFLAGS} 2>&1"
	OUT="${OUT}\n`echo "${CC} -o .config .config.o ${LDFLAGS}"|sh 2>&1`"
	test $? -eq 0 && {
		echo " using: ${LDFLAGS}" 1>&2
		echo "${LDFLAGS}"
		exit 0
	}
	i=1
	while test $i -lt $NPARMS; do
		OUT="${OUT}\n${CC} -o .config .config.o ${LDFLAGS} ${1} 2>&1"
		OUT="${OUT}\n`echo "${CC} -o .config .config.o ${LDFLAGS} ${1}"|sh 2>&1`"
		test $? -eq 0 && {
			echo " using: ${LDFLAGS} ${1}" 1>&2
			echo "${LDFLAGS} ${1}"
			exit 0
		}
		shift
		i=$(($i+1))
	done
	echo "failed!" 1>&2
	echo 1>&2
	printf "The following linker commands were executed:" 1>&2
	echo -e "${OUT}\n" 1>&2
	echo "This is a fatal error, configure is exiting!" 1>&2
	echo 1>&2
	echo "You can try to fix this by manually setting LDFLAGS in the environment before" 1>&2
	echo "running configure. E.g.:" 1>&2
	echo "LDFLAGS=-L/opt/somedir/lib ./configure" 1>&2
	echo 1>&2
	exit 1
}

CC=`findprog "compiler" "${CC}" gcc cc icc` || exit
INSTALL=`findprog "install" "${INSTALL}" install ginstall` || exit

test -n "$DEBUG" && myCFLAGS="-O2 -g" || myCFLAGS="-Os"
CFLAGS="${CFLAGS} ${myCFLAGS} -Wall -Werror"

cat > .config.c << EOF
#include <pci/pci.h>
struct pci_access *pacc;
int main(int argc, char *argv[])
{ pacc = pci_alloc(); return 0; }
EOF

pc_CFLAGS="`pkg-config libpci --cflags 2>/dev/null`"
pc_LDFLAGS="`pkg-config libpci --libs 2>/dev/null`"
CFLAGS=`trycompile "libpci (from pciutils)" "${pc_CFLAGS}" "-I/usr/local/include"` || {
	rm -f .config.c
	exit 1
}
LDFLAGS=`trylink "libpci (from pciutils)" "${pc_LDFLAGS}" "-lpci -lz" "-L/usr/local/lib -lpci -lz" "-framework DirectIO -lpci -lz"` || {
	rm -f .config.c .config.o
	exit 1
}
rm -f .config.c .config.o .config

PREFIX="${PREFIX:-/usr/local}"

echo
echo "configured using the following settings:"
echo
echo "VERSION=${VERSION}"
echo "CC=${CC}"
echo "CFLAGS=${CFLAGS}"
echo "LDFLAGS=${LDFLAGS}"
echo "INSTALL=${INSTALL}"
echo "PREFIX=${PREFIX}"
echo
printf "creating Makefile..."
echo "# This file was automatically generated by configure" > Makefile
sed -e "s#@VERSION@#${VERSION}#g" \
	-e "s#@CC@#${CC}#g" \
	-e "s#@CFLAGS@#${CFLAGS}#g" \
	-e "s#@LDFLAGS@#${LDFLAGS}#g" \
	-e "s#@INSTALL@#${INSTALL}#g" \
	-e "s#@PREFIX@#${PREFIX}#g" \
	Makefile.in >> Makefile
echo "  done"
echo