PROGS	= tdump
DATAS	= lsvpd-functions.bash debug.bash

LIBS	= libvpd.a

TESTS	= test_growbuf test_growbuf.out test_vpd

LIBOBJS	= vpd.o growbuf.o util.o

GEN	= list.h

AR	= ar cq
RANLIB	= ranlib

INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = $(INSTALL) -m 755
INSTALL_DATA = $(INSTALL) -m 644

all:		$(GEN) $(PROGS) $(LIBS) $(TESTS)

tdump:		tdump.o util.o

tdump.o:	tdump.c util.h

vpd.o:		vpd.c growbuf.h

libvpd.a:	$(LIBOBJS)
		$(RM) $@
		$(AR) $@ $(LIBOBJS)
		$(RANLIB) $@

growbuf.o:	growbuf.c growbuf.h

util.h:		util.h.in
	        sed -e "s%@libdir@%$(libdir)%g" $< > $@

util.o:		util.c util.h

list.h:		Makefile
		echo '#ifndef _LSVPD_LIST_H' >$@
		echo '#define _LSVPD_LIST_H' >>$@
		echo '#include <stdlib.h>' >>$@
		grep -A 2 '^#define container_of(' /usr/include/linux/kernel.h >>$@
		grep 'void prefetch(' /usr/include/linux/prefetch.h >>$@
		echo '#define __KERNEL__ 1' >>$@
		echo '#define smp_wmb()' >>$@
		grep -v '^#include' /usr/include/linux/list.h >>$@
		echo '#undef __KERNEL__' >>$@
		echo '#endif' >>$@

test_growbuf:	test_growbuf.o growbuf.o

test_vpd:	test_vpd.o libvpd.a

test:		test_growbuf
		./test_growbuf < test_growbuf.c > test_growbuf.out
		diff -u test_growbuf.c test_growbuf.out

install:	all
	@ for f in $(PROGS) ; do \
		$(INSTALL_PROGRAM) $$f $(DESTDIR)$(libdir) ;\
	done
	@ for f in $(DATAS) ; do \
		$(INSTALL_DATA) $$f $(DESTDIR)$(libdir) ;\
	done

clean:
	rm -f $(PROGS) $(LIBS) $(TESTS) *.o *~
