blob: 0f945f7eb2af7f2631ee1e97e06bef371986616e [file] [log] [blame]
Lev Walkin700df492017-08-10 14:59:15 -07001#!/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
8set -e
9
10if [ "x$1" = "x" ]; then
11 echo "Usage: $0 <check-NN.c>"
12 exit
13fi
14
15srcdir="${srcdir:-.}"
16abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}"
17abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}"
18
19if echo "$*" | grep -q -- -- ; then
20 TEST_DRIVER=$(echo "$*" | sed -e 's/ -- .*/ -- /g')
21 source_full=$(echo "$*" | sed -e 's/.* //g')
22else
23 TEST_DRIVER=""
24 source_full="$1"
25fi
26
27# Compute the .asn1 spec name by the given file name.
28source_short=$(echo "$source_full" | sed -e 's/.*\///')
29testno=$(echo "$source_short" | cut -f2 -d'-' | cut -f1 -d'.')
30
Lev Walkin122002e2017-08-25 12:55:25 -070031CODECS_FLAGS=""
32has_oer=$(echo "$source_short" | grep "gen-OER" || :)
33if [ ! "$has_oer" ]; then
34 CODECS_FLAGS="${CODECS_FLAGS} -DASN_DISABLE_OER_SUPPORT"
35fi
36has_per=$(echo "$source_short" | grep "gen-PER" || :)
37if [ ! "$has_per" ]; then
38 CODECS_FLAGS="${CODECS_FLAGS} -DASN_DISABLE_PER_SUPPORT"
39fi
40
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070041args=$(echo "$source_short" | sed -E -e 's/\.c+$//')
Lev Walkin387a8f02017-09-15 23:24:00 -070042source_obj=$(echo "$source_short" | sed -E -e 's/\.c+$/.o/')
Lev Walkin7b04d792017-09-16 00:04:18 -070043ext=$(echo "$source_short" | sed -E -e 's/.*\.(c+)$/\1/')
44
45if [ "$ext" = "cc" ]; then
46 COMPILER='$(CXX) $(CXX_CFLAGS)'
47else
48 COMPILER='$(CC)'
49fi
Lev Walkin700df492017-08-10 14:59:15 -070050
51OFS=$IFS
52IFS="."
53set $args
Lev Walkinc8c286a2017-09-18 03:19:37 -070054data_dir=$(echo "$1" | sed -e s/check-/data-/)
Lev Walkin700df492017-08-10 14:59:15 -070055shift
56IFS=$OFS
57AFLAGS="$*"
58
Lev Walkinc8c286a2017-09-18 03:19:37 -070059if [ -d ${data_dir} ]; then
60 OPT_DATA_DIR="../${data_dir}"
61else
62 OPT_DATA_DIR=""
63fi
64
65if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null;
66then
67 MAKE_FUZZER=yes
68else
69 MAKE_FUZZER=no
70fi
71
Lev Walkin700df492017-08-10 14:59:15 -070072# Assume the test fails. Will be removed when it passes well.
73testdir=test-${args}
74if [ -f "${testdir}-FAILED" ]; then
75 rm -rf "${testdir}"
76fi
77touch "${testdir}-FAILED"
78
79mkdir -p "${testdir}"
80ln -fns "../${source_full}" "${testdir}"
81
82asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1)
83
84AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}"
85
86# Create a common Makefile for the project
87cat <<TARGETS > "${testdir}/Makefile.targets"
88${AUTOGENERATED}
89
Lev Walkin026055f2017-08-27 01:28:59 -070090COMMON_FLAGS= -I.
Lev Walkin24cf97d2017-09-15 20:34:25 -070091CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1
Lev Walkin40846582017-09-13 23:02:03 +000092CFLAGS += -DSRCDIR=../${srcdir} ${CODECS_FLAGS}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070093CXXFLAGS = \${CFLAGS} ${CXXFLAGS}
Lev Walkin387a8f02017-09-15 23:24:00 -070094LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS}
Lev Walkin700df492017-08-10 14:59:15 -070095LDFLAGS = ${LDFLAGS:-}
96
97CC ?= ${CC}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070098CXX ?= ${CXX}
Lev Walkin700df492017-08-10 14:59:15 -070099
100all: compiled-module
101 \$(MAKE) check-executable
102
103check-executable: \$(OBJS)
104 @rm -f *.core
Lev Walkin7b04d792017-09-16 00:04:18 -0700105 ${COMPILER} \$(CFLAGS) \$(LDFLAGS) -o check-executable \$(OBJS) -lm
Lev Walkin700df492017-08-10 14:59:15 -0700106
107# Compile the corresponding .asn1 spec.
108compiled-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}
113 rm -f converter-sample.c
114 @touch compiled-module
115
Lev Walkin387a8f02017-09-15 23:24:00 -0700116TARGETS
117
118
119if [ "${MAKE_FUZZER}" != "yes" ]; then
120 CHECK_FUZZER=": # No fuzzer defined"
121cat <<TARGETS >> "${testdir}/Makefile.targets"
122.PHONY check-fuzzer:
123check-fuzzer:
124TARGETS
125else
Lev Walkindcae9ce2017-09-18 20:13:36 -0700126 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 Walkin387a8f02017-09-15 23:24:00 -0700127cat <<TARGETS >> "${testdir}/Makefile.targets"
128check-fuzzer: \$(OBJS)
129 rm -f ${source_obj}
Lev Walkin7b04d792017-09-16 00:04:18 -0700130 ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) -c -o ${source_obj} ${source_short}
131 ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$(OBJS)
Lev Walkin387a8f02017-09-15 23:24:00 -0700132 rm -f ${source_obj}
133TARGETS
134fi
135
136cat <<TARGETS >> "${testdir}/Makefile.targets"
137
Lev Walkin700df492017-08-10 14:59:15 -0700138check-succeeded: compiled-module
139 \$(MAKE) check-executable
Lev Walkin387a8f02017-09-15 23:24:00 -0700140 \$(MAKE) check-fuzzer
Lev Walkin700df492017-08-10 14:59:15 -0700141 @rm -f check-succeeded
142 ./check-executable
Lev Walkindcae9ce2017-09-18 20:13:36 -0700143 \$(MAKE) fuzz
Lev Walkin700df492017-08-10 14:59:15 -0700144 @touch check-succeeded
145
Lev Walkindcae9ce2017-09-18 20:13:36 -0700146.PHONY: fuzz
147fuzz:
148 ${CHECK_FUZZER}
149
Lev Walkin700df492017-08-10 14:59:15 -0700150check: check-succeeded
151
152clean:
153 @rm -f *.o check-executable
154TARGETS
155
156# Create a BSD- or GNU-specific Makefile for the project.
157produce_specific_makefile() {
158 local make_type=$1
159 local make_file="$testdir/${make_type}makefile"
160
161 if [ ${make_type} = "BSD" ]; then
162 cat <<-OBJECTS > ${make_file}
163 ${AUTOGENERATED}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -0700164 SRCS_C!=find . -name \*.c
165 SRCS_CXX!=find . -name \*.cc
Lev Walkin387a8f02017-09-15 23:24:00 -0700166 SRCS=\$(SRCS_C) \$(SRCS_CXX)
Lev Walkinb5cdc5d2017-09-15 21:57:46 -0700167 OBJS=\${SRCS_C:.c=.o} ${SRCS_CXX:.cc=.o}
Lev Walkin700df492017-08-10 14:59:15 -0700168 .sinclude <Makefile.targets>
169 OBJECTS
170 else
171 cat <<-OBJECTS > ${make_file}
172 ${AUTOGENERATED}
Lev Walkin387a8f02017-09-15 23:24:00 -0700173 SRCS_C := \$(wildcard *.c)
174 SRCS_CXX := \$(wildcard *.cc)
175 SRCS = \$(SRCS_C) \$(SRCS_CXX)
176 OBJS =\$(patsubst %.c,%.o,\$(SRCS_C))
177 OBJS+=\$(patsubst %.cc,%.o,\$(SRCS_CXX))
Lev Walkin700df492017-08-10 14:59:15 -0700178 -include Makefile.targets
179 OBJECTS
180 fi
181
182}
183
184produce_specific_makefile BSD
185produce_specific_makefile GNU
186
187# Perform building and checking
188${TEST_DRIVER} make -C "$testdir" check
189
190# Make sure the test is not marked as failed any longer.
191rm -f "${testdir}-FAILED"