
CFLAGS += -g
LDFLAGS += -g

CFLAGS += -I../include

BASEDIR ?= $(shell pwd)

KERNELDIR ?= $(shell echo /lib/modules/$$(uname -r)/build)

KVERREL = $(patsubst /lib/modules/%/build,%,$(KERNELDIR))

DESTDIR =

INSTALLDIR = $(patsubst %/build,%/extra,$(KERNELDIR))

LIBELF_OBJS := libelf-loader.o libelf-relocate.o libelf-tools.o

KBUILD_LIBELF_OBJS := $(patsubst %,../common/libelf/%,$(LIBELF_OBJS))

all: hxen.hex
	$(MAKE) -C $(KERNELDIR) M=`pwd` "$$@"

install:
	mkdir -p $(DESTDIR)/$(INSTALLDIR)
	cp *.ko $(DESTDIR)/$(INSTALLDIR)
	/sbin/depmod -a

clean:
	$(MAKE) -C $(KERNELDIR) M=`pwd` $@
	rm -f hxen.hex
	rm -f hxen_prog
	rm -f $(KBUILD_LIBELF_OBJS)
	rm -f Module.symvers

hxen.hex: ../hxen/hxen.elf
	./mkhex hxen_elf $< >$@

../hxen/hxen.elf:
	$(MAKE) -C ../hxen hxen.elf

hxen_prog: hxen_prog.o $(LIBELF_OBJS)

hxen_prog.o: hxen_mod.c hxen.hex
	$(CC) -DKXEN_STANDALONE $(CFLAGS) -c -o $@ $<

$(LIBELF_OBJS): %.o : ../common/libelf/%.c
	$(CC) -DKXEN_STANDALONE $(CFLAGS) -c -o $@ $<