# 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
include @TOP_OBJDIR@/src/config/Makefile.lwp

INCLS=${TOP_INCDIR}/afs/afsint.h \
	${TOP_INCDIR}/afs/cmd.h \
	${TOP_INCDIR}/afs/afsutil.h

AFSIO_INCLS=${TOP_INCDIR}/afs/afsint.h \
	${TOP_INCDIR}/afs/cmd.h \
	${TOP_INCDIR}/afs/auth.h \
	${TOP_INCDIR}/afs/vlserver.h \
	${TOP_INCDIR}/afs/ihandle.h \
	${TOP_INCDIR}/afs/com_err.h \
	${TOP_INCDIR}/afs/afscp.h \
	${TOP_INCDIR}/afs/afsutil.h

AFSIO_LIBS=${TOP_LIBDIR}/libafscp.a \
	$(top_builddir)/src/rxkad/liboafs_rxkad.la \
	$(top_builddir)/src/fsint/liboafs_fsint.la \
	$(top_builddir)/src/vlserver/liboafs_vldb.la \
	$(top_builddir)/src/cmd/liboafs_cmd.la \
	$(top_builddir)/src/util/liboafs_util.la \
	$(top_builddir)/src/opr/liboafs_opr.la

FSLIBS=${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/libvldb.a \
	 ${TOP_LIBDIR}/libubik.a \
	 ${TOP_LIBDIR}/vlib.a \
	 ${TOP_LIBDIR}/libauth.a \
	 ${TOP_LIBDIR}/librxkad.a \
	 ${TOP_LIBDIR}/libafscom_err.a \
	 ${TOP_LIBDIR}/libcmd.a \
	 ${TOP_LIBDIR}/libkauth.a \
	 ${TOP_LIBDIR}/librx.a \
	 ${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/liblwp.a \
	 ${TOP_LIBDIR}/libaudit.a \
	 $(TOP_LIBDIR)/libafsutil.a \
	 $(TOP_LIBDIR)/libopr.a \
	 ${TOP_LIBDIR}/libafsrfc3961.a \
	 ${TOP_LIBDIR}/libafshcrypto_lwp.a

CMLIBS=${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/libafsint.a \
	 ${TOP_LIBDIR}/librxkad.a \
	 ${TOP_LIBDIR}/libauth.a \
	 ${TOP_LIBDIR}/libafscom_err.a \
	 ${TOP_LIBDIR}/libcmd.a \
	 ${TOP_LIBDIR}/librx.a \
	 ${TOP_LIBDIR}/libafshcrypto_lwp.a \
	 ${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/liblwp.a \
	 $(TOP_LIBDIR)/libopr.a \
	 $(TOP_LIBDIR)/libafsutil.a

LIBS = ${FSLIBS}

all: fs up fstrace cmdebug livesys kdump-build cacheout afsio

#
# Build targets
#
cacheout: cacheout.o
	$(AFS_LDRULE) cacheout.o ${LIBS} $(LIB_roken) ${XLIBS} ${CMLIBS}

cacheout.o: cacheout.c


up.o: up.c AFS_component_version_number.c

up: up.o
	$(AFS_LDRULE) up.o ${LIBS} $(LIB_roken) ${XLIBS}

fs.o: fs.c ${INCLS} AFS_component_version_number.c

fs: fs.o $(LIBS)
	$(AFS_LDRULE) fs.o ${TOP_LIBDIR}/libprot.a $(LIBS) \
		$(LIB_roken) ${XLIBS}

afsio.o: afsio.c ${AFSIO_INCLS} AFS_component_version_number.c
	${PTH_CCRULE} ${srcdir}/afsio.c
afscbint.ss.o: ../fsint/afscbint.ss.c
	${PTH_CCRULE} ../fsint/afscbint.ss.c

afsio: afsio.o afscbint.ss.o ${AFSIO_LIBS}
	$(LT_LDRULE_static) afsio.o afscbint.ss.o \
		${AFSIO_LIBS} \
		$(LIB_hcrypto) $(LIB_roken) ${MT_LIBS} \
		$(LDFLAGS_krb5) $(LIB_krb5)

livesys.o: livesys.c ${INCLS} AFS_component_version_number.c

livesys: livesys.o $(LIBS)
	$(AFS_LDRULE) livesys.o $(LIBS) $(LIB_roken) ${XLIBS}

twiddle: twiddle.o $(LIBS)
	$(AFS_LDRULE) twiddle.o $(LIBS) $(LIB_roken) ${XLIBS}

gcpags: gcpags.o $(LIBS)
	$(AFS_LDRULE) gcpags.o $(LIBS) $(LIB_roken) ${XLIBS}

whatfid.o: whatfid.c ${INCLS} AFS_component_version_number.c

whatfid: whatfid.o ${LIBS}
	$(AFS_LDRULE) whatfid.o ${LIBS} $(LIB_roken) ${XLIBS}

fstrace: fstrace.o
	$(AFS_LDRULE) fstrace.o $(LIBS) $(LIB_roken) ${XLIBS}

cmdebug.o: cmdebug.c ${INCLS} AFS_component_version_number.c

cmdebug: cmdebug.o ${CMLIBS}
	$(AFS_LDRULE) cmdebug.o ${CMLIBS} $(LIB_roken) ${XLIBS}

dedebug.o: dedebug.c ${INCLS} AFS_component_version_number.c

dedebug: dedebug.o ${CMLIBS}
	$(AFS_LDRULE) dedebug.o ${CMLIBS} $(LIB_roken) ${XLIBS}

#
# Kernel info dumper - these are done with submakes so that
# the build process does not attempt to rebuild them every time it runs.
#

#
# Branching target, run the actual build depending on sysname
#
kdump-build: kdump.c ${INCLS} AFS_component_version_number.c
	$(MAKE) kdump kdump64
	touch kdump-build

#
# Build targets - one for each type of kdump build process we have
#
kdump: kdump.o
	-set -x; \
	case ${SYS_NAME} in \
	sun4c_51 | sun4c_52 | sun4m_51 | sun4m_52 | sun4c_53 | sun4m_53 | sun4_53 | sun4_54 | sun4c_54 | sun4m_54 | sunx86_5? ) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a /usr/lib/libkvm.a -lelf ${XLIBS} ;; \
	sun*_5? | sun*_5?? ) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a  ${XLIBELFA} ${XLIBKVM} ${XLIBS} ;; \
	sgi_6? ) \
		for f in ../libafs/STATIC.IP*/CPU_KDEFS; \
		do	IP=`expr "$$f" : '../libafs/STATIC.\(IP..\)'`; \
			CPU_KDEFS=`sed 's/-mips.//' $$f`; \
			echo IP = $$IP; \
			echo CPU_KDEFS = $$CPU_KDEFS; \
			case $$CPU_KDEFS in \
			*-64*)	${CC} ${XCFLAGS64} \
					$$CPU_KDEFS \
					-o kdump.$$IP kdump.$$IP.o \
					${TOP_LIBDIR}/libcmd64.a -lelf \
				;; \
			*) 	${CC} ${XCFLAGS} ${ARCHFLAGS} \
					$$CPU_KDEFS \
					-o kdump.$$IP kdump.$$IP.o \
					${TOP_LIBDIR}/libcmd.a -lelf \
				;; \
			esac || exit $$? ; \
		done ;; \
	*linux26* | *linux_26* ) \
		touch kdump ;; \
	*alpha_linux* ) \
		$(MAKE) kdump-alpha_linux-@LINUX_VERSION@ ;; \
	*linux* ) \
		$(MAKE) kdump-linux-@LINUX_VERSION@ ;; \
	alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c | alpha_dux??) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a ${XLIBS} -ll -lmld;; \
	ncrx86_* ) ${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a -lelf ${XLIBS} ;; \
	*bsd*) touch kdump ;; \
	* )     ${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a ${XLIBS} ;; \
	esac

kdump64:
	-set -x; \
	case ${SYS_NAME} in \
	sun4x_5[789] | sun4x_510 | hp_ux11* | sunx86_5[789] | sunx86_510 )  \
		$(MAKE) kdump64.o ; \
		${CC} ${XCFLAGS64} -o kdump64 kdump64.o ${TOP_LIBDIR}/libcmd64.a ${XLIBELFA} ${XLIBKVM} ${XLIBS} ;; \
	esac


kdump.o: kdump.c ${INCLS} AFS_component_version_number.c
	-set -x; \
	case ${SYS_NAME} in \
		*linux26* | *linux_26* ) \
			touch kdump.o ;; \
		alpha_linux* ) \
			$(MAKE) kdump-alpha_linux-${LINUX_VERSION}.o ;; \
		*linux* ) \
			$(MAKE) kdump-linux-${LINUX_VERSION}.o ;; \
		alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c| alpha_dux?? ) \
			${CC} ${AFS_CFLAGS} -I/usr/sys/include -I/usr/sys/BINARY -I/usr/sys/AFS -DDEBUGGER -c ${srcdir}/kdump.c ;;\
		sgi_6? ) \
			for f in ../libafs/STATIC.IP*/CPU_KDEFS; \
			do	IP=`expr "$$f" : '../libafs/STATIC.\(IP..\)'`; \
				CPU_KDEFS=`sed 's/-mips.//' $$f`; \
				echo IP = $$IP; \
				echo CPU_KDEFS = $$CPU_KDEFS; \
				case $$CPU_KDEFS in \
				*-64*)	${CC} -D_KMEMUSER -woff 1178 \
						${KERN_DBG} ${KERN_OPTMZ} -I${TOP_INCDIR} \
						-I${TOP_OBJDIR}/src/config \
						$$CPU_KDEFS \
						${XCFLAGS64} \
						-c ${srcdir}/kdump.c -o kdump.$$IP.o \
					;; \
				*)	${CC} -D_KMEMUSER -woff 1178 \
						${KERN_DBG} ${KERN_OPTMZ} -I${TOP_INCDIR} \
						-I${TOP_OBJDIR}/src/config \
						$$CPU_KDEFS \
						${XCFLAGS} ${ARCHFLAGS} -DAFS_32BIT_KERNEL_ENV \
						-c ${srcdir}/kdump.c -o kdump.$$IP.o \
					;; \
				esac || exit $$?; \
			done \
			;; \
		*bsd*) \
			touch kdump.o ;; \
		*) \
			${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${TOP_SRCDIR} -I${TOP_INCDIR}/afs \
			-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src -I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} \
			-o kdump.o -c ${srcdir}/kdump.c ;; \
	esac ;

kdump64.o : kdump.c ${INCLS} AFS_component_version_number.c
	-set -x; \
	case ${SYS_NAME} in \
	sun4x_5[789] | sun4x_510 | hp_ux11* | sunx86_5[789] | sunx86_510 ) \
		${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${TOP_SRCDIR} -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src -I${TOP_INCDIR} ${XCFLAGS64} \
		-o kdump64.o -c ${srcdir}/kdump.c ;; \
	esac

# *linux* - Builds kdump-X.Y.Z according to kernel version
kdump-linux-@LINUX_VERSION@.o: kdump.c ${INCLS} AFS_component_version_number.c
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${LINUX_KERNEL_PATH}/include -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src \
		-I${TOP_OBJDIR}/src/afs/${MKAFS_OSTYPE} \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src/libafs/afs \
		-I${TOP_SRCDIR} -I${TOP_SRCDIR}/afs/${MKAFS_OSTYPE} \
		-I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} -o kdump-linux-${LINUX_VERSION}.o \
		-c ${srcdir}/kdump.c

kdump-linux-@LINUX_VERSION@: kdump-linux-@LINUX_VERSION@.o
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -o kdump-linux-${LINUX_VERSION} kdump-linux-${LINUX_VERSION}.o \
		${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ${XLIBS}

# *alpha_linux* - Builds kdump-X.Y.Z according to kernel version
kdump-alpha_linux-@LINUX_VERSION@.o: kdump.c ${INCLS} AFS_component_version_number.c
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${LINUX_KERNEL_PATH}/include -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src \
		-I${TOP_OBJDIR}/src/afs/${MKAFS_OSTYPE} \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src/libafs/afs \
		-I${TOP_SRCDIR} -I${TOP_SRCDIR}/afs/${MKAFS_OSTYPE} \
		-I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} -mno-fp-regs -ffixed-8 \
		-o kdump-alpha_linux-${LINUX_VERSION}.o -c ${srcdir}/kdump.c ;;

kdump-alpha_linux-@LINUX_VERSION@: kdump-alpha_linux-@LINUX_VERSION@.o
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -o kdump-alpha_linux-${LINUX_VERSION} kdump-alpha_linux-${LINUX_VERSION}.o \
		${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ${XLIBS}

#
# Install targets
#
install: kdump-build fs livesys up fstrace cmdebug afsio
	${INSTALL} -d ${DESTDIR}${bindir}
	${INSTALL} -d ${DESTDIR}${afssrvbindir}
	${INSTALL} -d ${DESTDIR}${sbindir}
	${INSTALL_PROGRAM} fs ${DESTDIR}${bindir}/fs
	${INSTALL_PROGRAM} livesys ${DESTDIR}${bindir}/livesys
	${INSTALL_PROGRAM} fs ${DESTDIR}${afssrvbindir}/fs
	${INSTALL_PROGRAM} up ${DESTDIR}${bindir}/up
	${INSTALL_PROGRAM} fstrace ${DESTDIR}${sbindir}/fstrace
	${INSTALL_PROGRAM} cmdebug ${DESTDIR}${bindir}/cmdebug
	${INSTALL_PROGRAM} afsio ${DESTDIR}${bindir}/afsio ;
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALL_SCRIPT} kdump.sh.sgi_ipnos ${DESTDIR}${sbindir}/kdump; \
		ln -fs kdump ${DESTDIR}${sbindir}/kdump32; \
		ln -fs kdump.IP20 ${DESTDIR}${sbindir}/kdump.IP22; \
		ln -fs kdump.IP20 ${DESTDIR}${sbindir}/kdump.IP32; \
		for f in kdump.IP??; \
			do ${INSTALL_PROGRAM} $$f ${DESTDIR}${sbindir}/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALL_SCRIPT} kdump.sh.solaris7 ${DESTDIR}${sbindir}/kdump32; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump;; \
	*linux26* | *linux_26* ) \
		;; \
	*linux* ) \
		${INSTALL_SCRIPT} kdump.sh.linux ${DESTDIR}${sbindir}/kdump; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	hp_ux11* ) \
		${INSTALL_SCRIPT} kdump.sh.hp_ux11 ${DESTDIR}${sbindir}/kdump; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	*bsd* ) \
		;; \
	*) \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	esac
	if [ -f kdump64 ]; then \
		${INSTALL_PROGRAM} kdump64 ${DESTDIR}${sbindir}/kdump; \
	fi

dest: kdump-build fs livesys up fstrace cmdebug afsio
	${INSTALL} -d ${DEST}/bin
	${INSTALL} -d ${DEST}/etc
	${INSTALL} -d ${DEST}/root.server/usr/afs/bin
	${INSTALL_PROGRAM} fs ${DEST}/bin/fs
	${INSTALL_PROGRAM} livesys ${DEST}/bin/livesys
	${INSTALL_PROGRAM} fs ${DEST}/root.server/usr/afs/bin/fs
	${INSTALL_PROGRAM} up ${DEST}/bin/up
	${INSTALL_PROGRAM} fstrace ${DEST}/etc/fstrace
	${INSTALL_PROGRAM} cmdebug ${DEST}/bin/cmdebug
	${INSTALL_PROGRAM} afsio ${DEST}/bin/afsio
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.sgi_ipnos ${DEST}/etc/kdump; \
		ln -fs kdump ${DEST}/etc/kdump32; \
		ln -fs kdump.IP20 ${DEST}/etc/kdump.IP22; \
		ln -fs kdump.IP20 ${DEST}/etc/kdump.IP32; \
		for f in kdump.IP??; \
			do ${INSTALL_PROGRAM} $$f ${DEST}/etc/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.solaris7 ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} ${srcdir}/kdump ${DEST}/etc/kdump32;; \
	*linux26* | *linux_26* ) \
		;; \
	*alpha_linux* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} kdump-alpha_linux-${LINUX_VERSION} ${DEST}/etc/kdump-${LINUX_VERSION} ;; \
	*linux* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} kdump-linux-${LINUX_VERSION} ${DEST}/etc/kdump-${LINUX_VERSION} ;; \
	hp_ux11* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.hp_ux11 ${DEST}/etc/kdump32; \
		${INSTALL_PROGRAM} kdump-build ${DEST}/etc/kdump ;; \
	*bsd*) \
		;; \
	*) \
		${INSTALL_PROGRAM} kdump-build ${DEST}/etc/kdump ;; \
	esac
	if [ -f kdump64 ]; then \
		${INSTALL_PROGRAM} kdump64 ${DEST}/etc/kdump; \
	fi

#
# Misc targets
#

clean:
	$(LT_CLEAN)
	$(RM) -f *.o *.a up fs kdump-* kdump kdump64 core cmdebug \
		AFS_component_version_number.c fstrace gcpags livesys dedebug \
		cacheout afsio

test:
	cd test; $(MAKE)

include ../config/Makefile.version
