EMBEDFILE = $(FS_PATH)/usr/bin
EMBEDLIB = $(FS_PATH)/lib
EMBEDMODULE = $(EMBEDLIB)/modules

all: modules nvram

libnvram.so: libnvram.o nvram_compatible.o
	$(CC) $(LDFLAGS) $^ -shared -o $@

nvram: main.o libnvram.so
	$(CC) $(LDFLAGS) $^ -o $@

ifneq ($(KERNEL_PATH),)
KERNELDIR:=$(KERNEL_PATH)
endif
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
# ifeq ($(KERNELRELEASE),)

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

install: 
	$(STRIP) nvram libnvram.so
	cp -f nvram $(EMBEDFILE)
	cp -f libnvram.so $(EMBEDLIB)
	cp -f devnvram.ko $(EMBEDMODULE)
	
clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
	rm -rf nvram *.so
.PHONY: modules modules_install clean

# else
    # called from kernel build system: just declare what our modules are
    devnvram-objs := nvram_linux.o nvram.o
    obj-m := devnvram.o
# endif

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