1#!/usr/bin/env bash 2 3srcdir=$(cd "$(dirname "$0")"; pwd) 4prefix=/usr/local 5cc=gcc 6cxx=g++ 7ld=ld 8objcopy=objcopy 9objdump=objdump 10ar=ar 11addr2line=addr2line 12arch=`uname -m | sed -e 's/i.86/i386/;s/arm.*/arm/;s/ppc64.*/ppc64/'` 13host=$arch 14cross_prefix= 15endian="" 16pretty_print_stacks=yes 17u32_long= 18 19usage() { 20 cat <<-EOF 21 Usage: $0 [options] 22 23 Options include: 24 --arch=ARCH architecture to compile for ($arch) 25 --processor=PROCESSOR processor to compile for ($arch) 26 --cross-prefix=PREFIX cross compiler prefix 27 --cc=CC c compiler to use ($cc) 28 --cxx=CXX c++ compiler to use ($cxx) 29 --ld=LD ld linker to use ($ld) 30 --prefix=PREFIX where to install things ($prefix) 31 --endian=ENDIAN endianness to compile for (little or big, ppc64 only) 32 --[enable|disable]-pretty-print-stacks 33 enable or disable pretty stack printing (enabled by default) 34EOF 35 exit 1 36} 37 38while [[ "$1" = -* ]]; do 39 opt="$1"; shift 40 arg= 41 if [[ "$opt" = *=* ]]; then 42 arg="${opt#*=}" 43 opt="${opt%%=*}" 44 fi 45 case "$opt" in 46 --prefix) 47 prefix="$arg" 48 ;; 49 --arch) 50 arch="$arg" 51 ;; 52 --processor) 53 processor="$arg" 54 ;; 55 --cross-prefix) 56 cross_prefix="$arg" 57 ;; 58 --endian) 59 endian="$arg" 60 ;; 61 --cc) 62 cc="$arg" 63 ;; 64 --cxx) 65 cxx="$arg" 66 ;; 67 --ld) 68 ld="$arg" 69 ;; 70 --enable-pretty-print-stacks) 71 pretty_print_stacks=yes 72 ;; 73 --disable-pretty-print-stacks) 74 pretty_print_stacks=no 75 ;; 76 --help) 77 usage 78 ;; 79 *) 80 usage 81 ;; 82 esac 83done 84 85arch_name=$arch 86[ "$arch" = "aarch64" ] && arch="arm64" 87[ "$arch_name" = "arm64" ] && arch_name="aarch64" 88 89[ -z "$processor" ] && processor="$arch" 90 91if [ "$processor" = "arm64" ]; then 92 processor="cortex-a57" 93elif [ "$processor" = "arm" ]; then 94 processor="cortex-a15" 95fi 96 97if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then 98 testdir=x86 99elif [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then 100 testdir=arm 101elif [ "$arch" = "ppc64" ]; then 102 testdir=powerpc 103 firmware="$testdir/boot_rom.bin" 104 if [ "$endian" != "little" ] && [ "$endian" != "big" ]; then 105 echo "You must provide endianness (big or little)!" 106 usage 107 fi 108else 109 testdir=$arch 110fi 111if [ ! -d "$srcdir/$testdir" ]; then 112 echo "$testdir does not exist!" 113 exit 1 114fi 115if [ -f "$srcdir/$testdir/run" ]; then 116 ln -fs "$srcdir/$testdir/run" $testdir-run 117fi 118 119# check if uint32_t needs a long format modifier 120cat << EOF > lib-test.c 121__UINT32_TYPE__ 122EOF 123u32_long=$($cross_prefix$cc -E lib-test.c | grep -v '^#' | grep -q long && echo yes) 124rm -f lib-test.c 125 126# api/: check for dependent 32 bit libraries and gnu++11 support 127if [ "$testdir" = "x86" ]; then 128 echo 'int main () {}' > lib-test.c 129 $cc -m32 -o /dev/null -lstdc++ -lpthread -lrt lib-test.c &> /dev/null 130 exit=$? 131 $cxx -m32 -o /dev/null -std=gnu++11 lib-test.c &> /dev/null 132 if [ $? -eq 0 -a $exit -eq 0 ]; then 133 api=true 134 fi 135 rm -f lib-test.c 136fi 137 138# Are we in a separate build tree? If so, link the Makefile 139# and shared stuff so that 'make' and run_tests.sh work. 140if test ! -e Makefile; then 141 echo "linking Makefile..." 142 ln -s "$srcdir/Makefile" . 143 144 echo "linking tests..." 145 mkdir -p $testdir 146 ln -sf "$srcdir/$testdir/run" $testdir/ 147 ln -sf "$srcdir/$testdir/unittests.cfg" $testdir/ 148 ln -sf "$srcdir/run_tests.sh" 149 150 echo "linking scripts..." 151 ln -sf "$srcdir/scripts" 152fi 153 154# link lib/asm for the architecture 155rm -f lib/asm 156asm=asm-generic 157if [ -d "$srcdir/lib/$arch/asm" ]; then 158 asm="$srcdir/lib/$arch/asm" 159elif [ -d "$srcdir/lib/$testdir/asm" ]; then 160 asm="$srcdir/lib/$testdir/asm" 161fi 162mkdir -p lib 163ln -sf "$asm" lib/asm 164 165 166# create the config 167cat <<EOF > config.mak 168SRCDIR=$srcdir 169PREFIX=$prefix 170HOST=$host 171ARCH=$arch 172ARCH_NAME=$arch_name 173PROCESSOR=$processor 174CC=$cross_prefix$cc 175CXX=$cross_prefix$cxx 176LD=$cross_prefix$ld 177OBJCOPY=$cross_prefix$objcopy 178OBJDUMP=$cross_prefix$objdump 179AR=$cross_prefix$ar 180ADDR2LINE=$cross_prefix$addr2line 181API=$api 182TEST_DIR=$testdir 183FIRMWARE=$firmware 184ENDIAN=$endian 185PRETTY_PRINT_STACKS=$pretty_print_stacks 186U32_LONG_FMT=$u32_long 187EOF 188