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