correctly mark code segments as code in SELF
[coreboot.git] / util / msrtool / configure
1 #!/bin/sh
2 #
3 # This file is part of msrtool.
4 #
5 # Copyright (c) 2008, 2009 Peter Stuge <peter@stuge.se>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 #
20
21 # If this is left unset, try to set the version string from the highest
22 # revision number of the checked out files.
23 VERSION=""
24
25 REV="`svnversion -c . 2>/dev/null | sed 's,.*:,,' 2>/dev/null`"
26 VERSION="${VERSION:-$REV}"
27
28 findprog() {
29         NPARMS=$#
30         WHAT="${1}"
31         shift
32         FROMENV="${1}"
33         shift
34         printf "searching for ${WHAT} (${*})..." 1>&2
35         test -n "${FROMENV}" && {
36                 echo "  using environment: ${FROMENV}" 1>&2
37                 echo "${FROMENV}"
38                 exit 0
39         }
40         i=2
41         while test $i -lt $NPARMS; do
42                 test -z "${1}" && {
43                         shift
44                         i=$(($i+1))
45                         continue
46                 }
47                 FILE="`which "${1}" 2>/dev/null`"
48                 test $? -eq 0 && {
49                         echo "${1}"
50                         break
51                 }
52                 shift
53                 i=$(($i+1))
54         done
55         test -z "${1}" && {
56                 echo "  not found!" 1>&2
57                 echo 1>&2
58                 echo "This is a fatal error, configure is exiting!" 1>&2
59                 exit 1
60         }
61         echo "  using ${FILE} in PATH" 1>&2
62         exit 0
63 }
64
65 trycompile() {
66         NPARMS=$#
67         WHAT="${1}"
68         shift
69         printf "finding CFLAGS for ${WHAT}... " 1>&2
70         OUT="${OUT}\n${CC} ${CFLAGS} -o .config.o -c .config.c"
71         OUT="${OUT}\n`echo "${CC} ${CFLAGS} -o .config.o -c .config.c"|sh 2>&1`"
72         test $? -eq 0 && {
73                 echo " using: ${CFLAGS}" 1>&2
74                 echo "${CFLAGS}"
75                 exit 0
76         }
77         i=1
78         while test $i -lt $NPARMS; do
79                 OUT="${OUT}\n${CC} ${CFLAGS} ${1} -o .config.o -c .config.c 2>&1"
80                 OUT="${OUT}\n`echo "${CC} ${CFLAGS} ${1} -o .config.o -c .config.c"|sh 2>&1`"
81                 test $? -eq 0 && {
82                         echo " using: ${CFLAGS} ${1}" 1>&2
83                         echo "${CFLAGS} ${1}"
84                         exit 0
85                 }
86                 shift
87                 i=$(($i+1))
88         done
89         echo "failed!" 1>&2
90         echo 1>&2
91         printf "The following compiler commands were executed:" 1>&2
92         echo -e "${OUT}\n" 1>&2
93         echo "This is a fatal error, configure is exiting!" 1>&2
94         echo 1>&2
95         echo "You can try to fix this by manually setting CFLAGS in the environment before" 1>&2
96         echo "running configure. E.g.:" 1>&2
97         echo "CFLAGS=-I/opt/somedir/include ./configure" 1>&2
98         echo 1>&2
99         exit 1
100 }
101
102 trylink() {
103         NPARMS=$#
104         WHAT="${1}"
105         shift
106         printf "finding LDFLAGS for ${WHAT}... " 1>&2
107         OUT="${OUT}\n${CC} -o .config .config.o ${LDFLAGS} 2>&1"
108         OUT="${OUT}\n`echo "${CC} -o .config .config.o ${LDFLAGS}"|sh 2>&1`"
109         test $? -eq 0 && {
110                 echo " using: ${LDFLAGS}" 1>&2
111                 echo "${LDFLAGS}"
112                 exit 0
113         }
114         i=1
115         while test $i -lt $NPARMS; do
116                 OUT="${OUT}\n${CC} -o .config .config.o ${LDFLAGS} ${1} 2>&1"
117                 OUT="${OUT}\n`echo "${CC} -o .config .config.o ${LDFLAGS} ${1}"|sh 2>&1`"
118                 test $? -eq 0 && {
119                         echo " using: ${LDFLAGS} ${1}" 1>&2
120                         echo "${LDFLAGS} ${1}"
121                         exit 0
122                 }
123                 shift
124                 i=$(($i+1))
125         done
126         echo "failed!" 1>&2
127         echo 1>&2
128         printf "The following linker commands were executed:" 1>&2
129         echo -e "${OUT}\n" 1>&2
130         echo "This is a fatal error, configure is exiting!" 1>&2
131         echo 1>&2
132         echo "You can try to fix this by manually setting LDFLAGS in the environment before" 1>&2
133         echo "running configure. E.g.:" 1>&2
134         echo "LDFLAGS=-L/opt/somedir/lib ./configure" 1>&2
135         echo 1>&2
136         exit 1
137 }
138
139 CC=`findprog "compiler" "${CC}" gcc cc icc` || exit
140 INSTALL=`findprog "install" "${INSTALL}" install ginstall` || exit
141
142 test -n "$DEBUG" && myCFLAGS="-O2 -g" || myCFLAGS="-Os"
143 CFLAGS="${CFLAGS} ${myCFLAGS} -Wall -Werror"
144
145 cat > .config.c << EOF
146 #include <pci/pci.h>
147 struct pci_access *pacc;
148 int main(int argc, char *argv[])
149 { pacc = pci_alloc(); return 0; }
150 EOF
151
152 pc_CFLAGS="`pkg-config libpci --cflags 2>/dev/null`"
153 pc_LDFLAGS="`pkg-config libpci --libs 2>/dev/null`"
154 CFLAGS=`trycompile "libpci (from pciutils)" "${pc_CFLAGS}" "-I/usr/local/include"` || {
155         rm -f .config.c
156         exit 1
157 }
158 LDFLAGS=`trylink "libpci (from pciutils)" "${pc_LDFLAGS}" "-lpci -lz" "-L/usr/local/lib -lpci -lz" "-framework DirectHW -lpci -lz"` || {
159         rm -f .config.c .config.o
160         exit 1
161 }
162 rm -f .config.c .config.o .config
163
164 PREFIX="${PREFIX:-/usr/local}"
165
166 echo
167 echo "configured using the following settings:"
168 echo
169 echo "VERSION=${VERSION}"
170 echo "CC=${CC}"
171 echo "CFLAGS=${CFLAGS}"
172 echo "LDFLAGS=${LDFLAGS}"
173 echo "INSTALL=${INSTALL}"
174 echo "PREFIX=${PREFIX}"
175 echo
176 printf "creating Makefile..."
177 echo "# This file was automatically generated by configure" > Makefile
178 sed -e "s#@VERSION@#${VERSION}#g" \
179         -e "s#@CC@#${CC}#g" \
180         -e "s#@CFLAGS@#${CFLAGS}#g" \
181         -e "s#@LDFLAGS@#${LDFLAGS}#g" \
182         -e "s#@INSTALL@#${INSTALL}#g" \
183         -e "s#@PREFIX@#${PREFIX}#g" \
184         Makefile.in >> Makefile
185 echo "  done"
186 echo