-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
105 lines (80 loc) · 2.62 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
99
100
101
102
103
104
105
# To build the program (which is called mem-sim by default), simply type:
# make
#
# To run the tests, type:
# make test
#
# To clean up and remove the compiled binary and other generated files, type:
# make clean
#
# To build AND run the program, type:
# make run
#
SHELL = /bin/bash
# The name of your binary.
NAME = mem-sim
# Flags passed to the preprocessor.
CPPFLAGS += -Werror -MMD -MP -Isrc -g -std=c++17
TEST_CPPFLAGS = $(CPPFLAGS) -isystem $(GTEST_DIR)/include
# All .cpp files.
SRCS = $(shell find src -name '*.cpp')
# All implementation sources, excluding main.cpp and test files.
IMPL_SRCS = $(shell find src \
-name '*.cpp' \
-not -name '*_tests.cpp' \
-not -name 'main.cpp')
# All test files.
TEST_SRCS = $(shell find src -name '*_tests.cpp')
IMPL_OBJS = $(IMPL_SRCS:src/%.cpp=bin/%.o)
TEST_OBJS = $(TEST_SRCS:src/%.cpp=bin/%.o)
DEPS = $(SRCS:src/%.cpp=bin/%.d)
# Points to the root of Google Test, relative to where this file is.
GTEST_DIR = googletest/googletest
# All Google Test headers and source files.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
TEST_FILTER = '*'
# Build the program.
$(NAME): bin/main.o $(IMPL_OBJS)
$(CXX) $(CPP_FLAGS) $^ -o $(NAME)
@echo "Successfully Compiled!"
# Build and run the program.
run: $(NAME)
./$(NAME)
# Build and run the unit tests.
test: bin/all_tests
./bin/all_tests --gtest_filter=$(TEST_FILTER)
# Remove all generated files.
clean:
rm -rf $(NAME)* bin/ tests/output/{*,*/*}/*.{actual,diff}
# Ensure the bin/ directories are created.
$(SRCS): | bin
# Initialize the googletest submodule if needed.
$(GTEST_DIR):
git submodule update --init
# Ensure googletest is ready to go.
$(GTEST_HEADERS): | $(GTEST_DIR)
# Mirror the directory structure of src/ under bin/.
bin:
mkdir -p $(shell find src -type d | sed "s/src/bin/")
# Build objects.
bin/%_tests.o: src/%_tests.cpp
$(CXX) $(TEST_CPPFLAGS) $< -c -o $@
# Build objects.
bin/%.o: src/%.cpp
$(CXX) $(CPPFLAGS) $< -c -o $@
# Build gtest_main.a.
bin/gtest-all.o: $(GTEST_HEADERS) | bin
$(CXX) $(TEST_CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc -o $@
bin/gtest_main.o: $(GTEST_HEADERS) | bin
$(CXX) $(TEST_CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc -o $@
bin/gtest_main.a: bin/gtest-all.o bin/gtest_main.o
$(AR) $(ARFLAGS) $@ $^
# Build the unit tests.
bin/all_tests: bin/gtest_main.a $(IMPL_OBJS) $(TEST_OBJS)
$(CXX) $(TEST_CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $@
# Auto dependency management.
-include $(DEPS)