-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
98 lines (80 loc) · 2.99 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Default target is 'help' (lists valid options/rules)
.DEFAULT_GOAL := help
# Variables for file + output locations
TARGET := Splash
BUILD := build
LIB := lib
OUTPUT := $(LIB)/lib$(TARGET).a
OBJDIR := build/objs
DEPDIR := build/deps
INCLUDE := include
SOURCE := source
# Flags to pass to the compiler
CXXFLAGS := -std=c++11 -Wall -O3 -I$(INCLUDE)
# Variables which store file locations
CPPFILES := $(shell find $(SOURCE)/ -name "*.cpp")
OBJS := $(CPPFILES:$(SOURCE)/%.cpp=$(OBJDIR)/%.o)
DEPS := $(CPPFILES:$(SOURCE)/%.cpp=$(DEPDIR)/%.d)
TREE := $(sort $(patsubst %/,%,$(dir $(OBJS))))
# Include dependency files if they already exist
ifeq "$(MAKECMDGOALS)" ""
-include $(DEPS)
endif
# Define virtual make targets
.PHONY: all clean-all example clean-example library clean-library tests clean-tests run-tests help
# 'help' displays the available targets
help:
@echo "----------------------------------------------------------------"
@echo "The following targets are available:"
@echo "----------------------------------------------------------------"
@echo "all: compile the example, library and tests"
@echo "example: compile the example program"
@echo "library: compile the library"
@echo "tests: compile (but do not run) the test cases"
@echo "run-tests: run (and compile if necessary) the test cases"
@echo "----------------------------------------------------------------"
@echo "clean-all: clean all build files"
@echo "clean-example: clean example build files"
@echo "clean-library: clean library build files"
@echo "clean-tests: clean test build files"
@echo "----------------------------------------------------------------"
# 'all' compiles the example, library, tests and runs the tests
all: example tests
# 'example' compiles the example program
example: library
@$(MAKE) -s -C example/ compile
# 'library' compiles the library
library: $(OUTPUT)
# 'tests' compiles the tests (in other Makefile)
tests: library
@$(MAKE) -s -C tests/ compile
# 'run-tests' compiles and runs the tests (in other Makefile)
run-tests: library
@$(MAKE) -s -C tests/ run
# 'clean-all' removes all build files
clean-all: clean-example clean-library clean-tests
# 'clean-example' removes all example build files
clean-example:
@$(MAKE) -s -C example/ clean
# 'clean-library' only removes library related build files
clean-library:
@echo "Removing library build files..."
@rm -rf $(BUILD) $(LIB)
# 'clean-tests' only removes test related build files (in other Makefile)
clean-tests:
@$(MAKE) -s -C tests/ clean
# Compiles each object file
.SECONDEXPANSION:
$(OBJDIR)/%.o: $(SOURCE)/%.cpp | $$(@D)
@echo Compiling $*.o...
@$(CXX) -MMD -MP -MF $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d) $(CXXFLAGS) -o $@ -c $<
# Builds the library archive from all .o files
$(OUTPUT): $(OBJS)
@[ -d $(LIB) ] || mkdir -p $(LIB)
@rm -rf $(OUTPUT)
@echo Creating $(TARGET) library archive at $(OUTPUT)...
@$(AR) -rc $(OUTPUT) $(OBJS)
# Creates a directory for each object/dependency file
$(TREE): %:
@mkdir -p $@
@mkdir -p $(@:$(OBJDIR)%=$(DEPDIR)%)