1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
#!/bin/bash
#-------------------------------------------------------------------------------
# License
#-------------------------------------------------------------------------------
# Copyright (C) 2006 Movial
# Authors: Jussi Hakala <jussi.hakala@movial.fi>
# Licensed under GNU GPL, see COPYING for details
#-------------------------------------------------------------------------------
# Configuration
#-------------------------------------------------------------------------------
INIT_PATH_TMP='/tmp/init'
INIT_COLOR_MSG_0='\033[0;34m'
INIT_COLOR_MSG_1='\033[1;34m'
INIT_COLOR_VER_0='\033[0;32m'
INIT_COLOR_VER_1='\033[0;31m'
INIT_COLOR_NORMAL='\033[0;37m'
#-------------------------------------------------------------------------------
# Defaults
#-------------------------------------------------------------------------------
OPTION_TESTS='compile,type,strip,execute'
OPTION_COLOR=0
OPTION_VERSION=0
OPTION_USAGE=0
#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
function init_message
{
local MESSAGE="$1"
cat << EOF
$MESSAGE
EOF
}
function init_warning
{
local MESSAGE="$1"
init_message "WARNING: $MESSAGE"
}
function init_error
{
local EXIT="$1"
local MESSAGE="$2"
init_message "ERROR: $MESSAGE"
exit "$EXIT"
}
function init_usage
{
cat << EOF
Usage: init_tests.sh [-h|-t <tests>|-v]
Options: -c colorize output
-h help
-t <string> comma separated list of tests in order
compile,dependencies,strip,type,execute
c,t,d,s,x (default)
-v version
EOF
exit 0
}
function init_version
{
cat << EOF
init_tests.sh: toolchain operation testing
Version 2.0
Original author: Ricardo Kekki <ricardo.kekki@movial.fi>
Adopted by: Jussi Hakala <jussi.hakala@movial.fi>
Copyright (C) 2006 Movial
EOF
exit 0
}
function init_dumplibc
{
local RESULT=
local PATH_LIBS='/lib'
local LIBC=
local PATH_LIBC=
local PATH_LIBC_FOUND=
for LIBC in libc.so.6 libc.so.0 ; do
PATH_LIBC="${PATH_LIBS}/$LIBC"
if [ -e "$PATH_LIBC" ] ; then
PATH_LIBC_FOUND="$PATH_LIBC"
break 1
fi
done
if [ "#EMPTY#$PATH_LIBC_FOUND" != "#EMPTY#" ] ; then
RESULT=`objdump -f "$PATH_LIBC_FOUND" \
| awk -F ' |,' '/architecture/ {print $2}'`
fi
echo -n "$RESULT"
}
function init_dumpgcc
{
local RESULT=
if which gcc &> /dev/null ; then
RESULT=`gcc -dumpmachine | cut -d - -f 1`
fi
echo -n "$RESULT"
}
function init_get_architecture
{
local RESULT=
local ARCH="$1"
case "$ARCH" in
arm) RESULT='ARM' ;;
i[34]86) RESULT='Intel 80386' ;;
mips) RESULT='MIPS' ;;
esac
echo -n "$RESULT"
}
function init_test_compile
{
local RESULT=0
# test the c compiler
cat > "${INIT_PATH_TMP}/hello.c" <<EOF
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
EOF
gcc -static "${INIT_PATH_TMP}/hello.c" \
-o "${INIT_PATH_TMP}/hello-static" || RESULT=1
gcc "${INIT_PATH_TMP}/hello.c" \
-o "${INIT_PATH_TMP}/hello" || RESULT=1
# test the c++ compiler
cat > "${INIT_PATH_TMP}/hello.cc" <<EOF
#include <iostream>
int main() {
std::cout << "Hello world!\n";
return 0;
}
EOF
g++ -static "${INIT_PATH_TMP}/hello.cc" \
-o "${INIT_PATH_TMP}/hellocc-static" || RESULT=1
g++ "${INIT_PATH_TMP}/hello.cc" \
-o "${INIT_PATH_TMP}/hellocc" || RESULT=1
return "$RESULT"
}
function init_test_execute
{
local RESULT=0
local REF=`echo "Hello world!"`
local BIN=
local OUT=
for BIN in hello hello-static hellocc hellocc-static ; do
OUT=`"${INIT_PATH_TMP}/$BIN" 2>&1`
echo "$OUT"
[ "$OUT" == "$REF" ] || RESULT=1
done
return "$RESULT"
}
function init_test_type
{
local RESULT=0
local ARCH="$1"
local BIN=
local TYPE=
local MATCH=
for BIN in hello hello-static hellocc hellocc-static ; do
TYPE=`file "${INIT_PATH_TMP}/$BIN"`
MATCH=`awk -v ARCH="$ARCH" -v TYPE="$TYPE" -- \
'BEGIN { print match(TYPE, ARCH) }' | tr -d $'\n'`
echo "$TYPE"
[ "$MATCH" -ne 0 ] || RESULT=1
done
return "$RESULT"
}
function init_test_strip
{
local RESULT=0
local BIN=
local OUT=
for BIN in hello hello-static hellocc hellocc-static ; do
OUT=`strip "${INIT_PATH_TMP}/$BIN" 2>&1`
[ $? -eq 0 ] || RESULT=1
[ "$OUT" ] && RESULT=1
done
return "$RESULT"
}
function init_do_tests
{
local TESTS="$1"
local ARCH="$2"
local RESULT=0
local VERB_0='OK'
local VERB_1='ERROR'
IFS=,
local TEST=
local TEST_RESULT=
for TEST in $TESTS ; do
TEST_RESULT=0
echo -e "${INIT_COLOR_MSG_0}Starting test: ${INIT_COLOR_MSG_1}${TEST}${INIT_COLOR_NORMAL}"
case $TEST in
c|C|compile)
init_test_compile
[ $? -ne 0 ] && TEST_RESULT=1
;;
d|D|dependencies)
init_test_dependencies
[ $? -ne 0 ] && TEST_RESULT=1
;;
s|S|strip)
init_test_strip
[ $? -ne 0 ] && TEST_RESULT=1
;;
t|T|type)
init_test_type "$ARCH"
[ $? -ne 0 ] && TEST_RESULT=1
;;
x|X|execute)
init_test_execute
[ $? -ne 0 ] && TEST_RESULT=1
;;
*)
init_warning "Unknown test, skipping..."
TEST_RESULT=1
;;
esac
echo -ne "${INIT_COLOR_MSG_0}Finishing test: ${INIT_COLOR_MSG_1}${TEST}${INIT_COLOR_MSG_0}, result: "
eval "echo -e \${INIT_COLOR_VER_$TEST_RESULT}\$VERB_$TEST_RESULT\${INIT_COLOR_NORMAL}"
[ "$TEST_RESULT" -ne 0 ] && RESULT=1
done
IFS=$'\n\t '
return "$RESULT"
}
function init_exit
{
echo rm -rf "$INIT_PATH_TMP" &> /dev/null
}
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
while getopts ":cht:v" OPTION ; do
case $OPTION in
c)
OPTION_COLOR=1
;;
h)
OPTION_USAGE=1
;;
t)
OPTION_TESTS="$OPTARG"
;;
v)
OPTION_VERSION=1
;;
*)
init_error 1 "Invalid argument(s). Try $0 -h for help."
;;
esac
done
if [ "$OPTION_COLOR" -ne 1 ] ; then
INIT_COLOR_MSG_0=
INIT_COLOR_MSG_1=
INIT_COLOR_VER_0=
INIT_COLOR_VER_1=
INIT_COLOR_NORMAL=
fi
if [ "$OPTION_USAGE" -ne 0 ] ; then
init_usage
fi
if [ "$OPTION_VERSION" -ne 0 ] ; then
init_version
fi
TYPE=`init_dumplibc`
if [ "#EMPTY#$TYPE" == "#EMPTY#" ] ; then
TYPE=`init_dumpgcc`
if [ "#EMPTY#$TYPE" == "#EMPTY#" ] ; then
init_error 1 "Cannot determine the architecture"
fi
fi
ARCH=`init_get_architecture "$TYPE"`
trap init_exit EXIT
rm -rf "$INIT_PATH_TMP" &> /dev/null
mkdir -p "$INIT_PATH_TMP"
init_do_tests "$OPTION_TESTS" "$ARCH"
STATUS=$?
exit "$STATUS"
#-------------------------------------------------------------------------------
# EOF
#-------------------------------------------------------------------------------
|