# 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

INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

AFS_OS_CLEAN = .makelog

# OS specific object files:
AFS_OS_OBJS = \
	osi_alloc.o \
	osi_cred.o \
	osi_crypto.o \
	osi_gcpags.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_vcache.o \
	osi_vfsops.o \
	osi_vm.o \
<ppc64_linux26>
	osi_flush.o \
<all>
	osi_export.o \
	osi_nfssrv.o \
	osi_ioctl.o \
	osi_proc.o \
	osi_vnodeops.o \
	osi_pagecopy.o \
	strlcpy-kernel.o

AFS_OS_PAGOBJS = \
	osi_alloc.o \
	osi_cred.o \
	osi_crypto.o \
	osi_gcpags.o \
	osi_groups.o \
	osi_inode.o \
	osi_misc.o \
	osi_probe.o \
	osi_sleep.o \
	osi_syscall.o \
<ppc64_linux26>
	osi_flush.o \
<all>
	osi_ioctl.o \
	osi_pag_module.o \
	strlcpy-kernel.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@
LINUX_KERNEL_BUILD=@LINUX_KERNEL_BUILD@
LINUX_KBUILD_CFLAGS_VAR=@LINUX_KBUILD_CFLAGS_VAR@

CFLAGS_fcrypt.o = -I${TOP_SRCDIR}/rxkad -I$(TOP_OBJDIR)/src/rxkad
CFLAGS_crypt_conn.o = -I${TOP_SRCDIR}/rxkad -I$(TOP_OBJDIR)/src/rxkad
CFLAGS_rxkad_client.o = -I${TOP_SRCDIR}/rxkad -I$(TOP_OBJDIR)/src/rxkad
CFLAGS_rxkad_common.o = -I${TOP_SRCDIR}/rxkad -I$(TOP_OBJDIR)/src/rxkad
CFLAGS_opr_rbtree.o = -I${TOP_SRCDIR}/opr

CFLAGS_evp.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto \
	       -DHAVE_CONFIG_H @CFLAGS_NOCAST_FUNCTION_TYPE@
CFLAGS_evp-algs.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto \
	       @CFLAGS_NOCAST_FUNCTION_TYPE@
CFLAGS_evp-kernel.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_rand-timer-kernel.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_rand-kernel.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_aes.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_rijndael-alg-fst.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto \
			    -DNO_CONFIG_H
CFLAGS_sha.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_sha256-kernel.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_md5.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto
CFLAGS_random.o = -I$(TOP_SRCDIR)/external/heimdal/hcrypto

RFC3961_INCLUDES = -I$(TOP_SRCDIR)/crypto/rfc3961 \
		   -I$(TOP_SRCDIR)/external/heimdal/krb5

CFLAGS_crypto.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-aes.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-context.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-copy.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-ct.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-evp.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-data.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-keyblock.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-store-int.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-random.o = $(RFC3961_INCLUDES)
CFLAGS_crypto-algs.o = $(RFC3961_INCLUDES)
CFLAGS_n-fold.o = $(RFC3961_INCLUDES)

# 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 =
DEFINES = $(COMMON_DEFINES)

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

<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

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

CFLAGS = $(CCFLAGS) $(KERN_DBG) $(DEFINES) $(INCLUDES) @CFLAGS_WERROR@

# 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

${INSTDIRS} ${DESTDIRS}: ${COMPDIRS}
	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 ../ ; \
	done

${COMPDIRS}: setup
	$(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_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-parisc asm
<alpha_linux_26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-alpha asm
<i386_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-i386 asm
<i386_umlinux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-um asm
	ln -fs ${LINUX_KERNEL_PATH}/arch/um/include/sysdep
<amd64_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-x86_64 asm
<s390_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-s390 asm
<s390x_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-s390x asm
<ppc_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-ppc asm 
<ppc64_linux26>
	ln -s ${LINUX_KERNEL_PATH}/include/asm-ppc64 asm 
<sparc_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-sparc asm
<sparc64_linux26>
	ln -fs ${LINUX_KERNEL_PATH}/include/asm-sparc64 asm
<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}

SPARSE_MAKEFLAGS = @SPARSE_MAKEFLAGS@

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_BUILD} M=@TOP_OBJDIR@/src/libafs/${KDIR} $(SPARSE_MAKEFLAGS) modules 2>&1 \
		|| echo "FAILURE: make exit code $$?" ) | tee .makelog
	@if grep -E -q '^(FAILURE|WARNING)' .makelog; then \
		grep -q '^WARNING' .makelog && echo "Error: Undefined symbols in modules" ; \
		rm .makelog ; \
		exit 1 ; \
	fi
	rm .makelog

<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:	$(LIBAFS) 
	${INSTALL} -d ${DESTDIR}${afskerneldir}
	${INSTALL} -m 644 $(LIBAFS) $(INST_LIBAFS)
	echo SP Install Complete

install_pack_libafs: $(LINUX_LIBAFS_NAME).ko
	${INSTALL} -d ${DESTDIR}/lib/modules/${CLIENT}/extra/${LINUX_LIBAFS_NAME}
	${INSTALL} -m 644 $(LINUX_LIBAFS_NAME).ko $(INST_SYSTEM)
	echo Packaging Friendly Install Complete

install_libafs.mp: $(LIBAFS_MP)
	${INSTALL} -d ${DESTDIR}${afskerneldir}
	${INSTALL} -m 644 $(LIBAFS_MP) $(INST_LIBAFS_MP)
	echo MP Install Complete

install_libafs.ep: $(LIBAFS_EP)
	${INSTALL} -d ${DESTDIR}${afskerneldir}
	${INSTALL} -m 644 $(LIBAFS_EP) $(INST_LIBAFS_EP)
	echo EP Install Complete

install_libafs.bm: $(LIBAFS_BM)
	${INSTALL} -d ${DESTDIR}${afskerneldir}
	${INSTALL} -m 644 $(LIBAFS_BM) $(INST_LIBAFS_BM)
	echo BM Install Complete

dest_libafs:	$(LIBAFS) 
	${INSTALL} -d ${DEST}/root.client/usr/vice/etc/modload
	${INSTALL} -m 644 $(LIBAFS) $(DEST_LIBAFS)
	echo SP Install Complete

dest_pack_libafs: $(LINUX_LIBAFS_NAME).ko
	${INSTALL} -d ${DEST}/root.client/lib/modules/${CLIENT}/extra/${LINUX_LIBAFS_NAME}
	${INSTALL} -m 644 $(LINUX_LIBAFS_NAME).ko $(DEST_SYSTEM)
	echo Packaging Friendly Install Complete

dest_libafs.mp: $(LIBAFS_MP)
	${INSTALL} -d ${DEST}/root.client/usr/vice/etc/modload
	${INSTALL} -m 644 $(LIBAFS_MP) $(DEST_LIBAFS_MP)
	echo MP Install Complete

dest_libafs.ep: $(LIBAFS_EP)
	${INSTALL} -d ${DEST}/root.client/usr/vice/etc/modload
	${INSTALL} -m 644 $(LIBAFS_EP) $(DEST_LIBAFS_EP)
	echo EP Install Complete

dest_libafs.bm: $(LIBAFS_BM)
	${INSTALL} -d ${DEST}/root.client/usr/vice/etc/modload
	${INSTALL} -m 644 $(LIBAFS_BM) $(DEST_LIBAFS_BM)
	echo BM Install Complete
