CFLAGS = -m486 -O2 -Wall
LDFLAGS = -s -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

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 -c $$i $(BINDIR) ; 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

