makefile 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # User-defined arguments
  2. ## MODE:
  3. ### Debug - enable debug flags, disable optimisation
  4. ### Release - enable optimisation
  5. MODE := Release
  6. ## TEST_AUTORUN:
  7. ### Yes - automatic run test after build
  8. TEST_AUTORUN := Yes
  9. ## ENABLE_WARNINGS:
  10. ### Yes - print all warnings
  11. ### No - disable all warnings
  12. ENABLE_WARNINGS := No
  13. # Directories
  14. ## Build directories
  15. BUILD_DIR := build
  16. TEST_DIR := $(BUILD_DIR)/test
  17. # Targets
  18. ## $(BUILD_DIR)/$(LIB_TARGET).a
  19. ## $(BUILD_DIR)/$(LIB_TARGET).so
  20. LIB_TARGET := libbinom
  21. ## $(BUILD_DIR)/$(TEST_EXEC)
  22. TEST_EXEC := binom_test
  23. # Programms
  24. CC := gcc-11
  25. CXX := g++-11
  26. RM := rm -rf
  27. MKDIR := mkdir -p
  28. # Build flags
  29. ## C
  30. C_MODE_FLAGS :=
  31. ifeq ($(MODE),Debug) # Debug
  32. C_MODE_FLAGS += -O0 -g
  33. else ifeq($(MODE),Release) # Release
  34. C_MODE_FLAGS += -O3
  35. endif
  36. C_WARNINGS_FLAGS :=
  37. ifeq ($(ENABLE_WARNINGS),Yes)
  38. C_WARNINGS_FLAGS = -Wall
  39. else ifeq($(ENABLE_WARNINGS),No)
  40. C_WARNINGS_FLAGS = -w
  41. endif
  42. C_FLAGS := -fPIC $(C_MODE_FLAGS)
  43. ## C++
  44. CXX_VERSION := c++20
  45. CXX_MODE_FLAGS :=
  46. ifeq ($(MODE),Debug) # Debug
  47. CXX_MODE_FLAGS += -O0 -g -fno-inline-small-functions
  48. else ifeq($(MODE),Release) # Release
  49. CXX_MODE_FLAGS += -O3
  50. endif
  51. CXX_WARNINGS_FLAGS :=
  52. ifeq ($(ENABLE_WARNINGS),Yes)
  53. CXX_WARNINGS_FLAGS = -Wall
  54. else ifeq($(ENABLE_WARNINGS),No)
  55. CXX_WARNINGS_FLAGS = -w
  56. endif
  57. CXX_FLAGS := -fPIC -std=$(CXX_VERSION) $(CXX_MODE_FLAGS) $(CXX_WARNINGS_FLAGS)
  58. ## Linker flags
  59. LD_FLAGS =
  60. LD_SHARED_FLAGS = -fPIC -shared
  61. ## Linker libs flags
  62. LD_LIBS = -lpthread
  63. ## Source directories
  64. INCLUDE_PATH = -I./
  65. SOURCES_PATH = ./libbinom/source/
  66. TEST_SOURCES_PATH = ./test
  67. # Files find rules
  68. ## Build
  69. SOURCES = $(shell find $(SOURCES_PATH) -name "*.cxx")
  70. OBJECTS = $(addprefix $(BUILD_DIR)/, $(notdir $(SOURCES:.cxx=.o)))
  71. OBJECTS_CLEAN = $(shell find $(BUILD_DIR) -name "*.o")
  72. ## Test
  73. TEST_SOURCES = $(shell find $(TEST_SOURCES_PATH) -name "*.cxx")
  74. TEST_OBJECTS = $(addprefix $(BUILD_DIR)/test/, $(notdir $(TEST_SOURCES:.cxx=.o)))
  75. # Public make flags
  76. all: lib test
  77. test: $(TEST_DIR)/$(TEST_EXEC)
  78. ifeq ($(TEST_AUTORUN),Yes)
  79. $(TEST_DIR)/$(TEST_EXEC)
  80. endif
  81. lib: $(BUILD_DIR)/$(LIB_TARGET).a $(BUILD_DIR)/$(LIB_TARGET).so
  82. $(BUILD_DIR):
  83. $(MKDIR) $@
  84. $(TEST_DIR):
  85. $(MKDIR) $@
  86. $(BUILD_DIR)/$(LIB_TARGET).a: $(OBJECTS)
  87. ar cr $@ $(OBJECTS)
  88. $(BUILD_DIR)/$(LIB_TARGET).so: $(OBJECTS)
  89. $(CXX) $(LD_SHARED_FLAGS) $(LD_LIBS) -o $@ $(OBJECTS)
  90. $(TEST_DIR)/$(TEST_EXEC): $(OBJECTS) $(TEST_OBJECTS)
  91. $(CXX) $(OBJECTS) $(TEST_OBJECTS) -o $@ $(LD_FLAGS) $(LD_LIBS)
  92. $(OBJECTS): $(BUILD_DIR) $(SOURCES)
  93. $(CXX) $(INCLUDE_PATH) $(CXX_FLAGS) -c $(filter-out %_$(subst .o,.cxx,$(notdir $@)), $(filter %$(subst .o,.cxx,$(notdir $@)), $(SOURCES))) -o $@
  94. $(TEST_OBJECTS): $(TEST_DIR) $(TEST_SOURCES)
  95. $(CXX) $(INCLUDE_PATH) $(CXX_FLAGS) -c $(filter-out %_$(subst .o,.cxx,$(notdir $@)), $(filter %$(subst .o,.cxx,$(notdir $@)), $(TEST_SOURCES))) -o $@
  96. .PHONY: clean
  97. clean:
  98. $(RM) $(BUILD_DIR)
  99. clean_obj:
  100. $(RM) $(OBJECTS_CLEAN)