-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
189 lines (146 loc) · 4.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# output binary
BIN := main
# test binary
TESTS := unittests
# intermediate directory for generated object files
OBJDIR := .o
# intermediate directory for generated dependency files
DEPDIR := .d
LIBRARY_DIR := library
$(shell mkdir -p $(LIBRARY_DIR) >/dev//null)
### google-test and unit test section ###
GTEST_DIR := include/googletest
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)
GTEST_INC_FLAGS := $(CPPFLAGS) -isystem $(GTEST_DIR)/include
GTEST_CXX_FLAGS := $(CXXFLAGS) -g -Wall -Wextra -pthread
GTEST_CXX := $(CXX)
TEST_SRCS := \
tests/dual_unittests.cpp \
tests/variable_unittests.cpp \
tests/graph_linear_unittests.cpp
TEST_OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(TEST_SRCS)))
TEST_DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(TEST_SRCS)))
$(shell mkdir -p $(dir $(TEST_OBJS)) >/dev/null)
$(shell mkdir -p $(dir $(TEST_DEPS)) >/dev/null)
GTEST_INCLUDE := $(GTEST_DIR)/include
### end google-test ###
VPATH := src
# source files
SRCS := \
variable.cpp \
dual.cpp \
graph.cpp \
randhelpers.cpp
MAIN_SRC := main.cpp
# files included in the tarball generated by 'make dist' (e.g. add LICENSE file)
DISTFILES := $(BIN)
# filename of the tar archive generated by 'make dist'
DISTOUTPUT := $(BIN).tar.gz
# include dir
INCLDIR := include
# object files, auto generated from source files
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
# dependency files, auto generated from source files
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
MAIN_OBJ := $(patsubst %,$(OBJDIR)/%.o,$(basename $(MAIN_SRC)))
MAIN_DEP := $(patsubst %,$(DEPDIR)/%.d,$(basename $(MAIN_SRC)))
# compilers (at least gcc and clang) don't create the subdirectories automatically
$(shell mkdir -p $(dir $(OBJS)) >/dev/null)
$(shell mkdir -p $(dir $(DEPS)) >/dev/null)
# C compiler
CC := clang
# C++ compiler
CXX := clang++
# linker
LD := clang++
# tar
TAR := tar
# C flags
CFLAGS := -std=c11
# C++ flags
CXXFLAGS := -std=c++14
# C/C++ flags
CPPFLAGS := -g -Wall -Wextra -pedantic -I $(INCLDIR) -I $(GTEST_INCLUDE)
# linker flags
LDFLAGS :=
# flags required for dependency generation; passed to compilers
DEPFLAGS = -MT $@ -MD -MP -MF $(DEPDIR)/$*.Td
# compile C source files
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@
# compile C++ source files
COMPILE.cc = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -o $@
# link object files to binary
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) -o $@
# precompile step
PRECOMPILE =
# postcompile step
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
all: $(BIN) $(TESTS)
tests: $(TESTS)
runtests: tests
./$(TESTS)
dist: $(DISTFILES)
$(TAR) -cvzf $(DISTOUTPUT) $^
.PHONY: clean
clean: gtest_clean
$(RM) -r $(OBJDIR) $(DEPDIR)
.PHONY: distclean
distclean: clean
$(RM) $(BIN) $(DISTOUTPUT)
.PHONY: install
install:
@echo no install tasks configured
.PHONY: uninstall
uninstall:
@echo no uninstall tasks configured
.PHONY: check
check:
@echo no tests configured
.PHONY: help
help:
@echo available targets: all dist clean distclean install uninstall check
$(TESTS): $(TEST_OBJS) $(OBJS) $(LIBRARY_DIR)/gtest_main.a
$(LINK.o) $^
$(BIN): $(OBJS) $(MAIN_OBJ)
$(LINK.o) $^
$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.c) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cpp
$(OBJDIR)/%.o: %.cpp $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cc
$(OBJDIR)/%.o: %.cc $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cxx
$(OBJDIR)/%.o: %.cxx $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
.PRECIOUS = $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;
### google-test targets ###
$(OBJDIR)/gtest-all.o: $(GTEST_SRCS_)
$(GTEST_CXX) $(GTEST_INC_FLAGS) -I$(GTEST_DIR) $(GTEST_CXX_FLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc -o $@
$(OBJDIR)/gtest_main.o: $(GTEST_SRCS_)
$(GTEST_CXX) $(GTEST_INC_FLAGS) -I$(GTEST_DIR) $(GTEST_CXX_FLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc -o $@
$(LIBRARY_DIR)/gtest.a: $(OBJDIR)/gtest-all.o
$(AR) $(ARFLAGS) $@ $^
$(LIBRARY_DIR)/gtest_main.a: $(OBJDIR)/gtest-all.o $(OBJDIR)/gtest_main.o
$(AR) $(ARFLAGS) $@ $^
gtest_clean:
$(RM) $(TESTS) $(LIBRARY_DIR)/gtest.a $(LIBRARY_DIR)/gtest_main.a *.o
### end google-test targets ###
-include $(DEPS)
-include $(MAIN_DEP)
-include $(TEST_DEPS)