forked from fpagliughi/sockpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
192 lines (136 loc) · 4.27 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
190
191
# Make file for the sockpp library
#
# This will compile all the .cpp files in the directory into the library.
# Any files to be excluded should be placed in the variable SRC_IGNORE, like:
# SRC_IGNORE = this.c that.cpp
#
MODULE = sockpp
# Define CROSS_COMPILE to specify a prefix for GCC
#CROSS_COMPILE=arm-linux-gnueabihf-
# ----- Tools -----
ifndef VERBOSE
QUIET := @
endif
INSTALL ?= install
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644
# ----- Directories -----
SRC_DIR ?= src
INC_DIR ?= src
HDR_DIR ?= $(INC_DIR)/$(MODULE)
LIB_DIR ?= $(CROSS_COMPILE)lib
OBJ_DIR ?= $(CROSS_COMPILE)obj
vpath %.cpp $(SRC_DIR)
INC_DIRS += $(INC_DIR)
prefix ?= /usr/local
exec_prefix ?= ${prefix}
includedir = $(prefix)/include/$(MODULE)
libdir = $(exec_prefix)/lib
_MK_OBJ_DIR := $(shell mkdir -p $(OBJ_DIR))
_MK_LIB_DIR := $(shell mkdir -p $(LIB_DIR))
ifeq "$(MAKECMDGOALS)" "install"
_MK_INSTALL_INC_DIR := $(shell mkdir -p $(DESTDIR)$(includedir))
_MK_INSTALL_LIB_DIR := $(shell mkdir -p $(DESTDIR)$(libdir))
endif
# ----- Definitions for the shared library -----
LIB_BASE ?= $(MODULE)
LIB_MAJOR ?= 0
LIB_MINOR ?= 2
LIB_LINK = lib$(LIB_BASE).so
LIB_MAJOR_LINK = $(LIB_LINK).$(LIB_MAJOR)
LIB = $(LIB_MAJOR_LINK).$(LIB_MINOR)
TGT = $(LIB_DIR)/$(LIB)
# ----- Sources -----
SRCS += $(notdir $(wildcard $(SRC_DIR)/*.cpp))
HDRS += $(notdir $(wildcard $(HDR_DIR)/*.h))
ifdef SRC_IGNORE
SRCS := $(filter-out $(SRC_IGNORE),$(SRCS))
endif
OBJS := $(addprefix $(OBJ_DIR)/,$(SRCS:.cpp=.o))
# ----- Dependencies created with .dep suffix in obj dir -----
DEPS := $(OBJS:.o=.dep)
DEPFLAGS = -MT $@ -MMD -MP -MF $(OBJ_DIR)/$*.Tdep
COMPILE.cc = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
POST_COMPILE = mv -f $(OBJ_DIR)/$*.Tdep $(OBJ_DIR)/$*.dep
# ----- Compiler flags, etc -----
ifneq ($(CROSS_COMPILE),)
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)ar
LD = $(CROSS_COMPILE)ld
endif
CPPFLAGS += -Wall -fPIC
# We need at least C++11 support, though 14 or 17 should work fine.
CXXFLAGS += -std=c++11
ifdef DEBUG
DEFS += DEBUG
CPPFLAGS += -g -O0
else
DEFS += _NDEBUG
CPPFLAGS += -O2 -Wno-unused-result -Werror
endif
CPPFLAGS += $(addprefix -D,$(DEFS)) $(addprefix -I,$(INC_DIRS))
LIB_DEPS += c stdc++ pthread
LIB_DEP_FLAGS += $(addprefix -l,$(LIB_DEPS))
LDFLAGS := -g -shared -Wl,-soname,$(LIB_MAJOR_LINK) -L$(LIB_DIR)
# ----- Compiler directives -----
$(OBJ_DIR)/%.o: %.cpp $(OBJ_DIR)/%.dep
@echo $(CXX) $<
$(QUIET) $(COMPILE.cpp) $(OUTPUT_OPTION) $<
$(QUIET) $(POST_COMPILE)
# ----- Build targets -----
.PHONY: all
all: $(TGT) $(LIB_DIR)/$(LIB_LINK) $(LIB_DIR)/$(LIB_MAJOR_LINK)
$(TGT): $(OBJS)
@echo Creating library: $@
$(QUIET) $(CC) $(LDFLAGS) -o $@ $^ $(LIB_DEP_FLAGS)
$(LIB_DIR)/$(LIB_LINK): $(LIB_DIR)/$(LIB_MAJOR_LINK)
$(QUIET) cd $(LIB_DIR) ; $(RM) $(LIB_LINK) ; ln -s $(LIB_MAJOR_LINK) $(LIB_LINK)
$(LIB_DIR)/$(LIB_MAJOR_LINK): $(TGT)
$(QUIET) cd $(LIB_DIR) ; $(RM) $(LIB_MAJOR_LINK) ; ln -s $(LIB) $(LIB_MAJOR_LINK)
.PHONY: dump
dump:
@echo LIB=$(LIB)
@echo TGT=$(TGT)
@echo LIB_DIR=$(LIB_DIR)
@echo OBJ_DIR=$(OBJ_DIR)
@echo INC_DIRS=$(INC_DIRS)
@echo CFLAGS=$(CFLAGS)
@echo CPPFLAGS=$(CPPFLAGS)
@echo CXX=$(CXX)
@echo COMPILE.cpp=$(COMPILE.cpp)
@echo SRCS=$(SRCS)
@echo HDRS=$(HDRS)
@echo OBJS=$(OBJS)
@echo DEPS:$(DEPS)
@echo LIB_DEPS=$(LIB_DEPS)
.PHONY: clean
clean:
$(QUIET) $(RM) $(TGT) $(LIB_DIR)/$(LIB_LINK) $(LIB_DIR)/$(LIB_MAJOR_LINK) \
$(OBJS)
.PHONY: distclean
distclean: clean
$(QUIET) rm -rf $(OBJ_DIR) $(LIB_DIR)
.PHONY: samples
samples: $(SRC_DIR)/samples
$(MAKE) -C $<
# ----- Installation targets -----
strip_options:
$(eval INSTALL_OPTS := -s)
install-strip: $(TGT) strip_options install
install: $(TGT)
for fil in $(HDRS) ; do $(INSTALL_DATA) ${INSTALL_OPTS} $(HDR_DIR)/$${fil} $(DESTDIR)${includedir} ; done
$(INSTALL_DATA) ${INSTALL_OPTS} ${TGT} $(DESTDIR)${libdir}
cd $(DESTDIR)${libdir} ; ln -s $(LIB) $(LIB_MAJOR_LINK)
cd $(DESTDIR)${libdir} ; ln -s $(LIB_MAJOR_LINK) $(LIB_LINK)
uninstall:
rm $(DESTDIR)${libdir}/${LIB}
rm $(DESTDIR)${libdir}/$(LIB_MAJOR_LINK)
rm $(DESTDIR)${libdir}/$(LIB_LINK)
# ----- Header dependencies -----
$(OBJ_DIR)/%.dep: ;
.PRECIOUS: $(OBJ_DIR)/%.dep
MKG := $(findstring $(MAKECMDGOALS),"clean distclean dump")
ifeq "$(MKG)" ""
-include $(DEPS)
endif