# Makefile for lockstat

# LINUX_INC_ROOT is the root of the include directory of the Linux tree
# for which you want to compile.  By default, the build uses a local copy
# of the lockmeter.h header, and other headers from /usr/include/... on
# the build host.
LINUX_INC_ROOT ?= ./include

# If you want to compile for the Linux version currently installed on your
# machine, or some other kernel, define it in your environment or on the make
# command line.  E.g.:
#LINUX_INC_ROOT ?= /lib/modules/$$(uname -r)/build/include

# Note that you can run into problems attempting to build against a
# kernel source tree, as this is shifting sand.  Upstream changes
# can and do break the lockstat builds.  Consider copying the lockmeter.h
# from target kernel version to ./include/linux, if it differs from the
# one already there.  Ultimately, lockmeter.h should not change between
# kernel versions--only when lockmeter/lockstat themselves are reved.

# USE_LOCKSTAT_SH determines whether or not to install lockstat with
# a kernel revision suffix and to install the lockstat.sh wrapper to
# invoke the appropriate version of lockstat for the running kernel.
# Not needed if same version of lockstat will work for multiple
# kernel revs.  Note, however, that the lockstat command must still
# match the lockmeter-to-lockstat interface in <linux/lockmeter.h>.
USE_LOCKSTAT_SH=n	# MUST be either 'y' or 'n'

#INSTALLROOT=
BINDIR=/usr/local/bin

PROJ	= lockstat

PROGS	= lockstat lockstat.sh
CC	= gcc
DEFS    = -D_GNU_SOURCE -DCONFIG_LOCKMETER -UDEBUGBUG -UTIMER
#CFLAGS	= -O2 $(DEFS) -fomit-frame-pointer -I$(LINUX_INC_ROOT)
CFLAGS	= -O2 $(DEFS) -I$(LINUX_INC_ROOT)

default all: $(PROGS)

OBJS = lockstat.o symbols.o getsetdata.o

.o:
	$(CC) $(CFLAGS) -c $*.c

lockstat: $(OBJS)
	$(CC) $(LDFLAGS) -o lockstat $(OBJS) $(LDOPT)

lockstat.sh: lockstat.sh.src Makefile
	sed 's,@BINDIR@,$(BINDIR),' lockstat.sh.src >lockstat.sh

install-y: $(PROGS)
	install -m 0755 lockstat $(INSTALLROOT)$(BINDIR)/lockstat-$$(uname -r)
	install -m 0755 lockstat.sh $(INSTALLROOT)$(BINDIR)/lockstat

install-n: $(PROGS)
	install -m 0755 lockstat $(INSTALLROOT)$(BINDIR)/lockstat

install:  install-$(USE_LOCKSTAT_SH)
	@:

cscope:
	cscope -I$(LINUX_INC_ROOT) *.[ch]

clean:
	$(RM) *.o Log cscope.*

clobber: clean
	$(RM) $(PROGS)

# ==========================================================================
# N.B., renames current directory to new version name!
VERSION=$$(cat $$_WD/version.h|grep '_VERSION[^_]'|sed 's/^.* "\([0-9.a-z+]*\)".*$$/\1/')
tarball:  clobber
	@_WD=`pwd`; _WD=`basename $$_WD`; cd ..;\
	_version=$(VERSION); _tarball=$(PROJ)-$${_version}.tar.gz; \
	_newWD=`echo $$_WD | sed  s:-.*:-$$_version:`; \
	if [ "$$_WD" != "$$_newWD" ] ; then \
	echo "Renaming '.' [$$_WD/] to $$_newWD/"; mv $$_WD $$_newWD; fi ; \
	find $$_newWD -type f -perm 00 -exec chmod u+r {} \; ; \
	tar czf - $$_newWD  >$$_tarball; \
	if [ $$? -eq 0 ]; then echo "tarball at ../$$_tarball"; \
	else echo "Error making tarball"; fi
