1#! /bin/sh
2
3if test -z "$source_path"; then
4  echo Do not invoke this script directly.  It is called
5  echo automatically by configure.
6  exit 1
7fi
8
9write_c_skeleton() {
10    cat > $TMPC <<EOF
11int main(void) { return 0; }
12EOF
13}
14
15has() {
16  command -v "$1" >/dev/null 2>&1
17}
18
19do_compiler() {
20  # Run the compiler, capturing its output to the log. First argument
21  # is compiler binary to execute.
22  local compiler="$1"
23  shift
24  if test -n "$BASH_VERSION"; then eval '
25      echo >>config.log "
26funcs: ${FUNCNAME[*]}
27lines: ${BASH_LINENO[*]}"
28  '; fi
29  echo $compiler "$@" >> config.log
30  $compiler "$@" >> config.log 2>&1 || return $?
31}
32
33
34TMPDIR1="config-temp"
35TMPC="${TMPDIR1}/qemu-conf.c"
36TMPE="${TMPDIR1}/qemu-conf.exe"
37
38container="no"
39if has "docker" || has "podman"; then
40  container=$($python $source_path/tests/docker/docker.py probe)
41fi
42
43# cross compilers defaults, can be overridden with --cross-cc-ARCH
44: ${cross_cc_aarch64="aarch64-linux-gnu-gcc"}
45: ${cross_cc_aarch64_be="$cross_cc_aarch64"}
46: ${cross_cc_cflags_aarch64_be="-mbig-endian"}
47: ${cross_cc_arm="arm-linux-gnueabihf-gcc"}
48: ${cross_cc_cflags_armeb="-mbig-endian"}
49: ${cross_cc_i386="i386-pc-linux-gnu-gcc"}
50: ${cross_cc_cflags_i386="-m32"}
51: ${cross_cc_x86_64="x86_64-pc-linux-gnu-gcc"}
52: ${cross_cc_cflags_x86_64="-m64"}
53: ${cross_cc_ppc="powerpc-linux-gnu-gcc"}
54: ${cross_cc_cflags_ppc="-m32"}
55: ${cross_cc_ppc64="powerpc-linux-gnu-gcc"}
56: ${cross_cc_cflags_ppc64="-m64"}
57: ${cross_cc_ppc64le="powerpc64le-linux-gnu-gcc"}
58: ${cross_cc_cflags_s390x="-m64"}
59: ${cross_cc_cflags_sparc="-m32 -mv8plus -mcpu=ultrasparc"}
60: ${cross_cc_cflags_sparc64="-m64 -mcpu=ultrasparc"}
61
62for target in $target_list; do
63  arch=${target%%-*}
64  case $arch in
65    arm|armeb)
66      arches=arm
67      ;;
68    aarch64|aarch64_be)
69      arches="aarch64 arm"
70      ;;
71    mips*)
72      arches=mips
73      ;;
74    ppc*)
75      arches=ppc
76      ;;
77    sh4|sh4eb)
78      arches=sh4
79      ;;
80    x86_64)
81      arches="x86_64 i386"
82      ;;
83    xtensa|xtensaeb)
84      arches=xtensa
85      ;;
86    alpha|cris|hppa|i386|lm32|m68k|openrisc|riscv64|s390x|sh4|sparc64)
87      arches=$target
88      ;;
89    *)
90      continue
91      ;;
92  esac
93
94  container_image=
95  case $target in
96    aarch64-*)
97      # We don't have any bigendian build tools so we only use this for AArch64
98      container_image=debian-arm64-cross
99      container_cross_cc=aarch64-linux-gnu-gcc
100      ;;
101    alpha-*)
102      container_image=debian-alpha-cross
103      container_cross_cc=alpha-linux-gnu-gcc
104      ;;
105    arm-*)
106      # We don't have any bigendian build tools so we only use this for ARM
107      container_image=debian-armhf-cross
108      container_cross_cc=arm-linux-gnueabihf-gcc
109      ;;
110    cris-*)
111      container_image=fedora-cris-cross
112      container_cross_cc=cris-linux-gnu-gcc
113      ;;
114    hppa-*)
115      container_image=debian-hppa-cross
116      container_cross_cc=hppa-linux-gnu-gcc
117      ;;
118    i386-*)
119      container_image=fedora-i386-cross
120      container_cross_cc=gcc
121      ;;
122    m68k-*)
123      container_image=debian-m68k-cross
124      container_cross_cc=m68k-linux-gnu-gcc
125      ;;
126    mips64el-*)
127      container_image=debian-mips64el-cross
128      container_cross_cc=mips64el-linux-gnuabi64-gcc
129      ;;
130    mips64-*)
131      container_image=debian-mips64-cross
132      container_cross_cc=mips64-linux-gnuabi64-gcc
133      ;;
134    mipsel-*)
135      container_image=debian-mipsel-cross
136      container_cross_cc=mipsel-linux-gnu-gcc
137      ;;
138    mips-*)
139      container_image=debian-mips-cross
140      container_cross_cc=mips-linux-gnu-gcc
141      ;;
142    ppc-*|ppc64abi32-*)
143      container_image=debian-powerpc-cross
144      container_cross_cc=powerpc-linux-gnu-gcc
145      ;;
146    ppc64-*)
147      container_image=debian-ppc64-cross
148      container_cross_cc=powerpc64-linux-gnu-gcc
149      ;;
150    ppc64le-*)
151      container_image=debian-ppc64el-cross
152      container_cross_cc=powerpc64le-linux-gnu-gcc
153      ;;
154    riscv64-*)
155      container_image=debian-riscv64-cross
156      container_cross_cc=riscv64-linux-gnu-gcc
157      ;;
158    s390x-*)
159      container_image=debian-s390x-cross
160      container_cross_cc=s390x-linux-gnu-gcc
161      ;;
162    sh4-*)
163      container_image=debian-sh4-cross
164      container_cross_cc=sh4-linux-gnu-gcc
165      ;;
166    sparc64-*)
167      container_image=debian-sparc64-cross
168      container_cross_cc=sparc64-linux-gnu-gcc
169      ;;
170    xtensa*-softmmu)
171      container_image=debian-xtensa-cross
172
173      # default to the dc232b cpu
174      container_cross_cc=/opt/2018.02/xtensa-dc232b-elf/bin/xtensa-dc232b-elf-gcc
175      ;;
176  esac
177
178  config_target_mak=tests/tcg/config-$target.mak
179
180  echo "# Automatically generated by configure - do not modify" > $config_target_mak
181  echo "TARGET_NAME=$arch" >> $config_target_mak
182  case $target in
183    *-linux-user | *-bsd-user)
184      echo "CONFIG_USER_ONLY=y" >> $config_target_mak
185      echo "QEMU=\$(BUILD_DIR)/$target/qemu-$arch" >> $config_target_mak
186      ;;
187    *-softmmu)
188      echo "CONFIG_SOFTMMU=y" >> $config_target_mak
189      echo "QEMU=\$(BUILD_DIR)/$target/qemu-system-$arch" >> $config_target_mak
190      ;;
191  esac
192
193  eval "target_compiler_cflags=\${cross_cc_cflags_$arch}"
194  echo "CROSS_CC_GUEST_CFLAGS=$target_compiler_cflags" >> $config_target_mak
195
196  got_cross_cc=no
197  for i in $arch $arches; do
198    if eval test "x\${cross_cc_$i+yes}" != xyes; then
199      continue
200    fi
201
202    eval "target_compiler=\${cross_cc_$i}"
203    if ! has $target_compiler; then
204      continue
205    fi
206    write_c_skeleton
207    if ! do_compiler "$target_compiler" $target_compiler_cflags -o $TMPE $TMPC -static ; then
208      # For host systems we might get away with building without -static
209      if ! do_compiler "$target_compiler" $target_compiler_cflags -o $TMPE $TMPC ; then
210        continue
211      fi
212      echo "CROSS_CC_GUEST_STATIC=y" >> $config_target_mak
213    else
214      echo "CROSS_CC_GUEST_STATIC=y" >> $config_target_mak
215    fi
216    echo "CROSS_CC_GUEST=$target_compiler" >> $config_target_mak
217    enabled_cross_compilers="$enabled_cross_compilers $target_compiler"
218    got_cross_cc=yes
219    break
220  done
221
222  if test $got_cross_cc = no && test "$container" != no && test -n "$container_image"; then
223    echo "DOCKER_IMAGE=$container_image" >> $config_target_mak
224    echo "DOCKER_CROSS_CC_GUEST=$container_cross_cc" >> $config_target_mak
225  fi
226done
227
228# report container support state
229echo "cross containers  $container"
230
231if test -n "$enabled_cross_compilers"; then
232    echo
233    echo "NOTE: guest cross-compilers enabled:$enabled_cross_compilers"
234fi
235