# Copyright 2000, International Business Machines Corporation and others.
# All Rights Reserved.
# 
# This software has been released under the terms of the IBM Public
# License.  For details, see the LICENSE file in the top-level source
# directory or online at http://www.openafs.org/dl/license10.html

srcdir=@srcdir@

include @TOP_OBJDIR@/src/config/Makefile.config

# OS specific object files:
AFS_OS_OBJS = \
	osi_alloc.o \
	osi_cred.o \
	osi_groups.o \
	osi_inode.o \
	osi_file.o \
	osi_misc.o \
	osi_module.o \
	osi_probe.o \
	osi_sleep.o \
	osi_syscall.o \
	osi_sysctl.o \
	osi_vfsops.o \
	osi_vm.o \
<ppc64_linux26>
	osi_flush.o \
<linux26 linux_26>
	osi_export.o \
	osi_nfssrv.o \
<linux24 linux_24 linux26 linux_26>
	osi_ioctl.o \
	osi_proc.o \
<all>
	osi_vnodeops.o 

AFS_OS_PAGOBJS = \
	osi_alloc.o \
	osi_cred.o \
	osi_groups.o \
	osi_inode.o \
	osi_misc.o \
	osi_probe.o \
	osi_sleep.o \
	osi_syscall.o \
<ppc64_linux26>
	osi_flush.o \
<linux24 linux_24 linux26 linux_26>
	osi_ioctl.o \
<all>
	osi_pag_module.o 

AFS_OS_NFSOBJS =

AFS_OS_NONFSOBJS =

COMMON_KERN_CFLAGS=$(KERN_DBG) $(KERN_OPTMZ) @LINUX_GCC_KOPTS@

COMMON_DEFINES=-D__KERNEL__ -DKERNEL -D_KERNEL -DMODULE ${SMP_DEF}

LINUX_KERNEL_PATH=@LINUX_KERNEL_PATH@

# System specific build commands and flags
<linux26 linux_26>
# All the platform-specific and kernel-related things are provided by
# the kernel build system.  So we should _not_ use COMMON_KERN_CFLAGS!
CCFLAGS = -Wno-strict-prototypes
DEFINES = $(COMMON_DEFINES)

# CFLAGS_* and AFLAGS_* will be copied into the kbuild makefile
CFLAGS_afs_pag_user.o = -DAFS_PAG_MANAGER

<i386_linux22 i386_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) @P5PLUS_KOPTS@
DEFINES = $(COMMON_DEFINES) -DCPU=586

<amd64_linux22 amd64_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -mcmodel=kernel -mno-red-zone
DEFINES = $(COMMON_DEFINES) 

<i386_umlinux22 i386_umlinux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) @P5PLUS_KOPTS@ -Wno-strict-prototypes -I$(LINUX_KERNEL_PATH)/arch/um/include -I$(LINUX_KERNEL_PATH)/arch/um/kernel/tt/include -I$(LINUX_KERNEL_PATH)/arch/um/kernel/skas/include
DEFINES = $(COMMON_DEFINES) -DCPU=586

<i386_umlinux26>
EXTRA_CFLAGS = -I$(LINUX_KERNEL_PATH)/arch/um/include -I$(LINUX_KERNEL_PATH)/arch/um/kernel/tt/include -I$(LINUX_KERNEL_PATH)/arch/um/kernel/skas/include

<alpha_linux_22 alpha_linux_24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -mno-fp-regs -ffixed-8
DEFINES = $(COMMON_DEFINES)

<s390_linux22 s390_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -fsigned-char
DEFINES = $(COMMON_DEFINES) -D__s390__ 

<s390x_linux22 s390x_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -fsigned-char
DEFINES = $(COMMON_DEFINES) -D__s390x__ 

<sparc_linux22 sparc_linux24>
LD = ld -m elf32_sparc
CCFLAGS = $(COMMON_KERN_CFLAGS) -mcpu=v8 -mno-fpu -fcall-used-g5 -fcall-used-g7
DEFINES = $(COMMON_DEFINES) -DCPU=sparc

<sparc64_linux22 sparc64_linux24>
CC = sparc64-linux-gcc
LD = ld -m elf64_sparc
CCFLAGS = $(COMMON_KERN_CFLAGS) \
      -mcpu=ultrasparc -m64 -mno-fpu -mcmodel=medlow -ffixed-g4 -fcall-used-g5 -fcall-used-g7 -Wno-sign-compare
DEFINES = $(COMMON_DEFINES) -DCPU=sparc64

<ppc_linux22 ppc_linux24 ppc64_linux22 ppc64_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -fsigned-char -msoft-float -fno-builtin -ffixed-r2
DEFINES = $(COMMON_DEFINES) -D__powerpc__

<parisc_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -fsigned-char -mno-space-regs \
      -mfast-indirect-calls -mdisable-fpregs -ffunction-sections \
      -march=1.1 -mschedule=7100
DEFINES = $(COMMON_DEFINES) -D__linux__

<ia64_linux24>
CCFLAGS = $(COMMON_KERN_CFLAGS) -ffixed-r13 -mfixed-range=f10-f15,f32-f127 -falign-functions=32 -mb-step
DEFINES = $(COMMON_DEFINES)

<all>
INCLUDES = -I. -I../ -I${TOP_OBJDIR}/src/config \
  -I${TOP_SRCDIR}/rx -I${TOP_OBJDIR}/src/rxstat

CFLAGS = $(CCFLAGS) $(DEFINES) $(INCLUDES)

# Name of directory to hold object files and libraries.
KOBJ = MODLOAD
MPS = @MPS@

# COMPDIRS is called in Makefile.common to do the actual builds.
COMPDIRS=linux_compdirs
INSTDIRS=linux_instdirs
DESTDIRS=linux_destdirs

include Makefile.common

<linux26 linux_26 umlinux26>
LINUX_MODULE_EXT=ko
<all -linux26 -linux_26 -umlinux26>
LINUX_MODULE_EXT=o
<all>

LINUX_MODULE_NAME=
LOCAL_SMP_DEF=

setup:
	-$(RM) -f h net netinet sys rpc
	-ln -fs rx rpc
	for m in ${MPS} ; do \
		KDIR=$(KOBJ)-${LINUX_VERSION}${LINUX_MODULE_NAME}-$$m; \
		mkdir -p $${KDIR}; \
		ln -fs ../Makefile $${KDIR}/Makefile.afs ; \
		ln -fs ../Makefile.common $${KDIR}/Makefile.common; \
		ln -fs ../config $${KDIR}/config; \
	done 
	
# Compile SP and MP clients as requested

${COMPDIRS} ${INSTDIRS} ${DESTDIRS}:
	$(RM) -f h 
	$(RM) -f sys
	$(RM) -f netinet 
	if [ -d ${LINUX_KERNEL_PATH}/include2 ] ; then                  \
	    ln -fs ${LINUX_KERNEL_PATH}/include2/asm/../linux h       ; \
	    ln -fs ${LINUX_KERNEL_PATH}/include2/asm/../linux sys     ; \
	    ln -fs ${LINUX_KERNEL_PATH}/include2/asm/../linux netinet ; \
	else                                                            \
	    ln -fs ${LINUX_KERNEL_PATH}/include/linux h               ; \
	    ln -fs ${LINUX_KERNEL_PATH}/include/linux sys             ; \
	    ln -fs ${LINUX_KERNEL_PATH}/include/linux netinet         ; \
	fi
	$(RM) -f linux 
	ln -fs ${LINUX_KERNEL_PATH}/include/linux linux 
	$(RM) -f net 
	ln -fs ${LINUX_KERNEL_PATH}/include/net net 
	$(RM) -f asm-generic
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-generic asm-generic
	$(RM) -f asm
<parisc_linux22 parisc_linux24 parisc_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-parisc asm
<alpha_linux_22 alpha_linux_24 alpha_linux_26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-alpha asm
<i386_linux22 i386_linux24 i386_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-i386 asm
<i386_umlinux22 i386_umlinux24 i386_umlinux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-um asm
	ln -fs ${LINUX_KERNEL_PATH}/arch/um/include/sysdep
<amd64_linux24 amd64_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-x86_64 asm
<s390_linux22 s390_linux24 s390_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-s390 asm
<s390x_linux22 s390x_linux24 s390x_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-s390x asm
<ppc_linux22 ppc_linux24 ppc_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-ppc asm 
<ppc64_linux24 ppc64_linux26>
	ln -s ${LINUX_KERNEL_PATH}/include/asm-ppc64 asm 
<sparc_linux22 sparc_linux24 sparc_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-sparc asm
<sparc64_linux22 sparc64_linux24 sparc64_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-sparc64 asm
<ia64_linux24 ia64_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-ia64 asm
<all>
	if [ "${LINUX_KERNEL_PACKAGING}" ] ; then \
	    TARGPREFIX="pack_"; \
	fi ; \
	for m in ${MPS} ; do \
		KDIR=${KOBJ}-${LINUX_VERSION}${LINUX_MODULE_NAME}-$$m ; \
		echo Building in directory: $${KDIR} ; \
		if [ "$$m" = "MP" ] ; then \
			SMP_DEF="-DAFS_SMP @RHCONFIG_MP@ ${LOCAL_SMP_DEF}" ; \
			TARG="libafs.mp" ; \
		elif [ "$$m" = "EP" ] ; then \
			SMP_DEF="-DAFS_SMP @RHCONFIG_MP@ ${LOCAL_SMP_DEF}" ; \
			TARG="libafs.ep" ; \
		elif [ "$$m" = "BM" ] ; then \
			SMP_DEF="-DAFS_SMP @RHCONFIG_MP@ ${LOCAL_SMP_DEF}" ; \
			TARG="libafs.bm" ; \
		else  \
			SMP_DEF="@RHCONFIG_SP@ ${LOCAL_SMP_DEF}" ; \
			TARG=libafs ; \
		fi ; \
		cd $${KDIR} ; \
		$(MAKE) -f Makefile.afs SMP_DEF="$${SMP_DEF}" $@_$${TARGPREFIX}$${TARG} CLIENT=${LINUX_VERSION}${LINUX_MODULE_NAME} KDIR=$${KDIR} || exit $$?; \
		cd ../ ; \
<all>
	done

linux_compdirs_libafs: depsrcs libafs
linux_compdirs_libafs.mp: depsrcs libafs.mp
linux_compdirs_libafs.ep: depsrcs libafs.ep
linux_compdirs_libafs.bm: depsrcs libafs.bm
linux_instdirs_libafs: install_libafs
linux_instdirs_libafs.mp: install_libafs.mp
linux_instdirs_libafs.ep: install_libafs.ep
linux_instdirs_libafs.bm: install_libafs.bm
linux_destdirs_libafs: dest_libafs
linux_destdirs_libafs.mp: dest_libafs.mp
linux_destdirs_libafs.ep: dest_libafs.ep
linux_destdirs_libafs.bm: dest_libafs.bm

linux_compdirs_pack_libafs: depsrcs pack_libafs
linux_instdirs_pack_libafs: install_pack_libafs
linux_destdirs_pack_libafs: dest_pack_libafs

# Below this line are targets when in the COMMON directory:
# For Linux there is no kernel NFS server.
LIBAFS = ${LINUX_LIBAFS_NAME}-${CLIENT}.${LINUX_MODULE_EXT}
LIBAFS_MP = ${LINUX_LIBAFS_NAME}-${CLIENT}.mp.${LINUX_MODULE_EXT}
LIBAFS_EP = ${LINUX_LIBAFS_NAME}-${CLIENT}.ep.${LINUX_MODULE_EXT}
LIBAFS_BM = ${LINUX_LIBAFS_NAME}-${CLIENT}.bm.${LINUX_MODULE_EXT}

INST_LIBAFS = ${DESTDIR}${afskerneldir}/${LIBAFS}
INST_LIBAFS_MP = ${DESTDIR}${afskerneldir}/${LIBAFS_MP}
INST_LIBAFS_EP = ${DESTDIR}${afskerneldir}/${LIBAFS_EP}
INST_LIBAFS_BM = ${DESTDIR}${afskerneldir}/${LIBAFS_BM}

DEST_LIBAFS = ${DEST}/root.client/usr/vice/etc/modload/${LIBAFS}
DEST_LIBAFS_MP = ${DEST}/root.client/usr/vice/etc/modload/${LIBAFS_MP}
DEST_LIBAFS_EP = ${DEST}/root.client/usr/vice/etc/modload/${LIBAFS_EP}
DEST_LIBAFS_BM = ${DEST}/root.client/usr/vice/etc/modload/${LIBAFS_BM}

SYSTEM_LIBAFS = /lib/modules/${CLIENT}/extra/${LINUX_LIBAFS_NAME}/${LINUX_LIBAFS_NAME}.ko

INST_SYSTEM = ${DESTDIR}${SYSTEM_LIBAFS}
DEST_SYSTEM = ${DEST}/root.client${SYSTEM_LIBAFS}

libafs:	$(LIBAFS) 
	echo SP Build Complete

libafs.mp: $(LIBAFS_MP)
	echo MP Build Complete

libafs.ep: $(LIBAFS_EP)
	echo EP Build Complete

libafs.bm: $(LIBAFS_BM)
	echo BM Build Complete

<linux26 linux_26 umlinux26>
pack_libafs: ${LINUX_LIBAFS_NAME}.ko
	echo "Packaging Friendly Build Complete"

${LIBAFS} ${LIBAFS_MP} ${LIBAFS_EP} ${LIBAFS_BM}: ${LINUX_LIBAFS_NAME}.ko
	cp ${LINUX_LIBAFS_NAME}.ko $@

.FORCE:
${LINUX_LIBAFS_NAME}.ko afspag.ko: .FORCE
	env EXTRA_CFLAGS="${EXTRA_CFLAGS}" @TOP_SRCDIR@/libafs/make_kbuild_makefile.pl ${KDIR} $@ @TOP_OBJDIR@/src/config/Makefile.config Makefile.afs Makefile.common
	env EXTRA_CFLAGS="${EXTRA_CFLAGS}" $(MAKE) -C ${LINUX_KERNEL_PATH} M=@TOP_OBJDIR@/src/libafs/${KDIR} modules
        
<all -linux26 -linux_26 -umlinux26>
${LIBAFS}: $(AFSAOBJS) $(AFSNONFSOBJS)
	$(RM) -f $@
	$(LD) -r -o $@ $(AFSAOBJS) $(AFSNONFSOBJS)

${LIBAFS_MP}: $(AFSAOBJS) $(AFSNONFSOBJS)
	$(RM) -f $@
	$(LD) -r -o $@ $(AFSAOBJS) $(AFSNONFSOBJS)

${LIBAFS_EP}: $(AFSAOBJS) $(AFSNONFSOBJS)
	$(RM) -f $@
	$(LD) -r -o $@ $(AFSAOBJS) $(AFSNONFSOBJS)

${LIBAFS_BM}: $(AFSAOBJS) $(AFSNONFSOBJS)
	$(RM) -f $@
	$(LD) -r -o $@ $(AFSAOBJS) $(AFSNONFSOBJS)
<all>

install_libafs:	$(INST_LIBAFS) 
	echo SP Install Complete

install_libafs.mp: $(INST_LIBAFS_MP)
	echo MP Install Complete

install_libafs.ep: $(INST_LIBAFS_EP)
	echo EP Install Complete

install_libafs.bm: $(INST_LIBAFS_BM)
	echo BM Install Complete

install_pack_libafs: $(INST_SYSTEM)
	echo Packinging Friendly Install Complete

dest_libafs:	$(DEST_LIBAFS) 
	echo SP Install Complete

dest_libafs.mp: $(DEST_LIBAFS_MP)
	echo MP Install Complete

dest_libafs.ep: $(DEST_LIBAFS_EP)
	echo EP Install Complete

dest_libafs.bm: $(DEST_LIBAFS_BM)
	echo BM Install Complete

dest_pack_libafs: $(DEST_SYSTEM)
	echo Packaging Friendly Install Complete

$(INST_LIBAFS): $(LIBAFS)
	${INSTALL} -f $? $@

$(INST_LIBAFS_MP): $(LIBAFS_MP)
	${INSTALL} -f $? $@

$(INST_LIBAFS_EP): $(LIBAFS_EP)
	${INSTALL} -f $? $@

$(INST_LIBAFS_BM): $(LIBAFS_BM)
	${INSTALL} -f $? $@

$(INST_SYSTEM) $(DEST_SYSTEM): $(LINUX_LIBAFS_NAME).ko
	${INSTALL} -f $? $@

$(DEST_LIBAFS): $(LIBAFS)
	${INSTALL} -f $? $@

$(DEST_LIBAFS_MP): $(LIBAFS_MP)
	${INSTALL} -f $? $@

$(DEST_LIBAFS_EP): $(LIBAFS_EP)
	${INSTALL} -f $? $@

$(DEST_LIBAFS_BM): $(LIBAFS_BM)
	${INSTALL} -f $? $@
