Micro template library A library for building device drivers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Makefile 6.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #
  2. # Makefile for utl Unit test
  3. #
  4. # Copyright (C) 2019-2020 Christos Choutouridis <christos@choutouridis.net>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Lesser General Public License as
  8. # published by the Free Software Foundation, either version 3
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # ult notes:
  20. # ==============
  21. # This makefile provides support for unix-like local builds and
  22. # docker based builds for gcc and clang.
  23. # ** msvc is not currently supported.
  24. #
  25. # Use cases:
  26. # 1) build localy using **local rules** in terminal.
  27. # example:
  28. # make -j4 MK_ARG="-std=c++14 -fconcepts" build-gcc
  29. #
  30. # 2) build localy using **docker based rules** via terminal or script
  31. # in order to compile for all compilers/versions/dialects.
  32. # example in bash:
  33. # #!/bin/bash
  34. # for im in gcc:8 gcc:9 gcc:10; do
  35. # for dial in -std=c++14 -std=c++17; do
  36. # make IMAGE="$im" MK_ARG="$dial" dock-gcc
  37. # done
  38. # done
  39. #
  40. # 3) build inside a docker instance using **local rules** in order to
  41. # build and test using a CD/CI system.
  42. # example in a yml file:
  43. # image: gcc:8
  44. # build:
  45. # stage: build
  46. # - make MK_ARG="-std=c++14" build-gcc
  47. # - make MK_ARG="-std=c++14 -fconcepts" build-gcc
  48. # - make MK_ARG="-std=c++17" build-gcc
  49. # - make MK_ARG="-std=c++17 -fconcepts" build-gcc
  50. # - make MK_ARG="-std=c++2a" build-gcc
  51. # ... etc
  52. # test:
  53. # stage: test
  54. # - bin/utlTest
  55. # ... etc
  56. # ============== Project settings ==============
  57. # Excecutable's name
  58. TARGET := utlTest
  59. # Source directories list(space seperated).
  60. # Relative path, under current directory only
  61. SRC_DIR_LIST := tests gtest
  62. # Include directories list(space seperated).
  63. # Relative path
  64. INC_DIR_LIST := ../include gtest
  65. # Exclude files list(space seperated). Filenames only.
  66. # EXC_FILE_LIST := bad.cpp old.cpp
  67. # Build directories
  68. BUILD_DIR := bin
  69. OBJ_DIR := $(BUILD_DIR)/obj
  70. DEP_DIR := $(BUILD_DIR)/.dep
  71. # ============== Compiler settings ==============
  72. CLANGXX := clang++
  73. GCCXX := g++
  74. CSIZE := size
  75. # Compiler flags for debug and release
  76. DEB_CFLAGS := -DDEBUG -g3 -Wall -Wextra
  77. REL_CFLAGS := -Wall -Wextra -O2
  78. # Pre-defines
  79. # PRE_DEFS := MYCAB=1729 SUPER_MODE
  80. # ============== Linker settings ==============
  81. # Linker flags
  82. LDFLAGS := -pthread
  83. # Map output file
  84. MAP_FILE := output.map
  85. MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
  86. # ============== Docker settings ==============
  87. # We need:
  88. # 1) Bind the entire project directory(the dir that icludes all the code) as volume.
  89. # 2) In docker instance change to working directory(where the makefile is).
  90. # For utl we use the directories `${PWD%/*}` and `test`.
  91. # We double-$$ for double evaluating.
  92. DOCKER_VOL_DIR := "$${PWD%/*}"
  93. DOCKER_WRK_DIR := test
  94. DOCKER_RUN := docker run --rm -v $(DOCKER_VOL_DIR):/usr/src/$(TARGET) -w /usr/src/$(TARGET)/$(DOCKER_WRK_DIR)
  95. # ============== Default settings ==============
  96. # compiler and compiler flags. By default docker is not used.
  97. CFLAGS := $(DEB_CFLAGS)
  98. CXX := $(GCCXX)
  99. DOCKER :=
  100. #
  101. # =========== Main body and Patterns ===========
  102. #
  103. INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
  104. DEF := $(foreach def,$(PRE_DEFS),-D$(def))
  105. EXC := $(foreach fil,$(EXC_FILE_LIST), \
  106. $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
  107. )
  108. # source files, object and dependencies list
  109. # recursive search into current and source directories
  110. SRC := $(wildcard *.cpp)
  111. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
  112. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
  113. #SRC := $(abspath $(filter-out $(EXC),$(SRC)))
  114. SRC := $(filter-out $(EXC),$(SRC))
  115. OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
  116. DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))
  117. # Make Dependencies pattern.
  118. # This little trick enables recompilation only when dependencies change
  119. # and it does so for changes both in source AND header files ;)
  120. #
  121. # It is based on Tom Tromey's method.
  122. #
  123. # Invoke cpp to create makefile rules with dependencies for each source file
  124. $(DEP_DIR)/%.d: %.cpp
  125. @mkdir -p $(@D)
  126. @$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<
  127. # objects depent on .cpp AND dependency files, which have an empty recipe
  128. $(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
  129. @mkdir -p $(@D)
  130. $(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
  131. # empty recipe for dependency files. This prevents make errors
  132. $(DEP):
  133. # now include all dependencies
  134. # After all they are makefile dependency rules ;)
  135. include $(wildcard $(DEP))
  136. # main target rule
  137. $(BUILD_DIR)/$(TARGET): $(OBJ)
  138. @mkdir -p $(@D)
  139. @echo Linking to target: $(TARGET)
  140. @echo $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) '$$(OBJ)'
  141. @$(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
  142. @echo
  143. @echo Print size information
  144. @$(CSIZE) $(@D)/$(TARGET)
  145. @echo Done
  146. .PHONY: clean
  147. clean:
  148. @echo Cleaning build directories
  149. @rm -rf $(OBJ_DIR)
  150. @rm -rf $(DEP_DIR)
  151. @rm -rf $(BUILD_DIR)
  152. #
  153. # ================ Local build rules =================
  154. # examples:
  155. # make MK_ARG="-std=c++14 -fconcepts" build-gcc
  156. # make MK_ARG="-std=c++17" build-clang
  157. #
  158. .PHONY: build-gcc
  159. build-gcc: CFLAGS += $(MK_ARG)
  160. build-gcc: $(BUILD_DIR)/$(TARGET)
  161. .PHONY: build-clang
  162. build-clang: CXX := $(CLANGXX)
  163. build-clang: CFLAGS += $(MK_ARG)
  164. build-clang: $(BUILD_DIR)/$(TARGET)
  165. .PHONY: debug
  166. debug: $(BUILD_DIR)/$(TARGET)
  167. .PHONY: release
  168. release: CFLAGS := $(REL_FLAGS)
  169. release: clean $(BUILD_DIR)/$(TARGET)
  170. .PHONY: all
  171. all: clean release
  172. #
  173. # ================ Docker based rules ================
  174. # examples:
  175. # make IMAGE="gcc:8.3" MK_ARG="-std=c++14 -fconcepts" dock-gcc
  176. # make IMAGE="a-clang-image" MK_ARG="-std=c++17" dock-clang
  177. #
  178. .PHONY: dock-gcc
  179. dock-gcc: DOCKER := $(DOCKER_RUN) $(IMAGE)
  180. dock-gcc: CFLAGS += $(MK_ARG)
  181. dock-gcc: $(BUILD_DIR)/$(TARGET)
  182. .PHONY: dock-clang
  183. dock-clang: CXX := $(CLANGXX)
  184. dock-clang: DOCKER := $(DOCKER_RUN) $(IMAGE)
  185. dock-clang: CFLAGS += $(MK_ARG)
  186. dock-clang: $(BUILD_DIR)/$(TARGET)