Skip to content

Commit

Permalink
Add AVR Makefile, various AVR fixes
Browse files Browse the repository at this point in the history
This hopefully completes AVR support for #80 and #79.
  • Loading branch information
ludocode committed Jul 30, 2021
1 parent 8615b7a commit a378e91
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/mpack/mpack-defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@
* reading/writing C files and makes debugging easier.
*/
#ifndef MPACK_STDIO
#define MPACK_STDIO 1
#ifdef __AVR__
#define MPACK_STDIO 0
#else
#define MPACK_STDIO 1
#endif
#endif

/**
Expand Down
10 changes: 10 additions & 0 deletions src/mpack/mpack-platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@
#define MPACK_SILENCE_WARNINGS_SHADOW /*nothing*/
#endif

// On platforms with small size_t (e.g. AVR) we get type limits warnings where
// we compare a size_t to e.g. UINT32_MAX.
#ifdef __AVR__
#define MPACK_SILENCE_WARNINGS_TYPE_LIMITS \
_Pragma ("GCC diagnostic ignored \"-Wtype-limits\"")
#else
#define MPACK_SILENCE_WARNINGS_TYPE_LIMITS /*nothing*/
#endif

// MPack uses declarations after statements. This silences warnings about it
// (e.g. when using MPack in a Linux kernel module.)
#if defined(__GNUC__) && !defined(__cplusplus)
Expand All @@ -237,6 +246,7 @@
MPACK_SILENCE_WARNINGS_PUSH \
MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES \
MPACK_SILENCE_WARNINGS_SHADOW \
MPACK_SILENCE_WARNINGS_TYPE_LIMITS \
MPACK_SILENCE_WARNINGS_DECLARATION_AFTER_STATEMENT

#define MPACK_SILENCE_WARNINGS_END \
Expand Down
51 changes: 51 additions & 0 deletions test/avr/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This builds MPack and its test suite with avr-gcc for AVR (e.g. Arduino.)
# It doesn't actually work yet; in fact it doesn't link because the resulting
# code is way too big. But it does actually compile all the source files at
# least. Eventually it would be nice to trim the unit test suite down into
# something that could run on an Arduino. It would also be nice to set up some
# wrapper scripts to make it run under simavr so we can run it on CI builds.

# Requires avr-gcc and avr-libc (even though it builds with MPACK_STDLIB
# disabled) since avr-libc has stdint.h and friends.

ifeq (Makefile, $(firstword $(MAKEFILE_LIST)))
$(error The current directory should be the root of the repository. Try "cd ../.." and then "make -f test/avr-gcc/Makefile")
endif

CC=avr-gcc
BUILD := test/.build/avr
PROG := mpack-avr

CPPFLAGS := -Isrc -Itest -DMPACK_HAS_CONFIG=1
CFLAGS := \
-Os -DNDEBUG \
-Wall -Wextra -Wpedantic -Werror \
-MMD -MP

SRCS := \
$(shell find src/ -type f -name '*.c') \
$(wildcard test/test*.c)

OBJS := $(patsubst %, $(BUILD)/%.o, $(SRCS))

GLOBAL_DEPENDENCIES := test/avr/Makefile

.PHONY: all
all: $(PROG)

.PHONY: clean
clean:
rm -rf $(BUILD)

-include $(patsubst %, $(BUILD)/%.d, $(SRCS))

.PHONY: $(PROG)
$(PROG): $(BUILD)/$(PROG)

$(OBJS): $(BUILD)/%.o: % $(GLOBAL_DEPENDENCIES)
@mkdir -p $(dir $@)
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

$(BUILD)/$(PROG): $(OBJS)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
14 changes: 9 additions & 5 deletions test/mpack-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
#ifndef MPACK_STDIO
#define MPACK_STDIO 0
#endif

#elif defined(__AVR__)
#define MPACK_STDLIB 0
#define MPACK_STDIO 0
#define MPACK_STRINGS 0

#else
#error
// For other platforms, we currently only test in a single configuration,
// so we enable everything and otherwise use the default for most settings.
#define MPACK_COMPATIBILITY 1
Expand All @@ -45,11 +52,8 @@
// We need MPACK_STDLIB and MPACK_STDIO defined before test-system.h to
// override their functions.
#define MPACK_STDLIB 1
#ifndef __AVR__
#define MPACK_STDIO 1
#else
#define MPACK_STDIO 0
#endif
#define MPACK_STDIO 1

#endif

// We've disabled the unit test for single inline under tcc.
Expand Down
12 changes: 10 additions & 2 deletions test/test-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ static const size_t test_buffer_sizes[] = {
129, 131, 160, 163, 191, 192, 193,
251, 256, 257, 509, 512, 521,
1021, 1024, 1031, 2039, 2048, 2053,
#ifndef __AVR__
4093, 4096, 4099, 7919, 8192,
16384, 32768
6384, 32768,
#endif
};

#if MPACK_READER
Expand Down Expand Up @@ -272,7 +274,13 @@ static void test_write_buffer(void) {
size_t i;
for (i = 0; i < sizeof(test_buffer_sizes) / sizeof(test_buffer_sizes[0]); ++i) {
size_t size = test_buffer_sizes[i];
size_t output_size = 0xffff;
size_t output_size =
#ifdef __AVR__
0xfff
#else
0xfffff
#endif
;
char* output = (char*)malloc(output_size);

// initialize the writer with our buffer writer function
Expand Down
16 changes: 10 additions & 6 deletions test/test-expect.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ static void test_expect_uint_signed() {

TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_i32(&reader));
TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_i64(&reader));
TEST_SIMPLE_READ("\xcd\xff\xff", 0xffff == mpack_expect_int(&reader));

if (sizeof(int) >= 4)
TEST_SIMPLE_READ("\xcd\xff\xff", (int)0xffff == mpack_expect_int(&reader));
else if (sizeof(int) < 4)
TEST_SIMPLE_READ_ERROR("\xcd\xff\xff", mpack_expect_int(&reader), mpack_error_type);

TEST_SIMPLE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_expect_i32(&reader));
TEST_SIMPLE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_expect_i64(&reader));
Expand Down Expand Up @@ -257,9 +261,9 @@ static void test_expect_int() {
TEST_SIMPLE_READ("\xd1\x80\x00", INT16_MIN == mpack_expect_i64(&reader));
TEST_SIMPLE_READ("\xd1\x80\x00", INT16_MIN == mpack_expect_int(&reader));

TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_i32(&reader));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_i64(&reader));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_expect_int(&reader));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_i32(&reader));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_i64(&reader));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_expect_int(&reader));

TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_expect_i32(&reader));
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_expect_i64(&reader));
Expand Down Expand Up @@ -305,7 +309,7 @@ static void test_expect_ints_dynamic_int() {
TEST_SIMPLE_READ("\xd0\x80", mpack_tag_equal(mpack_tag_int(INT8_MIN), mpack_read_tag(&reader)));
TEST_SIMPLE_READ("\xd1\xff\x7f", mpack_tag_equal(mpack_tag_int(INT8_MIN - 1), mpack_read_tag(&reader)));
TEST_SIMPLE_READ("\xd1\x80\x00", mpack_tag_equal(mpack_tag_int(INT16_MIN), mpack_read_tag(&reader)));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int(INT16_MIN - 1), mpack_read_tag(&reader)));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int((int32_t)INT16_MIN - 1), mpack_read_tag(&reader)));

TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", mpack_tag_equal(mpack_tag_int(INT32_MIN), mpack_read_tag(&reader)));
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)));
Expand Down Expand Up @@ -414,7 +418,7 @@ static void test_expect_int_match() {
TEST_SIMPLE_READ("\xd0\x80", (mpack_expect_int_match(&reader, INT8_MIN), true));
TEST_SIMPLE_READ("\xd1\xff\x7f", (mpack_expect_int_match(&reader, INT8_MIN - 1), true));
TEST_SIMPLE_READ("\xd1\x80\x00", (mpack_expect_int_match(&reader, INT16_MIN), true));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (mpack_expect_int_match(&reader, INT16_MIN - 1), true));
TEST_SIMPLE_READ("\xd2\xff\xff\x7f\xff", (mpack_expect_int_match(&reader, (int32_t)INT16_MIN - 1), true));
TEST_SIMPLE_READ("\xd2\x80\x00\x00\x00", (mpack_expect_int_match(&reader, INT32_MIN), true));
TEST_SIMPLE_READ("\xd3\xff\xff\xff\xff\x7f\xff\xff\xff", (mpack_expect_int_match(&reader, (int64_t)INT32_MIN - 1), true));
TEST_SIMPLE_READ("\xd3\x80\x00\x00\x00\x00\x00\x00\x00", (mpack_expect_int_match(&reader, INT64_MIN), true));
Expand Down
14 changes: 9 additions & 5 deletions test/test-node.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ static void test_node_read_uint_signed() {

TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_i32(node));
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_i64(node));
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", 0xffff == mpack_node_int(node));

if (sizeof(int) >= 4)
TEST_SIMPLE_TREE_READ("\xcd\xff\xff", (int)0xffff == mpack_node_int(node));
else if (sizeof(int) < 4)
TEST_SIMPLE_TREE_READ_ERROR("\xcd\xff\xff", mpack_node_int(node), mpack_error_type);

TEST_SIMPLE_TREE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_node_i32(node));
TEST_SIMPLE_TREE_READ("\xce\x00\x01\x00\x00", 0x10000 == mpack_node_i64(node));
Expand Down Expand Up @@ -290,9 +294,9 @@ static void test_node_read_int() {
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", INT16_MIN == mpack_node_i64(node));
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", INT16_MIN == mpack_node_int(node));

TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_i32(node));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_i64(node));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", INT16_MIN - 1 == mpack_node_int(node));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_i32(node));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_i64(node));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", (int32_t)INT16_MIN - 1 == mpack_node_int(node));

TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_node_i32(node));
TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", INT32_MIN == mpack_node_i64(node));
Expand Down Expand Up @@ -339,7 +343,7 @@ static void test_node_read_ints_dynamic_int() {
TEST_SIMPLE_TREE_READ("\xd0\x80", mpack_tag_equal(mpack_tag_int(INT8_MIN), mpack_node_tag(node)));
TEST_SIMPLE_TREE_READ("\xd1\xff\x7f", mpack_tag_equal(mpack_tag_int(INT8_MIN - 1), mpack_node_tag(node)));
TEST_SIMPLE_TREE_READ("\xd1\x80\x00", mpack_tag_equal(mpack_tag_int(INT16_MIN), mpack_node_tag(node)));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int(INT16_MIN - 1), mpack_node_tag(node)));
TEST_SIMPLE_TREE_READ("\xd2\xff\xff\x7f\xff", mpack_tag_equal(mpack_tag_int((int32_t)INT16_MIN - 1), mpack_node_tag(node)));

TEST_SIMPLE_TREE_READ("\xd2\x80\x00\x00\x00", mpack_tag_equal(mpack_tag_int(INT32_MIN), mpack_node_tag(node)));
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)));
Expand Down
29 changes: 17 additions & 12 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@
#define _CRT_SECURE_NO_WARNINGS 1
#endif

#include <stdio.h>
// mpack poisons double when MPACK_DOUBLE is disabled so we give ourselves a
// macro to use it manually in tests
#define TEST_DOUBLE double

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <setjmp.h>

#ifdef WIN32
#include <direct.h>
#define mkdir(path, mode) ((void)(mode), _mkdir(path))
#define rmdir _rmdir
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include "mpack/mpack.h"

// mpack poisons double when MPACK_DOUBLE is disabled so we give ourselves a
// macro to use it manually in tests
#define TEST_DOUBLE double
#include <stdio.h>

#if MPACK_STDIO
#ifdef WIN32
#include <direct.h>
#define mkdir(path, mode) ((void)(mode), _mkdir(path))
#define rmdir _rmdir
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
#endif

#include "mpack/mpack.h"

Expand Down

0 comments on commit a378e91

Please sign in to comment.