MakefileWorker.mk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #---------
  2. #
  3. # MakefileWorker.mk
  4. #
  5. # Include this helper file in your makefile
  6. # It makes
  7. # A static library
  8. # A test executable
  9. #
  10. # See this example for parameter settings
  11. # examples/Makefile
  12. #
  13. #----------
  14. # Inputs - these variables describe what to build
  15. #
  16. # INCLUDE_DIRS - Directories used to search for include files.
  17. # This generates a -I for each directory
  18. # SRC_DIRS - Directories containing source files to build into the library
  19. # SRC_FILES - Specific source files to build into library. Helpful when not all code
  20. # in a directory can be built for test (hopefully a temporary situation)
  21. # TEST_SRC_DIRS - Directories containing unit test code build into the unit test runner
  22. # These do not go in a library. They are explicitly included in the test runner
  23. # TEST_SRC_FILES - Specific source files to build into the unit test runner
  24. # These do not go in a library. They are explicitly included in the test runner
  25. # MOCKS_SRC_DIRS - Directories containing mock source files to build into the test runner
  26. # These do not go in a library. They are explicitly included in the test runner
  27. #----------
  28. # You can adjust these variables to influence how to build the test target
  29. # and where to put and name outputs
  30. # See below to determine defaults
  31. # COMPONENT_NAME - the name of the thing being built
  32. # TEST_TARGET - name of the test executable. By default it is
  33. # $(COMPONENT_NAME)_tests
  34. # Helpful if you want 1 > make files in the same directory with different
  35. # executables as output.
  36. # CPPUTEST_HOME - where CppUTest home dir found
  37. # TARGET_PLATFORM - Influences how the outputs are generated by modifying the
  38. # CPPUTEST_OBJS_DIR and CPPUTEST_LIB_DIR to use a sub-directory under the
  39. # normal objs and lib directories. Also modifies where to search for the
  40. # CPPUTEST_LIB to link against.
  41. # CPPUTEST_OBJS_DIR - a directory where o and d files go
  42. # CPPUTEST_LIB_DIR - a directory where libs go
  43. # CPPUTEST_ENABLE_DEBUG - build for debug
  44. # CPPUTEST_USE_MEM_LEAK_DETECTION - Links with overridden new and delete
  45. # CPPUTEST_USE_STD_CPP_LIB - Set to N to keep the standard C++ library out
  46. # of the test harness
  47. # CPPUTEST_USE_GCOV - Turn on coverage analysis
  48. # Clean then build with this flag set to Y, then 'make gcov'
  49. # CPPUTEST_MAPFILE - generate a map file
  50. # CPPUTEST_WARNINGFLAGS - overly picky by default
  51. # OTHER_MAKEFILE_TO_INCLUDE - a hook to use this makefile to make
  52. # other targets. Like CSlim, which is part of fitnesse
  53. # CPPUTEST_USE_VPATH - Use Make's VPATH functionality to support user
  54. # specification of source files and directories that aren't below
  55. # the user's Makefile in the directory tree, like:
  56. # SRC_DIRS += ../../lib/foo
  57. # It defaults to N, and shouldn't be necessary except in the above case.
  58. #----------
  59. #
  60. # Other flags users can initialize to sneak in their settings
  61. # CPPUTEST_CXXFLAGS - flags for the C++ compiler
  62. # CPPUTEST_CPPFLAGS - flags for the C++ AND C preprocessor
  63. # CPPUTEST_CFLAGS - flags for the C complier
  64. # CPPUTEST_LDFLAGS - Linker flags
  65. #----------
  66. # Some behavior is weird on some platforms. Need to discover the platform.
  67. # Platforms
  68. UNAME_OUTPUT = "$(shell uname -a)"
  69. MACOSX_STR = Darwin
  70. MINGW_STR = MINGW
  71. CYGWIN_STR = CYGWIN
  72. LINUX_STR = Linux
  73. SUNOS_STR = SunOS
  74. UNKNWOWN_OS_STR = Unknown
  75. # Compilers
  76. CC_VERSION_OUTPUT ="$(shell $(CXX) -v 2>&1)"
  77. CLANG_STR = clang
  78. SUNSTUDIO_CXX_STR = SunStudio
  79. UNAME_OS = $(UNKNWOWN_OS_STR)
  80. ifeq ($(findstring $(MINGW_STR),$(UNAME_OUTPUT)),$(MINGW_STR))
  81. UNAME_OS = $(MINGW_STR)
  82. endif
  83. ifeq ($(findstring $(CYGWIN_STR),$(UNAME_OUTPUT)),$(CYGWIN_STR))
  84. UNAME_OS = $(CYGWIN_STR)
  85. endif
  86. ifeq ($(findstring $(LINUX_STR),$(UNAME_OUTPUT)),$(LINUX_STR))
  87. UNAME_OS = $(LINUX_STR)
  88. endif
  89. ifeq ($(findstring $(MACOSX_STR),$(UNAME_OUTPUT)),$(MACOSX_STR))
  90. UNAME_OS = $(MACOSX_STR)
  91. #lion has a problem with the 'v' part of -a
  92. UNAME_OUTPUT = "$(shell uname -pmnrs)"
  93. endif
  94. ifeq ($(findstring $(SUNOS_STR),$(UNAME_OUTPUT)),$(SUNOS_STR))
  95. UNAME_OS = $(SUNOS_STR)
  96. SUNSTUDIO_CXX_ERR_STR = CC -flags
  97. ifeq ($(findstring $(SUNSTUDIO_CXX_ERR_STR),$(CC_VERSION_OUTPUT)),$(SUNSTUDIO_CXX_ERR_STR))
  98. CC_VERSION_OUTPUT ="$(shell $(CXX) -V 2>&1)"
  99. COMPILER_NAME = $(SUNSTUDIO_CXX_STR)
  100. endif
  101. endif
  102. ifeq ($(findstring $(CLANG_STR),$(CC_VERSION_OUTPUT)),$(CLANG_STR))
  103. COMPILER_NAME = $(CLANG_STR)
  104. endif
  105. #Kludge for mingw, it does not have cc.exe, but gcc.exe will do
  106. ifeq ($(UNAME_OS),$(MINGW_STR))
  107. CC := gcc
  108. endif
  109. #And another kludge. Exception handling in gcc 4.6.2 is broken when linking the
  110. # Standard C++ library as a shared library. Unbelievable.
  111. ifeq ($(UNAME_OS),$(MINGW_STR))
  112. CPPUTEST_LDFLAGS += -static
  113. endif
  114. ifeq ($(UNAME_OS),$(CYGWIN_STR))
  115. CPPUTEST_LDFLAGS += -static
  116. endif
  117. #Kludge for MacOsX gcc compiler on Darwin9 who can't handle pendantic
  118. ifeq ($(UNAME_OS),$(MACOSX_STR))
  119. ifeq ($(findstring Version 9,$(UNAME_OUTPUT)),Version 9)
  120. CPPUTEST_PEDANTIC_ERRORS = N
  121. endif
  122. endif
  123. ifndef COMPONENT_NAME
  124. COMPONENT_NAME = name_this_in_the_makefile
  125. endif
  126. # Debug on by default
  127. ifndef CPPUTEST_ENABLE_DEBUG
  128. CPPUTEST_ENABLE_DEBUG = Y
  129. endif
  130. # new and delete for memory leak detection on by default
  131. ifndef CPPUTEST_USE_MEM_LEAK_DETECTION
  132. CPPUTEST_USE_MEM_LEAK_DETECTION = Y
  133. endif
  134. # Use the standard C library
  135. ifndef CPPUTEST_USE_STD_C_LIB
  136. CPPUTEST_USE_STD_C_LIB = Y
  137. endif
  138. # Use the standard C++ library
  139. ifndef CPPUTEST_USE_STD_CPP_LIB
  140. CPPUTEST_USE_STD_CPP_LIB = Y
  141. endif
  142. # Use long long, off by default
  143. ifndef CPPUTEST_USE_LONG_LONG
  144. CPPUTEST_USE_LONG_LONG = N
  145. endif
  146. # Use gcov, off by default
  147. ifndef CPPUTEST_USE_GCOV
  148. CPPUTEST_USE_GCOV = N
  149. endif
  150. ifndef CPPUTEST_PEDANTIC_ERRORS
  151. CPPUTEST_PEDANTIC_ERRORS = Y
  152. endif
  153. # Default warnings
  154. ifndef CPPUTEST_WARNINGFLAGS
  155. CPPUTEST_WARNINGFLAGS = -Wall -Wextra -Werror -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wno-long-long
  156. ifeq ($(CPPUTEST_PEDANTIC_ERRORS), Y)
  157. CPPUTEST_WARNINGFLAGS += -pedantic-errors
  158. endif
  159. ifeq ($(UNAME_OS),$(LINUX_STR))
  160. CPPUTEST_WARNINGFLAGS += -Wsign-conversion
  161. endif
  162. CPPUTEST_CXX_WARNINGFLAGS = -Woverloaded-virtual
  163. CPPUTEST_C_WARNINGFLAGS = -Wstrict-prototypes
  164. endif
  165. #Wonderful extra compiler warnings with clang
  166. ifeq ($(COMPILER_NAME),$(CLANG_STR))
  167. # -Wno-disabled-macro-expansion -> Have to disable the macro expansion warning as the operator new overload warns on that.
  168. # -Wno-padded -> I sort-of like this warning but if there is a bool at the end of the class, it seems impossible to remove it! (except by making padding explicit)
  169. # -Wno-global-constructors Wno-exit-time-destructors -> Great warnings, but in CppUTest it is impossible to avoid as the automatic test registration depends on the global ctor and dtor
  170. # -Wno-weak-vtables -> The TEST_GROUP macro declares a class and will automatically inline its methods. Thats ok as they are only in one translation unit. Unfortunately, the warning can't detect that, so it must be disabled.
  171. # -Wno-old-style-casts -> We only use old style casts by decision
  172. # -Wno-c++11-long-long -> When it detects long long, then we can use it and no need for a warning about that
  173. CPPUTEST_CXX_WARNINGFLAGS += -Weverything -Wno-disabled-macro-expansion -Wno-padded -Wno-global-constructors -Wno-exit-time-destructors -Wno-weak-vtables -Wno-old-style-cast -Wno-c++11-long-long
  174. CPPUTEST_C_WARNINGFLAGS += -Weverything -Wno-padded
  175. # Clang "7" (Xcode 7 command-line tools) introduced new warnings by default that don't exist on previous versions of clang and cause errors when present.
  176. ifeq ($(findstring clang-7,$(CC_VERSION_OUTPUT)),clang-7)
  177. # -Wno-reserved-id-macro -> Many CppUTest macros start with __, which is a reserved namespace
  178. # -Wno-keyword-macro -> CppUTest redefines the 'new' keyword for memory leak tracking
  179. CPPUTEST_CXX_WARNINGFLAGS += -Wno-reserved-id-macro -Wno-keyword-macro
  180. CPPUTEST_C_WARNINGFLAGS += -Wno-reserved-id-macro -Wno-keyword-macro
  181. endif
  182. endif
  183. # Uhm. Maybe put some warning flags for SunStudio here?
  184. ifeq ($(COMPILER_NAME),$(SUNSTUDIO_CXX_STR))
  185. CPPUTEST_CXX_WARNINGFLAGS =
  186. CPPUTEST_C_WARNINGFLAGS =
  187. endif
  188. # Default dir for temporary files (d, o)
  189. ifndef CPPUTEST_OBJS_DIR
  190. ifndef TARGET_PLATFORM
  191. CPPUTEST_OBJS_DIR = objs
  192. else
  193. CPPUTEST_OBJS_DIR = objs/$(TARGET_PLATFORM)
  194. endif
  195. endif
  196. # Default dir for the outout library
  197. ifndef CPPUTEST_LIB_DIR
  198. ifndef TARGET_PLATFORM
  199. CPPUTEST_LIB_DIR = lib
  200. else
  201. CPPUTEST_LIB_DIR = lib/$(TARGET_PLATFORM)
  202. endif
  203. endif
  204. # No map by default
  205. ifndef CPPUTEST_MAP_FILE
  206. CPPUTEST_MAP_FILE = N
  207. endif
  208. # No extentions is default
  209. ifndef CPPUTEST_USE_EXTENSIONS
  210. CPPUTEST_USE_EXTENSIONS = N
  211. endif
  212. # No VPATH is default
  213. ifndef CPPUTEST_USE_VPATH
  214. CPPUTEST_USE_VPATH := N
  215. endif
  216. # Make empty, instead of 'N', for usage in $(if ) conditionals
  217. ifneq ($(CPPUTEST_USE_VPATH), Y)
  218. CPPUTEST_USE_VPATH :=
  219. endif
  220. ifndef TARGET_PLATFORM
  221. CPPUTEST_LIB_LINK_DIR = $(CPPUTEST_HOME)/lib
  222. else
  223. CPPUTEST_LIB_LINK_DIR = $(CPPUTEST_HOME)/lib/$(TARGET_PLATFORM)
  224. endif
  225. # --------------------------------------
  226. # derived flags in the following area
  227. # --------------------------------------
  228. # Without the C library, we'll need to disable the C++ library and ...
  229. ifeq ($(CPPUTEST_USE_STD_C_LIB), N)
  230. CPPUTEST_USE_STD_CPP_LIB = N
  231. CPPUTEST_USE_MEM_LEAK_DETECTION = N
  232. CPPUTEST_CPPFLAGS += -DCPPUTEST_STD_C_LIB_DISABLED
  233. CPPUTEST_CPPFLAGS += -nostdinc
  234. endif
  235. ifeq ($(CPPUTEST_USE_MEM_LEAK_DETECTION), N)
  236. CPPUTEST_CPPFLAGS += -DCPPUTEST_MEM_LEAK_DETECTION_DISABLED
  237. else
  238. ifndef CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE
  239. CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
  240. endif
  241. ifndef CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE
  242. CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
  243. endif
  244. endif
  245. ifeq ($(CPPUTEST_USE_LONG_LONG), Y)
  246. CPPUTEST_CPPFLAGS += -DCPPUTEST_USE_LONG_LONG
  247. endif
  248. ifeq ($(CPPUTEST_ENABLE_DEBUG), Y)
  249. CPPUTEST_CXXFLAGS += -g
  250. CPPUTEST_CFLAGS += -g
  251. CPPUTEST_LDFLAGS += -g
  252. endif
  253. ifeq ($(CPPUTEST_USE_STD_CPP_LIB), N)
  254. CPPUTEST_CPPFLAGS += -DCPPUTEST_STD_CPP_LIB_DISABLED
  255. ifeq ($(CPPUTEST_USE_STD_C_LIB), Y)
  256. CPPUTEST_CXXFLAGS += -nostdinc++
  257. endif
  258. endif
  259. ifdef $(GMOCK_HOME)
  260. GTEST_HOME = $(GMOCK_HOME)/gtest
  261. CPPUTEST_CPPFLAGS += -I$(GMOCK_HOME)/include
  262. GMOCK_LIBRARY = $(GMOCK_HOME)/lib/.libs/libgmock.a
  263. LD_LIBRARIES += $(GMOCK_LIBRARY)
  264. CPPUTEST_CPPFLAGS += -DCPPUTEST_INCLUDE_GTEST_TESTS
  265. CPPUTEST_WARNINGFLAGS =
  266. CPPUTEST_CPPFLAGS += -I$(GTEST_HOME)/include -I$(GTEST_HOME)
  267. GTEST_LIBRARY = $(GTEST_HOME)/lib/.libs/libgtest.a
  268. LD_LIBRARIES += $(GTEST_LIBRARY)
  269. endif
  270. ifeq ($(CPPUTEST_USE_GCOV), Y)
  271. CPPUTEST_CXXFLAGS += -fprofile-arcs -ftest-coverage
  272. CPPUTEST_CFLAGS += -fprofile-arcs -ftest-coverage
  273. endif
  274. CPPUTEST_CXXFLAGS += $(CPPUTEST_WARNINGFLAGS) $(CPPUTEST_CXX_WARNINGFLAGS)
  275. CPPUTEST_CPPFLAGS += $(CPPUTEST_WARNINGFLAGS)
  276. CPPUTEST_CXXFLAGS += $(CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE)
  277. CPPUTEST_CPPFLAGS += $(CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE)
  278. CPPUTEST_CFLAGS += $(CPPUTEST_C_WARNINGFLAGS)
  279. TARGET_MAP = $(COMPONENT_NAME).map.txt
  280. ifeq ($(CPPUTEST_MAP_FILE), Y)
  281. CPPUTEST_LDFLAGS += -Wl,-map,$(TARGET_MAP)
  282. endif
  283. # Link with CppUTest lib
  284. CPPUTEST_LIB = $(CPPUTEST_LIB_LINK_DIR)/libCppUTest.a
  285. ifeq ($(CPPUTEST_USE_EXTENSIONS), Y)
  286. CPPUTEST_LIB += $(CPPUTEST_LIB_LINK_DIR)/libCppUTestExt.a
  287. endif
  288. ifdef CPPUTEST_STATIC_REALTIME
  289. LD_LIBRARIES += -lrt
  290. endif
  291. TARGET_LIB = \
  292. $(CPPUTEST_LIB_DIR)/lib$(COMPONENT_NAME).a
  293. ifndef TEST_TARGET
  294. ifndef TARGET_PLATFORM
  295. TEST_TARGET = $(COMPONENT_NAME)_tests
  296. else
  297. TEST_TARGET = $(COMPONENT_NAME)_$(TARGET_PLATFORM)_tests
  298. endif
  299. endif
  300. #Helper Functions
  301. get_src_from_dir = $(wildcard $1/*.cpp) $(wildcard $1/*.cc) $(wildcard $1/*.c)
  302. get_dirs_from_dirspec = $(wildcard $1)
  303. get_src_from_dir_list = $(foreach dir, $1, $(call get_src_from_dir,$(dir)))
  304. __src_to = $(subst .c,$1, $(subst .cc,$1, $(subst .cpp,$1,$(if $(CPPUTEST_USE_VPATH),$(notdir $2),$2))))
  305. src_to = $(addprefix $(CPPUTEST_OBJS_DIR)/,$(call __src_to,$1,$2))
  306. src_to_o = $(call src_to,.o,$1)
  307. src_to_d = $(call src_to,.d,$1)
  308. src_to_gcda = $(call src_to,.gcda,$1)
  309. src_to_gcno = $(call src_to,.gcno,$1)
  310. time = $(shell date +%s)
  311. delta_t = $(eval minus, $1, $2)
  312. debug_print_list = $(foreach word,$1,echo " $(word)";) echo;
  313. #Derived
  314. STUFF_TO_CLEAN += $(TEST_TARGET) $(TEST_TARGET).exe $(TARGET_LIB) $(TARGET_MAP)
  315. SRC += $(call get_src_from_dir_list, $(SRC_DIRS)) $(SRC_FILES)
  316. OBJ = $(call src_to_o,$(SRC))
  317. STUFF_TO_CLEAN += $(OBJ)
  318. TEST_SRC += $(call get_src_from_dir_list, $(TEST_SRC_DIRS)) $(TEST_SRC_FILES)
  319. TEST_OBJS = $(call src_to_o,$(TEST_SRC))
  320. STUFF_TO_CLEAN += $(TEST_OBJS)
  321. MOCKS_SRC += $(call get_src_from_dir_list, $(MOCKS_SRC_DIRS))
  322. MOCKS_OBJS = $(call src_to_o,$(MOCKS_SRC))
  323. STUFF_TO_CLEAN += $(MOCKS_OBJS)
  324. ALL_SRC = $(SRC) $(TEST_SRC) $(MOCKS_SRC)
  325. # If we're using VPATH
  326. ifeq ($(CPPUTEST_USE_VPATH), Y)
  327. # gather all the source directories and add them
  328. VPATH += $(sort $(dir $(ALL_SRC)))
  329. # Add the component name to the objs dir path, to differentiate between same-name objects
  330. CPPUTEST_OBJS_DIR := $(addsuffix /$(COMPONENT_NAME),$(CPPUTEST_OBJS_DIR))
  331. endif
  332. #Test coverage with gcov
  333. GCOV_OUTPUT = gcov_output.txt
  334. GCOV_REPORT = gcov_report.txt
  335. GCOV_ERROR = gcov_error.txt
  336. GCOV_GCDA_FILES = $(call src_to_gcda, $(ALL_SRC))
  337. GCOV_GCNO_FILES = $(call src_to_gcno, $(ALL_SRC))
  338. TEST_OUTPUT = $(TEST_TARGET).txt
  339. STUFF_TO_CLEAN += \
  340. $(GCOV_OUTPUT)\
  341. $(GCOV_REPORT)\
  342. $(GCOV_REPORT).html\
  343. $(GCOV_ERROR)\
  344. $(GCOV_GCDA_FILES)\
  345. $(GCOV_GCNO_FILES)\
  346. $(TEST_OUTPUT)
  347. #The gcda files for gcov need to be deleted before each run
  348. #To avoid annoying messages.
  349. GCOV_CLEAN = $(SILENCE)rm -f $(GCOV_GCDA_FILES) $(GCOV_OUTPUT) $(GCOV_REPORT) $(GCOV_ERROR)
  350. RUN_TEST_TARGET = $(SILENCE) $(GCOV_CLEAN) ; echo "Running $(TEST_TARGET)"; ./$(TEST_TARGET) $(CPPUTEST_EXE_FLAGS)
  351. ifeq ($(CPPUTEST_USE_GCOV), Y)
  352. ifeq ($(COMPILER_NAME),$(CLANG_STR))
  353. LD_LIBRARIES += --coverage
  354. else
  355. LD_LIBRARIES += -lgcov
  356. endif
  357. endif
  358. INCLUDES_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(INCLUDE_DIRS))
  359. INCLUDES += $(foreach dir, $(INCLUDES_DIRS_EXPANDED), -I$(dir))
  360. MOCK_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(MOCKS_SRC_DIRS))
  361. INCLUDES += $(foreach dir, $(MOCK_DIRS_EXPANDED), -I$(dir))
  362. CPPUTEST_CPPFLAGS += $(INCLUDES)
  363. DEP_FILES = $(call src_to_d, $(ALL_SRC))
  364. STUFF_TO_CLEAN += $(DEP_FILES) $(PRODUCTION_CODE_START) $(PRODUCTION_CODE_END)
  365. STUFF_TO_CLEAN += $(STDLIB_CODE_START) $(MAP_FILE) cpputest_*.xml junit_run_output
  366. # We'll use the CPPUTEST_CFLAGS etc so that you can override AND add to the CppUTest flags
  367. CFLAGS = $(CPPUTEST_CFLAGS) $(CPPUTEST_ADDITIONAL_CFLAGS)
  368. CPPFLAGS = $(CPPUTEST_CPPFLAGS) $(CPPUTEST_ADDITIONAL_CPPFLAGS)
  369. CXXFLAGS = $(CPPUTEST_CXXFLAGS) $(CPPUTEST_ADDITIONAL_CXXFLAGS)
  370. LDFLAGS = $(CPPUTEST_LDFLAGS) $(CPPUTEST_ADDITIONAL_LDFLAGS)
  371. # Don't consider creating the archive a warning condition that does STDERR output
  372. ARFLAGS := $(ARFLAGS)c
  373. DEP_FLAGS=-MMD -MP
  374. # Some macros for programs to be overridden. For some reason, these are not in Make defaults
  375. RANLIB = ranlib
  376. # Targets
  377. .PHONY: all
  378. all: start $(TEST_TARGET)
  379. $(RUN_TEST_TARGET)
  380. .PHONY: start
  381. start: $(TEST_TARGET)
  382. $(SILENCE)START_TIME=$(call time)
  383. .PHONY: all_no_tests
  384. all_no_tests: $(TEST_TARGET)
  385. .PHONY: flags
  386. flags:
  387. @echo
  388. @echo "OS ${UNAME_OS}"
  389. @echo "Compile C and C++ source with CPPFLAGS:"
  390. @$(call debug_print_list,$(CPPFLAGS))
  391. @echo "Compile C++ source with CXXFLAGS:"
  392. @$(call debug_print_list,$(CXXFLAGS))
  393. @echo "Compile C source with CFLAGS:"
  394. @$(call debug_print_list,$(CFLAGS))
  395. @echo "Link with LDFLAGS:"
  396. @$(call debug_print_list,$(LDFLAGS))
  397. @echo "Link with LD_LIBRARIES:"
  398. @$(call debug_print_list,$(LD_LIBRARIES))
  399. @echo "Create libraries with ARFLAGS:"
  400. @$(call debug_print_list,$(ARFLAGS))
  401. TEST_DEPS = $(TEST_OBJS) $(MOCKS_OBJS) $(PRODUCTION_CODE_START) $(TARGET_LIB) $(USER_LIBS) $(PRODUCTION_CODE_END) $(CPPUTEST_LIB) $(STDLIB_CODE_START)
  402. test-deps: $(TEST_DEPS)
  403. $(TEST_TARGET): $(TEST_DEPS)
  404. @echo Linking $@
  405. $(SILENCE)$(CXX) -o $@ $^ $(LD_LIBRARIES) $(LDFLAGS)
  406. $(TARGET_LIB): $(OBJ)
  407. @echo Building archive $@
  408. $(SILENCE)mkdir -p $(dir $@)
  409. $(SILENCE)$(AR) $(ARFLAGS) $@ $^
  410. $(SILENCE)$(RANLIB) $@
  411. test: $(TEST_TARGET)
  412. $(RUN_TEST_TARGET) | tee $(TEST_OUTPUT)
  413. vtest: $(TEST_TARGET)
  414. $(RUN_TEST_TARGET) -v | tee $(TEST_OUTPUT)
  415. $(CPPUTEST_OBJS_DIR)/%.o: %.cc
  416. @echo compiling $(notdir $<)
  417. $(SILENCE)mkdir -p $(dir $@)
  418. $(SILENCE)$(COMPILE.cpp) $(DEP_FLAGS) $(OUTPUT_OPTION) $<
  419. $(CPPUTEST_OBJS_DIR)/%.o: %.cpp
  420. @echo compiling $(notdir $<)
  421. $(SILENCE)mkdir -p $(dir $@)
  422. $(SILENCE)$(COMPILE.cpp) $(DEP_FLAGS) $(OUTPUT_OPTION) $<
  423. $(CPPUTEST_OBJS_DIR)/%.o: %.c
  424. @echo compiling $(notdir $<)
  425. $(SILENCE)mkdir -p $(dir $@)
  426. $(SILENCE)$(COMPILE.c) $(DEP_FLAGS) $(OUTPUT_OPTION) $<
  427. ifneq "$(MAKECMDGOALS)" "clean"
  428. -include $(DEP_FILES)
  429. endif
  430. .PHONY: clean
  431. clean:
  432. @echo Making clean
  433. $(SILENCE)$(RM) $(STUFF_TO_CLEAN)
  434. $(SILENCE)rm -rf gcov $(CPPUTEST_OBJS_DIR)
  435. $(SILENCE)find . -name "*.gcno" | xargs rm -f
  436. $(SILENCE)find . -name "*.gcda" | xargs rm -f
  437. #realclean gets rid of all gcov, o and d files in the directory tree
  438. #not just the ones made by this makefile
  439. .PHONY: realclean
  440. realclean: clean
  441. $(SILENCE)rm -rf gcov
  442. $(SILENCE)find . -name "*.gdcno" | xargs rm -f
  443. $(SILENCE)find . -name "*.[do]" | xargs rm -f
  444. gcov: test
  445. ifeq ($(CPPUTEST_USE_VPATH), Y)
  446. $(SILENCE)gcov $(GCOV_ARGS) --object-directory $(CPPUTEST_OBJS_DIR) $(SRC) >> $(GCOV_OUTPUT) 2>> $(GCOV_ERROR)
  447. else
  448. $(SILENCE)for d in $(SRC_DIRS) ; do \
  449. FILES=`ls $$d/*.c $$d/*.cc $$d/*.cpp 2> /dev/null` ; \
  450. gcov $(GCOV_ARGS) --object-directory $(CPPUTEST_OBJS_DIR)/$$d $$FILES >> $(GCOV_OUTPUT) 2>>$(GCOV_ERROR) ; \
  451. done
  452. $(SILENCE)for f in $(SRC_FILES) ; do \
  453. gcov $(GCOV_ARGS) --object-directory $(CPPUTEST_OBJS_DIR)/$$f $$f >> $(GCOV_OUTPUT) 2>>$(GCOV_ERROR) ; \
  454. done
  455. endif
  456. ./scripts/filterGcov.sh $(GCOV_OUTPUT) $(GCOV_ERROR) $(GCOV_REPORT) $(TEST_OUTPUT)
  457. $(SILENCE)cat $(GCOV_REPORT)
  458. $(SILENCE)mkdir -p gcov
  459. $(SILENCE)mv *.gcov gcov
  460. $(SILENCE)mv gcov_* gcov
  461. @echo "See gcov directory for details"
  462. .PHONY: format
  463. format:
  464. $(CPPUTEST_HOME)/scripts/reformat.sh $(PROJECT_HOME_DIR)
  465. .PHONY: debug
  466. debug:
  467. @echo
  468. @echo "Target Source files:"
  469. @$(call debug_print_list,$(SRC))
  470. @echo "Target Object files:"
  471. @$(call debug_print_list,$(OBJ))
  472. @echo "Test Source files:"
  473. @$(call debug_print_list,$(TEST_SRC))
  474. @echo "Test Object files:"
  475. @$(call debug_print_list,$(TEST_OBJS))
  476. @echo "Mock Source files:"
  477. @$(call debug_print_list,$(MOCKS_SRC))
  478. @echo "Mock Object files:"
  479. @$(call debug_print_list,$(MOCKS_OBJS))
  480. @echo "All Input Dependency files:"
  481. @$(call debug_print_list,$(DEP_FILES))
  482. @echo Stuff to clean:
  483. @$(call debug_print_list,$(STUFF_TO_CLEAN))
  484. @echo Includes:
  485. @$(call debug_print_list,$(INCLUDES))
  486. -include $(OTHER_MAKEFILE_TO_INCLUDE)