.NOTPARALLEL:

SHELL          := /bin/bash
ifeq ($(SHARKCLONEROOT),)
  $(error SHARKCLONEROOT not set)
endif
BLDMODE := $(shell if [ -e $(SHARKCLONEROOT)/config/kdef.mk ]; \
                   then echo "GPL"; else echo "CVS"; fi)
ifeq ($(BLDMODE), GPL)
        CONFIGDIR := $(SHARKCLONEROOT)/config
        GPLDIR    := $(SHARKCLONEROOT)/gpl-linux
        LPPBIN    := $(SHARKCLONEROOT)/../bin
        INSTALLMOD:=
else
        CONFIGDIR := $(SHARKCLONEROOT)/../gconfig
        GPLDIR    := $(SHARKCLONEROOT)/kernext/gpl-linux
        LPPBIN    := $(SHARKCLONEROOT)/bin
        INSTALLMOD:= install_mod
endif
export CONFIGDIR
export LPPBIN
export GPLDIR

include $(CONFIGDIR)/kdef.mk

KERNEL_BUILD_DIR_TO_CLEAN := $(shell if [ -e $(KERNEL_BUILD_DIR) ]; \
                             then echo "$(KERNEL_BUILD_DIR)";       \
                             else echo "/usr/src/linux"; fi)

# Linux won't load a kernel module unless it was compiled with the same
# compiler (by version number) as the rest of the kernel.  So site.mk
# may need to specify a different compiler for the kernel modules than for
# the rest of GPFS.

ifdef KERNEL_CC
CCOPT = CC=$(KERNEL_CC)
endif

PWD := $(shell pwd)
GPL_HEADER_DIR := $(SHARKCLONEROOT)/include/gpl-linux
MODPATH        := /$(GPLDIR)/gpl_kernel.tmp.ver
DASH_I  := *-[^=\ ][i]*

HEADERS = Shark-gpl.h prelinux.h postlinux.h linux2gpfs.h verdep.h \
          Logger-gpl.h arch-gpl.h oplock.h tracelin.h

ifdef DEFINE_GDS
HEADERS += nvfs.h
endif

default: modules
Headers:: headers
Headers:: install.he
install.he:: $(HEADERS)
	@(case '$(MAKEFLAGS)' in $(DASH_I)) set +e;; esac; \
	        changed="$?"; \
	if [ ! -e $(GPL_HEADER_DIR) ]; then  \
	        echo "Making directory $(GPL_HEADER_DIR)"; \
	        mkdir -p $(GPL_HEADER_DIR); \
	fi; \
	for i in $$changed; do \
	        (set -x; $(INSTALL) $(INSTINCFLAGS) $$i $(GPL_HEADER_DIR)/$$i) done)
install.he::
	touch install.he

ifeq ($(BLDMODE), GPL)
headers::
else
TRACELIST = gplInit.c dir.c file.c inode.c super.c \
            cxiVFSStats.c cxiSystem.c cxiIOBuffer.c cxiCache.c kx.c ss.c \
            mmap.c acl.c mmpmem.c dynpool.c
trcid:  $(TRACELIST)
	$(MKTRACE) -hm  $(TRACELIST)
headers:: trcid.h gpl-linux.trclst
trcid.h: trcid.def $(TRACELIST)
	$(MKTRACE) -hD $(TRACELIST)
gpl-linux.trclst:: trcid.def $(TRACELIST)
	$(RM) gpl-linux.trclst
	$(MKTRACE) -l gpl-linux.trclst  $(TRACELIST)
endif

pre-kbuild1:
	@echo "Pre-kbuild step 1..."
	@if [ $(KBNAME) = Makefile ]; then \
		if [ -e "./Makefile" ]; then mv Makefile Makefile.bk; fi; \
		cp -f Kbuild Makefile; \
	fi
pre-kbuild2: pre-kbuild1
	@echo "Pre-kbuild step 2..."
	@$(INSTALL) -c -m 644 $(PWD)/../../bin/$(MMFS) $(PWD)/$(MMFSNAME)
	@echo "" > $(PWD)/.$(MMFSNAME).cmd
ifeq ($(GPFS_EX_TABLE_VER), V4)
	@objcopy --rename-section __ex_table_v4=__ex_table $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
else ifeq ($(GPFS_EX_TABLE_VER), V3)
	@objcopy --rename-section __ex_table_v3=__ex_table $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
else ifeq ($(GPFS_EX_TABLE_VER), V2)
	@objcopy --rename-section __ex_table_v2=__ex_table $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
else ifeq ($(GPFS_EX_TABLE_VER), V1)
	@objcopy --rename-section __ex_table_v1=__ex_table $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
endif

ifeq ($(GPFS_ALTERNATIVE_VER), V6)
	@objcopy --rename-section .altinstructions_v6=.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --rename-section .rela.altinstructions_v6=.rela.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
else ifeq ($(GPFS_ALTERNATIVE_VER), V4)
	@objcopy --rename-section .altinstructions_v4=.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --rename-section .rela.altinstructions_v4=.rela.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
else ifeq ($(GPFS_ALTERNATIVE_VER), V3)
	@objcopy --rename-section .altinstructions_v3=.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --rename-section .rela.altinstructions_v3=.rela.altinstructions $(PWD)/$(MMFSNAME) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
endif
ifdef KLDGCCNAME
	@$(CP) -fp $(KLDGCCPATH) $(PWD)/$(KLDGCCNAME)
	@echo "" > $(PWD)/.$(KLDGCCNAME).cmd
endif

modules: pre-kbuild2 Headers
	@echo "Invoking Kbuild..."
	$(MAKE) -C $(KERNEL_BUILD_DIR) ARCH=$(GPFS_ARCH) M=$(PWD) CONFIGDIR=$(CONFIGDIR) $(CCOPT) ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
ifeq ($(GPFS_ARCH), powerpc)
	@objcopy --remove-section=__ex_table_v1 $(PWD)/mmfs26.ko ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --remove-section=__ex_table_v2 $(PWD)/mmfs26.ko ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --remove-section=__ex_table_v1 $(PWD)/tracedev.ko ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
	@objcopy --remove-section=__ex_table_v2 $(PWD)/tracedev.ko ; \
	if [ $$? -ne 0 ]; then \
		exit 1;\
	fi
endif

clean: Clean
Clean: pre-kbuild1
	$(MAKE) -C $(KERNEL_BUILD_DIR_TO_CLEAN) M=$(PWD) clean
ifeq ($(DONOT_REMOVE_KERNEL_MOD), TRUE)
	@echo "Skip cleanup kernel modules ..."
else ifeq ($(BLDMODE), GPL)
	$(RM) -f /lib/modules/`cat $(MODPATH)`/extra/tracedev.ko
	$(RM) -f /lib/modules/`cat $(MODPATH)`/extra/mmfslinux.ko
	$(RM) -f /lib/modules/`cat $(MODPATH)`/extra/mmfs26.ko
	$(RM) -f $(LPPBIN)/lxtrace-`cat $(MODPATH)`
else
	$(RM) -f $(LPPBIN)/mmfs26.ko	
	$(RM) -f $(LPPBIN)/mmfslinux.ko
	$(RM) -f $(LPPBIN)/tracedev.ko
	$(RM) -f $(DESTDIR)/bin/lxtrace-`cat $(MODPATH)`
endif
	$(RM) -f *.o .depends .*.cmd *.ko *.a *.mod.c core *_shipped *map *mod.c.saved *.symvers *.ko.ver ./*.ver install.he
	$(RM) -rf usr

install: $(INSTALLMOD)
	$(INSTALL) -c -m 0500 lxtrace $(DESTDIR)/bin/lxtrace-`cat $(MODPATH)`
install_mod:
	$(INSTALL) -c -m 0500 mmfs26.ko $(LPPBIN)
	$(INSTALL) -c -m 0500 mmfslinux.ko $(LPPBIN)
	$(INSTALL) -c -m 0500 tracedev.ko $(LPPBIN)

Install: modules install
InstallImages: pre-kbuild1
	@$(MAKE) -C $(KERNEL_BUILD_DIR) ARCH=$(GPFS_ARCH) M=$(PWD) CONFIGDIR=$(CONFIGDIR) $(CCOPT) > /dev/null 2>&1 ; \
	if [ $$? -ne 0 ]; then \
		echo "Try make World again..."; \
		exit 1;\
	fi
	@/bin/mkdir -p $(INSTALL_MOD_PATH)/lib/modules/`cat $(MODPATH)`/extra
	@$(CP) mmfs26.ko $(INSTALL_MOD_PATH)/lib/modules/`cat $(MODPATH)`/extra
	@$(CP) mmfslinux.ko $(INSTALL_MOD_PATH)/lib/modules/`cat $(MODPATH)`/extra
	@$(CP) tracedev.ko $(INSTALL_MOD_PATH)/lib/modules/`cat $(MODPATH)`/extra
	@$(INSTALL) -c -m 0500 lxtrace $(LPPBIN)/lxtrace-`cat $(MODPATH)`
	@if [ -x /sbin/depmod ]; then /sbin/depmod -A `uname -r`;  fi;
Depend::
Libs::
InstallLibs::
Makefiles::
subdirMakefiles::
	#dummy target for backward compatibility
