mirror of https://github.com/rusefi/bldc.git
[Make] Add unit testing
This adds the Makefile scaffolding, as well as the tool installer.
This commit is contained in:
parent
44c355fdbf
commit
96fd43f8bf
79
Makefile
79
Makefile
|
@ -67,6 +67,10 @@ help:
|
|||
@echo " [Tool Installers]"
|
||||
@echo " arm_sdk_install - Install the GNU ARM gcc toolchain"
|
||||
@echo
|
||||
@echo " [Unit Tests]"
|
||||
@echo " all_ut - Build all unit tests"
|
||||
@echo " all_ut_xml - Run all unit tests and capture all XML output to files"
|
||||
@echo " all_ut_run - Run all unit tests and dump XML output to console"
|
||||
@echo
|
||||
@echo " [Firmware]"
|
||||
@echo " fw - Build firmware for default target"
|
||||
|
@ -121,3 +125,78 @@ debug-start:
|
|||
clean:
|
||||
$(V0) @echo " CLEAN $$@"
|
||||
$(V1) [ ! -d "$(BUILD_DIR)" ] || $(RM) -r "$(BUILD_DIR)"
|
||||
|
||||
|
||||
##############################
|
||||
#
|
||||
# Unit Tests
|
||||
#
|
||||
##############################
|
||||
|
||||
ALL_UNITTESTS := utils
|
||||
|
||||
UT_OUT_DIR := $(BUILD_DIR)/unit_tests
|
||||
|
||||
$(UT_OUT_DIR):
|
||||
$(V1) mkdir -p $@
|
||||
|
||||
.PHONY: all_ut
|
||||
all_ut: $(addsuffix _elf, $(addprefix ut_, $(ALL_UNITTESTS))) $(ALL_PYTHON_UNITTESTS)
|
||||
|
||||
.PHONY: all_ut_xml
|
||||
all_ut_xml: $(addsuffix _xml, $(addprefix ut_, $(ALL_UNITTESTS)))
|
||||
|
||||
.PHONY: all_ut_run
|
||||
all_ut_run: $(addsuffix _run, $(addprefix ut_, $(ALL_UNITTESTS))) $(ALL_PYTHON_UNITTESTS)
|
||||
|
||||
.PHONY: all_ut_gcov
|
||||
all_ut_gcov: | $(addsuffix _gcov, $(addprefix ut_, $(ALL_UNITTESTS)))
|
||||
|
||||
.PHONY: all_ut_clean
|
||||
all_ut_clean:
|
||||
$(V0) @echo " CLEAN $@"
|
||||
$(V1) [ ! -d "$(UT_OUT_DIR)" ] || $(RM) -r "$(UT_OUT_DIR)"
|
||||
|
||||
# $(1) = Unit test name
|
||||
define UT_TEMPLATE
|
||||
.PHONY: ut_$(1)
|
||||
ut_$(1): ut_$(1)_run
|
||||
ut_$(1)_gcov: | ut_$(1)_xml
|
||||
|
||||
ut_$(1)_%: TARGET=$(1)
|
||||
ut_$(1)_%: OUTDIR=$(UT_OUT_DIR)/$$(TARGET)
|
||||
ut_$(1)_%: UT_ROOT_DIR=$(ROOT_DIR)/tests/$(1)
|
||||
ut_$(1)_%: $$(UT_OUT_DIR)
|
||||
$(V1) mkdir -p $(UT_OUT_DIR)/$(1)
|
||||
$(V1) cd $$(UT_ROOT_DIR) && \
|
||||
$$(MAKE) -r --no-print-directory \
|
||||
BUILD_TYPE=ut \
|
||||
TCHAIN_PREFIX="" \
|
||||
REMOVE_CMD="$(RM)" \
|
||||
\
|
||||
MAKE_INC_DIR=$(MAKE_INC_DIR) \
|
||||
ROOT_DIR=$(ROOT_DIR) \
|
||||
TARGET=$$(TARGET) \
|
||||
OUTDIR=$$(OUTDIR) \
|
||||
\
|
||||
GTEST_DIR=$(GTEST_DIR) \
|
||||
\
|
||||
$$*
|
||||
|
||||
.PHONY: ut_$(1)_clean
|
||||
ut_$(1)_clean: TARGET=$(1)
|
||||
ut_$(1)_clean: OUTDIR=$(UT_OUT_DIR)/$$(TARGET)
|
||||
ut_$(1)_clean:
|
||||
$(V0) @echo " CLEAN $(1)"
|
||||
$(V1) [ ! -d "$$(OUTDIR)" ] || $(RM) -r "$$(OUTDIR)"
|
||||
endef
|
||||
|
||||
# Expand the unittest rules
|
||||
$(foreach ut, $(ALL_UNITTESTS), $(eval $(call UT_TEMPLATE,$(ut))))
|
||||
|
||||
# Disable parallel make when the all_ut_run target is requested otherwise the TAP/XML
|
||||
# output is interleaved with the rest of the make output.
|
||||
ifneq ($(strip $(filter all_ut_run,$(MAKECMDGOALS))),)
|
||||
.NOTPARALLEL:
|
||||
$(info *NOTE* Parallel make disabled by all_ut_run target so we have sane console output)
|
||||
endif
|
||||
|
|
|
@ -46,6 +46,34 @@ arm_sdk_clean:
|
|||
$(V1) [ ! -d "$(ARM_SDK_DIR)" ] || $(RM) -r $(ARM_SDK_DIR)
|
||||
|
||||
|
||||
###############
|
||||
# Google Test #
|
||||
###############
|
||||
|
||||
# Set up Google Test (gtest) tools
|
||||
GTEST_DIR := $(TOOLS_DIR)/gtest-1.11.0
|
||||
|
||||
.PHONY: gtest_install
|
||||
gtest_install: | $(DL_DIR) $(TOOLS_DIR)
|
||||
gtest_install: GTEST_URL := https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
|
||||
gtest_install: GTEST_FILE := $(notdir $(GTEST_URL))
|
||||
gtest_install: gtest_clean
|
||||
# download the file unconditionally since google code gives back 404
|
||||
# for HTTP HEAD requests which are used when using the wget -N option
|
||||
$(V1) [ ! -f "$(DL_DIR)/$(GTEST_FILE)" ] || $(RM) -f "$(DL_DIR)/$(GTEST_FILE)"
|
||||
$(V1) wget -P "$(DL_DIR)" "$(GTEST_URL)"
|
||||
|
||||
# extract the source
|
||||
$(V1) [ ! -d "$(GTEST_DIR)" ] || $(RM) -rf "$(GTEST_DIR)"
|
||||
$(V1) mkdir -p "$(GTEST_DIR)"
|
||||
$(V1) unzip -q -d "$(TOOLS_DIR)" "$(DL_DIR)/$(GTEST_FILE)"
|
||||
|
||||
.PHONY: gtest_clean
|
||||
gtest_clean:
|
||||
$(V0) @echo " CLEAN $(GTEST_DIR)"
|
||||
$(V1) [ ! -d "$(GTEST_DIR)" ] || $(RM) -rf "$(GTEST_DIR)"
|
||||
|
||||
|
||||
##############################
|
||||
#
|
||||
# Set up paths to tools
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
CCACHE :=
|
||||
|
||||
|
||||
# Define toolchain component names.
|
||||
CC = $(CCACHE) $(TCHAIN_PREFIX)gcc
|
||||
CXX = $(CCACHE) $(TCHAIN_PREFIX)g++
|
||||
AR = $(TCHAIN_PREFIX)ar
|
||||
OBJCOPY = $(TCHAIN_PREFIX)objcopy
|
||||
OBJDUMP = $(TCHAIN_PREFIX)objdump
|
||||
SIZE = $(TCHAIN_PREFIX)size
|
||||
NM = $(TCHAIN_PREFIX)nm
|
||||
STRIP = $(TCHAIN_PREFIX)strip
|
||||
GCOV = $(TCHAIN_PREFIX)gcov
|
||||
|
||||
THUMB = -mthumb
|
||||
|
||||
toprel = $(subst $(realpath $(ROOT_DIR))/,,$(abspath $(1)))
|
||||
|
||||
|
||||
|
||||
# Compile: create object files from C source files.
|
||||
define COMPILE_C_TEMPLATE
|
||||
$(OUTDIR)/$(notdir $(basename $(1))).o : EXTRA_FLAGS := $(2)
|
||||
$(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
|
||||
@echo $(MSG_COMPILING) $$(call toprel, $$<)
|
||||
$(V1) $(CC) -c $(THUMB) $$(CFLAGS) $$(CONLYFLAGS) $$(EXTRA_FLAGS) $$< -o $$@
|
||||
endef
|
||||
|
||||
# Compile: create object files from C++ source files.
|
||||
define COMPILE_CXX_TEMPLATE
|
||||
$(OUTDIR)/$(notdir $(basename $(1))).o : EXTRA_FLAGS := $(2)
|
||||
$(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
|
||||
@echo $(MSG_COMPILINGCXX) $$(call toprel, $$<)
|
||||
$(V1) $(CXX) -c $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $$(EXTRA_FLAGS) $$< -o $$@
|
||||
endef
|
||||
|
||||
# Link: create ELF output file from object files.
|
||||
# $1 = elf file to produce
|
||||
# $2 = list of object files that make up the elf file
|
||||
define LINK_CXX_TEMPLATE
|
||||
.SECONDARY : $(1)
|
||||
.PRECIOUS : $(2)
|
||||
$(1): $(2)
|
||||
@echo $(MSG_LINKING) $$(call toprel, $$@)
|
||||
$(V1) $(CXX) $(THUMB) $$(CFLAGS) $(2) --output $$@ $$(LDFLAGS)
|
||||
endef
|
||||
|
||||
|
||||
# Generate GCOV summary
|
||||
# $(1) = name of source file to analyze with gcov
|
||||
define GCOV_TEMPLATE
|
||||
$(OUTDIR)/$(1).gcov: $(OUTDIR)/$$(basename $(1)).gcda
|
||||
$(V0) @echo $(MSG_GCOV) $$(call toprel, $$@)
|
||||
$(V1) ( \
|
||||
cd $(OUTDIR) && \
|
||||
$(GCOV) $(1) 2>&1 > /dev/null ; \
|
||||
)
|
||||
endef
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
###############################################################################
|
||||
# @file unittest.mk
|
||||
# @author
|
||||
# @addtogroup
|
||||
# @{
|
||||
# @addtogroup
|
||||
# @{
|
||||
# @brief Makefile template for unit tests
|
||||
###############################################################################
|
||||
|
||||
# Flags passed to the preprocessor.
|
||||
CPPFLAGS += -I$(GTEST_DIR)/include
|
||||
|
||||
# Flags passed to the C++ compiler.
|
||||
CXXFLAGS += -g -Wall -Wextra -Wno-missing-field-initializers -std=c++11
|
||||
|
||||
# Google Test needs the pthread library
|
||||
LDFLAGS += -lpthread
|
||||
|
||||
# Google Test requires visibility of gtest includes
|
||||
GTEST_CXXFLAGS := -I$(GTEST_DIR)
|
||||
|
||||
# gcov requires specific link options to enable the coverage hooks
|
||||
LDFLAGS += -fprofile-arcs
|
||||
|
||||
# gcov requires specific compile options to enable the profiling hooks
|
||||
GCOV_CFLAGS := -fprofile-arcs -ftest-coverage
|
||||
|
||||
|
||||
#################################
|
||||
#
|
||||
# Template to build the user test
|
||||
#
|
||||
#################################
|
||||
|
||||
# Need to disable THUMB mode for unit tests
|
||||
override THUMB :=
|
||||
|
||||
EXTRAINCDIRS += .
|
||||
UTMOCKSRC := $(wildcard ./*.c)
|
||||
ALLSRC := $(SRC) $(UTMOCKSRC)
|
||||
ALLCPPSRC := $(wildcard ./*.cpp) $(GTEST_DIR)/src/gtest_main.cc
|
||||
ALLSRCBASE := $(notdir $(basename $(ALLSRC) $(ALLCPPSRC)))
|
||||
ALLOBJ := $(addprefix $(OUTDIR)/, $(addsuffix .o, $(ALLSRCBASE)))
|
||||
|
||||
# Build mock versions of APIs required to wrap the code being unit tested
|
||||
$(foreach src,$(UTMOCKSRC),$(eval $(call COMPILE_C_TEMPLATE,$(src))))
|
||||
|
||||
# Build the code being unit tested.
|
||||
# Enable gcov flags for these files so we can measure the coverage of our unit test
|
||||
$(foreach src,$(SRC),$(eval $(call COMPILE_C_TEMPLATE,$(src),$(GCOV_CFLAGS))))
|
||||
|
||||
# Build any C++ supporting files
|
||||
$(foreach src,$(ALLCPPSRC),$(eval $(call COMPILE_CXX_TEMPLATE,$(src))))
|
||||
|
||||
# Specific extensions to CXXFLAGS only for the google test library
|
||||
$(eval $(call COMPILE_CXX_TEMPLATE, $(GTEST_DIR)/src/gtest-all.cc,$(GTEST_CXXFLAGS)))
|
||||
|
||||
$(eval $(call LINK_CXX_TEMPLATE,$(OUTDIR)/$(TARGET).elf,$(ALLOBJ) $(OUTDIR)/gtest-all.o))
|
||||
|
||||
.PHONY: elf
|
||||
elf: $(OUTDIR)/$(TARGET).elf
|
||||
|
||||
.PHONY: xml
|
||||
xml: $(OUTDIR)/$(TARGET).xml
|
||||
|
||||
$(OUTDIR)/$(TARGET).xml: $(OUTDIR)/$(TARGET).elf
|
||||
$(V0) @echo " TEST XML $(MSG_EXTRA) $(call toprel, $@)"
|
||||
$(V1) $< --gtest_output=xml:$(OUTDIR)/$(TARGET).xml > /dev/null
|
||||
|
||||
.PHONY: run
|
||||
run: $(OUTDIR)/$(TARGET).elf
|
||||
$(V0) @echo " TEST RUN $(MSG_EXTRA) $(call toprel, $<)"
|
||||
$(V1) $<
|
||||
|
||||
GCOV_INPUT_FILES := $(notdir $(SRC))
|
||||
$(foreach src,$(GCOV_INPUT_FILES),$(eval $(call GCOV_TEMPLATE,$(src))))
|
||||
|
||||
.PHONY: gcov
|
||||
gcov: $(OUTDIR)/$(TARGET).xml
|
||||
gcov: $(foreach gc,$(GCOV_INPUT_FILES),$(addsuffix .gcov, $(OUTDIR)/$(gc)))
|
Loading…
Reference in New Issue