Implement generator support for extension fields (no encoder/decoder support yet)
[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 test_encode_extensions
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 -fstack-protector-all
19         LDFLAGS+=--coverage
20 endif
21 ifneq (,$(findstring clang,$(CC_VERSION)))
22         CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wconversion
23 endif
24
25 # Also use mudflap if it is available
26 # To enable, run with    make -B USE_MUDFLAP=y
27 USE_MUDFLAP ?= n
28 ifeq ($(USE_MUDFLAP),y)
29         CFLAGS += -fmudflap
30         LDFLAGS += -lmudflap -fmudflap
31 endif
32
33 all: breakpoints $(TESTS) run_unittests
34
35 clean:
36         rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno *.pb.h *.pb.c
37
38 %.pb.o: %.pb.c %.pb.h
39         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
40
41 %.o: %.c
42 %.o: %.c $(DEPS)
43         $(CC) $(CFLAGS) -c -o $@ $<
44
45 pb_encode.o: ../pb_encode.c $(DEPS)
46         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
47 pb_decode.o: ../pb_decode.c $(DEPS)
48         $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
49
50 # Test for compilability with c++ compiler
51
52 pb_encode.cxx.o: ../pb_encode.c $(DEPS)
53         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
54 pb_decode.cxx.o: ../pb_decode.c $(DEPS)
55         $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
56
57 # Test for PB_BUF_ONLY compilation option
58
59 pb_encode.buf.o: ../pb_encode.c $(DEPS)
60         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
61 pb_decode.buf.o: ../pb_decode.c $(DEPS)
62         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
63 %.buf.o: %.c $(DEPS)
64         $(CC) -DPB_BUFFER_ONLY $(CFLAGS) -c -o $@ $<    
65 test_encode3_buf: test_encode3.buf.o pb_encode.buf.o alltypes.pb.o
66         $(CC) $(LDFLAGS) $^ -o $@
67 test_decode3_buf: test_decode3.buf.o pb_decode.buf.o alltypes.pb.o
68         $(CC) $(LDFLAGS) $^ -o $@
69
70 test_cxxcompile: pb_encode.cxx.o pb_decode.cxx.o
71 test_decode1: test_decode1.o pb_decode.o person.pb.o
72 test_decode2: test_decode2.o pb_decode.o person.pb.o
73 test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
74 test_encode1: test_encode1.o pb_encode.o person.pb.o
75 test_encode2: test_encode2.o pb_encode.o person.pb.o
76 test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
77 test_multiple_files: test_multiple_files.o pb_encode.o callbacks2.pb.o callbacks.pb.o
78 test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
79 test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
80 test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
81 decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
82 encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
83 test_no_messages: no_messages.pb.h no_messages.pb.c no_messages.pb.o
84 test_funny_name: funny-proto+name.pb.h funny-proto+name.pb.o
85 bc_encode: bc_alltypes.pb.o pb_encode.o bc_encode.o
86 bc_decode: bc_alltypes.pb.o pb_decode.o bc_decode.o
87 test_encode_extensions: test_encode_extensions.c pb_encode.o alltypes.pb.o extensions.pb.o
88
89 %.pb: %.proto
90         protoc -I. -I../generator -I/usr/include -o$@ $<
91
92 %.pb.c %.pb.h: %.pb ../generator/nanopb_generator.py
93         python ../generator/nanopb_generator.py $<
94
95 breakpoints: ../*.c *.c
96         grep -n 'return false;' $^ | cut -d: -f-2 | xargs -n 1 echo b > $@
97
98 coverage: run_unittests
99         gcov pb_encode.gcda
100         gcov pb_decode.gcda
101
102 run_unittests: $(TESTS)
103         rm -f *.gcda
104         
105         ./decode_unittests > /dev/null
106         ./encode_unittests > /dev/null
107         
108         [ "`./test_encode1 | ./test_decode1`" = \
109         "`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
110
111         [ "`./test_encode2 | ./test_decode1`" = \
112         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
113
114         [ "`./test_encode2 | ./test_decode2`" = \
115         "`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
116         
117         [ "`./test_decode2 < person_with_extra_field.pb`" = \
118         "`./test_encode2 | ./test_decode2`" ]
119         
120         [ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
121         "`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
122
123         ./test_encode3 | ./test_decode3
124         ./test_encode3 1 | ./test_decode3 1
125         ./test_encode3 1 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
126         ./test_encode3_buf 1 | ./test_decode3_buf 1
127         ./test_decode3 < alltypes_with_extra_fields.pb
128         ./bc_encode | ./bc_decode
129
130         ./test_missing_fields
131
132 test_options: options.pb.h options.expected options.pb.o
133         cat options.expected | while read -r p; do \
134             if ! grep -q "$$p" $<; then \
135                 echo Expected: "$$p"; \
136                 exit 1; \
137             fi \
138         done
139
140 run_fuzztest: test_decode3
141         bash -c 'ulimit -c unlimited; I=1; while true; do cat /dev/urandom | ./test_decode3 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'