9a0a295c75d86e2a91b9bf09978e5db1bce23aa8
[coreboot.git] / util / xcompile / xcompile
1 #!/bin/sh
2 #
3 # This file is part of the coreboot project.
4 #
5 # Copyright (C) 2007-2010 coresystems GmbH
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 as published by
9 # the Free Software Foundation; version 2 of the License.
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 testcc()
22 {
23         echo "_start(void) {}" > .$$$$.c
24         $1 -nostdlib $2 .$$$$.c -o .$$$$.tmp 2>/dev/null >/dev/null
25         ret=$?
26         rm -f .$$$$.c .$$$$.tmp
27         return $ret
28 }
29
30 for make in make gmake gnumake; do
31         if [ "`$make --version 2>/dev/null | grep -c GNU`" -gt 0 ]; then
32                 MAKE=$make
33                 break
34         fi
35 done
36
37 GCCPREFIX=invalid
38 XGCCPATH=${1:-"`pwd`/util/crossgcc/xgcc/bin/"}
39 echo '#XGCCPATH='${XGCCPATH}
40 TMPFILE=`mktemp /tmp/temp.XXXX 2>/dev/null || echo /tmp/temp.78gOIUGz`
41 touch $TMPFILE
42
43 # This should be a loop over all supported architectures
44 TARCH=i386
45 TWIDTH=32
46 for gccprefixes in ${XGCCPATH}${TARCH}-elf- ${TARCH}-elf- ""; do
47         if ! which ${gccprefixes}as 2>/dev/null >/dev/null; then
48                 continue
49         fi
50         rm -f ${TMPFILE}.o
51         if ${gccprefixes}as -o ${TMPFILE}.o ${TMPFILE}; then
52                 TYPE=`${gccprefixes}objdump -p ${TMPFILE}.o`
53                 if [ ${TYPE##* } == "elf${TWIDTH}-${TARCH}" ]; then
54                         GCCPREFIX=$gccprefixes
55                         ASFLAGS=
56                         CFLAGS=
57                         LDFLAGS=
58                         break
59                 fi
60         fi
61         if ${gccprefixes}as --32 -o ${TMPFILE}.o ${TMPFILE}; then
62                 TYPE=`${gccprefixes}objdump -p ${TMPFILE}.o`
63                 if [ ${TYPE##* } == "elf${TWIDTH}-${TARCH}" ]; then
64                         GCCPREFIX=$gccprefixes
65                         ASFLAGS=--32
66                         CFLAGS="-m32 "
67                         LDFLAGS="-b elf32-i386"
68                         break
69                 fi
70         fi
71 done
72 rm -f $TMPFILE ${TMPFILE}.o
73
74 if [ "$GCCPREFIX" = "invalid" ]; then
75         echo '$(error no suitable gcc found)'
76         exit 1
77 fi
78
79 CC="${GCCPREFIX}gcc"
80 testcc "$CC" "$CFLAGS-Wa,--divide " && CFLAGS="$CFLAGS-Wa,--divide "
81 testcc "$CC" "$CFLAGS-fno-stack-protector " && CFLAGS="$CFLAGS-fno-stack-protector "
82 testcc "$CC" "$CFLAGS-Wl,--build-id=none " && CFLAGS="$CFLAGS-Wl,--build-id=none "
83 # GCC 4.6 is much more picky about unused variables. Turn off it's warnings for
84 # now:
85 testcc "$CC" "$CFLAGS-Wno-unused-but-set-variable " && \
86                CFLAGS="$CFLAGS-Wno-unused-but-set-variable "
87
88 if which gcc 2>/dev/null >/dev/null; then
89         HOSTCC=gcc
90 else
91         HOSTCC=cc
92 fi
93
94 if [ "`${XGCCPATH}/iasl 2>/dev/null | grep -c ACPI`" -gt 0 ]; then
95         IASL=${XGCCPATH}iasl
96 else
97         IASL=iasl
98 fi
99
100 cat << EOF
101 # elf${TWIDTH}-${TARCH} toolchain
102 AS:=${GCCPREFIX}as ${ASFLAGS}
103 CC:=${GCCPREFIX}gcc ${CFLAGS}
104 AR:=${GCCPREFIX}ar
105 LD:=${GCCPREFIX}ld ${LDFLAGS}
106 STRIP:=${GCCPREFIX}strip
107 NM:=${GCCPREFIX}nm
108 OBJCOPY:=${GCCPREFIX}objcopy
109 OBJDUMP:=${GCCPREFIX}objdump
110
111 IASL:=${IASL}
112
113 # native toolchain
114 HOSTCC:=${HOSTCC}
115 EOF
116