demo/Makefile

149 lines
4.0 KiB
Makefile
Raw Permalink Normal View History

2020-02-20 21:19:09 +00:00
PLATFORM ?= posix
2020-02-13 11:00:05 +00:00
CROSS_COMPILE ?=
TARGET_FILE ?= test_c
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp
CHECK = cppcheck
LCOV = lcov
GENHTML = genhtml
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
COVERAGE_DIR = coverage
UNIT_TEST_SRC_DIR = test/unit
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
PREFIX ?= /usr
BIN_INSTALL_DIR = $(PREFIX)/bin
INCLUDES += include/
2020-02-20 21:19:09 +00:00
INCLUDES += include/platform/$(PLATFORM)/
2020-02-13 11:00:05 +00:00
INCLUDES += $(PREFIX)/include/
INCLUDES += $(EXTRA_INC)
LIBS =
LD_FLAGS += $(addprefix -L,$(EXTRA_LIB_DIR))
2020-02-14 12:11:17 +00:00
LD_FLAGS += -lm
2020-02-13 11:00:05 +00:00
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
C_FLAGS += --coverage
LD_FLAGS += --coverage
endif
C_FLAGS += -fpic
2020-02-20 20:04:11 +00:00
C_FLAGS += -O0 -ggdb3 -Wall
2020-02-13 11:00:05 +00:00
CPP_FLAGS += $(addprefix -I, $(INCLUDES))
2020-02-20 21:19:09 +00:00
CPP_FLAGS += -DPLATFORM=$(PLATFORM) -DPLATFORM_$(PLATFORM)
2020-02-13 11:00:05 +00:00
CHECK_FLAGS = $(addprefix -I,$(filter-out $(PREFIX)/include/,$(INCLUDES)))
2020-02-20 21:19:09 +00:00
CHECK_FLAGS += --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr --force --xml --xml-version=2 2> cppcheck-report.xml
2020-02-13 11:00:05 +00:00
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
2020-02-20 21:19:09 +00:00
C_SRCS += $(wildcard $(SRC_DIR)/platform/$(PLATFORM)/*.c)
2020-02-13 11:00:05 +00:00
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,$(CC_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)
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
TARGET = $(BIN_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all install uninstall clean distclean debug
debug:
@echo $(C_SRCS)
@echo $(C_OBJS)
2020-02-20 21:19:09 +00:00
@echo $(PLATFORM)
2020-02-13 11:00:05 +00:00
@echo $(CPP_FLAGS)
all: $(TARGET)
install: all
install -d $(BIN_INSTALL_DIR)
install -m 0666 $(TARGET) $(BIN_INSTALL_DIR)
uninstall:
rm -f $(BIN_INSTALL_DIR)/$(TARGET_FILE)
clean:
rm -f $(TARGET)
rm -f $(OBJS) $(patsubst %.o,%.d,$(OBJS))
rm -f $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
distclean:
rm -rf $(OBJ_DIR) $(BIN_DIR) $(COVERAGE_DIR)
2020-02-20 21:19:09 +00:00
.PHONY: check
check: $(C_SRCS)
echo $(C_SRCS)
cppcheck $(CHECK_FLAGS) $(C_SRCS)
2020-02-13 11:00:05 +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)
$(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)
2020-02-20 20:04:11 +00:00
$(CC) $(C_FLAGS) $(LD_FLAGS) $(UNIT_TEST_OBJS) -o $@
2020-02-13 11:00:05 +00:00
$(TARGET): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR)
2020-02-20 20:04:11 +00:00
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
2020-02-13 11:00:05 +00:00
.PRECIOUS: $(OBJ_DIR)/%.d
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
2020-02-20 21:19:09 +00:00
@mkdir -p $(OBJ_DIR)/platform/$(PLATFORM)
2020-02-13 11:00:05 +00:00
$(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
2020-02-20 21:19:09 +00:00
@mkdir -p $(OBJ_DIR)/platform/$(PLATFORM)
2020-02-13 11:00:05 +00:00
$(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 $@
ifneq ($(MAKECMDGOALS),clean)
-include $(patsubst %.o,%.d,$(OBJS))
endif