blob: 29734fa9383036766090573b9f29cdd6d0dc39cf [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
54shift
55IFS=$OFS
56AFLAGS="$*"
57
58# Assume the test fails. Will be removed when it passes well.
59testdir=test-${args}
60if [ -f "${testdir}-FAILED" ]; then
61 rm -rf "${testdir}"
62fi
63touch "${testdir}-FAILED"
64
65mkdir -p "${testdir}"
66ln -fns "../${source_full}" "${testdir}"
67
Lev Walkin387a8f02017-09-15 23:24:00 -070068if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null;
69then
70 MAKE_FUZZER=yes
71else
72 MAKE_FUZZER=no
73fi
74
Lev Walkin700df492017-08-10 14:59:15 -070075asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1)
76
77AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}"
78
79# Create a common Makefile for the project
80cat <<TARGETS > "${testdir}/Makefile.targets"
81${AUTOGENERATED}
82
Lev Walkin026055f2017-08-27 01:28:59 -070083COMMON_FLAGS= -I.
Lev Walkin24cf97d2017-09-15 20:34:25 -070084CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1
Lev Walkin40846582017-09-13 23:02:03 +000085CFLAGS += -DSRCDIR=../${srcdir} ${CODECS_FLAGS}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070086CXXFLAGS = \${CFLAGS} ${CXXFLAGS}
Lev Walkin387a8f02017-09-15 23:24:00 -070087LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS}
Lev Walkin700df492017-08-10 14:59:15 -070088LDFLAGS = ${LDFLAGS:-}
89
90CC ?= ${CC}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070091CXX ?= ${CXX}
Lev Walkin700df492017-08-10 14:59:15 -070092
93all: compiled-module
94 \$(MAKE) check-executable
95
96check-executable: \$(OBJS)
97 @rm -f *.core
Lev Walkin7b04d792017-09-16 00:04:18 -070098 ${COMPILER} \$(CFLAGS) \$(LDFLAGS) -o check-executable \$(OBJS) -lm
Lev Walkin700df492017-08-10 14:59:15 -070099
100# Compile the corresponding .asn1 spec.
101compiled-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 Walkin387a8f02017-09-15 23:24:00 -0700109TARGETS
110
111
112if [ "${MAKE_FUZZER}" != "yes" ]; then
113 CHECK_FUZZER=": # No fuzzer defined"
114cat <<TARGETS >> "${testdir}/Makefile.targets"
115.PHONY check-fuzzer:
116check-fuzzer:
117TARGETS
118else
Lev Walkine0236da2017-09-17 22:03:57 -0700119 CHECK_FUZZER="UBSAN_OPTIONS=print_stacktrace=1 ./check-fuzzer -timeout=3 -max_total_time=60 -max_len=512 -detect_leaks=1"
Lev Walkin387a8f02017-09-15 23:24:00 -0700120cat <<TARGETS >> "${testdir}/Makefile.targets"
121check-fuzzer: \$(OBJS)
122 rm -f ${source_obj}
Lev Walkin7b04d792017-09-16 00:04:18 -0700123 ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) -c -o ${source_obj} ${source_short}
124 ${COMPILER} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$(OBJS)
Lev Walkin387a8f02017-09-15 23:24:00 -0700125 rm -f ${source_obj}
126TARGETS
127fi
128
129cat <<TARGETS >> "${testdir}/Makefile.targets"
130
Lev Walkin700df492017-08-10 14:59:15 -0700131check-succeeded: compiled-module
132 \$(MAKE) check-executable
Lev Walkin387a8f02017-09-15 23:24:00 -0700133 \$(MAKE) check-fuzzer
Lev Walkin700df492017-08-10 14:59:15 -0700134 @rm -f check-succeeded
135 ./check-executable
Lev Walkin387a8f02017-09-15 23:24:00 -0700136 ${CHECK_FUZZER}
Lev Walkin700df492017-08-10 14:59:15 -0700137 @touch check-succeeded
138
139check: check-succeeded
140
141clean:
142 @rm -f *.o check-executable
143TARGETS
144
145# Create a BSD- or GNU-specific Makefile for the project.
146produce_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 Walkinb5cdc5d2017-09-15 21:57:46 -0700153 SRCS_C!=find . -name \*.c
154 SRCS_CXX!=find . -name \*.cc
Lev Walkin387a8f02017-09-15 23:24:00 -0700155 SRCS=\$(SRCS_C) \$(SRCS_CXX)
Lev Walkinb5cdc5d2017-09-15 21:57:46 -0700156 OBJS=\${SRCS_C:.c=.o} ${SRCS_CXX:.cc=.o}
Lev Walkin700df492017-08-10 14:59:15 -0700157 .sinclude <Makefile.targets>
158 OBJECTS
159 else
160 cat <<-OBJECTS > ${make_file}
161 ${AUTOGENERATED}
Lev Walkin387a8f02017-09-15 23:24:00 -0700162 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 Walkin700df492017-08-10 14:59:15 -0700167 -include Makefile.targets
168 OBJECTS
169 fi
170
171}
172
173produce_specific_makefile BSD
174produce_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.
180rm -f "${testdir}-FAILED"