-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.mk
159 lines (123 loc) · 4.83 KB
/
common.mk
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
##
##
## Makefile
## (by sasha) -fpermissive was added to CFLAGS
###########################################################################
##
## GLOBAL VARIABLES
##
###########################################################################
THISLIB = $(shell basename `pwd`)
##
##
## DIRECTORIES
##
OBJ_DIR = src/obj
BIN_DIR = bin
MAIN_DIR = main
LIB_DIR = lib
SRC_DIR = src
TESTS_DIR = test
##
## SET SEARCH PATHs
##
vpath %.cpp $(SRC_DIR) $(MAIN_DIR)
vpath %.o $(OBJ_DIR)
vpath %.a $(LIB_DIR)
##
##
##
## COMPILER PARAMETERS
##
##
include ../build-config.mk
LDFLAGS += -L $(LIB_DIR)/ $(foreach dir,$(DEPEND_ON),-L ../$(dir)/$(LIB_DIR))
LIBS += -l$(THISLIB) $(foreach name,$(DEPEND_ON),-l$(name)) $(LOCAL_LIBS)
INCLUDE += -I include/ $(foreach dir,$(DEPEND_ON),-I../$(dir)/include) $(LOCAL_INCLUDE)
DEPENDONLIBS = $(DEPEND_ON)
OBJFILES = $(foreach file,$(SRC),$(OBJ_DIR)/$(file).o)
LIBTESTS = $(wildcard $(TESTS_DIR)/*.cpp)
#############################################################################
##
## COMPILATION
##
#############################################################################
# $@ stands for the target name (i.e., the resulting executable)
# $? stands for the dependency list (i.e., the .o files)
#
.PHONY: all lib $(THISLIB) lib$(THISLIB) clean $(MAIN) tests check
# These are all the files to be compiled.
ALL = lib $(MAIN)
all: $(ALL)
lib lib$(THISLIB): $(LIB_DIR)/lib$(THISLIB).a
## Make local library
$(LIB_DIR)/lib$(THISLIB).a : $(OBJFILES)
if [ ! -d $(LIB_DIR) ]; then mkdir $(LIB_DIR); fi
$(AR) cr $@ $(OBJFILES)
ranlib $@
# Compile the executable targets
$(MAIN) : % : $(OBJ_DIR)/%.o $(LIB_DIR)/lib$(THISLIB).a $(DEPENDONLIBS)
if [ ! -d $(BIN_DIR) ]; then mkdir -p $(BIN_DIR); fi
@echo "$(DEPENDONLIBS)"
$(CXX) $(CPPFLAGS) $(INCLUDE) $(CXXFLAGS) $< $(LDFLAGS) $(LIBS) -o $(BIN_DIR)/$@
@echo
@echo " ./$@ : compiled sucessfully."
@echo
# Make libs that current library depends on
$(DEPENDONLIBS): % :
cd ../$*; $(MAKE) -f Makefile lib
## compile object files
$(OBJ_DIR)/%.o : %.cpp
$(CXX) $(CPPFLAGS) $(INCLUDE) $(CXXFLAGS) -c $< -o $@
## Build dependencies
$(OBJ_DIR)/%.d: %.cpp
if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi
$(SHELL) -ec '$(CXX) $(CPPFLAGS) -MM $(INCLUDE) $(CXXFLAGS) $< | sed "s:$*.o:$(OBJ_DIR)/& $@:g"' > $@
# Google test framework integration
GTEST_DIR = ../gtest
# All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h \
../general/include/gmp_boost_pool_allocator.h
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
$(GTEST_DIR)/gtest-all.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR)/include -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc -o $(GTEST_DIR)/gtest-all.o
$(GTEST_DIR)/gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR)/include -I$(GTEST_DIR) -I../general/include $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc -o $(GTEST_DIR)/gtest_main.o
$(GTEST_DIR)/gtest_main.a : $(GTEST_DIR)/gtest-all.o $(GTEST_DIR)/gtest_main.o
$(AR) $(ARFLAGS) $@ $^
## compile object files for unit tests
$(OBJ_DIR)/unittest_%.o : $(TESTS_DIR)/%.cpp $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(INCLUDE) -I$(GTEST_DIR)/include $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/unittest_%.d : $(TESTS_DIR)/%.cpp $(GTEST_HEADERS)
if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi
$(SHELL) -ec '$(CXX) $(CPPFLAGS) -MM $(INCLUDE) -I$(GTEST_DIR)/include $(CXXFLAGS) $< | sed "s:$*.o:$(OBJ_DIR)/unittest_& $@:g"' > $@
tests: $(BIN_DIR)/all_unittests
$(BIN_DIR)/all_unittests: $(LIBTESTS:$(TESTS_DIR)/%.cpp=$(OBJ_DIR)/unittest_%.o) $(GTEST_DIR)/gtest_main.a \
$(LIB_DIR)/lib$(THISLIB).a $(DEPENDONLIBS) general
@echo "found tests: $(LIBTESTS:$(TESTS_DIR)/%.cpp=%)"
@if [ ! -d $(BIN_DIR) ]; then mkdir -p $(BIN_DIR); fi
$(CXX) $(CPPFLAGS) $(INCLUDE)-I$(GTEST_DIR)/include $(CXXFLAGS) $(LIBTESTS:$(TESTS_DIR)/%.cpp=$(OBJ_DIR)/unittest_%.o) $(GTEST_DIR)/gtest_main.a \
$(LDFLAGS) -lpthread -lgeneral $(LIBS) -o $@
@echo
@echo " ./$@ : compiled sucessfully."
@echo
check: tests
ifdef FILTER
$(BIN_DIR)/all_unittests --gtest_filter="$(FILTER)"
else
$(BIN_DIR)/all_unittests
endif
# Clean target to remove backup, object, and core files
clean:
rm -f $(OBJ_DIR)/*.d $(OBJ_DIR)/*.o $(LIB_DIR)/*.a $(BIN_DIR)/*
# Include all the dependency files, this will automatically remake them if they are
# out of date
include $(foreach file,$(SRC) $(MAIN) $(LIBTESTS:$(TESTS_DIR)/%.cpp=unittest_%),$(OBJ_DIR)/$(file).d)