# 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

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

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}/libcom_err.a \
	 ${TOP_LIBDIR}/libcmd.a \
	 ${TOP_LIBDIR}/libkauth.a \
	 ${TOP_LIBDIR}/libdes.a \
	 ${TOP_LIBDIR}/librx.a \
	 ${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/liblwp.a \
	 ${TOP_LIBDIR}/libaudit.a \
	 $(TOP_LIBDIR)/libafsutil.a

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

LIBS = ${FSLIBS} 

all: fs up fstrace cmdebug livesys kdump-build

#
# Build targets
#
cacheout: cacheout.o
	$(CC) ${CFLAGS} -o cacheout cacheout.o ${LIBS} ${XLIBS} ${CMLIBS}

cacheout.o: cacheout.c

${DEST}/bin/fs ${DEST}/root.server/usr/afs/bin/fs: fs
	${INSTALL} $? $@

${DEST}/bin/livesys: livesys
	${INSTALL} $? $@

${DEST}/bin/up: up
	${INSTALL} $? $@

${DEST}/etc/fstrace: fstrace
	${INSTALL} $? $@

${DEST}/bin/cmdebug: cmdebug
	${INSTALL} $? $@

${DEST}/bin/dedebug: dedebug
	${INSTALL} $? $@


up.o: up.c AFS_component_version_number.c

up: up.o
	${CC} ${CFLAGS} -o up up.o ${LIBS} ${XLIBS}

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

fs: fs.o $(LIBS)
	${CC} ${CFLAGS} -o fs fs.o ${TOP_LIBDIR}/libprot.a $(LIBS) ${XLIBS}

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

livesys: livesys.c $(LIBS)
	${CC} ${CFLAGS} -o livesys ${srcdir}/livesys.c $(LIBS) ${XLIBS}

twiddle: twiddle.c $(LIBS)
	${CC} ${CFLAGS} -o twiddle ${srcdir}/twiddle.c $(LIBS) ${XLIBS}

gcpags: gcpags.c $(LIBS)
	${CC} ${CFLAGS} -o gcpags ${srcdir}/gcpags.c $(LIBS) ${XLIBS}

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

whatfid: whatfid.o ${LIBS}
	${CC} ${CFLAGS} -o whatfid whatfid.o ${LIBS} ${XLIBS}

fstrace.o: fstrace.c AFS_component_version_number.c
	case ${SYS_NAME} in \
		sun4_411 | sun4c_411 | sun4m_412 ) \
			${CCXPG2} ${DBG} ${OPTMZ} -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c ${srcdir}/fstrace.c ;; \
		sun*_4* ) \
			${CC} ${DBG} ${OPTMZ} -I/usr/xpg2include -I/usr/5include -g -I${TOP_SRCDIR}/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c fstrace.c ;; \
		* ) \
			${CC} ${CFLAGS} -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c ${srcdir}/fstrace.c ;; \
	esac

fstrace: fstrace.o
	case ${SYS_NAME} in \
		pmax_ul43 | pmax_ul43a ) \
			${CC} ${CFLAGS} -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a /usr/lib/libi.a ;; \
		sun4_411 | sun4c_411 | sun4m_412 ) \
			${CCXPG2} ${CFLAGS} -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ;; \
		sun*_4* ) \
			${CC} -L/usr/xpg2lib -L/usr/5lib ${CFLAGS} -g -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a -lxpg ;; \
		hp700_ux100 | hp800_ux100 | hp?00_ux10? | hp_ux10? | hp_ux11?) \
			${CC} -I${TOP_OBJDIR}/src/config  -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ;; \
		* ) \
			${CC} ${CFLAGS} -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ;; \
	esac

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

cmdebug: cmdebug.o ${CMLIBS}
	$(CC) -o cmdebug cmdebug.o ${CFLAGS} ${CMLIBS} ${XLIBS}

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

dedebug: dedebug.o ${CMLIBS}
	$(CC) -o dedebug dedebug.o ${CFLAGS} ${CMLIBS} ${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 ;; \
	*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} ;; \
	*nbsd*) 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 \
		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} ${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 \
			;; \
		*nbsd*) \
			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}

#
# kdump install targets
#
${DEST}/etc/kdump: kdump-build
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALLex} -f ${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} $$f ${DEST}/etc/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALLex} -f ${srcdir}/kdump.sh.solaris7 ${DEST}/etc/kdump; \
		${INSTALL} -f ${srcdir}/kdump ${DEST}/etc/kdump32;; \
	*alpha_linux* ) \
		${INSTALLex} -f ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL} kdump-alpha_linux-${LINUX_VERSION} $@-${LINUX_VERSION} ;; \
	*linux* ) \
		${INSTALLex} -f ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL} kdump-linux-${LINUX_VERSION} $@-${LINUX_VERSION} ;; \
	hp_ux11* ) \
		${INSTALLex} -f ${srcdir}/kdump.sh.hp_ux11 ${DEST}/etc/kdump; \
		${INSTALL} -f $? ${DEST}/etc/kdump32;; \
	*nbsd*) \
		;; \
	*) \
		${INSTALL} $? $@ ;; \
	esac

${DEST}/etc/kdump64: kdump-build
	if [ -f kdump64 ]; then \
		${INSTALL} kdump64 $@; \
	fi


#
# Install targets
#
install: \
	${DESTDIR}${bindir}/fs \
	${DESTDIR}${bindir}/livesys \
	${DESTDIR}${afssrvbindir}/fs \
	${DESTDIR}${bindir}/up \
	${DESTDIR}${sbindir}/fstrace \
	${DESTDIR}${bindir}/cmdebug \
	${DESTDIR}${sbindir}/kdump \
	${DESTDIR}${sbindir}/kdump64

#
# Misc targets
#

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

test:
	cd test; $(MAKE)

include ../config/Makefile.version

${DESTDIR}${bindir}/fs: fs
	${INSTALL} $? $@

${DESTDIR}${bindir}/livesys: livesys
	${INSTALL} $? $@

${DESTDIR}${afssrvbindir}/fs: fs
	${INSTALL} $? $@

${DESTDIR}${bindir}/up: up
	${INSTALL} $? $@

${DESTDIR}${sbindir}/fstrace: fstrace
	${INSTALL} $? $@

${DESTDIR}${bindir}/cmdebug: cmdebug
	${INSTALL} $? $@

${DESTDIR}${bindir}/dedebug: dedebug
	${INSTALL} $? $@

${DESTDIR}${sbindir}/kdump: kdump-build
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALLex} -f 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} $$f ${DESTDIR}${sbindir}/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALLex} -f kdump.sh.solaris7 ${DESTDIR}${sbindir}/kdump; \
		${INSTALL} -f kdump ${DESTDIR}${sbindir}/kdump32;; \
	*alpha_linux* ) \
		${INSTALLex} -f kdump.sh.linux ${DESTDIR}${sbindir}/kdump; \
		${INSTALL} kdump-alpha_linux-${LINUX_VERSION} $@-${LINUX_VERSION} ;; \
	*linux* ) \
		${INSTALLex} -f kdump.sh.linux ${DESTDIR}${sbindir}/kdump; \
		${INSTALL} kdump-linux-${LINUX_VERSION} $@-${LINUX_VERSION} ;; \
	hp_ux11* ) \
		${INSTALLex} -f kdump.sh.hp_ux11 ${DESTDIR}${sbindir}/kdump; \
		${INSTALL} -f kdump ${DESTDIR}${sbindir}/kdump32;; \
	*) \
		${INSTALL} kdump $@ ;; \
	esac

${DESTDIR}${sbindir}/kdump64: kdump-build
	if [ -f kdump64 ]; then \
		${INSTALL} kdump64 $@; \
	fi

dest: \
	${DEST}/bin/fs \
	${DEST}/bin/livesys \
	${DEST}/root.server/usr/afs/bin/fs \
	${DEST}/bin/up \
	${DEST}/etc/fstrace \
	${DEST}/bin/cmdebug \
	${DEST}/etc/kdump \
	${DEST}/etc/kdump64

