7385fe35618a150d4e3d6c3b1055dc63a3b733f5
[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 test_decode3_buf \
7         test_encode1 test_encode2 test_encode3 test_encode3_buf \
8         test_decode_callbacks test_encode_callbacks \
9         test_missing_fields test_no_messages test_funny_name \
10         test_multiple_files test_cxxcompile test_options \
11         bc_encode bc_decode
12
13 # More strict checks for the core part of nanopb
14 CC_VERSION=$(shell $(CC) -v 2>&1)
15 CFLAGS_CORE=
16 ifneq (,$(findstring gcc,$(CC_VERSION)))
17         CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion
18         CFLAGS+=--coverage
19         LDFLAGS+=--coverage
20 endif
21 ifneq (,$(findstring clang,$(CC_VERSION)))
22         CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wconversion
23 endif
24
25 all: breakpoints $(TESTS) run_unittests
26
27 clean:
28         rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno *.pb.h *.pb.c
29
30 %.pb.o: %.pb.c %.pb.h
31         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
32
33 %.o: %.c
34 %.o: %.c $(DEPS)
35         $(CC) $(CFLAGS) -c -o $@ $<
36
37 pb_encode.o: ../pb_encode.c $(DEPS)
38         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
39 pb_decode.o: ../pb_decode.c $(DEPS)
40         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
41
42 # Test for compilability with c++ compiler
43
44 pb_encode.cxx.o: ../pb_encode.c $(DEPS)
45         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
46 pb_decode.cxx.o: ../pb_decode.c $(DEPS)
47         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
48
49 # Test for PB_BUF_ONLY compilation option
50
51 pb_encode.buf.o: ../pb_encode.c $(DEPS)
52         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
53 pb_decode.buf.o: ../pb_decode.c $(DEPS)
54         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
55 %.buf.o: %.c $(DEPS)
56         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) -c -o $@ $<    
57 test_encode3_buf: test_encode3.buf.o pb_encode.buf.o alltypes.pb.o
58         $(CC) $(LDFLAGS) $^ -o $@
59 test_decode3_buf: test_decode3.buf.o pb_decode.buf.o alltypes.pb.o
60         $(CC) $(LDFLAGS) $^ -o $@
61
62 test_cxxcompile: pb_encode.cxx.o pb_decode.cxx.o
63 test_decode1: test_decode1.o pb_decode.o person.pb.o
64 test_decode2: test_decode2.o pb_decode.o person.pb.o
65 test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
66 test_encode1: test_encode1.o pb_encode.o person.pb.o
67 test_encode2: test_encode2.o pb_encode.o person.pb.o
68 test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
69 test_multiple_files: test_multiple_files.o pb_encode.o callbacks2.pb.o callbacks.pb.o
70 test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
71 test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
72 test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
73 decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
74 encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
75 test_no_messages: no_messages.pb.h no_messages.pb.c no_messages.pb.o
76 test_funny_name: funny-proto+name.pb.h funny-proto+name.pb.o
77 bc_encode: bc_alltypes.pb.o pb_encode.o bc_encode.o
78 bc_decode: bc_alltypes.pb.o pb_decode.o bc_decode.o
79
80 %.pb: %.proto
81         protoc -I. -I../generator -I/usr/include -o$@ $<
82
83 %.pb.c %.pb.h: %.pb ../generator/nanopb_generator.py
84         python ../generator/nanopb_generator.py $<
85
86 breakpoints: ../*.c *.c
87         grep -n 'return false;' $^ | cut -d: -f-2 | xargs -n 1 echo b > $@
88
89 coverage: run_unittests
90         gcov pb_encode.gcda
91         gcov pb_decode.gcda
92
93 run_unittests: $(TESTS)
94         rm -f *.gcda
95         
96         ./decode_unittests > /dev/null
97         ./encode_unittests > /dev/null
98         
99         [ "`./test_encode1 | ./test_decode1`" = \
100         "`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
101
102         [ "`./test_encode2 | ./test_decode1`" = \
103         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
104
105         [ "`./test_encode2 | ./test_decode2`" = \
106         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
107         
108         [ "`./test_decode2 < person_with_extra_field.pb`" = \
109         "`./test_encode2 | ./test_decode2`" ]
110         
111         [ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
112         "`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
113
114         ./test_encode3 | ./test_decode3
115         ./test_encode3 1 | ./test_decode3 1
116         ./test_encode3 1 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
117         ./test_encode3_buf 1 | ./test_decode3_buf 1
118         ./bc_encode | ./bc_decode
119         
120         ./test_missing_fields
121
122 test_options: options.pb.h options.expected options.pb.o
123         cat options.expected | while read -r p; do \
124             if ! grep -q "$$p" $<; then \
125                 echo Expected: "$$p"; \
126                 exit 1; \
127             fi \
128         done
129
130 run_fuzztest: test_decode2
131         bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'