Compare commits

...

1 Commits

Author SHA1 Message Date
Thomas Klaehn
759e133f32 Add unit test framework and simple unit test example 2019-07-15 22:38:04 +02:00

View File

@ -1,6 +1,6 @@
CROSS_COMPILE ?= CROSS_COMPILE ?=
TARGET_FILE ?= test TARGET_FILE ?= ftdi_gpio
CC = $(CROSS_COMPILE)gcc CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp CPP = $(CROSS_COMPILE)cpp
@ -9,9 +9,14 @@ CHECK = cppcheck
SRC_DIR = src SRC_DIR = src
OBJ_DIR = obj OBJ_DIR = obj
BIN_DIR = bin BIN_DIR = bin
UNIT_TEST_SRC_DIR = test/unit
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
INCLUDES := inc INCLUDES := inc
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test)" ""
INCLUDES += test/inc
else
#Alpine Linux names libftdi libftdi1 #Alpine Linux names libftdi libftdi1
ifneq "$(findstring Alpine,$(shell head -1 /etc/issue))" "" ifneq "$(findstring Alpine,$(shell head -1 /etc/issue))" ""
INCLUDES += /usr/include/libftdi1 INCLUDES += /usr/include/libftdi1
@ -19,6 +24,7 @@ LIBS := ftdi1
else else
LIBS := ftdi LIBS := ftdi
endif endif
endif
LD_FLAGS := LD_FLAGS :=
C_FLAGS := -O0 -g -Wall -Wextra -Werror C_FLAGS := -O0 -g -Wall -Wextra -Werror
@ -28,18 +34,34 @@ CHECK_CLAGS := --enable=all --template=gcc --error-exitcode=1 --suppress=missing
C_SRCS = $(wildcard $(SRC_DIR)/*.c) C_SRCS = $(wildcard $(SRC_DIR)/*.c)
C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS))) C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS)))
UNIT_TEST_SRCS = $(wildcard $(UNIT_TEST_SRC_DIR)/*.c) $(filter-out %main.c,$(C_SRCS))
UNIT_TEST_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst $(UNIT_TEST_SRC_DIR)%,$(UNIT_TEST_OBJ_DIR)%,$(patsubst %.c,%.o,$(UNIT_TEST_SRCS))))
OBJS = $(C_OBJS) OBJS = $(C_OBJS)
TARGET = $(BIN_DIR)/$(TARGET_FILE) TARGET = $(BIN_DIR)/$(TARGET_FILE)
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all .PHONY: all
all: $(TARGET) all: $(TARGET)
.PHONY: unit_test
build_unit_test: $(UNIT_TEST_TARGET)
.PHONY: unit_test
exec_unit_test: $(UNIT_TEST_TARGET)
$(UNIT_TEST_TARGET)
.PHONY: check .PHONY: check
check: $(C_SRCS) check: $(C_SRCS)
$(CHECK) $(CPP_FLAGS) $(CHECK_CLAGS) $(C_SRCS) $(CHECK) $(CPP_FLAGS) $(CHECK_CLAGS) $(C_SRCS)
$(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(UNIT_TEST_OBJS) $(addprefix -l,$(LIBS)) -o $@
$(TARGET): $(OBJS) $(THIS_MAKEFILE) $(TARGET): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR) @mkdir -p $(BIN_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@ $(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
@ -49,13 +71,23 @@ $(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR) @mkdir -p $(OBJ_DIR)
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@) $(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@)
.PRECIOUS: $(UNIT_TEST_OBJ_DIR)/%.d
$(UNIT_TEST_OBJ_DIR)/%.d: $(UNIT_TEST_SRC_DIR)/%.c
@mkdir -p $(UNIT_TEST_OBJ_DIR)
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(UNIT_TEST_OBJ_DIR)/%.d,$(UNIT_TEST_SRC_DIR)/%.c,$@)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
@mkdir -p $(OBJ_DIR) @mkdir -p $(OBJ_DIR)
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@ $(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
$(UNIT_TEST_OBJ_DIR)/%.o: $(UNIT_TEST_SRC_DIR)/%.c $(UNIT_TEST_OBJ_DIR)/%.d
@mkdir -p $(UNIT_TEST_OBJ_DIR)
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf $(OBJS) $(patsubst %.o,%.d,$(OBJS)) $(TARGET) rm -rf $(OBJS) $(patsubst %.o,%.d,$(OBJS)) $(TARGET)
rm -rf $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
ifneq ($(MAKECMDGOALS),clean) ifneq ($(MAKECMDGOALS),clean)
-include $(patsubst %.o,%.d,$(OBJS)) -include $(patsubst %.o,%.d,$(OBJS))