Skip to content

Commit e445ed0

Browse files
committed
Add AVR Makefile, various AVR fixes
This hopefully completes AVR support for #74 and #79.
1 parent 8615b7a commit e445ed0

File tree

8 files changed

+124
-32
lines changed

8 files changed

+124
-32
lines changed

src/mpack/mpack-defaults.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@
148148
* reading/writing C files and makes debugging easier.
149149
*/
150150
#ifndef MPACK_STDIO
151-
#define MPACK_STDIO 1
151+
#ifdef __AVR__
152+
#define MPACK_STDIO 0
153+
#else
154+
#define MPACK_STDIO 1
155+
#endif
152156
#endif
153157

154158
/**
@@ -231,7 +235,11 @@
231235
* mpack_error_to_string() and mpack_type_to_string() return an empty string.
232236
*/
233237
#ifndef MPACK_STRINGS
234-
#define MPACK_STRINGS 1
238+
#ifdef __AVR__
239+
#define MPACK_STRINGS 0
240+
#else
241+
#define MPACK_STRINGS 1
242+
#endif
235243
#endif
236244

237245
/**

src/mpack/mpack-platform.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@
223223
#define MPACK_SILENCE_WARNINGS_SHADOW /*nothing*/
224224
#endif
225225

226+
// On platforms with small size_t (e.g. AVR) we get type limits warnings where
227+
// we compare a size_t to e.g. UINT32_MAX.
228+
#ifdef __AVR__
229+
#define MPACK_SILENCE_WARNINGS_TYPE_LIMITS \
230+
_Pragma ("GCC diagnostic ignored \"-Wtype-limits\"")
231+
#else
232+
#define MPACK_SILENCE_WARNINGS_TYPE_LIMITS /*nothing*/
233+
#endif
234+
226235
// MPack uses declarations after statements. This silences warnings about it
227236
// (e.g. when using MPack in a Linux kernel module.)
228237
#if defined(__GNUC__) && !defined(__cplusplus)
@@ -237,6 +246,7 @@
237246
MPACK_SILENCE_WARNINGS_PUSH \
238247
MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES \
239248
MPACK_SILENCE_WARNINGS_SHADOW \
249+
MPACK_SILENCE_WARNINGS_TYPE_LIMITS \
240250
MPACK_SILENCE_WARNINGS_DECLARATION_AFTER_STATEMENT
241251

242252
#define MPACK_SILENCE_WARNINGS_END \

test/avr/Makefile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This builds MPack and its test suite with avr-gcc for AVR (e.g. Arduino.)
2+
# It doesn't actually work yet; in fact it doesn't link because the resulting
3+
# code is way too big. But it does actually compile all the source files at
4+
# least. Eventually it would be nice to trim the unit test suite down into
5+
# something that could run on an Arduino. It would also be nice to set up some
6+
# wrapper scripts to make it run under simavr so we can run it on CI builds.
7+
8+
# Requires avr-gcc and avr-libc (even though it builds with MPACK_STDLIB
9+
# disabled) since avr-libc has stdint.h and friends.
10+
11+
ifeq (Makefile, $(firstword $(MAKEFILE_LIST)))
12+
$(error The current directory should be the root of the repository. Try "cd ../.." and then "make -f test/avr-gcc/Makefile")
13+
endif
14+
15+
CC=avr-gcc
16+
BUILD := test/.build/avr
17+
PROG := mpack-avr
18+
19+
CPPFLAGS := -Isrc -Itest -DMPACK_HAS_CONFIG=1
20+
CFLAGS := \
21+
-Os -DNDEBUG \
22+
-Wall -Wextra -Wpedantic -Werror \
23+
-MMD -MP
24+
25+
SRCS := \
26+
$(shell find src/ -type f -name '*.c') \
27+
$(wildcard test/test*.c)
28+
29+
OBJS := $(patsubst %, $(BUILD)/%.o, $(SRCS))
30+
31+
GLOBAL_DEPENDENCIES := test/avr/Makefile
32+
33+
.PHONY: all
34+
all: $(PROG)
35+
36+
.PHONY: clean
37+
clean:
38+
rm -rf $(BUILD)
39+
40+
-include $(patsubst %, $(BUILD)/%.d, $(SRCS))
41+
42+
.PHONY: $(PROG)
43+
$(PROG): $(BUILD)/$(PROG)
44+
45+
$(OBJS): $(BUILD)/%.o: % $(GLOBAL_DEPENDENCIES)
46+
@mkdir -p $(dir $@)
47+
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
48+
49+
$(BUILD)/$(PROG): $(OBJS)
50+
@mkdir -p $(dir $@)
51+
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)

test/mpack-config.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
#ifndef MPACK_STDIO
3333
#define MPACK_STDIO 0
3434
#endif
35+
36+
#elif defined(__AVR__)
37+
#define MPACK_STDLIB 0
38+
#define MPACK_STDIO 0
39+
3540
#else
3641
// For other platforms, we currently only test in a single configuration,
3742
// so we enable everything and otherwise use the default for most settings.
@@ -45,11 +50,8 @@
4550
// We need MPACK_STDLIB and MPACK_STDIO defined before test-system.h to
4651
// override their functions.
4752
#define MPACK_STDLIB 1
48-
#ifndef __AVR__
49-
#define MPACK_STDIO 1
50-
#else
51-
#define MPACK_STDIO 0
52-
#endif
53+
#define MPACK_STDIO 1
54+
5355
#endif
5456

5557
// We've disabled the unit test for single inline under tcc.

test/test-buffer.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ static const size_t test_buffer_sizes[] = {
101101
129, 131, 160, 163, 191, 192, 193,
102102
251, 256, 257, 509, 512, 521,
103103
1021, 1024, 1031, 2039, 2048, 2053,
104+
#ifndef __AVR__
104105
4093, 4096, 4099, 7919, 8192,
105-
16384, 32768
106+
6384, 32768,
107+
#endif
106108
};
107109

108110
#if MPACK_READER
@@ -272,7 +274,13 @@ static void test_write_buffer(void) {
272274
size_t i;
273275
for (i = 0; i < sizeof(test_buffer_sizes) / sizeof(test_buffer_sizes[0]); ++i) {
274276
size_t size = test_buffer_sizes[i];
275-
size_t output_size = 0xffff;
277+
size_t output_size =
278+
#ifdef __AVR__
279+
0xfff
280+
#else
281+
0xfffff
282+
#endif
283+
;
276284
char* output = (char*)malloc(output_size);
277285

278286
// initialize the writer with our buffer writer function

test/test-expect.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ static void test_expect_uint_signed() {
221221

222222
TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_i32(&reader));
223223
TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_i64(&reader));
224-
TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_int(&reader));
224+
225+
if (sizeof(int) >= 4)
226+
TEST_SIMPLE_READ("\xcd\xff\xff", (int)0xffff == mpack_expect_int(&reader));
227+
else if (sizeof(int) < 4)
228+
TEST_SIMPLE_READ_ERROR("\xcd\xff\xff", mpack_expect_int(&reader), mpack_error_type);
225229

226230
TEST_SIMPLE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_expect_i32(&reader));
227231
TEST_SIMPLE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_expect_i64(&reader));
@@ -257,9 +261,9 @@ static void test_expect_int() {
257261
TEST_SIMPLE_READ("\xd1\x80\x00", INT16_MIN == mpack_expect_i64(&reader));
258262
TEST_SIMPLE_READ("\xd1\x80\x00", INT16_MIN == mpack_expect_int(&reader));
259263

260-
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_i32(&reader));
261-
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_i64(&reader));
262-
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_int(&reader));
264+
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_i32(&reader));
265+
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_i64(&reader));
266+
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_int(&reader));
263267

264268
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_expect_i32(&reader));
265269
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_expect_i64(&reader));
@@ -305,7 +309,7 @@ static void test_expect_ints_dynamic_int() {
305309
TEST_SIMPLE_READ("\xd0\x80", mpack_tag_equal(mpack_tag_int(INT8_MIN), mpack_read_tag(&reader)));
306310
TEST_SIMPLE_READ("\xd1\xff\x7f", mpack_tag_equal(mpack_tag_int(INT8_MIN - 1), mpack_read_tag(&reader)));
307311
TEST_SIMPLE_READ("\xd1\x80\x00", mpack_tag_equal(mpack_tag_int(INT16_MIN), mpack_read_tag(&reader)));
308-
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int(INT16_MIN - 1), mpack_read_tag(&reader)));
312+
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int((int32_t)INT16_MIN - 1), mpack_read_tag(&reader)));
309313

310314
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", mpack_tag_equal(mpack_tag_int(INT32_MIN), mpack_read_tag(&reader)));
311315
TEST_SIMPLE_READ("\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", mpack_tag_equal(mpack_tag_int((int64_t)INT32_MIN - 1), mpack_read_tag(&reader)));
@@ -414,7 +418,7 @@ static void test_expect_int_match() {
414418
TEST_SIMPLE_READ("\xd0\x80", (mpack_expect_int_match(&reader, INT8_MIN), true));
415419
TEST_SIMPLE_READ("\xd1\xff\x7f", (mpack_expect_int_match(&reader, INT8_MIN - 1), true));
416420
TEST_SIMPLE_READ("\xd1\x80\x00", (mpack_expect_int_match(&reader, INT16_MIN), true));
417-
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (mpack_expect_int_match(&reader, INT16_MIN - 1), true));
421+
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (mpack_expect_int_match(&reader, (int32_t)INT16_MIN - 1), true));
418422
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", (mpack_expect_int_match(&reader, INT32_MIN), true));
419423
TEST_SIMPLE_READ("\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", (mpack_expect_int_match(&reader, (int64_t)INT32_MIN - 1), true));
420424
TEST_SIMPLE_READ("\xd3\x80\x00\x00\x00\x00\x00\x00\x00", (mpack_expect_int_match(&reader, INT64_MIN), true));

test/test-node.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ static void test_node_read_uint_signed() {
253253

254254
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_i32(node));
255255
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_i64(node));
256-
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_int(node));
256+
257+
if (sizeof(int) >= 4)
258+
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", (int)0xffff == mpack_node_int(node));
259+
else if (sizeof(int) < 4)
260+
TEST_SIMPLE_TREE_READ_ERROR("\xcd\xff\xff", mpack_node_int(node), mpack_error_type);
257261

258262
TEST_SIMPLE_TREE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_node_i32(node));
259263
TEST_SIMPLE_TREE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_node_i64(node));
@@ -290,9 +294,9 @@ static void test_node_read_int() {
290294
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", INT16_MIN == mpack_node_i64(node));
291295
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", INT16_MIN == mpack_node_int(node));
292296

293-
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_i32(node));
294-
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_i64(node));
295-
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_int(node));
297+
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_i32(node));
298+
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_i64(node));
299+
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_int(node));
296300

297301
TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_node_i32(node));
298302
TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_node_i64(node));
@@ -339,7 +343,7 @@ static void test_node_read_ints_dynamic_int() {
339343
TEST_SIMPLE_TREE_READ("\xd0\x80", mpack_tag_equal(mpack_tag_int(INT8_MIN), mpack_node_tag(node)));
340344
TEST_SIMPLE_TREE_READ("\xd1\xff\x7f", mpack_tag_equal(mpack_tag_int(INT8_MIN - 1), mpack_node_tag(node)));
341345
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", mpack_tag_equal(mpack_tag_int(INT16_MIN), mpack_node_tag(node)));
342-
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int(INT16_MIN - 1), mpack_node_tag(node)));
346+
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int((int32_t)INT16_MIN - 1), mpack_node_tag(node)));
343347

344348
TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", mpack_tag_equal(mpack_tag_int(INT32_MIN), mpack_node_tag(node)));
345349
TEST_SIMPLE_TREE_READ("\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", mpack_tag_equal(mpack_tag_int((int64_t)INT32_MIN - 1), mpack_node_tag(node)));

test/test.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,29 @@
2929
#define _CRT_SECURE_NO_WARNINGS 1
3030
#endif
3131

32-
#include <stdio.h>
32+
// mpack poisons double when MPACK_DOUBLE is disabled so we give ourselves a
33+
// macro to use it manually in tests
34+
#define TEST_DOUBLE double
35+
3336
#include <string.h>
3437
#include <stdlib.h>
3538
#include <math.h>
3639
#include <setjmp.h>
3740

38-
#ifdef WIN32
39-
#include <direct.h>
40-
#define mkdir(path, mode) ((void)(mode), _mkdir(path))
41-
#define rmdir _rmdir
42-
#else
43-
#include <sys/stat.h>
44-
#include <sys/types.h>
45-
#endif
41+
#include "mpack/mpack.h"
4642

47-
// mpack poisons double when MPACK_DOUBLE is disabled so we give ourselves a
48-
// macro to use it manually in tests
49-
#define TEST_DOUBLE double
43+
#include <stdio.h>
44+
45+
#if MPACK_STDIO
46+
#ifdef WIN32
47+
#include <direct.h>
48+
#define mkdir(path, mode) ((void)(mode), _mkdir(path))
49+
#define rmdir _rmdir
50+
#else
51+
#include <sys/stat.h>
52+
#include <sys/types.h>
53+
#endif
54+
#endif
5055

5156
#include "mpack/mpack.h"
5257

0 commit comments

Comments
 (0)