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 |
Lev Walkin | 15734f9 | 2017-09-30 11:41:04 -0700 | [diff] [blame] | 11 | echo "Usage: $0 check-src/<check-NN.c>" |
| 12 | exit 1 |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 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 |
Lev Walkin | c8c286a | 2017-09-18 03:19:37 -0700 | [diff] [blame] | 54 | data_dir=$(echo "$1" | sed -e s/check-/data-/) |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 55 | shift |
| 56 | IFS=$OFS |
| 57 | AFLAGS="$*" |
| 58 | |
Lev Walkin | c8c286a | 2017-09-18 03:19:37 -0700 | [diff] [blame] | 59 | if [ -d ${data_dir} ]; then |
| 60 | OPT_DATA_DIR="../${data_dir}" |
| 61 | else |
| 62 | OPT_DATA_DIR="" |
| 63 | fi |
| 64 | |
| 65 | if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null; |
| 66 | then |
| 67 | MAKE_FUZZER=yes |
| 68 | else |
| 69 | MAKE_FUZZER=no |
| 70 | fi |
| 71 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 72 | # Assume the test fails. Will be removed when it passes well. |
| 73 | testdir=test-${args} |
| 74 | if [ -f "${testdir}-FAILED" ]; then |
| 75 | rm -rf "${testdir}" |
| 76 | fi |
| 77 | touch "${testdir}-FAILED" |
| 78 | |
| 79 | mkdir -p "${testdir}" |
| 80 | ln -fns "../${source_full}" "${testdir}" |
| 81 | |
| 82 | asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1) |
| 83 | |
| 84 | AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}" |
| 85 | |
| 86 | # Create a common Makefile for the project |
| 87 | cat <<TARGETS > "${testdir}/Makefile.targets" |
| 88 | ${AUTOGENERATED} |
| 89 | |
Lev Walkin | 026055f | 2017-08-27 01:28:59 -0700 | [diff] [blame] | 90 | COMMON_FLAGS= -I. |
Lev Walkin | 24cf97d | 2017-09-15 20:34:25 -0700 | [diff] [blame] | 91 | CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1 |
Lev Walkin | 4084658 | 2017-09-13 23:02:03 +0000 | [diff] [blame] | 92 | CFLAGS += -DSRCDIR=../${srcdir} ${CODECS_FLAGS} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 93 | CXXFLAGS = \${CFLAGS} ${CXXFLAGS} |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 94 | LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 95 | LDFLAGS = ${LDFLAGS:-} |
| 96 | |
| 97 | CC ?= ${CC} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 98 | CXX ?= ${CXX} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 99 | |
| 100 | all: compiled-module |
| 101 | \$(MAKE) check-executable |
| 102 | |
| 103 | check-executable: \$(OBJS) |
| 104 | @rm -f *.core |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 105 | ${COMPILER} \$(CFLAGS) \$(LDFLAGS) -o check-executable \$(OBJS) -lm |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 106 | |
| 107 | # Compile the corresponding .asn1 spec. |
| 108 | compiled-module: ${asn_module} ${abs_top_builddir}/asn1c/asn1c |
| 109 | ${abs_top_builddir}/asn1c/asn1c \\ |
| 110 | -S ${abs_top_srcdir}/skeletons \\ |
| 111 | -Wdebug-compiler \\ |
| 112 | ${AFLAGS} ${asn_module} |
Lev Walkin | 15b26e6 | 2017-09-26 23:04:57 -0700 | [diff] [blame] | 113 | rm -f converter-example.c |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 114 | @touch compiled-module |
| 115 | |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 116 | TARGETS |
| 117 | |
| 118 | |
| 119 | if [ "${MAKE_FUZZER}" != "yes" ]; then |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 120 | CHECK_FUZZER="@echo \"No fuzzer defined, skipping.\"" |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 121 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 122 | .PHONY: check-fuzzer |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 123 | check-fuzzer: |
| 124 | TARGETS |
| 125 | else |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 126 | CHECK_FUZZER="ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1 ./check-fuzzer -timeout=3 -max_total_time=60 -max_len=512 ${OPT_DATA_DIR}" |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 127 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
| 128 | check-fuzzer: \$(OBJS) |
| 129 | rm -f ${source_obj} |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 130 | ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) -c -o ${source_obj} ${source_short} |
| 131 | ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$(OBJS) |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 132 | rm -f ${source_obj} |
| 133 | TARGETS |
| 134 | fi |
| 135 | |
| 136 | cat <<TARGETS >> "${testdir}/Makefile.targets" |
| 137 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 138 | check-succeeded: compiled-module |
| 139 | \$(MAKE) check-executable |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 140 | \$(MAKE) check-fuzzer |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 141 | @rm -f check-succeeded |
| 142 | ./check-executable |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 143 | \$(MAKE) fuzz |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 144 | @touch check-succeeded |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 145 | @echo "OK: ${source_full}" |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 146 | |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 147 | .PHONY: fuzz |
| 148 | fuzz: |
| 149 | ${CHECK_FUZZER} |
| 150 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 151 | check: check-succeeded |
| 152 | |
| 153 | clean: |
| 154 | @rm -f *.o check-executable |
| 155 | TARGETS |
| 156 | |
| 157 | # Create a BSD- or GNU-specific Makefile for the project. |
| 158 | produce_specific_makefile() { |
| 159 | local make_type=$1 |
| 160 | local make_file="$testdir/${make_type}makefile" |
| 161 | |
| 162 | if [ ${make_type} = "BSD" ]; then |
| 163 | cat <<-OBJECTS > ${make_file} |
| 164 | ${AUTOGENERATED} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 165 | SRCS_C!=find . -name \*.c |
| 166 | SRCS_CXX!=find . -name \*.cc |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 167 | SRCS=\$(SRCS_C) \$(SRCS_CXX) |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 168 | OBJS=\${SRCS_C:.c=.o} \${SRCS_CXX:.cc=.o} |
Lev Walkin | 16555a1 | 2017-09-26 19:04:15 -0700 | [diff] [blame] | 169 | include Makefile.targets |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 170 | OBJECTS |
| 171 | else |
| 172 | cat <<-OBJECTS > ${make_file} |
| 173 | ${AUTOGENERATED} |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 174 | SRCS_C := \$(wildcard *.c) |
| 175 | SRCS_CXX := \$(wildcard *.cc) |
| 176 | SRCS = \$(SRCS_C) \$(SRCS_CXX) |
| 177 | OBJS =\$(patsubst %.c,%.o,\$(SRCS_C)) |
| 178 | OBJS+=\$(patsubst %.cc,%.o,\$(SRCS_CXX)) |
Lev Walkin | 16555a1 | 2017-09-26 19:04:15 -0700 | [diff] [blame] | 179 | include Makefile.targets |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 180 | OBJECTS |
| 181 | fi |
| 182 | |
| 183 | } |
| 184 | |
| 185 | produce_specific_makefile BSD |
| 186 | produce_specific_makefile GNU |
| 187 | |
| 188 | # Perform building and checking |
| 189 | ${TEST_DRIVER} make -C "$testdir" check |
| 190 | |
| 191 | # Make sure the test is not marked as failed any longer. |
| 192 | rm -f "${testdir}-FAILED" |