Merge branch 'refs/heads/t/lopsub'
[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 headers := osl.h
11 executables := oslfsck
12 man_pages := oslfsck.1
13
14 INSTALL := install
15 ifeq "$(origin CC)" "default"
16 CC := gcc
17 endif
18 MKDIR := mkdir -p
19 RM := rm -f
20 LN := ln
21 LD := ld
22 OBJCOPY := objcopy
23
24 # libosl's versioning consists of three numbers. Let's call them x, y and z.
25 # The way x, y and z are interpreted depends on the OS.
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 uname_s := $(shell uname -s 2>/dev/null || echo "UNKNOWN_OS")
51
52 libname := osl
53
54 ifeq ($(uname_s),Linux)
55 format := elf
56 LDFLAGS += -Wl,-soname,$(soname)
57 # disallow undefined symbols
58 LDFLAGS += -Wl,-z,defs
59 endif
60 ifeq ($(uname_s),Darwin)
61 # Darwin has its own idea on version numbers:
62 realname := lib$(libname).$(x).dylib
63 soname := $(realname)
64 linkername := lib$(libname).dylib
65 # The minor version number is an incremental number using the format
66 # X[.Y[.Z]]. To set the minor version number of a dynamic library, use
67 # the gcc -current_version option.
68 LDFLAGS += -current_version $(y).$(z)
69 #
70 # The compatibility version number of a library release specifies the
71 # earliest minor version of the clients linked against that release can
72 # use.
73 LDFLAGS += -compatibility_version $(y).0
74 LDFLAGS += -dynamiclib
75 endif
76 ifeq ($(uname_s),SunOS)
77 format := elf
78 # Solaris needs another set of flags
79 LDFLAGS += -z text
80 LDFLAGS += -z defs
81 CPPFLAGS += -I/opt/csw/include
82 LDFLAGS += -lc
83 endif
84
85 ifeq ($(uname_s),NetBSD)
86 format := elf
87 LDFLAGS += -Wl,-soname,$(soname)
88 endif
89
90 ifeq ($(uname_s),FreeBSD)
91 format := elf
92 LDFLAGS += -Wl,-soname,$(soname)
93 endif
94
95 ifeq ($(format),elf)
96 # On ELf-based systems, the following conventions apply (see dhweeler's
97 # Program Library HOWTO):
98 #
99 # The soname has the prefix ``lib'', the name of the library, the
100 # phrase ``.so'', followed by a period and a version number that is
101 # incremented whenever the interface changes.
102 soname := lib$(libname).so.$(x)
103
104 # The real name adds to the soname a period, a minor number, another
105 # period, and the release number.
106 realname := $(soname).$(y).$(z)
107
108 # In addition, there's the name that the compiler uses when requesting
109 # a library, (I'll call it the ``linker name''), which is simply the
110 # soname without any version number.
111 linkername := lib$(libname).so
112 LDFLAGS += --shared
113 endif
114
115 all: $(realname) $(executables) $(man_pages)
116 shared: $(realname)
117
118 Makefile.deps: $(wildcard *.c *.h)
119 $(CC) -MM -MG *.c > $@
120 osl.c fsck.c:
121
122 -include Makefile.deps
123
124 %.o: %.c Makefile errtab.h
125 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(LIBCFLAGS) $<
126
127 oslfsck: $(fsck_objects)
128 $(CC) -o $@ $(fsck_objects) -llopsub
129
130 %.lsg.c: %.suite
131 lopsubgen --gen-c < $<
132
133 %.lsg.h: %.suite
134 lopsubgen --gen-header < $<
135
136 %.1: %.suite
137 lopsubgen --gen-man=$@ < $<
138
139 $(realname): $(objects)
140 $(CC) $(LDFLAGS) -o $@ $(objects)
141
142 $(libname).sym: osl.h.in
143 sed -Ene '/^int|^const/{s/.*(osl_.*)\(.*/\1/; p;}' $< > $@
144 $(libname).ga: $(objects)
145 $(LD) -r -o $@ $(objects)
146 lib$(libname).a: $(libname).ga $(libname).sym
147 $(OBJCOPY) --keep-global-symbols $(libname).sym $(libname).ga $@
148
149 osl_errors.h: errlist
150 echo '/** public error codes of the osl library. */' > $@
151 sed -e 's/\([A-Z_]*\) .*/ E_OSL_\1/' \
152 -e '1s/^/enum osl_errors {/1' \
153 -e '1s/$$/=1/1' \
154 -e '$$!s/$$/,/g' \
155 -e '$$s/$$/};/1' $< >> $@
156
157 errtab.h: errlist
158 sed -e 's/^\([A-Z_]*\)\s*\(.*\)/OSL_ERROR(E_OSL_\1, \2)/g' $< > $@
159
160 osl.h: osl.h.in osl_errors.h Makefile
161 echo '#ifndef _OSL_H' > $@
162 echo '#define _OSL_H' >> $@
163 cat osl.h.in osl_errors.h >> $@
164 echo '#endif /* _OSL_H */' >> $@
165 clean:
166 rm -f *.o $(realname) osl.h osl_errors.h errtab.h \
167 oslfsck *.a *.ga *.sym *.lsg.*
168
169 distclean: clean
170 rm -f web/index.html web/oslfsck.1.html web/osl.png
171 rm -rf web/doxygen
172
173 install-bin: $(executables)
174 $(MKDIR) $(bindir)
175 $(INSTALL) -m 755 $(executables) $(bindir)
176
177 install-man: $(man_pages)
178 $(MKDIR) $(mandir)
179 $(INSTALL) -m 644 $(man_pages) $(mandir)
180
181 install-lib: $(realname) $(headers)
182 $(MKDIR) $(libdir) $(includedir)
183 $(RM) $(libdir)/$(linkername)
184 $(LN) -s $(soname) $(libdir)/$(linkername)
185 $(INSTALL) -m 755 $(realname) $(libdir)
186 $(INSTALL) -m 644 $(headers) $(includedir)
187
188 install: all install-bin install-man install-lib
189 www: web/index.html web/osl.png web/doxygen/index.html
190
191 .PHONY: all shared clean install install-bin install-man install-lib www
192
193 web/osl.png: web/osl.pdf Makefile
194 convert -scale 200x200 $< $@
195
196 web/index.html: oslfsck.1 web/index.html.in INSTALL README
197 sed -e '/@README@/,$$d' web/index.html.in > $@
198 markdown < README >> $@
199 sed -e '1,/@README@/d' -e '/@INSTALL@/,$$d' web/index.html.in >> $@
200 markdown < INSTALL >> $@
201 sed -e '1,/@INSTALL@/d' -e '/@MAN_PAGE@/,$$d' web/index.html.in >> $@
202 groff -m man -Thtml -P -l oslfsck.1 | sed -e '1,/^<body>/d; /^<\/body>/,$$d' >> $@
203 sed -e '1,/@MAN_PAGE@/d' web/index.html.in >> $@
204
205 web/doxygen/index.html: $(wildcard *.c *.h) web/Doxyfile web/header.html \
206 web/footer.html
207 doxygen web/Doxyfile