-uname_s := $(shell uname -s 2>/dev/null || echo "UNKNOWN_OS")
-uname_rs := $(shell uname -rs)
+# where to install
+PREFIX ?= /usr/local
+libdir := $(PREFIX)/lib
+includedir := $(PREFIX)/include
+bindir := $(PREFIX)/bin
+mandir := $(PREFIX)/man/man1
-objects := osl.o fd.o rbtree.o sha1.o
-major_version := 0
-minor_version := 1
-patchlevel_version := 0
+objects := osl.o util.o rbtree.o sha1.o
+fsck_objects := fsck.o osl.o util.o rbtree.o sha1.o oslfsck.lsg.o
+deps := $(sort $(objects:.o=.d) $(fsck_objects:.o=.d))
+headers := osl.h
+executables := oslfsck
+man_pages := oslfsck.1
+
+INSTALL := install
+ifeq "$(origin CC)" "default"
+ CC := cc
+endif
+MKDIR := mkdir -p
+RM := rm -f
+LN := ln
+LD := ld
+OBJCOPY := objcopy
+
+# libosl's versioning consists of three numbers. Let's call them x, y and z.
+x := 0
+y := 1
+z := 3
+VERSION := $(x).$(y).$(z)
+
+OSL_CPPFLAGS += -DOSL_VERSION='"$(VERSION)"'
+
+OSL_CFLAGS += -Wno-sign-compare -g -Wunused -Wundef -W
+OSL_CFLAGS += -Wredundant-decls
+OSL_CFLAGS += -Os
+OSL_CFLAGS += -Wall
+OSL_CFLAGS += -Wuninitialized
+OSL_CFLAGS += -Wchar-subscripts
+OSL_CFLAGS += -Wformat-security
+OSL_CFLAGS += -Werror-implicit-function-declaration
+OSL_CFLAGS += -Wmissing-format-attribute
+OSL_CFLAGS += -Wunused-macros
+OSL_CFLAGS += -Wbad-function-cast
+OSL_CFLAGS += -fPIC
+OSL_CFLAGS += -fvisibility=hidden
+
+OSL_LDFLAGS += -Wl,-soname,$(soname)
+OSL_LDFLAGS += -Wl,-z,defs
+OSL_LDFLAGS += --shared
+
+# On ELf-based systems, the following conventions apply (see dhweeler's
+# Program Library HOWTO):
+#
+# The soname has the prefix ``lib'', the name of the library, the
+# phrase ``.so'', followed by a period and a version number that is
+# incremented whenever the interface changes.
libname := osl
+soname := lib$(libname).so.$(x)
+# The real name adds to the soname a period, a minor number, another
+# period, and the release number.
+realname := $(soname).$(y).$(z)
-ifeq ($(uname_s),Linux)
- dso_opts := --shared -Wl,-soname,libosl.so.$(major_version)
- dso_filename :=lib$(libname).so.$(major_version).$(minor_version).$(patchlevel_version)
-endif
-ifeq ($(uname_s),Darwin)
- dso_opts := -dynamiclib -current_version $(major_version).$(minor_version) \
- -compatibility_version $(major_version).$(minor_version) -fvisibility=hidden
- dso_filename := lib$(libname).$(major_version)
-endif
-ifeq ($(uname_s),SunOS)
- dso_opts := --shared -z text -z defs
- dso_filename :=lib$(libname).so.$(major_version).$(minor_version).$(patchlevel_version)
- CPPFLAGS += -I/opt/csw/include
-endif
-ifeq ($(uname_s),FreeBSD)
- dso_opts := --shared -Wl,-soname,libosl.so.$(major_version)
- dso_filename :=lib$(libname).so.$(major_version).$(minor_version).$(patchlevel_version)
-endif
-ifeq ($(uname_s),NetBSD)
- dso_opts := --shared -Wl,-soname,libosl.so.$(major_version)
- dso_filename :=lib$(libname).so.$(major_version).$(minor_version).$(patchlevel_version)
+# In addition, there's the name that the compiler uses when requesting
+# a library, (I'll call it the ``linker name''), which is simply the
+# soname without any version number.
+linkername := lib$(libname).so
+
+all: $(realname) $(executables) $(man_pages) $(headers)
+shared: $(realname)
+
+ifeq ($(findstring clean, $(MAKECMDGOALS)),)
+-include $(deps)
endif
-all: $(dso_filename)
-
-# disallow undefined symbols
-LDFLAGS += -Wl,-z,defs
-
-DEBUG_CPPFLAGS += -Wno-sign-compare -g -Wunused -Wundef -W
-DEBUG_CPPFLAGS += -Wredundant-decls
-CPPFLAGS += -Os
-CPPFLAGS += -Wall
-CPPFLAGS += -Wuninitialized
-CPPFLAGS += -Wchar-subscripts
-CPPFLAGS += -Wformat-security
-CPPFLAGS += -Werror-implicit-function-declaration
-CPPFLAGS += -Wmissing-format-attribute
-CPPFLAGS += -Wunused-macros
-CPPFLAGS += -Wbad-function-cast
-CPPFLAGS += -fPIC
-CPPFLAGS += -fvisibility=hidden
-
-Makefile.deps: $(wildcard *.c *.h)
- gcc -MM -MG *.c > $@
-
--include Makefile.deps
-
-%.o: %.c Makefile
- $(CC) -c $(CPPFLAGS) $(DEBUG_CPPFLAGS) $<
-$(dso_filename): $(objects)
- $(CC) $(dso_opts) -o $@ $(objects) $(LDFLAGS) -lcrypto
-
-osl_errors.h: errlist
- sed -e 's/\([A-Z_]*\) .*/ E_OSL_\1/' \
- -e '1s/^/enum osl_errors {\n/1' \
- -e '$$!s/$$/,/g' \
- -e '$$s/$$/\n};/1' $< > $@
+
+# List osl.h in the prerequisites to make sure we generate it before attempting
+# to run the compiler. This matters only when the .d file does not exist.
+%.o: %.c osl.h Makefile errtab.h
+ $(CC) $(OSL_CPPFLAGS) $(CPPFLAGS) \
+ -c -MMD -MF $(*F).d -MT $@ \
+ $(OSL_CFLAGS) $(CFLAGS) $<
+
+fsck.o: oslfsck.lsg.h
+oslfsck: $(fsck_objects)
+ $(CC) -o $@ $(fsck_objects) $(LDFLAGS) -llopsub
+
+%.lsg.c: %.suite
+ lopsubgen --gen-c < $<
+
+%.lsg.h: %.suite
+ lopsubgen --gen-header < $<
+
+%.1: %.suite
+ lopsubgen --gen-man=$@ < $<
+
+$(realname): $(objects)
+ $(CC) $(OSL_LDFLAGS) $(LDFLAGS) -o $@ $(objects)
+
+$(libname).sym: osl.h.in
+ sed -Ene '/^int|^const/{s/.*(osl_.*)\(.*/\1/; p;}' $< > $@
+$(libname).ga: $(objects)
+ $(LD) -r -o $@ $(objects)
+lib$(libname).a: $(libname).ga $(libname).sym
+ $(OBJCOPY) --keep-global-symbols $(libname).sym $(libname).ga $@
errtab.h: errlist
- sed -e 's/^\([A-Z_]*\)\s*\(.*\)/_S(E_OSL_\1, \2)/g' $< > $@
-osl.h: osl.h.in osl_errors.h
- cat $^ > $@
+ sed -e 's/^\([A-Z_]*\)\s*\(.*\)/OSL_ERROR(E_OSL_\1, \2)/g' $< > $@
+
+osl.h: osl.h.in errlist Makefile
+ echo '#ifndef _OSL_H' > $@
+ echo '#define _OSL_H' >> $@
+ cat osl.h.in >> $@
+ echo '/** public error codes of the osl library. */' >> $@
+ sed -e 's/\([A-Z_]*\) .*/ E_OSL_\1/' \
+ -e '1s/^/enum osl_errors {/1' \
+ -e '1s/$$/=1/1' \
+ -e '$$!s/$$/,/g' \
+ -e '$$s/$$/};/1' errlist >> $@
+ echo '#endif /* _OSL_H */' >> $@
clean:
- rm -f *.o $(dso_filename) osl.h osl_errors.h errtab.h
+ rm -f *.o $(realname) osl.h errtab.h oslfsck *.a *.ga *.sym *.lsg.* *.d
+
+distclean: clean
+ rm -f web/index.html web/oslfsck.1.html web/osl.png
+ rm -rf web/doxygen
+
+install-bin: $(executables)
+ $(MKDIR) $(bindir)
+ $(INSTALL) -m 755 $(executables) $(bindir)
+
+install-man: $(man_pages)
+ $(MKDIR) $(mandir)
+ $(INSTALL) -m 644 $(man_pages) $(mandir)
+
+install-lib: $(realname) $(headers)
+ $(MKDIR) $(libdir) $(includedir)
+ $(RM) $(libdir)/$(linkername)
+ $(LN) -s $(soname) $(libdir)/$(linkername)
+ $(INSTALL) -m 755 $(realname) $(libdir)
+ $(INSTALL) -m 644 $(headers) $(includedir)
+
+install: all install-bin install-man install-lib
+www: web/index.html web/osl.png web/doxygen/index.html
+
+.PHONY: all shared clean install install-bin install-man install-lib www
+
+web/osl.png: web/osl.pdf Makefile
+ convert -scale 200x200 $< $@
+
+web/index.html: oslfsck.1 web/index.html.in INSTALL README
+ sed -e '/@README@/,$$d' web/index.html.in > $@
+ markdown < README >> $@
+ sed -e '1,/@README@/d' -e '/@INSTALL@/,$$d' web/index.html.in >> $@
+ markdown < INSTALL >> $@
+ sed -e '1,/@INSTALL@/d' -e '/@MAN_PAGE@/,$$d' web/index.html.in >> $@
+ groff -m man -Thtml -P -l oslfsck.1 | sed -e '1,/^<body>/d; /^<\/body>/,$$d' >> $@
+ sed -e '1,/@MAN_PAGE@/d' web/index.html.in >> $@
+
+web/doxygen/index.html: $(wildcard *.c *.h) web/Doxyfile web/header.html \
+ web/footer.html
+ doxygen web/Doxyfile