-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.rules
95 lines (74 loc) · 3.04 KB
/
Makefile.rules
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
# define TARGETS to be build
# define OBJECTS to be used
# define SOURCES to be used
# define CLEANFILES to be deleted
# define DISTCLEANFILES to be deleted
# define INST_HDR for header files to be installed
# define INST_STC for static libraries to be installed
# define INST_SHD for shared libraries to be installed
# define INST_PRG for executables to be installed
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
DEPDIR = .depend
OBJDIR = .object
CLEANFILES += $(DEPDIR) $(OBJDIR)
SRCS = $(SOURCES)
OBJS = $(addprefix $(OBJDIR)/,$(OBJECTS))
OBJS_PIC = $(addprefix $(OBJDIR)/,$(OBJECTS:.o=.pic.o))
all: $(TARGETS)
# include rules file for each object file
-include $(addprefix $(DEPDIR)/,$(SRCS:.c=.d))
%.a: $(OBJS)
$(AR) rc $@ $?
%.so: $(OBJS_PIC)
$(CC) $(LDFLAGS) -shared -Wl,-soname,$@.$(VERSION) -o $@.$(VERSION) $^ $(LIBS)
@rm -f $@
ln -s $@.$(VERSION) $@
# generate rules file with all dependencies for each object file
$(DEPDIR)/%.d: %.c | $(DEPDIR)
@set -e; rm -f $@; \
$(CC) -MM $(CFLAGS) $(CPPFLAGS) $< > $@.$$$$; \
> $@ \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ >> $@; \
sed 's,\($*\)\.o[ :]*,\1.pic.o $@ : ,g' < $@.$$$$ >> $@; \
sed 's,\($*\)\.o[ :]*,$(OBJDIR)/\1.o $@ : ,g' < $@.$$$$ >> $@; \
sed 's,\($*\)\.o[ :]*,$(OBJDIR)/\1.pic.o $@ : ,g' < $@.$$$$ >> $@; \
rm -f $@.$$$$
$(DEPDIR):
mkdir -p $@
# generate each object file
$(OBJDIR)/%.o: %.c | $(OBJDIR)
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
# position independent code compiling
$(OBJDIR)/%.pic.o: %.c | $(OBJDIR)
$(CC) -c -fPIC $(CFLAGS) $(CPPFLAGS) $< -o $@
%.pic.o: %.c
$(CC) -c -fPIC $(CFLAGS) $(CPPFLAGS) $< -o $@
$(OBJDIR):
mkdir -p $@
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
clean:
rm -rf $(CLEANFILES)
distclean:
rm -rf $(DISTCLEANFILES) Makefile
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
install:
#install header
@[ -d $(DESTDIR)${includedir} ] || (mkdir -p $(DESTDIR)${includedir}; chmod 755 $(DESTDIR)${includedir})
$(INSTALL_HEADER) $(INST_HDR) $(DESTDIR)${includedir}/
#install static library
@[ -d $(DESTDIR)${libdir} ] || (mkdir -p $(DESTDIR)${libdir}; chmod 755 $(DESTDIR)${libdir})
$(INSTALL_DATA) $(INST_STC) $(DESTDIR)/${libdir}/
#install shared library
$(INSTALL_DATA) $(INST_SHD).$(VERSION) $(DESTDIR)${libdir}/
ln -sf $(INST_SHD).$(VERSION) $(DESTDIR)${libdir}/$(INST_SHD)
uninstall:
rm -f $(addprefix $(DESTDIR)${includedir}/, $(INST_HDR))
rm -f $(addprefix $(DESTDIR)${libdir}/, $(INST_STC))
rm -f $(addprefix $(DESTDIR)${libdir}/, $(INST_SHD) $(INST_SHD).$(VERSION))
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------