Parallel and distributed systems exercise 2: ditributed all-kNN algorithm.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Makefile 5.6 KiB

3 år sedan
3 år sedan
3 år sedan
3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #
  2. # Makefile for C++
  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. # ============== Project settings ==============
  20. # Project's name
  21. PROJECT := kNN
  22. # Excecutable's name
  23. TARGET := knn
  24. # Source directories list(space seperated). Makefile-relative path, UNDER current directory.
  25. # Note: To support parent relative paths uncomment abspath line in main body below
  26. SRC_DIR_LIST := src
  27. # Include directories list(space seperated). Makefile-relative path.
  28. INC_DIR_LIST := inc
  29. # Exclude files list(space seperated). Filenames only.
  30. # EXC_FILE_LIST := bad.cpp old.cpp
  31. # Build directories
  32. BUILD_DIR := bin
  33. OBJ_DIR := $(BUILD_DIR)/obj
  34. DEP_DIR := $(BUILD_DIR)/.dep
  35. # ========== Compiler settings ==========
  36. # Compiler flags for debug and release
  37. DEB_CFLAGS := -DDEBUG -g3 -Wall -Wextra
  38. REL_CFLAGS := -Wall -Wextra -O2
  39. # Pre-defines
  40. # PRE_DEFS := MYCAB=1729 SUPER_MODE
  41. # ============== Linker settings ==============
  42. # Linker flags (example: -pthread -lm)
  43. LDFLAGS := -lm -lopenblas
  44. # Map output file
  45. MAP_FILE := output.map
  46. MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
  47. # ============== Docker settings ==============
  48. # We need:
  49. # - Bind the entire project directory(the dir that icludes all the code) as volume.
  50. # - In docker instance, change to working directory(where the makefile is).
  51. DOCKER_VOL_DIR := $${PWD}
  52. DOCKER_WRK_DIR :=
  53. DOCKER_RUN := docker run --rm
  54. DOCKER_FLAGS := -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)
  55. # docker invoke mechanism (edit with care)
  56. # note:
  57. # Here, `DOCKER` variable is empty. Rules can assign `DOCKER := DOCKER_CMD` when docker
  58. # functionality is needed.
  59. DOCKER_CMD = $(DOCKER_RUN) $(DOCKER_FLAGS) $(IMAGE)
  60. DOCKER :=
  61. # ============== Tool selection ==============
  62. # compiler and compiler flags.
  63. CSIZE := size
  64. CFLAGS := $(DEB_CFLAGS)
  65. CXX := g++
  66. #
  67. # =========== Main body and Patterns ===========
  68. #
  69. # Uncomment to support Win file extension for local builds
  70. #ifeq ($(OS), Windows_NT)
  71. # TARGET := $(TARGET).exe
  72. #endif
  73. INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
  74. DEF := $(foreach def,$(PRE_DEFS),-D$(def))
  75. EXC := $(foreach fil,$(EXC_FILE_LIST), \
  76. $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
  77. )
  78. # source files. object and dependencies list
  79. # recursive search into current and source directories
  80. SRC := $(wildcard *.cpp)
  81. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
  82. SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
  83. SRC := $(filter-out $(EXC),$(SRC))
  84. # Uncomment to support parent relative source paths
  85. #SRC := $(abspath $(SRC))
  86. OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
  87. DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))
  88. # Make Dependencies pattern.
  89. # This little trick enables recompilation only when dependencies change
  90. # and it does so for changes both in source AND header files ;)
  91. #
  92. # It is based on Tom Tromey's method.
  93. #
  94. # Invoke cpp to create makefile rules with dependencies for each source file
  95. $(DEP_DIR)/%.d: %.cpp
  96. @mkdir -p $(@D)
  97. @$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<
  98. # objects depent on .cpp AND dependency files, which have an empty recipe
  99. $(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
  100. @mkdir -p $(@D)
  101. $(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
  102. # empty recipe for dependency files. This prevents make errors
  103. $(DEP):
  104. # now include all dependencies, after all they are makefile dependency rules ;)
  105. include $(wildcard $(DEP))
  106. # main target rule
  107. $(BUILD_DIR)/$(TARGET): $(OBJ)
  108. @mkdir -p $(@D)
  109. @echo Linking to target: $(TARGET)
  110. @echo $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) '$$(OBJ)'
  111. @$(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
  112. @echo
  113. @echo Print size information
  114. @$(CSIZE) $(@D)/$(TARGET)
  115. @echo Done
  116. .PHONY: clean
  117. clean:
  118. @echo Cleaning build directories
  119. @rm -rf $(OBJ_DIR)
  120. @rm -rf $(DEP_DIR)
  121. @rm -rf $(BUILD_DIR)
  122. #
  123. # ================ Local build rules =================
  124. # Use cases:
  125. # - local builds in terminal
  126. # example:
  127. # make debug
  128. # - build inside container
  129. # example in a yml:
  130. # image: gcc:8
  131. # build:
  132. # stage: build
  133. # - make release
  134. # ... etc
  135. # test:
  136. # stage: test
  137. # - bin/out
  138. # ... etc
  139. #
  140. debug: CFLAGS += $(DEB_FLAGS)
  141. debug: $(BUILD_DIR)/$(TARGET)
  142. release: CFLAGS += $(REL_FLAGS)
  143. release: $(BUILD_DIR)/$(TARGET)
  144. .PHONY: all
  145. all: release
  146. #
  147. # ================ Docker based rules ================
  148. # Use case:
  149. # - build localy using via terminal or script in order
  150. # to compile for various compilers, versions, etc...
  151. # example in bash:
  152. # #!/bin/bash
  153. # for im in gcc:8 gcc:9 myImage; do
  154. # make IMAGE="$im" dock
  155. # done
  156. #
  157. dock: DOCKER := $(DOCKER_CMD)
  158. dock: $(BUILD_DIR)/$(TARGET)