#  Makefile to compile NI-KAL clients against the kernel
#
#   Copyright 2002-2010
#  National Instruments Corporation.
#  All Rights reserved.


# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language. (LDD3, ch 2, pg 24)
ifneq ($(KERNELRELEASE),)

   SHORTSYMS_DB := $(src)/shortsyms.txt

   ifeq ($(CLIENT_NAME),)
      $(error ERROR: CLIENT_NAME is undefined.  Consider using the nikalKernelInstaller script to install your module)
   endif
   ifeq ($(CLIENT_MODULE),)
      $(error ERROR: CLIENT_MODULE is undefined.  Consider using the nikalKernelInstaller script to install your module)
   endif

   CLIENT_COPYRIGHT   := $(shell strings $(CLIENT_MODULE) | grep nNIVersion_LegalCopyright | sed 's/nNIVersion_LegalCopyright\=//')
   ifneq ($(CLIENT_COPYRIGHT),)
      EXTRA_CFLAGS += -DnNIKAL100_kCopyrightString="$(moduleCopyright)"
   endif

   CLIENT_AUTHOR      := $(shell strings $(CLIENT_MODULE) | grep nNIVersion_CompanyName | sed 's/nNIVersion_CompanyName\=//')
   ifneq ($(CLIENT_AUTHOR),)
      EXTRA_CFLAGS += -DnNIKAL100_kAuthorString="$(moduleAuthor)"
   endif

   CLIENT_DESCRIPTION := $(shell strings $(CLIENT_MODULE) | grep nNIVersion_FileDescription | sed 's/nNIVersion_FileDescription\=//')
   ifneq ($(CLIENT_DESCRIPTION),)
      EXTRA_CFLAGS += -DnNIKAL100_kDescriptionString="$(moduleDescription)"
   endif

   obj-m := $(CLIENT_NAME).o
   $(CLIENT_NAME)-objs := $(CLIENT_NAME)-interface.o $(CLIENT_NAME)-processed.o

   EXTRA_CFLAGS := -I$(src)/../nikal/

.PHONY: update-symdb
update-symdb:
	@$(src)/processmodule.sh --cmd_updatesymrename --symfile=$(SHORTSYMS_DB) --module=$(CLIENT_MODULE)

$(obj)/$(CLIENT_NAME)-interface.c: update-symdb
	@$(src)/processmodule.sh --cmd_geninterface --symfile=$(SHORTSYMS_DB) --module=$(CLIENT_MODULE) > $@

# Processing of client module:
$(obj)/$(CLIENT_NAME)-processed.o_shipped : $(CLIENT_MODULE) update-symdb
	@objcopy --redefine-syms=$(SHORTSYMS_DB) --rename-section .ctors=.ni_ctors --remove-section .exportedSymbols $< $@

else

   include ../nikal/Makefile.in

.PHONY: all
all:
	@$(MAKE) --no-print-directory -C $(KERNELDIRECTORY) M=$(shell pwd) modules

.PHONY: clean
clean:
	@rm -rf *-interface.c *.o_shipped .tmp_versions *.o .*.cmd *.mod.c Module.*

endif
