-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
70 lines (53 loc) · 2.04 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
# This is a modified version of Job Vranish's Makefile
# (https://spin.atomicobject.com/2016/08/26/makefile-c-projects/)
TARGET_EXEC ?= resreq
TARGET_TEST ?= test_resreq
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
TEST_DIRS ?= ./test
MAINS ?= $(SRC_DIRS)/main.cpp
EXT ?= cpp
# Find all the C++ files we want to compile
SRCS := $(wildcard $(SRC_DIRS)/*.$(EXT))
TESTS := $(filter-out $(MAINS),$(SRCS)) $(wildcard $(TEST_DIRS)/*.$(EXT))
# String substitution for every C++ file.
# As an example, hello.cpp turns into ./build/hello.cpp.o
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
TOBJS := $(TESTS:%=$(BUILD_DIR)/%.o)
# String substitution (suffix version without %).
# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d
DEPS := $(OBJS:.o=.d) $(TOBJS:.o=.d)
# Every folder in ./src will need to be passed to GCC so that it can find header files
INC_FLAGS := $(wildcard */)
# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# The -MMD and -MP flags together generate Makefiles for us!
# These files will have .d instead of .o as the output.
LDFLAGS ?= -lX11 -lGL -lpthread -lpng -lstdc++fs
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
CXXFLAGS ?= -std=c++17
WARNINGS ?= -g -Wall -Wextra -Werror -pedantic-errors \
-Wwrite-strings -Wno-parentheses -Warray-bounds
all: $(BUILD_DIR)/$(TARGET_EXEC)
test: $(BUILD_DIR)/$(TARGET_TEST)
# The final build step.
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(WARNINGS) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
# Compile tests
$(BUILD_DIR)/$(TARGET_TEST): $(TOBJS)
$(CXX) $(TOBJS) -o $@ $(LDFLAGS)
$TOBJS: TESTS
$(MKDIR_P) $(dir $@)
$(CXX) $(WARNINGS) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: all test clean
clean:
$(RM) -r $(BUILD_DIR)
# Include the .d makefiles. The - at the front suppresses the errors of missing
# Makefiles. Initially, all the .d files will be missing, and we don't want those
# errors to show up.
-include $(DEPS)
MKDIR_P ?= mkdir -p