-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
73 lines (58 loc) · 1.69 KB
/
Makefile
File metadata and controls
73 lines (58 loc) · 1.69 KB
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
# Lesson 16: Make vs CMake — the Makefile version
#
# This is a "production-quality" Makefile that combines
# everything from lessons 01–15:
# - Variables, pattern rules, automatic variables
# - Separate src/include
# - Auto header dependencies (-MMD -MP)
# - Out-of-source builds
# - Debug/release via BUILD=
# - Test target
# - .PHONY, clean
#
# Compare this with CMakeLists.txt in the same directory.
# CMake generates a Makefile (or Ninja file) automatically
# from a higher-level description. Everything you see below
# is what CMake does for you behind the scenes.
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Iinclude -MMD -MP
BUILD ?= debug
ifeq ($(BUILD),release)
CXXFLAGS += -O2 -DNDEBUG
BUILDDIR = build/release
else
CXXFLAGS += -g -O0
BUILDDIR = build/debug
endif
LIB_SRCS = src/matrix.cpp
APP_SRCS = src/main.cpp
TEST_SRCS = tests/test_matrix.cpp
LIB_OBJS = $(LIB_SRCS:%.cpp=$(BUILDDIR)/%.o)
APP_OBJS = $(APP_SRCS:%.cpp=$(BUILDDIR)/%.o)
TEST_OBJS = $(TEST_SRCS:%.cpp=$(BUILDDIR)/%.o)
ALL_DEPS = $(LIB_OBJS:.o=.d) $(APP_OBJS:.o=.d) $(TEST_OBJS:.o=.d)
APP_TARGET = $(BUILDDIR)/app
TEST_TARGET = $(BUILDDIR)/test_matrix
LIB = $(BUILDDIR)/libmatrix.a
AR = ar
ARFLAGS = rcs
.PHONY: all clean test
all: $(APP_TARGET)
$(LIB): $(LIB_OBJS)
@mkdir -p $(@D)
$(AR) $(ARFLAGS) $@ $^
$(APP_TARGET): $(APP_OBJS) $(LIB)
@mkdir -p $(@D)
$(CXX) -o $@ $(APP_OBJS) $(LIB)
$(TEST_TARGET): $(TEST_OBJS) $(LIB)
@mkdir -p $(@D)
$(CXX) -o $@ $(TEST_OBJS) $(LIB)
test: $(TEST_TARGET)
@echo "Running tests..."
@./$(TEST_TARGET)
$(BUILDDIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -rf build
-include $(ALL_DEPS)