Update xcompile to search for x86_64 toolchain.
[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 -Werror $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 loops over all supported architectures in TARCH
44 TARCH=('i386' 'x86_64')
45 TWIDTH=32
46 for search_for in "${TARCH[@]}"; do
47         TARCH_SEARCH=("${TARCH_SEARCH[@]}" ${XGCCPATH}${search_for}-elf- ${search_for}-elf-)
48 done
49 echo '# TARCH_SEARCH='${TARCH_SEARCH[@]}
50
51 for gccprefixes in "${TARCH_SEARCH[@]}" ""; do
52         if ! which ${gccprefixes}as 2>/dev/null >/dev/null; then
53                 continue
54         fi
55         rm -f ${TMPFILE}.o
56         if ${gccprefixes}as -o ${TMPFILE}.o ${TMPFILE}; then
57                 TYPE=`${gccprefixes}objdump -p ${TMPFILE}.o`
58                 if [ ${TYPE##* } == "elf${TWIDTH}-${TARCH}" ]; then
59                         GCCPREFIX=$gccprefixes
60                         ASFLAGS=
61                         CFLAGS=
62                         LDFLAGS=
63                         break
64                 fi
65         fi
66         if ${gccprefixes}as --32 -o ${TMPFILE}.o ${TMPFILE}; then
67                 TYPE=`${gccprefixes}objdump -p ${TMPFILE}.o`
68                 if [ ${TYPE##* } == "elf${TWIDTH}-${TARCH}" ]; then
69                         GCCPREFIX=$gccprefixes
70                         ASFLAGS=--32
71                         CFLAGS="-m32 -Wl,-b,elf32-i386 -Wl,-melf_i386 "
72                         LDFLAGS="-b elf32-i386 -melf_i386"
73                         break
74                 fi
75         fi
76 done
77 rm -f $TMPFILE ${TMPFILE}.o
78
79 if [ "$GCCPREFIX" = "invalid" ]; then
80         echo '$(error no suitable gcc found)'
81         exit 1
82 fi
83
84 CC="${GCCPREFIX}gcc"
85 testcc "$CC" "$CFLAGS-Wa,--divide " && CFLAGS="$CFLAGS-Wa,--divide "
86 testcc "$CC" "$CFLAGS-fno-stack-protector " && CFLAGS="$CFLAGS-fno-stack-protector "
87 testcc "$CC" "$CFLAGS-Wl,--build-id=none " && CFLAGS="$CFLAGS-Wl,--build-id=none "
88 # GCC 4.6 is much more picky about unused variables. Turn off it's warnings for
89 # now:
90 testcc "$CC" "$CFLAGS-Wno-unused-but-set-variable " && \
91                CFLAGS="$CFLAGS-Wno-unused-but-set-variable "
92
93 if which gcc 2>/dev/null >/dev/null; then
94         HOSTCC=gcc
95 else
96         HOSTCC=cc
97 fi
98
99 if [ "`${XGCCPATH}/iasl 2>/dev/null | grep -c ACPI`" -gt 0 ]; then
100         IASL=${XGCCPATH}iasl
101 else
102         IASL=iasl
103 fi
104
105 cat << EOF
106 # elf${TWIDTH}-${TARCH} toolchain
107 AS:=${GCCPREFIX}as ${ASFLAGS}
108 CC:=${GCCPREFIX}gcc ${CFLAGS}
109 AR:=${GCCPREFIX}ar
110 LD:=${GCCPREFIX}ld ${LDFLAGS}
111 STRIP:=${GCCPREFIX}strip
112 NM:=${GCCPREFIX}nm
113 OBJCOPY:=${GCCPREFIX}objcopy
114 OBJDUMP:=${GCCPREFIX}objdump
115
116 IASL:=${IASL}
117
118 # native toolchain
119 HOSTCC:=${HOSTCC}
120 EOF
121