58b84a3b41f6a8013a50f0c3a0ac346726319968
[apps/agl-service-can-low-level.git] / tests / Makefile
1 CFLAGS=-ansi -Wall -Werror -I .. -g -O0
2 DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h \
3         callbacks2.pb.h callbacks.pb.h unittests.h unittestproto.pb.h \
4         alltypes.pb.h missing_fields.pb.h
5 TESTS=  decode_unittests encode_unittests \
6         test_decode1 test_decode2 test_decode3 \
7         test_encode1 test_encode2 test_encode3 \
8         test_decode_callbacks test_encode_callbacks \
9         test_missing_fields test_no_messages \
10         test_multiple_files test_cxxcompile test_options
11
12 # More strict checks for the core part of nanopb
13 CC_VERSION=$(shell $(CC) -v 2>&1)
14 CFLAGS_CORE=
15 ifneq (,$(findstring gcc,$(CC_VERSION)))
16         CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion
17         CFLAGS+=--coverage
18         LDFLAGS+=--coverage
19 endif
20 ifneq (,$(findstring clang,$(CC_VERSION)))
21         CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wconversion
22 endif
23
24 all: breakpoints $(TESTS) run_unittests
25
26 clean:
27         rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno *.pb.h *.pb.c
28
29 %.pb.o: %.pb.c %.pb.h
30         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
31
32 %.o: %.c
33 %.o: %.c $(DEPS)
34         $(CC) $(CFLAGS) -c -o $@ $<
35
36 pb_encode.o: ../pb_encode.c $(DEPS)
37         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
38 pb_decode.o: ../pb_decode.c $(DEPS)
39         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
40
41 pb_encode.cxx.o: ../pb_encode.c $(DEPS)
42         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
43 pb_decode.cxx.o: ../pb_decode.c $(DEPS)
44         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
45
46 test_cxxcompile: pb_encode.cxx.o pb_decode.cxx.o
47 test_decode1: test_decode1.o pb_decode.o person.pb.o
48 test_decode2: test_decode2.o pb_decode.o person.pb.o
49 test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
50 test_encode1: test_encode1.o pb_encode.o person.pb.o
51 test_encode2: test_encode2.o pb_encode.o person.pb.o
52 test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
53 test_multiple_files: test_multiple_files.o pb_encode.o callbacks2.pb.o callbacks.pb.o
54 test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
55 test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
56 test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
57 decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
58 encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
59 test_no_messages: no_messages.pb.h no_messages.pb.c no_messages.pb.o
60
61 %.pb: %.proto
62         protoc -I. -I../generator -I/usr/include -o$@ $<
63
64 %.pb.c %.pb.h: %.pb ../generator/nanopb_generator.py
65         python ../generator/nanopb_generator.py $<
66
67 breakpoints: ../*.c *.c
68         grep -n 'return false;' $^ | cut -d: -f-2 | xargs -n 1 echo b > $@
69
70 coverage: run_unittests
71         gcov pb_encode.gcda
72         gcov pb_decode.gcda
73
74 run_unittests: $(TESTS)
75         rm -f *.gcda
76         
77         ./decode_unittests > /dev/null
78         ./encode_unittests > /dev/null
79         
80         [ "`./test_encode1 | ./test_decode1`" = \
81         "`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
82
83         [ "`./test_encode2 | ./test_decode1`" = \
84         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
85
86         [ "`./test_encode2 | ./test_decode2`" = \
87         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
88         
89         [ "`./test_decode2 < person_with_extra_field.pb`" = \
90         "`./test_encode2 | ./test_decode2`" ]
91         
92         [ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
93         "`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
94
95         ./test_encode3 | ./test_decode3
96         ./test_encode3 1 | ./test_decode3 1
97         ./test_encode3 1 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
98         
99         ./test_missing_fields
100
101 test_options: options.pb.h options.expected options.pb.o
102         cat options.expected | while read -r p; do \
103             if ! grep -q "$$p" $<; then \
104                 echo Expected: "$$p"; \
105                 exit 1; \
106             fi \
107         done
108
109 run_fuzztest: test_decode2
110         bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'