#!/bin/bash # Run tests for a single compiler variation. set -euo pipefail if [ $# != 3 ]; then echo "$0 " exit 1 fi SOURCE=$1 CC=$2 STD=$3 if [[ "$CC" == "gcc" ]]; then # Need an extra flag to silence one warning for GCC, due to: # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89549 CFLAGS="-O3 -Wall -Werror -pedantic -Wno-misleading-indentation" else CFLAGS="-O3 -Wall -Werror -pedantic" fi # Output executable names. # # $OUTPUT_PREFIX matches what Makefile expected for "make clean". # $CC-$STD-$$ suffix produces different output files for each compiler # variation, so that this script can be run in parallel. # # The ".exe" suffix is needed for cygwin, otherwise we may get both # "prog" and "prog.exe" output files, and cygwin has a tendency to run # the wrong one. OUTPUT_PREFIX=test_output ENCODER_BIN="./$OUTPUT_PREFIX-$CC-$STD-$$.exe" OUTPUT_BIN="./$OUTPUT_PREFIX-verify-$CC-$STD-$$.exe" OUTPUT_SOURCE="$OUTPUT_PREFIX-$CC-$STD-$$.c" function die() { echo "Failed for $CC/$STD: $1" # Exit with failure status, and leave temporary files behind for debugging. exit 1 } function run_tests() { DATA=$1 # Build encoder. rm -f "$ENCODER_BIN" $CC -std=$STD $CFLAGS "$SOURCE" -o "$ENCODER_BIN" # Encode data to C output. # # The "sed" patch below is to workaround this clang bug: # https://bugs.llvm.org/show_bug.cgi?id=44480 echo "$DATA" \ | "$ENCODER_BIN" 65535 \ | sed -e "s/__FILE__/\"$OUTPUT_SOURCE\"/" \ > "$OUTPUT_SOURCE" # Verify that #error is reached if we didn't define PIN. ( $CC "$OUTPUT_SOURCE" > /dev/null 2>&1 ) && die "#error test failed" # Verify correct PIN. rm -f "$OUTPUT_BIN" $CC -std=$STD $CFLAGS -DPIN=65535 "$OUTPUT_SOURCE" -o "$OUTPUT_BIN" "$OUTPUT_BIN" | grep -qF "$DATA" || die "Correct PIN test failed" # Verify incorrect PIN. rm -f "$OUTPUT_BIN" $CC -std=$STD $CFLAGS -DPIN=65534 "$OUTPUT_SOURCE" -o "$OUTPUT_BIN" "$OUTPUT_BIN" | grep -qF "$DATA" && die "Incorrect PIN test failed" return 0 } # Run tests with short readable text data. Note that we use # different data for each run, so that we are not going to # accidentally pass due to leftover binaries from previous run. run_tests "$$ $RANDOM $(date) OK" # Cleanup temporary output files before exiting with success status. rm -f "$ENCODER_BIN" "$OUTPUT_SOURCE" "$OUTPUT_BIN" exit 0