A C++ toolbox repo until the pair uTL/dTL arives
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

244 rindas
7.7 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. # Exclude files list(space seperated). Filenames only.
  82. # EXC_FILE_LIST := bad.cpp old.cpp
  83. # Build directories
  84. BUILD_DIR := bin
  85. OBJ_DIR := $(BUILD_DIR)/obj
  86. DEP_DIR := $(BUILD_DIR)/.dep
  87. # ============== Compiler settings ==============
  88. CLANGXX := clang++
  89. GCCXX := g++
  90. CSIZE := size
  91. ODUMP := objdump
  92. OCOPY := objcopy
  93. # Compiler flags for debug and release
  94. DEB_CFLAGS := -std=c++17 -DDEBUG -g3 -Wall -Wextra
  95. REL_CFLAGS := -std=c++17 -Wall -Wextra -O2
  96. # Pre-defines
  97. # PRE_DEFS := MYCAB=1729 SUPER_MODE
  98. # ============== Linker settings ==============
  99. # Linker flags
  100. LDFLAGS := -pthread
  101. # Map output file
  102. MAP_FILE := output.map
  103. MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
  104. # ============== Docker settings ==============
  105. # We need:
  106. # 1) Bind the entire project directory(the dir that icludes all the code) as volume.
  107. # 2) In docker instance change to working directory(where the makefile is).
  108. # For utl we use the directories `${PWD%/*}` and `test`.
  109. # We double-$$ for double evaluating.
  110. DOCKER_VOL_DIR := "$${PWD%/*}"
  111. DOCKER_WRK_DIR := test
  112. DOCKER_RUN := docker run --rm -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)
  113. # ============== Default settings ==============
  114. # compiler and compiler flags. By default docker is not used.
  115. CFLAGS := $(DEB_CFLAGS)
  116. CXX := $(GCCXX)
  117. DOCKER :=
  118. #
  119. # =========== Main body and Patterns ===========
  120. #
  121. ifeq ($(OS), Windows_NT)
  122. TARGET := $(TARGET).exe
  123. endif
  124. INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
  125. DEF := $(foreach def,$(PRE_DEFS),-D$(def))
  126. EXC := $(foreach fil,$(EXC_FILE_LIST), \
  127. $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
  128. )
  129. # source files, object and dependencies list
  130. # recursive search into current and source directories
  131. SRC := $(wildcard *.cpp)
  132. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
  133. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
  134. #SRC := $(abspath $(filter-out $(EXC),$(SRC)))
  135. SRC := $(filter-out $(EXC),$(SRC))
  136. OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
  137. DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))
  138. # Make Dependencies pattern.
  139. # This little trick enables recompilation only when dependencies change
  140. # and it does so for changes both in source AND header files ;)
  141. #
  142. # It is based on Tom Tromey's method.
  143. #
  144. # Invoke cpp to create makefile rules with dependencies for each source file
  145. $(DEP_DIR)/%.d: %.cpp
  146. @mkdir -p $(@D)
  147. @$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<
  148. # objects depent on .cpp AND dependency files, which have an empty recipe
  149. $(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
  150. @mkdir -p $(@D)
  151. $(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
  152. # empty recipe for dependency files. This prevents make errors
  153. $(DEP):
  154. # now include all dependencies
  155. # After all they are makefile dependency rules ;)
  156. include $(wildcard $(DEP))
  157. # main target rule
  158. $(BUILD_DIR)/$(TARGET): $(OBJ)
  159. @mkdir -p $(@D)
  160. @echo Linking to target: $(TARGET)
  161. $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
  162. $(DOCKER) $(ODUMP) -h -S $(BUILD_DIR)/$(TARGET) > $(BUILD_DIR)/$(basename $(TARGET)).list
  163. $(DOCKER) $(OCOPY) -O ihex $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(basename $(TARGET)).hex
  164. @echo
  165. @echo Print size information
  166. @$(CSIZE) $(@D)/$(TARGET)
  167. @echo Done
  168. .PHONY: clean
  169. clean:
  170. @echo Cleaning build directories
  171. @rm -rf $(OBJ_DIR)
  172. @rm -rf $(DEP_DIR)
  173. @rm -rf $(BUILD_DIR)
  174. #
  175. # ================ Local build rules =================
  176. # examples:
  177. # make MK_ARG="-std=c++14 -fconcepts" build-gcc
  178. # make MK_ARG="-std=c++17" build-clang
  179. #
  180. .PHONY: build-gcc
  181. build-gcc: CFLAGS += $(MK_ARG)
  182. build-gcc: $(BUILD_DIR)/$(TARGET)
  183. .PHONY: build-clang
  184. build-clang: CXX := $(CLANGXX)
  185. build-clang: CFLAGS += $(MK_ARG)
  186. build-clang: $(BUILD_DIR)/$(TARGET)
  187. .PHONY: debug
  188. debug: $(BUILD_DIR)/$(TARGET)
  189. .PHONY: release
  190. release: CFLAGS := $(REL_FLAGS)
  191. release: clean $(BUILD_DIR)/$(TARGET)
  192. .PHONY: all
  193. all: clean release
  194. #
  195. # ================ Docker based rules ================
  196. # examples:
  197. # make IMAGE="gcc:8.3" MK_ARG="-std=c++14 -fconcepts" dock-gcc
  198. # make IMAGE="a-clang-image" MK_ARG="-std=c++17" dock-clang
  199. #
  200. .PHONY: dock-gcc
  201. dock-gcc: DOCKER := $(DOCKER_RUN) $(IMAGE)
  202. dock-gcc: CFLAGS += $(MK_ARG)
  203. dock-gcc: $(BUILD_DIR)/$(TARGET)
  204. .PHONY: dock-clang
  205. dock-clang: CXX := $(CLANGXX)
  206. dock-clang: DOCKER := $(DOCKER_RUN) $(IMAGE)
  207. dock-clang: CFLAGS += $(MK_ARG)
  208. dock-clang: $(BUILD_DIR)/$(TARGET)