-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
57 lines (47 loc) · 1.53 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
# Default target is 'compile' (compiles all test related files)
.DEFAULT_GOAL := compile
# Variables for file + output locations
BUILD := build
OBJDIR := build/objs
DEPDIR := build/deps
EXE := run-tests
INCLUDE := include ../include
SOURCE := source
LIBDIR := ../lib
LIBNAME := Splash
# Flags to pass to the compiler
CXXFLAGS := -std=c++11 -Wall -O3 $(foreach dir, $(INCLUDE), -I$(dir))
# 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))))
LIB := $(LIBDIR)/lib$(LIBNAME).a
# Include dependency files if they already exist
ifeq "$(MAKECMDGOALS)" ""
-include $(DEPS)
endif
# Define virtual make targets
.PHONY: compile run clean
# 'compile' compiles all related files for testing
compile: $(EXE)
$(EXE): $(LIB) $(OBJS)
@echo "Compiling test executable..."
@$(CXX) -o $(EXE) $(OBJS) -L$(LIBDIR) -l$(LIBNAME)
# 'run' runs the tests (and compiles the executable if necessary)
run: compile
@echo "Running all tests..."
@$(CURDIR)/$(EXE)
# 'clean' removes all build files
clean:
@echo "Removing test build files..."
@rm -rf $(BUILD) $(EXE)
# Compiles each object file
.SECONDEXPANSION:
$(OBJDIR)/%.o: $(SOURCE)/%.cpp | $$(@D)
@echo Compiling $*.o...
@$(CXX) -MMD -MP -MF $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d) $(CXXFLAGS) -o $@ -c $<
# Creates a directory for each object/dependency file
$(TREE): %:
@mkdir -p $@
@mkdir -p $(@:$(OBJDIR)%=$(DEPDIR)%)