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