test.c/Makefile
2019-07-16 12:13:44 +02:00

104 lines
3.0 KiB
Makefile

CROSS_COMPILE ?=
TARGET_FILE ?= ftdi_gpio
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp
CHECK = cppcheck
LCOV = lcov
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
UNIT_TEST_SRC_DIR = test/unit
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
INCLUDES := inc
INCLUDES += /usr/include/libftdi1
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
C_FLAGS += -fprofile-arcs -ftest-coverage
L_FLAGS += -fprofile-arcs -ftest-coverage
else
LIBS := ftdi1
endif
C_FLAGS += -O0 -g -Wall -Wextra -Werror
CPP_FLAGS += $(addprefix -I, $(INCLUDES))
CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
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)
TARGET = $(BIN_DIR)/$(TARGET_FILE)
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all
all: $(TARGET)
.PHONY: coverage
coverage: $(UNIT_TEST_TARGET)
$(UNIT_TEST_TARGET)
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
$(LCOV) -c -d obj -d bin/test/unit/ -d src/ -d test/unit/ -o test.info --no-external
$(eval COVERAGE:=$(shell genhtml -o coverage/html test.info | grep 'lines' | egrep -o '[0-9]+.[0-9]+%'))
@echo
@echo Line coverage: $(COVERAGE)
@echo
.PHONY: unit_test
build_unit_test: $(UNIT_TEST_TARGET)
.PHONY: unit_test
exec_unit_test: $(UNIT_TEST_TARGET)
$(UNIT_TEST_TARGET)
.PHONY: check
check: $(C_SRCS)
$(CHECK) $(CPP_FLAGS) $(CHECK_FLAGS) $(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)
@mkdir -p $(BIN_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
.PRECIOUS: $(OBJ_DIR)/%.d
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(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
@mkdir -p $(OBJ_DIR)
$(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
clean:
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)
-include $(patsubst %.o,%.d,$(OBJS))
endif