A C++ toolbox repo until the pair uTL/dTL arives
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

264 Zeilen
8.3 KiB

  1. #
  2. # Makefile for tbx unit testing
  3. #
  4. # Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  5. #
  6. # The MIT License (MIT)
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. #
  26. # notes:
  27. # ==============
  28. # This makefile provides support for unix-like local builds and
  29. # docker based builds for gcc and clang.
  30. # ** msvc is not currently supported.
  31. #
  32. # Use cases:
  33. # 1) build localy using **local rules** in terminal.
  34. # example:
  35. # make -j4 MK_ARG="-std=c++14 -fconcepts" build-gcc
  36. #
  37. # 2) build localy using **docker based rules** via terminal or script
  38. # in order to compile for all compilers/versions/dialects.
  39. # example in bash:
  40. # #!/bin/bash
  41. # for im in gcc:8 gcc:9 gcc:10; do
  42. # for dial in -std=c++14 -std=c++17; do
  43. # make IMAGE="$im" MK_ARG="$dial" dock-gcc
  44. # done
  45. # done
  46. #
  47. # 3) build inside a docker instance using **local rules** in order to
  48. # build and test using a CD/CI system.
  49. # example in a yml file:
  50. # image: gcc:8
  51. # build:
  52. # stage: build
  53. # - make MK_ARG="-std=c++14" build-gcc
  54. # - make MK_ARG="-std=c++14 -fconcepts" build-gcc
  55. # - make MK_ARG="-std=c++17" build-gcc
  56. # - make MK_ARG="-std=c++17 -fconcepts" build-gcc
  57. # - make MK_ARG="-std=c++2a" build-gcc
  58. # ... etc
  59. # test:
  60. # stage: test
  61. # - bin/utlTest
  62. # ... etc
  63. #
  64. #
  65. # ============== Project settings ==============
  66. #
  67. # note: STM32f101RC/D/E device configuration on SMT32CubeF1 library requires:
  68. # 1) STM2F101xE pre-define
  69. # 2) startup_stm32f101xe.s startup file
  70. #
  71. # Project's name
  72. PROJECT := tbx
  73. # Excecutable's name
  74. TARGET := tbxTest
  75. # Source directories list(space seperated). Relative path, under current directory only
  76. SRC_DIR_LIST := tests gtest
  77. # Source files list(space seperated).
  78. SRC_FILES_LIST :=
  79. # Include directories list(space seperated). Relative path
  80. INC_DIR_LIST := ../include gtest
  81. ifeq ($(OS), Windows_NT)
  82. INC_DIR_LIST += mingw-std-threads
  83. endif
  84. # Exclude files list(space seperated). Filenames only.
  85. # EXC_FILE_LIST := bad.cpp old.cpp
  86. # Build directories
  87. BUILD_DIR := bin
  88. OBJ_DIR := $(BUILD_DIR)/obj
  89. DEP_DIR := $(BUILD_DIR)/.dep
  90. # ============== Compiler settings ==============
  91. CLANGXX := clang++
  92. GCCXX := g++
  93. CSIZE := size
  94. ODUMP := objdump
  95. OCOPY := objcopy
  96. # Compiler flags for debug and release
  97. DEB_CFLAGS := -std=gnu++17 -DDEBUG -g3 -Wall -Wextra -fmessage-length=0
  98. REL_CFLAGS := -std=gnu++17 -Wall -Wextra -O2 -fmessage-length=0
  99. # Pre-defines
  100. PRE_DEFS :=
  101. ifeq ($(OS), Windows_NT)
  102. PRE_DEFS += WIN_TRHEADS
  103. endif
  104. # ============== Linker settings ==============
  105. # Linker flags
  106. LDFLAGS := -pthread
  107. # Map output file
  108. MAP_FILE := output.map
  109. MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
  110. # ============== Docker settings ==============
  111. # We need:
  112. # 1) Bind the entire project directory(the dir that icludes all the code) as volume.
  113. # 2) In docker instance change to working directory(where the makefile is).
  114. # For utl we use the directories `${PWD%/*}` and `test`.
  115. # We double-$$ for double evaluating.
  116. DOCKER_VOL_DIR := "$${PWD%/*}"
  117. DOCKER_WRK_DIR := test
  118. DOCKER_RUN := docker run --rm -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)
  119. # ============== Default settings ==============
  120. # compiler and compiler flags. By default docker is not used.
  121. CFLAGS := $(DEB_CFLAGS)
  122. CXX := $(GCCXX)
  123. DOCKER :=
  124. #
  125. # =========== Main body and Patterns ===========
  126. #
  127. ifeq ($(OS), Windows_NT)
  128. TARGET := $(TARGET).exe
  129. endif
  130. INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
  131. DEF := $(foreach def,$(PRE_DEFS),-D$(def))
  132. EXC := $(foreach fil,$(EXC_FILE_LIST), \
  133. $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
  134. )
  135. # source files, object and dependencies list
  136. # recursive search into current and source directories
  137. SRC := $(wildcard *.cpp)
  138. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
  139. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
  140. #SRC := $(abspath $(filter-out $(EXC),$(SRC)))
  141. SRC := $(filter-out $(EXC),$(SRC))
  142. OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
  143. DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))
  144. # Make Dependencies pattern.
  145. # This little trick enables recompilation only when dependencies change
  146. # and it does so for changes both in source AND header files ;)
  147. #
  148. # It is based on Tom Tromey's method.
  149. #
  150. # Invoke cpp to create makefile rules with dependencies for each source file
  151. $(DEP_DIR)/%.d: %.cpp
  152. @mkdir -p $(@D)
  153. @$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<
  154. # objects depent on .cpp AND dependency files, which have an empty recipe
  155. $(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
  156. @mkdir -p $(@D)
  157. $(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
  158. # empty recipe for dependency files. This prevents make errors
  159. $(DEP):
  160. # now include all dependencies
  161. # After all they are makefile dependency rules ;)
  162. include $(wildcard $(DEP))
  163. # main target rule
  164. $(BUILD_DIR)/$(TARGET): $(OBJ)
  165. @mkdir -p $(@D)
  166. @echo Linking to target: $(TARGET)
  167. $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
  168. $(DOCKER) $(ODUMP) -h -S $(BUILD_DIR)/$(TARGET) > $(BUILD_DIR)/$(basename $(TARGET)).list
  169. # $(DOCKER) $(OCOPY) -O ihex $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(basename $(TARGET)).hex
  170. @echo
  171. @echo Print size information
  172. @$(CSIZE) $(@D)/$(TARGET)
  173. @echo Done
  174. .PHONY: clean
  175. clean:
  176. @echo Cleaning build directories
  177. @rm -rf $(OBJ_DIR)
  178. @rm -rf $(DEP_DIR)
  179. @rm -rf $(BUILD_DIR)
  180. #
  181. # ================ Local build rules =================
  182. # examples:
  183. # make MK_ARG="-std=c++14 -fconcepts" build-gcc
  184. # make MK_ARG="-std=c++17" build-clang
  185. #
  186. .PHONY: build-gcc
  187. build-gcc: CFLAGS += $(MK_ARG)
  188. build-gcc: $(BUILD_DIR)/$(TARGET)
  189. .PHONY: build-clang
  190. build-clang: CXX := $(CLANGXX)
  191. build-clang: CFLAGS += $(MK_ARG)
  192. build-clang: $(BUILD_DIR)/$(TARGET)
  193. .PHONY: debug
  194. debug: $(BUILD_DIR)/$(TARGET)
  195. .PHONY: test_asan
  196. test_asan: CFLAGS := $(REL_CFLAGS)
  197. test_asan: CFLAGS += -g3 -fsanitize=address -fsanitize=leak -fsanitize=bounds-strict
  198. test_asan: LDFLAGS += -fsanitize=address -fsanitize=leak -fsanitize=bounds-strict
  199. test_asan: $(BUILD_DIR)/$(TARGET)
  200. .PHONY: test_tsan
  201. test_asan: CFLAGS := $(REL_CFLAGS)
  202. test_tsan: CFLAGS += -g3 -fsanitize=thread
  203. test_tsan: LDFLAGS += -fsanitize=thread
  204. test_tsan: $(BUILD_DIR)/$(TARGET)
  205. .PHONY: release
  206. release: CFLAGS := $(REL_CFLAGS)
  207. release: $(BUILD_DIR)/$(TARGET)
  208. .PHONY: all
  209. all: clean release
  210. #
  211. # ================ Docker based rules ================
  212. # examples:
  213. # make IMAGE="gcc:8.3" MK_ARG="-std=c++14 -fconcepts" dock-gcc
  214. # make IMAGE="a-clang-image" MK_ARG="-std=c++17" dock-clang
  215. #
  216. .PHONY: dock-gcc
  217. dock-gcc: DOCKER := $(DOCKER_RUN) $(IMAGE)
  218. dock-gcc: CFLAGS += $(MK_ARG)
  219. dock-gcc: $(BUILD_DIR)/$(TARGET)
  220. .PHONY: dock-clang
  221. dock-clang: CXX := $(CLANGXX)
  222. dock-clang: DOCKER := $(DOCKER_RUN) $(IMAGE)
  223. dock-clang: CFLAGS += $(MK_ARG)
  224. dock-clang: $(BUILD_DIR)/$(TARGET)