xref: /kvm-unit-tests/configure (revision d864b580863bc86c89f92936b115eb3303756f8a)
1#!/usr/bin/env bash
2
3if [ -z "${BASH_VERSINFO[0]}" ] || [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
4    echo "Error: Bash version 4 or newer is required for the kvm-unit-tests"
5    exit 1
6fi
7
8srcdir=$(cd "$(dirname "$0")"; pwd)
9prefix=/usr/local
10cc=gcc
11ld=ld
12objcopy=objcopy
13objdump=objdump
14ar=ar
15addr2line=addr2line
16arch=`uname -m | sed -e 's/i.86/i386/;s/arm.*/arm/;s/ppc64.*/ppc64/'`
17host=$arch
18cross_prefix=
19endian=""
20pretty_print_stacks=yes
21environ_default=yes
22u32_long=
23wa_divide=
24vmm="qemu"
25errata_force=0
26erratatxt="$srcdir/errata.txt"
27host_key_document=
28
29usage() {
30    cat <<-EOF
31	Usage: $0 [options]
32
33	Options include:
34	    --arch=ARCH            architecture to compile for ($arch)
35	    --processor=PROCESSOR  processor to compile for ($arch)
36	    --vmm=VMM              virtual machine monitor to compile for (qemu
37	                           or kvmtool, default is qemu) (arm/arm64 only)
38	    --cross-prefix=PREFIX  cross compiler prefix
39	    --cc=CC		   c compiler to use ($cc)
40	    --ld=LD		   ld linker to use ($ld)
41	    --prefix=PREFIX        where to install things ($prefix)
42	    --endian=ENDIAN        endianness to compile for (little or big, ppc64 only)
43	    --[enable|disable]-pretty-print-stacks
44	                           enable or disable pretty stack printing (enabled by default)
45	    --[enable|disable]-default-environ
46	                           enable or disable the generation of a default environ when
47	                           no environ is provided by the user (enabled by default)
48	    --erratatxt=FILE       specify a file to use instead of errata.txt. Use
49	                           '--erratatxt=' to ensure no file is used.
50	    --host-key-document=HOST_KEY_DOCUMENT
51	                           Specify the machine-specific host-key document for creating
52	                           a PVM image with 'genprotimg' (s390x only)
53EOF
54    exit 1
55}
56
57while [[ "$1" = -* ]]; do
58    opt="$1"; shift
59    arg=
60    if [[ "$opt" = *=* ]]; then
61	arg="${opt#*=}"
62	opt="${opt%%=*}"
63    fi
64    case "$opt" in
65	--prefix)
66	    prefix="$arg"
67	    ;;
68        --arch)
69	    arch="$arg"
70	    ;;
71        --processor)
72	    processor="$arg"
73	    ;;
74	--vmm)
75	    vmm="$arg"
76	    ;;
77	--cross-prefix)
78	    cross_prefix="$arg"
79	    ;;
80	--endian)
81	    endian="$arg"
82	    ;;
83	--cc)
84	    cc="$arg"
85	    ;;
86	--ld)
87	    ld="$arg"
88	    ;;
89	--enable-pretty-print-stacks)
90	    pretty_print_stacks=yes
91	    ;;
92	--disable-pretty-print-stacks)
93	    pretty_print_stacks=no
94	    ;;
95	--enable-default-environ)
96	    environ_default=yes
97	    ;;
98	--disable-default-environ)
99	    environ_default=no
100	    ;;
101	--erratatxt)
102	    erratatxt=
103	    [ "$arg" ] && erratatxt=$(eval realpath "$arg")
104	    ;;
105	--host-key-document)
106	    host_key_document="$arg"
107	    ;;
108	--help)
109	    usage
110	    ;;
111	*)
112	    usage
113	    ;;
114    esac
115done
116
117if [ "$erratatxt" ] && [ ! -f "$erratatxt" ]; then
118    echo "erratatxt: $erratatxt does not exist or is not a regular file"
119    exit 1
120fi
121
122arch_name=$arch
123[ "$arch" = "aarch64" ] && arch="arm64"
124[ "$arch_name" = "arm64" ] && arch_name="aarch64"
125
126[ -z "$processor" ] && processor="$arch"
127
128if [ "$processor" = "arm64" ]; then
129    processor="cortex-a57"
130elif [ "$processor" = "arm" ]; then
131    processor="cortex-a15"
132fi
133
134if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
135    testdir=x86
136elif [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then
137    testdir=arm
138    if [ "$vmm" = "qemu" ]; then
139        arm_uart_early_addr=0x09000000
140    elif [ "$vmm" = "kvmtool" ]; then
141        arm_uart_early_addr=0x3f8
142        errata_force=1
143    else
144        echo '--vmm must be one of "qemu" or "kvmtool"!'
145        usage
146    fi
147elif [ "$arch" = "ppc64" ]; then
148    testdir=powerpc
149    firmware="$testdir/boot_rom.bin"
150    if [ "$endian" != "little" ] && [ "$endian" != "big" ]; then
151        echo "You must provide endianness (big or little)!"
152        usage
153    fi
154else
155    testdir=$arch
156fi
157if [ ! -d "$srcdir/$testdir" ]; then
158    echo "$testdir does not exist!"
159    exit 1
160fi
161if [ -f "$srcdir/$testdir/run" ]; then
162    ln -fs "$srcdir/$testdir/run" $testdir-run
163fi
164
165# check if uint32_t needs a long format modifier
166cat << EOF > lib-test.c
167__UINT32_TYPE__
168EOF
169u32_long=$("$cross_prefix$cc" -E lib-test.c | grep -v '^#' | grep -q long && echo yes)
170rm -f lib-test.c
171
172# check if slash can be used for division
173if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
174  cat << EOF > lib-test.S
175foo:
176    movl (8 / 2), %eax
177EOF
178  wa_divide=$("$cross_prefix$cc" -c lib-test.S >/dev/null 2>&1 || echo yes)
179  rm -f lib-test.{o,S}
180fi
181
182# require enhanced getopt
183getopt -T > /dev/null
184if [ $? -ne 4 ]; then
185    echo "Enhanced getopt is not available, add it to your PATH?"
186    exit 1
187fi
188
189# Are we in a separate build tree? If so, link the Makefile
190# and shared stuff so that 'make' and run_tests.sh work.
191if test ! -e Makefile; then
192    echo "linking Makefile..."
193    ln -s "$srcdir/Makefile" .
194
195    echo "linking tests..."
196    mkdir -p $testdir
197    ln -sf "$srcdir/$testdir/run" $testdir/
198    ln -sf "$srcdir/$testdir/unittests.cfg" $testdir/
199    ln -sf "$srcdir/run_tests.sh"
200
201    echo "linking scripts..."
202    ln -sf "$srcdir/scripts"
203fi
204
205# link lib/asm for the architecture
206rm -f lib/asm
207asm="asm-generic"
208if [ -d "$srcdir/lib/$arch/asm" ]; then
209	asm="$srcdir/lib/$arch/asm"
210elif [ -d "$srcdir/lib/$testdir/asm" ]; then
211	asm="$srcdir/lib/$testdir/asm"
212fi
213mkdir -p lib
214ln -sf "$asm" lib/asm
215
216
217# create the config
218cat <<EOF > config.mak
219SRCDIR=$srcdir
220PREFIX=$prefix
221HOST=$host
222ARCH=$arch
223ARCH_NAME=$arch_name
224PROCESSOR=$processor
225CC=$cross_prefix$cc
226LD=$cross_prefix$ld
227OBJCOPY=$cross_prefix$objcopy
228OBJDUMP=$cross_prefix$objdump
229AR=$cross_prefix$ar
230ADDR2LINE=$cross_prefix$addr2line
231TEST_DIR=$testdir
232FIRMWARE=$firmware
233ENDIAN=$endian
234PRETTY_PRINT_STACKS=$pretty_print_stacks
235ENVIRON_DEFAULT=$environ_default
236ERRATATXT=$erratatxt
237U32_LONG_FMT=$u32_long
238WA_DIVIDE=$wa_divide
239GENPROTIMG=${GENPROTIMG-genprotimg}
240HOST_KEY_DOCUMENT=$host_key_document
241EOF
242
243cat <<EOF > lib/config.h
244#ifndef CONFIG_H
245#define CONFIG_H 1
246/*
247 * Generated file. DO NOT MODIFY.
248 *
249 */
250
251EOF
252if [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then
253cat <<EOF >> lib/config.h
254
255#define CONFIG_UART_EARLY_BASE ${arm_uart_early_addr}
256#define CONFIG_ERRATA_FORCE ${errata_force}
257
258EOF
259fi
260echo "#endif" >> lib/config.h
261