-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.mk
84 lines (60 loc) · 2.47 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
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
STRIP = $(CROSS_COMPILE)strip
OBJ_PREFIX = .obj
DEP_PREFIX = .dep
C_SOURCES = $(filter %.c, $(SOURCES))
CXX_SOURCES = $(filter %.cpp, $(SOURCES))
C_OBJECTS = $(patsubst %.c, $(OBJ_PREFIX)/%.o, $(C_SOURCES))
CXX_OBJECTS = $(patsubst %.cpp, $(OBJ_PREFIX)/%.o, $(CXX_SOURCES))
OBJECTS = $(C_OBJECTS) $(CXX_OBJECTS)
C_DEPS = $(patsubst %.c, $(DEP_PREFIX)/%.d, $(C_SOURCES))
CXX_DEPS = $(patsubst %.cpp, $(DEP_PREFIX)/%.d, $(CXX_SOURCES))
DEPS = $(C_DEPS) $(CXX_DEPS)
ifneq ($(strip $(CXX_OBJECTS)),)
LINKER=$(CXX)
else
LINKER=$(CC)
endif
SHARED_LIB_SUFFIX ?= .so
ifdef SHARED_LIB
SHARED_LIB_LINKNAME = lib$(SHARED_LIB)$(SHARED_LIB_SUFFIX)
SHARED_LIB_NAME = $(SHARED_LIB_LINKNAME).$(SHARED_LIB_VER)
SHARED_LIB_SONAME = $(SHARED_LIB_LINKNAME).$(SHARED_LIB_SOVER)
endif
SUBDIRS_CLEAN = $(addsuffix .clean, $(SUBDIRS))
SUBDIRS_MAKE = $(addsuffix .subdir, $(SUBDIRS))
.PHONY: all clean strip $(SUBDIRS_MAKE) $(SUBDIRS_CLEAN)
all: $(BIN) $(SHARED_LIB_NAME)
$(SUBDIRS_MAKE):
$(MAKE) -C $(patsubst %.subdir, %, $@)
strip: $(BIN) $(SHARED_LIB_NAME)
$(STRIP) --strip-unneeded $<
%/.stamp:
mkdir -p $(dir $@)
touch $@
$(OBJECTS): $(sort $(foreach X, $(OBJECTS), $(dir $(X)).stamp))
$(DEPS): $(sort $(foreach X, $(DEPS), $(dir $(X)).stamp))
# C/C++ compilation rules
$(C_OBJECTS): $(OBJ_PREFIX)/%.o: %.c $(DEP_PREFIX)/%.d
$(CC) $(CPPFLAGS) $(CFLAGS) $($(BIN)_CFLAGS) $($(SHARED_LIB)_CFLAGS) -c $< -o $@
$(CXX_OBJECTS): $(OBJ_PREFIX)/%.o: %.cpp $(DEP_PREFIX)/%.d
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $($(BIN)_CXXFLAGS) $($(SHARED_LIB)_CXXFLAGS) -c $< -o $@
# C/C++ dependencies
$(C_DEPS): $(DEP_PREFIX)/%.d: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) $($(BIN)_CFLAGS) $($(SHARED_LIB)_CFLAGS) -M $< -MT "$(OBJ_PREFIX)/$*.o" -o $@
$(CXX_DEPS): $(DEP_PREFIX)/%.d: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $($(BIN)_CXXFLAGS) $($(SHARED_LIB)_CXXFLAGS) -M $< -MT "$(OBJ_PREFIX)/$*.o" -o $@
$(BIN): $(OBJECTS)
$(LINKER) -o $@ $(filter-out $(SUBDIRS_MAKE), $^) $(LDFLAGS) $($(BIN)_LDFLAGS)
$(SHARED_LIB_NAME): $(OBJECTS)
$(LINKER) -shared -Wl,-soname,$(SHARED_LIB_SONAME) -o $@ $(filter-out $(SUBDIRS_MAKE), $^) \
$(LDFLAGS) $($(SHARED_LIB)_LDFLAGS)
ln -sf $(@F) $(@D)/$(SHARED_LIB_SONAME)
ln -sf $(@F) $(@D)/$(SHARED_LIB_LINKNAME)
$(BIN) $(SHARED_LIB_NAME): $(SUBDIRS_MAKE)
$(SUBDIRS_CLEAN):
$(MAKE) -C $(patsubst %.clean, %, $@) clean
clean: $(SUBDIRS_CLEAN)
$(RM) $(BIN) $(OBJECTS) $(DEPS) $(SHARED_LIB_NAME) $(SHARED_LIB_SONAME) $(SHARED_LIB_LINKNAME)
-include $(DEPS)