#
# The following definition enables persistent module information to be used
# for initialisation of module symbols.  It also implies libgdbm !!!
#
#PERSIST= -DPERSIST

CFLAGS = -O6 -pipe -fomit-frame-pointer -Wall $(PERSIST)
#LDFLAGS = -N
LDFLAGS = -s
ifdef PERSIST
LDLIBS = -lgdbm
BELT_AND_SUSPENDERS=/usr/include/gdbm.h
endif
CC=gcc
BINDIR=/sbin
MODCFLAGS := $(CFLAGS) -DMODULE -D__KERNEL__ -DLINUX

# If block device 42 is in use, change the definition below:
HW_MAJOR=42

PROGS=insmod lsmod
INSMOD_LINKS=rmmod ksyms

MANS1=insmod.1 ksyms.1 lsmod.1 rmmod.1
MANS2=modules.2
MAN1DIR=/usr/man/man1
MAN2DIR=/usr/man/man2

all: $(PROGS) links

insmod: insmod.o load_aout.o load_elf.o error.o

insmod.o load_aout.o load_elf.o: insmod.h $(BELT_AND_SUSPENDERS)

links: insmod
	for i in $(INSMOD_LINKS); do ln -sf insmod $$i; done

drv_hello.o:	drv_hello.c  /usr/include/linux/version.h
	$(CC) $(MODCFLAGS) -c drv_hello.c 

/dev/hw:
	if [ ! -c /dev/hw ]; then mknod /dev/hw c $(HW_MAJOR) 0;fi

test: $(PROGS) drv_hello.o /dev/hw
	@echo Installing drv_hello.o with insmod
	./insmod drv_hello.o major=$(HW_MAJOR)
	@echo This is the output from lsmod
	./lsmod
	@echo You should now see 5 lines of the greeting:
	head -5 /dev/hw
	@echo Removing drv_hello with rmmod
	./rmmod drv_hello
	@echo It is gone...
	./lsmod

clean:
	rm -f *.o $(PROGS) $(INSMOD_LINKS) /dev/hw

install-binaries: $(PROGS)
	@set -x ;for i in $(PROGS) ; do install -s -c $$i $(BINDIR) 2>/dev/null ; done
	@set -x ;(cd $(BINDIR);for i in $(INSMOD_LINKS) ; do ln -sf insmod $$i; done)
	@echo "Have you read the README? Things might have changed..."

install-docs:
	@set -x ;for i in $(MANS1) ; do install -m 644 -c $$i $(MAN1DIR) ; done
	@set -x ;for i in $(MANS2) ; do install -m 644 -c $$i $(MAN2DIR) ; done

install: install-binaries install-docs
