build: Tell make(1) that the compiler creates .d files.
[osl.git] / Makefile
1 # Implicit rules are implemented in make as suffix rules. The following rule
2 # empties the suffix list to disable the predefined implicit rules. This
3 # increases performance and avoids hard-to-debug behaviour.
4 .SUFFIXES:
5 MAKEFLAGS += -Rr
6 ifeq ("$(origin CC)", "default")
7         CC := cc
8 endif
9
10 # where to install
11 PREFIX ?= /usr/local
12 libdir := $(PREFIX)/lib
13 includedir := $(PREFIX)/include
14 bindir := $(PREFIX)/bin
15 mandir := $(PREFIX)/man/man1
16
17 objects := osl.o util.o rbtree.o sha1.o
18 fsck_objects := fsck.o osl.o util.o rbtree.o sha1.o oslfsck.lsg.o
19 deps := $(sort $(objects:.o=.d) $(fsck_objects:.o=.d))
20 headers := osl.h
21 executables := oslfsck
22 man_pages := oslfsck.1
23
24 INSTALL := install
25 ifeq "$(origin CC)" "default"
26         CC := cc
27 endif
28 MKDIR := mkdir -p
29 RM := rm -f
30 LN := ln
31 LD := ld
32 M4 := m4 -g
33 OBJCOPY := objcopy
34
35 # libosl's versioning consists of three numbers. Let's call them x, y and z.
36 x := 0
37 y := 1
38 z := 3
39 VERSION := $(x).$(y).$(z)
40
41 OSL_CPPFLAGS += -DOSL_VERSION='"$(VERSION)"'
42
43 OSL_CFLAGS += -g -Wunused -Wundef -W
44 OSL_CFLAGS += -Wredundant-decls
45 OSL_CFLAGS += -Os
46 OSL_CFLAGS += -Wall
47 OSL_CFLAGS += -Wuninitialized
48 OSL_CFLAGS += -Wchar-subscripts
49 OSL_CFLAGS += -Wformat-security
50 OSL_CFLAGS += -Werror-implicit-function-declaration
51 OSL_CFLAGS += -Wmissing-format-attribute
52 OSL_CFLAGS += -Wunused-macros
53 OSL_CFLAGS += -Wbad-function-cast
54 OSL_CFLAGS += -fPIC
55 OSL_CFLAGS += -fvisibility=hidden
56
57 OSL_LDFLAGS += -Wl,-soname,$(soname)
58 OSL_LDFLAGS += -Wl,-z,defs
59 OSL_LDFLAGS += --shared
60
61 # On ELf-based systems, the following conventions apply (see dhweeler's
62 # Program Library HOWTO):
63 #
64 # The soname has the prefix ``lib'', the name of the library, the
65 # phrase ``.so'', followed by a period and a version number that is
66 # incremented whenever the interface changes.
67 libname := osl
68 soname := lib$(libname).so.$(x)
69
70 # The real name adds to the soname a period, a minor number, another
71 # period, and the release number.
72 realname := $(soname).$(y).$(z)
73
74 # In addition, there's the name that the compiler uses when requesting
75 # a library, (I'll call it the ``linker name''), which is simply the
76 # soname without any version number.
77 linkername := lib$(libname).so
78
79 all: $(realname) $(executables) $(man_pages) $(headers)
80 shared: $(realname)
81
82 ifeq ($(findstring clean, $(MAKECMDGOALS)),)
83 -include $(deps)
84 endif
85
86 # The files to generate before attempting to run the compiler. These
87 # are the order-only prerequisites in the rule below.
88 pre_deps := osl.h errtab.h oslfsck.lsg.h
89
90 # The .d and .o files are both created from a single cc invocation.
91 define CC_CMD
92         $(CC) $(OSL_CPPFLAGS) $(CPPFLAGS) \
93                 -c -MMD -MF $(*F).d -MT $(*F).o \
94                 $(OSL_CFLAGS) $(CFLAGS) $<
95 endef
96 %.o: %.c Makefile | $(pre_deps)
97         $(CC_CMD)
98 %.d: %.c Makefile | $(pre_deps)
99         $(CC_CMD)
100
101 fsck.o: oslfsck.lsg.h
102 oslfsck: $(fsck_objects)
103         $(CC) -o $@ $(fsck_objects) $(LDFLAGS) -llopsub
104
105 .PRECIOUS: %.lsg.h %.lsg.c
106 %.lsg.c: %.suite
107         lopsubgen --gen-c < $<
108
109 %.lsg.h: %.suite
110         lopsubgen --gen-header < $<
111
112 %.1: %.suite
113         lopsubgen --gen-man=$@ < $<
114
115 $(realname): $(objects)
116         $(CC) $(OSL_LDFLAGS) $(LDFLAGS) -o $@ $(objects)
117
118 errtab.h: errlist
119         sed -e 's/^\([A-Z_]*\)\s*\(.*\)/OSL_ERROR(E_OSL_\1, \2)/g' $< > $@
120
121 osl.h: osl.h.m4 errlist Makefile
122         echo '#ifndef _OSL_H' > $@
123         echo '#define _OSL_H' >> $@
124         $(M4) -DOUTPUT_MODE=C gendoc.m4 $< >> $@
125         echo '/** public error codes of the osl library. */' >> $@
126         sed -e 's/\([A-Z_]*\)   .*/     E_OSL_\1/' \
127                 -e '1s/^/enum osl_errors {/1' \
128                 -e '1s/$$/=1/1' \
129                 -e '$$!s/$$/,/g' \
130                 -e '$$s/$$/};/1' errlist >> $@
131         echo '#endif /* _OSL_H */' >> $@
132 clean:
133         rm -f *.o $(realname) osl.h errtab.h oslfsck *.lsg.* *.d
134
135 distclean: clean
136         rm -f web/index.html web/oslfsck.1.html web/osl.png
137
138 install-bin: $(executables)
139         $(MKDIR) $(bindir)
140         $(INSTALL) -m 755 $(executables) $(bindir)
141
142 install-man: $(man_pages)
143         $(MKDIR) $(mandir)
144         $(INSTALL) -m 644 $(man_pages) $(mandir)
145
146 install-lib: $(realname) $(headers)
147         $(MKDIR) $(libdir) $(includedir)
148         $(RM) $(libdir)/$(linkername)
149         $(LN) -s $(soname) $(libdir)/$(linkername)
150         $(INSTALL) -m 755 $(realname) $(libdir)
151         $(INSTALL) -m 644 $(headers) $(includedir)
152
153 install: all install-bin install-man install-lib
154 www: web/index.html web/osl.png web/api.html
155
156 .PHONY: all shared clean install install-bin install-man install-lib www
157
158 web/osl.png: web/osl.pdf Makefile
159         convert -scale 200x200 $< $@
160
161 web/index.html: oslfsck.1 web/header.html web/index.html.in INSTALL README QUICK_START
162         cat web/header.html > $@
163         sed -e '/@README@/,$$d' web/index.html.in >> $@
164         markdown < README >> $@
165         sed -e '1,/@README@/d' -e '/@INSTALL@/,$$d' web/index.html.in >> $@
166         markdown < INSTALL >> $@
167         sed -e '1,/@INSTALL@/d' -e '/@QUICK_START@/,$$d' web/index.html.in >> $@
168         markdown < QUICK_START >> $@
169         sed -e '1,/@QUICK_START@/d' -e '/@MAN_PAGE@/,$$d' web/index.html.in >> $@
170         groff -m man -Thtml -P -l oslfsck.1 | sed -e '1,/^<body>/d; /^<\/body>/,$$d' >> $@
171         sed -e '1,/@MAN_PAGE@/d' web/index.html.in >> $@
172
173 web/api.html: osl.h.m4 web/header.html web/footer.html
174         cat web/header.html > $@
175         $(M4) -DOUTPUT_MODE=HTML gendoc.m4 $< >> $@
176         cat web/footer.html >> $@