Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # |
| 4 | # This script is designed to quickly create lots of files in underlying |
| 5 | # test-* directories, do lots of other magic stuff and exit cleanly. |
| 6 | # |
| 7 | |
| 8 | set -e |
| 9 | |
| 10 | if [ "x$1" = "x" ]; then |
| 11 | echo "Usage: $0 <check-NN.c>" |
| 12 | exit |
| 13 | fi |
| 14 | |
| 15 | srcdir="${srcdir:-.}" |
| 16 | abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}" |
| 17 | abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}" |
| 18 | |
| 19 | if echo "$*" | grep -q -- -- ; then |
| 20 | TEST_DRIVER=$(echo "$*" | sed -e 's/ -- .*/ -- /g') |
| 21 | source_full=$(echo "$*" | sed -e 's/.* //g') |
| 22 | else |
| 23 | TEST_DRIVER="" |
| 24 | source_full="$1" |
| 25 | fi |
| 26 | |
| 27 | # Compute the .asn1 spec name by the given file name. |
| 28 | source_short=$(echo "$source_full" | sed -e 's/.*\///') |
| 29 | testno=$(echo "$source_short" | cut -f2 -d'-' | cut -f1 -d'.') |
| 30 | |
Lev Walkin | 122002e | 2017-08-25 12:55:25 -0700 | [diff] [blame] | 31 | CODECS_FLAGS="" |
| 32 | has_oer=$(echo "$source_short" | grep "gen-OER" || :) |
| 33 | if [ ! "$has_oer" ]; then |
| 34 | CODECS_FLAGS="${CODECS_FLAGS} -DASN_DISABLE_OER_SUPPORT" |
| 35 | fi |
| 36 | has_per=$(echo "$source_short" | grep "gen-PER" || :) |
| 37 | if [ ! "$has_per" ]; then |
| 38 | CODECS_FLAGS="${CODECS_FLAGS} -DASN_DISABLE_PER_SUPPORT" |
| 39 | fi |
| 40 | |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 41 | args=$(echo "$source_short" | sed -E -e 's/\.c+$//') |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 42 | source_obj=$(echo "$source_short" | sed -E -e 's/\.c+$/.o/') |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 43 | ext=$(echo "$source_short" | sed -E -e 's/.*\.(c+)$/\1/') |
| 44 | |
| 45 | if [ "$ext" = "cc" ]; then |
| 46 | COMPILER='$(CXX) $(CXX_CFLAGS)' |
| 47 | else |
| 48 | COMPILER='$(CC)' |
| 49 | fi |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 50 | |
| 51 | OFS=$IFS |
| 52 | IFS="." |
| 53 | set $args |
| 54 | shift |
| 55 | IFS=$OFS |
| 56 | AFLAGS="$*" |
| 57 | |
| 58 | # Assume the test fails. Will be removed when it passes well. |
| 59 | testdir=test-${args} |
| 60 | if [ -f "${testdir}-FAILED" ]; then |
| 61 | rm -rf "${testdir}" |
| 62 | fi |
| 63 | touch "${testdir}-FAILED" |
| 64 | |
| 65 | mkdir -p "${testdir}" |
| 66 | ln -fns "../${source_full}" "${testdir}" |
| 67 | |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 68 | if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null; |
| 69 | then |
| 70 | MAKE_FUZZER=yes |
| 71 | else |
| 72 | MAKE_FUZZER=no |
| 73 | fi |
| 74 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 75 | asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1) |
| 76 | |
| 77 | AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}" |
| 78 | |
| 79 | # Create a common Makefile for the project |
| 80 | cat <<TARGETS > "${testdir}/Makefile.targets" |
| 81 | ${AUTOGENERATED} |
| 82 | |
Lev Walkin | 026055f | 2017-08-27 01:28:59 -0700 | [diff] [blame] | 83 | COMMON_FLAGS= -I. |
Lev Walkin | 24cf97d | 2017-09-15 20:34:25 -0700 | [diff] [blame] | 84 | CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1 |
Lev Walkin | 4084658 | 2017-09-13 23:02:03 +0000 | [diff] [blame] | 85 | CFLAGS += -DSRCDIR=../${srcdir} ${CODECS_FLAGS} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 86 | CXXFLAGS = \${CFLAGS} ${CXXFLAGS} |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 87 | LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 88 | LDFLAGS = ${LDFLAGS:-} |
| 89 | |
| 90 | CC ?= ${CC} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 91 | CXX ?= ${CXX} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 92 | |
| 93 | all: compiled-module |
| 94 | \$(MAKE) check-executable |
| 95 | |
| 96 | check-executable: \$(OBJS) |
| 97 | @rm -f *.core |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 98 | ${COMPILER} \$(CFLAGS) \$(LDFLAGS) -o check-executable \$(OBJS) -lm |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 99 | |
| 100 | # Compile the corresponding .asn1 spec. |
| 101 | compiled-module: ${asn_module} ${abs_top_builddir}/asn1c/asn1c |
| 102 | ${abs_top_builddir}/asn1c/asn1c \\ |
| 103 | -S ${abs_top_srcdir}/skeletons \\ |
| 104 | -Wdebug-compiler \\ |
| 105 | ${AFLAGS} ${asn_module} |
| 106 | rm -f converter-sample.c |
| 107 | @touch compiled-module |
| 108 | |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 109 | TARGETS |
| 110 | |
| 111 | |
| 112 | if [ "${MAKE_FUZZER}" != "yes" ]; then |
| 113 | CHECK_FUZZER=": # No fuzzer defined" |
| 114 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
| 115 | .PHONY check-fuzzer: |
| 116 | check-fuzzer: |
| 117 | TARGETS |
| 118 | else |
Lev Walkin | e0236da | 2017-09-17 22:03:57 -0700 | [diff] [blame] | 119 | CHECK_FUZZER="UBSAN_OPTIONS=print_stacktrace=1 ./check-fuzzer -timeout=3 -max_total_time=60 -max_len=512 -detect_leaks=1" |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 120 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
| 121 | check-fuzzer: \$(OBJS) |
| 122 | rm -f ${source_obj} |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 123 | ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) -c -o ${source_obj} ${source_short} |
| 124 | ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$(OBJS) |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 125 | rm -f ${source_obj} |
| 126 | TARGETS |
| 127 | fi |
| 128 | |
| 129 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
| 130 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 131 | check-succeeded: compiled-module |
| 132 | \$(MAKE) check-executable |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 133 | \$(MAKE) check-fuzzer |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 134 | @rm -f check-succeeded |
| 135 | ./check-executable |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 136 | ${CHECK_FUZZER} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 137 | @touch check-succeeded |
| 138 | |
| 139 | check: check-succeeded |
| 140 | |
| 141 | clean: |
| 142 | @rm -f *.o check-executable |
| 143 | TARGETS |
| 144 | |
| 145 | # Create a BSD- or GNU-specific Makefile for the project. |
| 146 | produce_specific_makefile() { |
| 147 | local make_type=$1 |
| 148 | local make_file="$testdir/${make_type}makefile" |
| 149 | |
| 150 | if [ ${make_type} = "BSD" ]; then |
| 151 | cat <<-OBJECTS > ${make_file} |
| 152 | ${AUTOGENERATED} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 153 | SRCS_C!=find . -name \*.c |
| 154 | SRCS_CXX!=find . -name \*.cc |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 155 | SRCS=\$(SRCS_C) \$(SRCS_CXX) |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 156 | OBJS=\${SRCS_C:.c=.o} ${SRCS_CXX:.cc=.o} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 157 | .sinclude <Makefile.targets> |
| 158 | OBJECTS |
| 159 | else |
| 160 | cat <<-OBJECTS > ${make_file} |
| 161 | ${AUTOGENERATED} |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 162 | SRCS_C := \$(wildcard *.c) |
| 163 | SRCS_CXX := \$(wildcard *.cc) |
| 164 | SRCS = \$(SRCS_C) \$(SRCS_CXX) |
| 165 | OBJS =\$(patsubst %.c,%.o,\$(SRCS_C)) |
| 166 | OBJS+=\$(patsubst %.cc,%.o,\$(SRCS_CXX)) |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 167 | -include Makefile.targets |
| 168 | OBJECTS |
| 169 | fi |
| 170 | |
| 171 | } |
| 172 | |
| 173 | produce_specific_makefile BSD |
| 174 | produce_specific_makefile GNU |
| 175 | |
| 176 | # Perform building and checking |
| 177 | ${TEST_DRIVER} make -C "$testdir" check |
| 178 | |
| 179 | # Make sure the test is not marked as failed any longer. |
| 180 | rm -f "${testdir}-FAILED" |