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