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