From 8f17684810718973c8ea0f1150e46d6c1b8c0ef1 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Fri, 7 Aug 2020 22:47:46 +0200 Subject: server: Add basic unit test framework --- Makefile | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 41621f9..fe01fb6 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,24 @@ -CC = gcc -CFLAGS = -Wall -Wextra -std=c11 -g -O2 -fwrapv -D_DEFAULT_SOURCE -LDFLAGS = -ldl +CC := gcc +CFLAGS := -Wall -Wextra -std=c11 -g -O2 -fwrapv -D_DEFAULT_SOURCE +LDFLAGS := -ldl CFLAGS += $(shell pkg-config --cflags sqlite3 libsodium) LDFLAGS += $(shell pkg-config --libs sqlite3 libsodium) -TARGETS = tomsg_server +TARGETS := tomsg_server +TEST_TARGET := test/main -PLUGINDIR = plugins +PLUGINDIR := plugins -PLUGINS = $(filter-out _%,$(patsubst $(PLUGINDIR)/%,%,$(shell find $(PLUGINDIR)/ -maxdepth 1 -type d))) +PLUGINS := $(filter-out _%,$(patsubst $(PLUGINDIR)/%,%,$(shell find $(PLUGINDIR)/ -maxdepth 1 -type d))) -.PHONY: all clean +SOURCES := $(wildcard *.c) +OBJECTS := $(SOURCES:.c=.o) +TEST_ONLY_SOURCES := $(wildcard test/*.c) +TEST_ONLY_OBJECTS := $(TEST_ONLY_SOURCES:.c=.o) +TEST_SOURCES := $(TEST_ONLY_SOURCES) $(filter-out main.c,$(SOURCES)) +TEST_OBJECTS := $(TEST_SOURCES:.c=.o) + +.PHONY: all clean test_build test # Clear all implicit suffix rules .SUFFIXES: @@ -19,18 +27,32 @@ PLUGINS = $(filter-out _%,$(patsubst $(PLUGINDIR)/%,%,$(shell find $(PLUGINDIR)/ .SECONDARY: all: $(TARGETS) - for pl in $(PLUGINS); do make --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done + for pl in $(PLUGINS); do $(MAKE) --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done clean: - rm -f $(TARGETS) *.o *.sql.h - for pl in $(PLUGINS); do make --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done + rm -f $(TARGETS) $(TEST_TARGET) *.o *.sql.h test/test_declarations.h + for pl in $(PLUGINS); do $(MAKE) --no-print-directory -C $(PLUGINDIR) $@ PLUGINNAME=$$pl; done + +test_build: $(TEST_TARGET) + +test: test_build + $(TEST_TARGET) -$(TARGETS): $(patsubst %.c,%.o,$(wildcard *.c)) +$(TARGETS): $(OBJECTS) $(CC) -o $@ $^ $(LDFLAGS) -%.o: %.c $(wildcard *.h) $(patsubst %.sql,%.sql.h,$(wildcard *.sql)) +$(OBJECTS) $(TEST_ONLY_OBJECTS): $(wildcard *.h) $(patsubst %.sql,%.sql.h,$(wildcard *.sql)) +$(TEST_ONLY_OBJECTS): test/test_declarations.h +%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< %.sql.h: %.sql xxd -i $^ $@ + + +$(TEST_TARGET): $(TEST_OBJECTS) $(wildcard *.h test/*.h) + $(CC) -o $@ $(filter %.o,$^) $(LDFLAGS) + +test/test_declarations.h: $(wildcard test/*.c) + $(CC) -E -D TEST_HEADER_GENERATION=1 test/*.c | sed -n 's/^XXTEST_DECLARATION(\([^)]*\)).*/int \1(void);/p' >$@ -- cgit v1.2.3-70-g09d2