test.c/Makefile

117 lines
3.2 KiB
Makefile
Raw Normal View History

2019-06-29 09:29:51 +00:00
CROSS_COMPILE ?=
TARGET_FILE ?= ftdi_gpio
2019-06-29 09:29:51 +00:00
CC = $(CROSS_COMPILE)gcc
2019-07-13 10:45:42 +00:00
CPP = $(CROSS_COMPILE)cpp
2019-07-15 11:52:12 +00:00
CHECK = cppcheck
2019-07-15 21:55:17 +00:00
LCOV = lcov
GENHTML = genhtml
2019-06-29 09:29:51 +00:00
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
2019-07-15 21:55:17 +00:00
COVERAGE_DIR = coverage
UNIT_TEST_SRC_DIR = test/unit
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
2019-06-29 09:29:51 +00:00
2019-07-13 10:45:42 +00:00
INCLUDES := inc
2019-07-13 17:17:18 +00:00
INCLUDES += /usr/include/libftdi1
2019-07-15 21:55:17 +00:00
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
2019-07-15 21:55:17 +00:00
C_FLAGS += --coverage
L_FLAGS += --coverage
else
LIBS := ftdi1
endif
2019-07-13 17:17:18 +00:00
2019-07-15 21:55:17 +00:00
C_FLAGS += -O0 -g -Wall -Wextra -Werror
CPP_FLAGS += $(addprefix -I, $(INCLUDES))
CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem
2019-07-09 10:27:35 +00:00
2019-06-29 09:29:51 +00:00
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
2019-07-13 10:45:42 +00:00
C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS)))
2019-06-29 09:29:51 +00:00
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))))
2019-06-29 09:29:51 +00:00
OBJS = $(C_OBJS)
TARGET = $(BIN_DIR)/$(TARGET_FILE)
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
2019-07-13 10:45:42 +00:00
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
2019-06-29 09:29:51 +00:00
.PHONY: all
all: $(TARGET)
2019-07-15 21:55:17 +00:00
.PHONY: coverage
coverage: _cov_genhtml
$(eval COVERAGE:=$(shell grep 'lines' .coverage.tmp | egrep -o '[0-9]+.[0-9]+%'))
@echo
@echo Line coverage: $(COVERAGE)
@echo
@rm -f .coverage.tmp
.PHONY: _cov_lcov
_cov_lcov: exec_unit_test
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
@rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
@mkdir -p $(COVERAGE_DIR)
$(LCOV) -c -d $(OBJ_DIR) -o $(COVERAGE_DIR)/coverage.info
.PHONY: _cov_genhtml
_cov_genhtml: _cov_lcov
$(GENHTML) -o $(COVERAGE_DIR)/html $(COVERAGE_DIR)/coverage.info > .coverage.tmp
.PHONY: unit_test
build_unit_test: $(UNIT_TEST_TARGET)
.PHONY: unit_test
exec_unit_test: $(UNIT_TEST_TARGET)
$(UNIT_TEST_TARGET)
2019-07-15 11:52:12 +00:00
.PHONY: check
check: $(C_SRCS)
2019-07-15 21:55:17 +00:00
$(CHECK) $(CPP_FLAGS) $(CHECK_FLAGS) $(C_SRCS)
2019-07-15 11:52:12 +00:00
$(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 $@
2019-07-13 10:45:42 +00:00
$(TARGET): $(OBJS) $(THIS_MAKEFILE)
2019-06-29 09:29:51 +00:00
@mkdir -p $(BIN_DIR)
2019-07-13 17:17:18 +00:00
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
2019-06-29 09:29:51 +00:00
2019-07-09 10:27:35 +00:00
.PRECIOUS: $(OBJ_DIR)/%.d
2019-07-13 10:45:42 +00:00
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
2019-07-09 10:27:35 +00:00
@mkdir -p $(OBJ_DIR)
2019-07-13 10:45:42 +00:00
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@)
2019-07-09 10:27:35 +00:00
.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,$@)
2019-07-09 10:27:35 +00:00
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
2019-06-29 09:29:51 +00:00
@mkdir -p $(OBJ_DIR)
2019-07-13 10:45:42 +00:00
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
2019-06-29 09:29:51 +00:00
$(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 $@
2019-06-29 09:29:51 +00:00
.PHONY: clean
clean:
2019-07-15 21:55:17 +00:00
rm -f $(OBJS) $(patsubst %.o,%.d,$(OBJS)) $(TARGET)
rm -f $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
2019-06-29 09:29:51 +00:00
ifneq ($(MAKECMDGOALS),clean)
2019-07-09 10:27:35 +00:00
-include $(patsubst %.o,%.d,$(OBJS))
endif