Allow to build a static version of libosl.
[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.fsck.o osl.fsck.o util.fsck.o rbtree.fsck.o sha1.fsck.o fsck.cmdline.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 := 1
29 VERSION := $(x).$(y).$(z)
30
31 # common flags
32 CFLAGS += -Wno-sign-compare -g -Wunused -Wundef -W
33 CFLAGS += -Wredundant-decls
34 CFLAGS += -Os
35 CFLAGS += -Wall
36 CFLAGS += -Wuninitialized
37 CFLAGS += -Wchar-subscripts
38 CFLAGS += -Wformat-security
39 CFLAGS += -Werror-implicit-function-declaration
40 CFLAGS += -Wmissing-format-attribute
41 CFLAGS += -Wunused-macros
42 CFLAGS += -Wbad-function-cast
43
44 # cflags used only for building library objects
45 LIBCFLAGS += -fPIC
46 LIBCFLAGS += -fvisibility=hidden
47
48 uname_s := $(shell uname -s 2>/dev/null || echo "UNKNOWN_OS")
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),Darwin)
59 # Darwin has its own idea on version numbers:
60 realname := lib$(libname).$(x).dylib
61 soname := $(realname)
62 linkername := lib$(libname).dylib
63 # The minor version number is an incremental number using the format
64 # X[.Y[.Z]]. To set the minor version number of a dynamic library, use
65 # the gcc -current_version option.
66 LDFLAGS += -current_version $(y).$(z)
67 #
68 # The compatibility version number of a library release specifies the
69 # earliest minor version of the clients linked against that release can
70 # use.
71 LDFLAGS += -compatibility_version $(y).0
72 LDFLAGS += -dynamiclib
73 endif
74 ifeq ($(uname_s),SunOS)
75 format := elf
76 # Solaris needs another set of flags
77 LDFLAGS += -z text
78 LDFLAGS += -z defs
79 CPPFLAGS += -I/opt/csw/include
80 LDFLAGS += -lc
81 endif
82
83 ifeq ($(uname_s),NetBSD)
84 format := elf
85 LDFLAGS += -Wl,-soname,$(soname)
86 endif
87
88 ifeq ($(uname_s),FreeBSD)
89 format := elf
90 LDFLAGS += -Wl,-soname,$(soname)
91 endif
92
93 ifeq ($(format),elf)
94 # On ELf-based systems, the following conventions apply (see dhweeler's
95 # Program Library HOWTO):
96 #
97 # The soname has the prefix ``lib'', the name of the library, the
98 # phrase ``.so'', followed by a period and a version number that is
99 # incremented whenever the interface changes.
100 soname := lib$(libname).so.$(x)
101
102 # The real name adds to the soname a period, a minor number, another
103 # period, and the release number.
104 realname := $(soname).$(y).$(z)
105
106 # In addition, there's the name that the compiler uses when requesting
107 # a library, (I'll call it the ``linker name''), which is simply the
108 # soname without any version number.
109 linkername := lib$(libname).so
110 LDFLAGS += --shared
111 endif
112
113 all: $(realname) $(executables) $(man_pages)
114 Makefile.deps: $(wildcard *.c *.h)
115 $(CC) -MM -MG *.c > $@
116 osl.c fsck.c:
117
118 -include Makefile.deps
119
120 fsck.cmdline.o: fsck.cmdline.c fsck.cmdline.h
121 $(CC) -c -DVERSION='"$(VERSION)"' $<
122
123 %.fsck.o: %.c Makefile fsck.cmdline.c fsck.cmdline.h osl.h errtab.h
124 $(CC) -c -DVERSION='"$(VERSION)"' $(CPPFLAGS) $(CFLAGS) $< -o $@
125
126 %.o: %.c Makefile errtab.h
127 $(CC) -c $(CPPFLAGS) $(CFLAGS) $(LIBCFLAGS) $<
128
129 fsck.cmdline.h fsck.cmdline.c: fsck.ggo Makefile
130 gengetopt \
131 --conf-parser \
132 --unamed-opts=table \
133 --no-handle-version \
134 --file-name=fsck.cmdline \
135 --func-name=fsck_cmdline_parser \
136 --set-package="oslfsck" \
137 --arg-struct-name=fsck_args_info \
138 < $<
139
140 oslfsck: $(fsck_objects)
141 $(CC) -o $@ $(fsck_objects)
142
143 oslfsck.1: oslfsck
144 help2man -h --detailed-help -N ./$< > $@
145
146 $(realname): $(objects)
147 $(CC) $(LDFLAGS) -o $@ $(objects)
148
149 $(libname).sym: osl.h.in
150 sed -Ene '/^int|^const/{s/.*(osl_.*)\(.*/\1/; p;}' $< > $@
151 $(libname).ga: $(objects)
152 $(LD) -r -o $@ $(objects)
153 lib$(libname).a: $(libname).ga $(libname).sym
154 $(OBJCOPY) --keep-global-symbols $(libname).sym $(libname).ga $@
155
156 osl_errors.h: errlist
157 echo '/** public error codes of the osl library. */' > $@
158 sed -e 's/\([A-Z_]*\) .*/ E_OSL_\1/' \
159 -e '1s/^/enum osl_errors {/1' \
160 -e '1s/$$/=1/1' \
161 -e '$$!s/$$/,/g' \
162 -e '$$s/$$/};/1' $< >> $@
163
164 errtab.h: errlist
165 sed -e 's/^\([A-Z_]*\)\s*\(.*\)/OSL_ERROR(E_OSL_\1, \2)/g' $< > $@
166
167 osl.h: osl.h.in osl_errors.h Makefile
168 echo '#ifndef _OSL_H' > $@
169 echo '#define _OSL_H' >> $@
170 cat osl.h.in osl_errors.h >> $@
171 echo '#endif /* _OSL_H */' >> $@
172 clean:
173 rm -f *.o $(realname) osl.h osl_errors.h errtab.h fsck.cmdline.h \
174 fsck.cmdline.c oslfsck *.a *.ga *.sym
175
176 distclean: clean
177 rm -f web/index.html web/oslfsck.1.html web/osl.png
178 rm -rf web/doxygen
179
180 install-bin: $(executables)
181 $(MKDIR) $(bindir)
182 $(INSTALL) -m 755 $(executables) $(bindir)
183
184 install-man: $(man_pages)
185 $(MKDIR) $(mandir)
186 $(INSTALL) -m 644 $(man_pages) $(mandir)
187
188 install-lib: $(realname) $(headers)
189 $(MKDIR) $(libdir) $(includedir)
190 $(RM) $(libdir)/$(linkername)
191 $(LN) -s $(libdir)/$(soname) $(libdir)/$(linkername)
192 $(INSTALL) -m 755 $(realname) $(libdir)
193 $(INSTALL) -m 644 $(headers) $(includedir)
194
195 install: all install-bin install-man install-lib
196
197 .PHONY: all clean install install-bin install-man install-lib
198
199 web/%.1.html: %.1
200 man2html $< > $@
201
202 web/osl.png: web/osl.pdf Makefile
203 convert -scale 200x200 $< $@
204
205 web/index.html: web/oslfsck.1.html web/index.html.in INSTALL README
206 sed -e '/@README@/,$$d' web/index.html.in > $@
207 grutatxt -nb < README >> $@
208 sed -e '1,/@README@/d' -e '/@INSTALL@/,$$d' web/index.html.in >> $@
209 grutatxt -nb < INSTALL >> $@
210 sed -e '1,/@INSTALL@/d' -e '/@MAN_PAGE@/,$$d' web/index.html.in >> $@
211 sed -e '1,/Return to Main Contents/d' -e '/Index/,$$d' web/oslfsck.1.html >> $@
212 sed -e '1,/@MAN_PAGE@/d' web/index.html.in >> $@