#
# NOTE: If you are _not_ using the kerneld package, comment away this:
#
CONFIG_KERNELD=-DCONFIG_KERNELD

CFLAGS = -m486 -O6 -pipe -fomit-frame-pointer -Wall $(CONFIG_KERNELD)
LDFLAGS = -ggdb
LDFLAGS = -N
CC=gcc
BINDIR=/sbin
MODCFLAGS := $(CFLAGS) -DMODULE -D__KERNEL__ -DLINUX

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

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

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: $(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)
	@set -x ;for i in $(MANS1) ; do install -c $$i $(MAN1DIR) ; done
	@set -x ;for i in $(MANS2) ; do install -c $$i $(MAN2DIR) ; done
	@echo "Have you read the README? Things might have changed..."

