1#!/bin/bash
2
3ulimit -s 8192
4set -e # fail on any error
5
6HERE=$(dirname $0)
7TSAN_DIR=$(dirname $0)/../../lib/tsan
8
9# Assume clang and clang++ are in path.
10: ${CC:=clang}
11: ${CXX:=clang++}
12: ${FILECHECK:=FileCheck}
13
14# TODO: add testing for all of -O0...-O3
15CFLAGS="-fsanitize=thread -O2 -g -Wall"
16LDFLAGS="-pthread -ldl -lrt -lm -Wl,--whole-archive $TSAN_DIR/rtl/libtsan.a -Wl,--no-whole-archive"
17
18test_file() {
19  SRC=$1
20  COMPILER=$2
21  echo ----- TESTING $(basename $1)
22  OBJ=$SRC.o
23  EXE=$SRC.exe
24  $COMPILER $SRC $CFLAGS -c -o $OBJ
25  $COMPILER $OBJ $LDFLAGS -o $EXE
26  RES=$($EXE 2>&1 || true)
27  printf "%s\n" "$RES" | $FILECHECK $SRC
28  if [ "$3" == "" ]; then
29    rm -f $EXE $OBJ
30  fi
31}
32
33if [ "$1" == "" ]; then
34  for c in $HERE/*.{c,cc}; do
35    if [[ $c == */failing_* ]]; then
36      echo SKIPPING FAILING TEST $c
37      continue
38    fi
39    if [[ $c == */load_shared_lib.cc ]]; then
40      echo TEST $c is not supported
41      continue
42    fi
43    if [[ $c == */*blacklist*.cc ]]; then
44      echo TEST $c is not supported
45      continue
46    fi
47    if [ "`grep "TSAN_OPTIONS" $c`" ]; then
48      echo SKIPPING $c -- requires TSAN_OPTIONS
49      continue
50    fi
51    if [ "`grep "XFAIL" $c`" ]; then
52      echo SKIPPING $c -- has XFAIL
53      continue
54    fi
55    COMPILER=$CXX
56    case $c in
57      *.c) COMPILER=$CC
58    esac
59    test_file $c $COMPILER &
60  done
61  for job in `jobs -p`; do
62    wait $job || exit 1
63  done
64else
65  test_file $HERE/$1 $CXX "DUMP"
66fi
67