| 1 |
#--------- |
| 2 |
# |
| 3 |
# ComponentMakefile |
| 4 |
# |
| 5 |
# Include this file in your makefile |
| 6 |
# It makes |
| 7 |
# A static library |
| 8 |
# A test executable |
| 9 |
# |
| 10 |
# The necessary parameters are shown in |
| 11 |
# ComponentMakefileExampleParameters |
| 12 |
# |
| 13 |
# Inputs |
| 14 |
# SRC_FILES - Specific source files to build into library |
| 15 |
# SRC_DIRS - Directories of source files to build into the library |
| 16 |
# TEST_SRC - unit test code build into the unit test runner |
| 17 |
# MOCKS_SRC - mock objects built into the test runner |
| 18 |
# INCLUDES - List of -I files |
| 19 |
# CPPUTEST_CXXFLAGS - flags for the C++ compiler |
| 20 |
# CPPUTEST_CPPFLAGS - flags for the C++ AND C compiler |
| 21 |
# CPPUTEST_CFLAGS - C complier |
| 22 |
# CPPUTEST_LDFLAGS - Linker flags |
| 23 |
# LIB_DIR - the directory for the created library |
| 24 |
# COMPONENT_NAME - the name of the thing being created |
| 25 |
# OTHER_MAKEFILE_TO_INCLUDE - a hook to use this makefile to make |
| 26 |
# other targets. Like CSlim, which is part of fitnesse |
| 27 |
#---------- |
| 28 |
ifeq ("$(COMPONENT_NAME)", "") |
| 29 |
COMPONENT_NAME = name_this_in_the_makefile |
| 30 |
endif |
| 31 |
|
| 32 |
ifeq ("$(OBJS_DIR)", "") |
| 33 |
OBJS_DIR = objs |
| 34 |
endif |
| 35 |
|
| 36 |
ifeq ("$(LIB_DIR)", "") |
| 37 |
LIB_DIR = lib |
| 38 |
endif |
| 39 |
|
| 40 |
ifeq ("$CPPUTEST_USE_MALLOC_LEAK_DETECTION","") |
| 41 |
CPPUTEST_USE_MALLOC_LEAK_DETECTION = Y |
| 42 |
echo "leak on by default" |
| 43 |
endif |
| 44 |
|
| 45 |
#CPPUTEST_USE_OPERATOR_NEW_LEAK_DETECTION = Y |
| 46 |
#CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h |
| 47 |
#CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h |
| 48 |
|
| 49 |
TARGET_LIB = \ |
| 50 |
$(LIB_DIR)/lib$(COMPONENT_NAME).a |
| 51 |
|
| 52 |
TEST_TARGET = \ |
| 53 |
$(COMPONENT_NAME)_tests |
| 54 |
|
| 55 |
#Helper Functions |
| 56 |
get_src_from_dir = $(wildcard $1/*.cpp) $(wildcard $1/*.c) |
| 57 |
get_dirs_from_dirspec = $(wildcard $1) |
| 58 |
get_src_from_dir_list = $(foreach dir, $1, $(call get_src_from_dir,$(dir))) |
| 59 |
src_to_o = $(subst .c,.o, $(subst .cpp,.o,$1)) |
| 60 |
src_to_d = $(subst .c,.d, $(subst .cpp,.d,$1)) |
| 61 |
change_o_file_location = $(patsubst %.o,$(OBJS_DIR)/%.o, $1) |
| 62 |
change_d_file_location = $(patsubst %.d,$(OBJS_DIR)/%.d, $1) |
| 63 |
src_to = $(subst .c,$1, $(subst .cpp,$1,$2)) |
| 64 |
|
| 65 |
#Derived |
| 66 |
STUFF_TO_CLEAN += $(TEST_TARGET) $(TEST_TARGET).exe $(TARGET_LIB) |
| 67 |
|
| 68 |
SRC += $(call get_src_from_dir_list, $(SRC_DIRS)) $(SRC_FILES) |
| 69 |
#OBJ = $(call src_to_o,$(SRC)) |
| 70 |
OBJ = $(call change_o_file_location, $(call src_to_o,$(SRC))) |
| 71 |
STUFF_TO_CLEAN += $(OBJ) |
| 72 |
|
| 73 |
TEST_SRC = $(call get_src_from_dir_list, $(TEST_SRC_DIRS)) |
| 74 |
#TEST_OBJS = $(call src_to_o,$(TEST_SRC)) |
| 75 |
TEST_OBJS = $(call change_o_file_location, $(call src_to_o,$(TEST_SRC))) |
| 76 |
STUFF_TO_CLEAN += $(TEST_OBJS) |
| 77 |
|
| 78 |
|
| 79 |
MOCKS_SRC = $(call get_src_from_dir_list, $(MOCKS_SRC_DIRS)) |
| 80 |
#MOCKS_OBJS = $(call src_to_o,$(MOCKS_SRC)) |
| 81 |
MOCKS_OBJS = $(call change_o_file_location, $(call src_to_o,$(MOCKS_SRC))) |
| 82 |
STUFF_TO_CLEAN += $(MOCKS_OBJS) |
| 83 |
|
| 84 |
ALL_SRC = $(SRC) $(TEST_SRC) $(MOCKS_SRC) |
| 85 |
|
| 86 |
#Test coverage with gcov |
| 87 |
GCOV_OUTPUT = gcov_output.txt |
| 88 |
GCOV_REPORT = gcov_report.txt |
| 89 |
GCOV_ERROR = gcov_error.txt |
| 90 |
GCOV_GCDA_FILES = $(call src_to,.gcda, $(ALL_SRC)) |
| 91 |
GCOV_GCNO_FILES = $(call src_to,.gcno, $(ALL_SRC)) |
| 92 |
TEST_OUTPUT = $(TEST_TARGET).txt |
| 93 |
STUFF_TO_CLEAN += \ |
| 94 |
$(GCOV_OUTPUT)\ |
| 95 |
$(GCOV_REPORT)\ |
| 96 |
$(GCOV_ERROR)\ |
| 97 |
$(GCOV_GCDA_FILES)\ |
| 98 |
$(GCOV_GCNO_FILES)\ |
| 99 |
$(TEST_OUTPUT) |
| 100 |
|
| 101 |
|
| 102 |
#Other stuff needed |
| 103 |
CPPUTEST_LIB = $(CPPUTEST_HOME)/lib/libCppUTest.a |
| 104 |
|
| 105 |
ifdef CPPUTEST_USE_EXTENSIONS |
| 106 |
CPPUTEST_LIB += $(CPPUTEST_HOME)/lib/libCppUTestExt.a |
| 107 |
endif |
| 108 |
|
| 109 |
CPPUTEST_CPPFLAGS += $(INCLUDES) $(GCOVFLAGS) |
| 110 |
|
| 111 |
#The gcda files for gcov need to be deleted before each run |
| 112 |
#To avoid annoying messages. |
| 113 |
GCOV_CLEAN = $(SILENCE)rm -f $(GCOV_GCDA_FILES) $(GCOV_OUTPUT) $(GCOV_REPORT) $(GCOV_ERROR) |
| 114 |
RUN_TEST_TARGET = $(SILENCE) $(GCOV_CLEAN) ; echo "Running $(TEST_TARGET)"; ./$(TEST_TARGET) $(CPPUTEST_EXE_FLAGS) |
| 115 |
|
| 116 |
ifneq "$(OTHER_MAKEFILE_TO_INCLUDE)" "" |
| 117 |
-include $(OTHER_MAKEFILE_TO_INCLUDE) |
| 118 |
endif |
| 119 |
|
| 120 |
ifneq "$(MAP_FILE)" "" |
| 121 |
CPPUTEST_LDFLAGS += -Wl,-map,$(MAP_FILE) |
| 122 |
endif |
| 123 |
|
| 124 |
INCLUDES_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(INCLUDE_DIRS)) |
| 125 |
INCLUDES += $(foreach dir, $(INCLUDES_DIRS_EXPANDED), -I$(dir)) |
| 126 |
MOCK_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(MOCKS_SRC_DIRS)) |
| 127 |
INCLUDES += $(foreach dir, $(MOCK_DIRS_EXPANDED), -I$(dir)) |
| 128 |
|
| 129 |
|
| 130 |
DEP_FILES = $(call src_to_d, $(ALL_SRC)) |
| 131 |
STUFF_TO_CLEAN += $(DEP_FILES) + $(PRODUCTION_CODE_START) + $(PRODUCTION_CODE_END) |
| 132 |
STUFF_TO_CLEAN += $(STDLIB_CODE_START) + $(MAP_FILE) + cpputest_*.xml junit_run_output |
| 133 |
|
| 134 |
# We'll use the CPPUTEST_CFLAGS etc so that you can override AND add to the CppUTest flags |
| 135 |
CFLAGS = $(CPPUTEST_CFLAGS) $(CPPUTEST_ADDITIONAL_CFLAGS) |
| 136 |
CPPFLAGS = $(CPPUTEST_CPPFLAGS) $(CPPUTEST_ADDITIONAL_CPPFLAGS) |
| 137 |
CXXFLAGS = $(CPPUTEST_CXXFLAGS) $(CPPUTEST_ADDITIONAL_CXXFLAGS) |
| 138 |
LDFLAGS = $(CPPUTEST_LDFLAGS) $(CPPUTEST_ADDITIONAL_LDFLAGS) |
| 139 |
|
| 140 |
# Targets |
| 141 |
.PHONY: all |
| 142 |
all: $(TEST_TARGET) |
| 143 |
$(RUN_TEST_TARGET) |
| 144 |
@echo "Component makefile is no longer supported, convert to MakefileInclude.mk" |
| 145 |
|
| 146 |
.PHONY: all_no_tests |
| 147 |
all_no_tests: $(TEST_TARGET) |
| 148 |
|
| 149 |
.PHONY: flags |
| 150 |
flags: |
| 151 |
$(SILENCE)echo Compile with these flags: |
| 152 |
$(SILENCE)for f in $(CPPFLAGS) ; do \ |
| 153 |
echo " C++ $$f" ; \ |
| 154 |
done |
| 155 |
$(SILENCE)for f in $(CFLAGS) ; do \ |
| 156 |
echo " C $$f" ; \ |
| 157 |
done |
| 158 |
$(SILENCE)for f in $(LDFLAGS) ; do \ |
| 159 |
echo " LD $$f" ; \ |
| 160 |
done |
| 161 |
$(SILENCE)for f in $(ARFLAGS) ; do \ |
| 162 |
echo " AR $$f" ; \ |
| 163 |
done |
| 164 |
|
| 165 |
|
| 166 |
$(TEST_TARGET): $(TEST_OBJS) $(MOCKS_OBJS) $(PRODUCTION_CODE_START) $(TARGET_LIB) $(USER_LIBS) $(PRODUCTION_CODE_END) $(CPPUTEST_LIB) $(STDLIB_CODE_START) |
| 167 |
$(SILENCE)echo Linking $@ |
| 168 |
$(SILENCE)$(LINK.o) -o $@ $^ $(LD_LIBRARIES) |
| 169 |
|
| 170 |
$(TARGET_LIB): $(OBJ) |
| 171 |
$(SILENCE)echo Building archive $@ |
| 172 |
$(SILENCE)mkdir -p lib |
| 173 |
$(SILENCE)$(AR) $(ARFLAGS) $@ $^ |
| 174 |
$(SILENCE)$(RANLIB) $@ |
| 175 |
|
| 176 |
test: $(TEST_TARGET) |
| 177 |
$(RUN_TEST_TARGET) | tee $(TEST_OUTPUT) |
| 178 |
|
| 179 |
vtest: $(TEST_TARGET) |
| 180 |
$(RUN_TEST_TARGET) -v | tee $(TEST_OUTPUT) |
| 181 |
|
| 182 |
$(OBJS_DIR)/%.o: %.cpp |
| 183 |
@echo compiling $(notdir $<) |
| 184 |
$(SILENCE)mkdir -p $(dir $@) |
| 185 |
$(SILENCE)$(COMPILE.cpp) -M -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $< |
| 186 |
$(SILENCE)$(COMPILE.cpp) $(OUTPUT_OPTION) $< |
| 187 |
|
| 188 |
$(OBJS_DIR)/%.o: %.c |
| 189 |
@echo compiling $(notdir $<) |
| 190 |
$(SILENCE)mkdir -p $(dir $@) |
| 191 |
$(SILENCE)$(COMPILE.c) -M -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $< |
| 192 |
$(SILENCE)$(COMPILE.c) $(OUTPUT_OPTION) $< |
| 193 |
|
| 194 |
ifneq "$(MAKECMDGOALS)" "clean" |
| 195 |
-include $(DEP_FILES) |
| 196 |
endif |
| 197 |
|
| 198 |
.PHONY: clean |
| 199 |
clean: |
| 200 |
$(SILENCE)echo Making clean |
| 201 |
$(SILENCE)$(RM) $(STUFF_TO_CLEAN) |
| 202 |
$(SILENCE)find . -name "*.gcov" | xargs rm -f |
| 203 |
$(SILENCE)find . -name "*.[do]" | xargs rm -f |
| 204 |
|
| 205 |
|
| 206 |
gcov: test |
| 207 |
$(SILENCE)for d in $(SRC_DIRS) ; do \ |
| 208 |
gcov -o $$d $$d/*.c $$d/*.cpp >> $(GCOV_OUTPUT) 2>>$(GCOV_ERROR) ; \ |
| 209 |
done |
| 210 |
$(CPPUTEST_HOME)/scripts/filterGcov.sh $(GCOV_OUTPUT) $(GCOV_ERROR) $(GCOV_REPORT) $(TEST_OUTPUT) |
| 211 |
cat $(GCOV_REPORT) |
| 212 |
|
| 213 |
.PHONY: format |
| 214 |
format: |
| 215 |
$(CPPUTEST_HOME)/scripts/reformat.sh $(PROJECT_HOME_DIR) |
| 216 |
|
| 217 |
debug: |
| 218 |
echo Stuff to clean |
| 219 |
$(SILENCE)for f in $(STUFF_TO_CLEAN) ; do \ |
| 220 |
echo "$$f" ; \ |
| 221 |
done |
| 222 |
echo Includes |
| 223 |
$(SILENCE)for i in $(INCLUDES) ; do \ |
| 224 |
echo "$$i" ; \ |
| 225 |
done |
| 226 |
|