blob: 3643d7977fb4721bf5ad3c6bfd3be5e8722a4e16 [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 Walkin700df492017-08-10 14:59:15 -070043
44OFS=$IFS
45IFS="."
46set $args
47shift
48IFS=$OFS
49AFLAGS="$*"
50
51# Assume the test fails. Will be removed when it passes well.
52testdir=test-${args}
53if [ -f "${testdir}-FAILED" ]; then
54 rm -rf "${testdir}"
55fi
56touch "${testdir}-FAILED"
57
58mkdir -p "${testdir}"
59ln -fns "../${source_full}" "${testdir}"
60
Lev Walkin387a8f02017-09-15 23:24:00 -070061if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null;
62then
63 MAKE_FUZZER=yes
64else
65 MAKE_FUZZER=no
66fi
67
Lev Walkin700df492017-08-10 14:59:15 -070068asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1)
69
70AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}"
71
72# Create a common Makefile for the project
73cat <<TARGETS > "${testdir}/Makefile.targets"
74${AUTOGENERATED}
75
Lev Walkin026055f2017-08-27 01:28:59 -070076COMMON_FLAGS= -I.
Lev Walkin24cf97d2017-09-15 20:34:25 -070077CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1
Lev Walkin40846582017-09-13 23:02:03 +000078CFLAGS += -DSRCDIR=../${srcdir} ${CODECS_FLAGS}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070079CXXFLAGS = \${CFLAGS} ${CXXFLAGS}
Lev Walkin387a8f02017-09-15 23:24:00 -070080LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS}
Lev Walkin700df492017-08-10 14:59:15 -070081LDFLAGS = ${LDFLAGS:-}
82
83CC ?= ${CC}
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070084CXX ?= ${CXX}
Lev Walkin700df492017-08-10 14:59:15 -070085
86all: compiled-module
87 \$(MAKE) check-executable
88
89check-executable: \$(OBJS)
90 @rm -f *.core
Lev Walkin40846582017-09-13 23:02:03 +000091 \$(CC) \$(CFLAGS) \$(LDFLAGS) -o check-executable \$(OBJS) -lm
Lev Walkin700df492017-08-10 14:59:15 -070092
93# Compile the corresponding .asn1 spec.
94compiled-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 Walkin387a8f02017-09-15 23:24:00 -0700102TARGETS
103
104
105if [ "${MAKE_FUZZER}" != "yes" ]; then
106 CHECK_FUZZER=": # No fuzzer defined"
107cat <<TARGETS >> "${testdir}/Makefile.targets"
108.PHONY check-fuzzer:
109check-fuzzer:
110TARGETS
111else
112 CHECK_FUZZER="./check-fuzzer -timeout=3 -max_total_time=60 -max-len 512 -detect_leaks=1"
113cat <<TARGETS >> "${testdir}/Makefile.targets"
114check-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}
119TARGETS
120fi
121
122cat <<TARGETS >> "${testdir}/Makefile.targets"
123
Lev Walkin700df492017-08-10 14:59:15 -0700124check-succeeded: compiled-module
125 \$(MAKE) check-executable
Lev Walkin387a8f02017-09-15 23:24:00 -0700126 \$(MAKE) check-fuzzer
Lev Walkin700df492017-08-10 14:59:15 -0700127 @rm -f check-succeeded
128 ./check-executable
Lev Walkin387a8f02017-09-15 23:24:00 -0700129 ${CHECK_FUZZER}
Lev Walkin700df492017-08-10 14:59:15 -0700130 @touch check-succeeded
131
132check: check-succeeded
133
134clean:
135 @rm -f *.o check-executable
136TARGETS
137
138# Create a BSD- or GNU-specific Makefile for the project.
139produce_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 Walkinb5cdc5d2017-09-15 21:57:46 -0700146 SRCS_C!=find . -name \*.c
147 SRCS_CXX!=find . -name \*.cc
Lev Walkin387a8f02017-09-15 23:24:00 -0700148 SRCS=\$(SRCS_C) \$(SRCS_CXX)
Lev Walkinb5cdc5d2017-09-15 21:57:46 -0700149 OBJS=\${SRCS_C:.c=.o} ${SRCS_CXX:.cc=.o}
Lev Walkin700df492017-08-10 14:59:15 -0700150 .sinclude <Makefile.targets>
151 OBJECTS
152 else
153 cat <<-OBJECTS > ${make_file}
154 ${AUTOGENERATED}
Lev Walkin387a8f02017-09-15 23:24:00 -0700155 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 Walkin700df492017-08-10 14:59:15 -0700160 -include Makefile.targets
161 OBJECTS
162 fi
163
164}
165
166produce_specific_makefile BSD
167produce_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.
173rm -f "${testdir}-FAILED"