-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile.common
132 lines (93 loc) · 3.12 KB
/
makefile.common
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
CC=gcc
CXX=g++
OBJDUMP=dump
MKDIR=mkdir
CP=cp
CXXFLAGS += -c -O$(OPTIMIZATION) --std=c++11
CXXFLAGS += -Wall -MMD -MP -MF"$(@:%.o=%.d)"
OBJ_C = $(patsubst %.c,%.o,$(SOURCES))
OBJ_CXX = $(patsubst %.cpp,%.o,$(OBJ_C))
OBJ_A = $(patsubst %.S,%.o,$(OBJ_CXX))
OBJ_ALL = $(OBJ_A)
OBJ_TST = $(patsubst %.cpp,%.o,$(TESTS))
SRC = $(foreach var,$(SOURCES),$(SRC_DIR)/$(var))
TST = $(foreach var,$(TESTS),$(TEST_DIR)/$(var))
OBJ = $(foreach var,$(OBJ_ALL),$(OBJ_DIR)/$(var))
OBJ_TEST = $(foreach var,$(OBJ_TST),$(OBJ_DIR)/$(TEST_DIR)/$(var))
LIB = $(foreach var,$(LIBS),"-l$(var)")
LIB_TEST = $(foreach var,$(LIBS),"-l$(var)") -lgtest -lgtest_main
DEP = $(foreach var,$(OBJ),$(var:%.o=%.d))
DEP_TEST = $(foreach var,$(OBJ_TEST),$(var:%.o=%.d))
INC = $(foreach var,$(INCLUDE_DIRS),-I"$(var)") -I$(INC_DIR)
LIB_DIR = $(foreach var,$(LIB_DIRS),-L"$(var)")
OBJ_DIRS = $(foreach var,$(OBJ),$(dir $(var)))
OBJ_TEST_DIRS = $(foreach var,$(OBJ_TEST),$(dir $(var)))
OBJ_MAIN = $(OBJ_DIR)/main.o
DEP_MAIN = $(OBJ_DIR)/main.d
LDFLAGS = $(LIB_DIR) $(LIB)
ifeq ($(TYPE),app)
TARGET=$(BIN_DIR)/$(PROJECT)
TEST_TARGET=$(BIN_DIR)/$(PROJECT)_tests
LISTING=$(BIN_DIR)/$(PROJECT).lst
MAP=$(BIN_DIR)/$(PROJECT).map
endif
ifeq ($(TYPE),lib)
TARGETS=$(LIB_DIR)/lib$(PROJECT).a
INSTALL_DIR=$(INSTALL_PREFIX)/lib
INSTALL_INCLUDE=$(INSTALL_PREFIX)/include
INSTALL_INCLUDES=$(foreach var,$(INCLUDE),$(INSTALL_INCLUDE)/$(var))
INSTALL_TARGETS= $(INSTALL_DIR)/lib$(PROJECT).a
endif
.PHONY: all test clean install
all: test $(TARGET)
test: $(TEST_TARGET)
$(TEST_TARGET)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@echo "CC $<"
@$(MKDIR) -p $(dir $@)
@$(CC) $(INC) $(CFLAGS) -o"$@" "$<"
$(OBJ_DIR)/$(TEST_DIR)/%.o: $(TEST_DIR)/%.c
@echo "CC $<"
@$(MKDIR) -p $(dir $@)
$(CC) $(INC) $(CFLAGS) -o"$@" "$<"
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@echo "CXX $<"
@$(MKDIR) -p $(dir $@)
@$(CXX) $(INC) $(CXXFLAGS) -o"$@" "$<"
$(OBJ_DIR)/$(TEST_DIR)/%.o: $(TEST_DIR)/%.cpp
@echo "CXX $<"
@$(MKDIR) -p $(dir $@)
@$(CXX) $(INC) $(CXXFLAGS) -o"$@" "$<"
$(BIN_DIR)/%.a: $(OBJ)
@$(MKDIR) -p $(BIN_DIR)
@echo "Link to library $@"
@$(AR) -r $@ $(OBJ)
$(TARGET): $(OBJ) $(OBJ_MAIN)
@echo "LD $(TARGET)"
@$(MKDIR) -p $(BIN_DIR)
@$(CC) $(LDFLAGS) -o"$@" $(OBJ) $(OBJ_MAIN)
$(TEST_TARGET): $(OBJ) $(OBJ_TEST)
@echo "LD $(TEST_TARGET)"
@$(MKDIR) -p $(BIN_DIR)
@$(CC) $(LDFLAGS) -o"$@" $(OBJ) $(OBJ_TEST) $(LIB_TEST)
$(BIN_DIR)/%.lst: $(TARGET)
@$(OBJDUMP) -h -S "$<" > "$@"
clean:
$(RM) $(OBJ) $(DEP) $(TARGET) $(LISTING) $(MAP) $(DEP) $(OBJ_TEST) $(DEP_TEST) $(TEST_TARGET) $(OBJ_MAIN) $(DEP_MAIN)
@find $(OBJ_DIR) -type d -empty -delete
@find $(BIN_DIR) -type d -empty -delete
install: $(INSTALL_TARGETS) $(INSTALL_INCLUDES)
$(INSTALL_INCLUDE)/%.h: $(INC_DIR)/%.h
@echo "Install include file $<"
@$(MKDIR) -p $(dir $@)
@$(CP) $< $@
$(INSTALL_INCLUDE)/%.inc: $(INC_DIR)/%.inc
@echo "Install inc file $<"
@$(MKDIR) -p $(dir $@)
@$(CP) $< $@
$(INSTALL_DIR)/%.a: $(BIN_DIR)/%.a | $(INSTALL_DIR)
@echo "Installing library from $<"
@$(CP) $< $@
-include $(DEP)
-include $(DEP_TEST)
-include $(DEP_MAIN)