# 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
#
# Portions Copyright (c) 2003 Apple Computer, Inc.

srcdir=@srcdir@
include @TOP_OBJDIR@/src/config/Makefile.config
include @TOP_OBJDIR@/src/config/Makefile.lwp
include @TOP_OBJDIR@/src/config/Makefile.lwptool

SFLAGS=-I${TOP_INCDIR}
LIBS=libsys.a \
     ${TOP_LIBDIR}/librx.a \
     libsys.a \
     ${TOP_LIBDIR}/liblwp.a \
     $(TOP_LIBDIR)/libafsutil.a \
     ${TOP_LIBDIR}/libopr.a \
     ${TOP_LIBDIR}/libafshcrypto_lwp.a \
     ${XLIBS}

LT_objs = afssyscalls.lo setpag.lo pioctl.lo glue.lo \
	 rmtsysnet.lo rmtsysc.lo rmtsys.cs.lo rmtsys.xdr.lo rmtsys.ss.lo \
	 rmtsyss.lo
LT_deps = $(top_builddir)/src/rx/liboafs_rx.la

all: \
    liboafs_sys.la \
    libafsrpc_sys.la \
    libsys_pic.la \
    rmtsysd \
    ${TOP_INCDIR}/afs/afssyscalls.h \
    ${TOP_INCDIR}/afs/afs.exp \
    ${TOP_INCDIR}/afs/xfsattrs.h \
    ${TOP_INCDIR}/afs/sys_prototypes.h \
    ${TOP_LIBDIR}/afs.exp \
    ${TOP_LIBDIR}/libsys.a \
    ${KERNELDIR}/afs/xfsattrs.h \
    ${UKERNELDIR}/afs/afsl.exp

generated: \
	rmtsys.cs.c rmtsys.ss.c rmtsys.xdr.c rmtsys.h \
	Krmtsys.cs.c Krmtsys.xdr.c Krmtsys.h

${TOP_INCDIR}/afs/afssyscalls.h: afssyscalls.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/afs/rmtsys.h: rmtsys.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/afs/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL_DATA} $? $@ ;; \
	esac

${TOP_LIBDIR}/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL_DATA} $? $@ ;; \
	esac

${TOP_INCDIR}/afs/xfsattrs.h: xfsattrs.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/afs/sys_prototypes.h: sys_prototypes.h
	${INSTALL_DATA} $? $@

${TOP_LIBDIR}/libsys.a: libsys.a
	${INSTALL_DATA} $? $@

${UKERNELDIR}/afs/afsl.exp: afsl.exp
	${INSTALL} -d ${UKERNELDIR}/afs
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL_DATA} $? $@ ;; \
	esac

${KERNELDIR}/afs/xfsattrs.h: xfsattrs.h
	${INSTALL} -d ${KERNELDIR}/afs
	${INSTALL_DATA} $? $@

depinstall: \
	${TOP_INCDIR}/afs/afssyscalls.h \
	${TOP_INCDIR}/afs/rmtsys.h \
	${TOP_INCDIR}/afs/afs.exp \
	${TOP_INCDIR}/afs/xfsattrs.h \
	${TOP_INCDIR}/afs/sys_prototypes.h \
	${TOP_LIBDIR}/afs.exp \
	${KERNELDIR}/afs/xfsattrs.h \
	${UKERNELDIR}/afs/afsl.exp \
	Krmtsys.cs.c Krmtsys.h Krmtsys.xdr.c rmtsys.h

libsys.a: $(LT_objs) afsl.exp syscall.lo
	@set -e; set -x; case "$(SYS_NAME)" in \
	rs_aix*) \
	    $(LT_LDLIB_lwp_NOQ) $(LT_objs) syscall.o afsl.exp ;; \
	*) \
	    $(LT_LDLIB_lwp_NOQ) $(LT_objs) syscall.o ;; \
	esac

# On AIX, liboafs_sys uses lsetpag() and lpioctl() which are syscall stubs,
# we need to include afsl.exp in order to link against them
liboafs_sys.la: liboafs_sys.la.sym $(LT_objs) $(LT_deps) afsl.exp
	@set -e; set -x; case "$(SYS_NAME)" in \
	rs_aix*) \
	    $(LT_LDLIB_shlib) -Wl,-bI:afsl.exp $(LT_objs) $(LT_deps) ;\
	    $(AR) crv .libs/liboafs_sys.a ../sys/afsl.exp ;; \
	*) \
	    $(LT_LDLIB_shlib) $(LT_objs) $(LT_deps) ;; \
	esac

libafsrpc_sys.la: syscall.lo
	$(LT_LDLIB_pic) syscall.lo

libsys_pic.la: $(LT_objs)
	$(LT_LDLIB_pic) $(LT_objs)

tests:	fixit iinc idec icreate iopen istat rmtsysd

syscall.lo: syscall.s
	@set -e; set -x; case "$(SYS_NAME)" in \
	sgi_*) \
	        ${CC} ${AFS_CFLAGS} -c ${srcdir}/syscall.s; \
		;; \
	rs_aix* | hp_ux10*) \
		$(PATH_CPP) -P ${SFLAGS} ${srcdir}/syscall.s > syscall.ss; \
		as -o syscall.o syscall.ss; \
		$(RM) syscall.ss; \
		;; \
	*) \
		touch syscall.c ; \
		$(CC) $(AFS_CFLAGS) -c syscall.c -o syscall.o; \
		;; \
	esac
	rm -f $@
	echo "# Generated by libtool (GNU libtool) 0.0.0" > $@
	echo "pic_object='syscall.o'" >> $@
	echo "non_pic_object='syscall.o'" >> $@

afssyscalls.lo: afssyscalls.c afssyscalls.h
glue.lo: glue.c afssyscalls.h
setpag.lo: setpag.c afssyscalls.h
pioctl.lo: pioctl.c afssyscalls.h

rmtsysnet.lo rmtsysc.lo rmtsyss.lo rmtsysd.lo: rmtsys.h
rmtsysd: AFS_component_version_number.o afs.exp afsl.exp

rmtsys.cs.c: rmtsys.xg
	${RXGEN} -A -C -o $@ ${srcdir}/rmtsys.xg

rmtsys.ss.c: rmtsys.xg
	${RXGEN} -A -S -o $@ ${srcdir}/rmtsys.xg

rmtsys.xdr.c: rmtsys.xg
	${RXGEN} -A -c -o $@ ${srcdir}/rmtsys.xg

rmtsys.h: rmtsys.xg
	${RXGEN} -A -h -o $@ ${srcdir}/rmtsys.xg

Krmtsys.cs.c: rmtsys.xg Krmtsys.h
	${RXGEN} -A -k -C -o Krmtsys.cs.c ${srcdir}/rmtsys.xg

Krmtsys.xdr.c: rmtsys.xg
	${RXGEN} -A -k -c -o Krmtsys.xdr.c ${srcdir}/rmtsys.xg

Krmtsys.h: rmtsys.xg
	${RXGEN} -A -k -h -o Krmtsys.h ${srcdir}/rmtsys.xg

rmtsysd: rmtsysd.o libsys.a
	$(AFS_LDRULE) rmtsysd.o ${LIBS} $(LIB_roken)



#
# Test programs.
#

iinc:	iinc.c
	$(CC) -o iinc $(AFS_CFLAGS) ${srcdir}/iinc.c ${LIBS}

idec:	idec.c  AFS_component_version_number.c
	$(CC) -o idec $(AFS_CFLAGS) ${srcdir}/idec.c ${LIBS}

icreate:icreate.c  AFS_component_version_number.c
	$(CC) -o icreate $(AFS_CFLAGS) ${srcdir}/icreate.c ${LIBS}

iopen:	iopen.c  AFS_component_version_number.c
	$(CC) -o iopen $(AFS_CFLAGS) ${srcdir}/iopen.c ${LIBS}

iread:	iread.c  AFS_component_version_number.c
	${CC} -o iread $(AFS_CFLAGS) ${srcdir}/iread.c ${LIBS}

iwrite:	iwrite.c  AFS_component_version_number.c
	${CC} -o iwrite $(AFS_CFLAGS) ${srcdir}/iwrite.c ${LIBS}

istat:	istat.c  AFS_component_version_number.c
	$(CC) -o istat $(AFS_CFLAGS) ${srcdir}/istat.c ${LIBS}

fixit:	fixit.c AFS_component_version_number.c
	$(CC) -o fixit $(AFS_CFLAGS) ${srcdir}/fixit.c ${LIBS}


xfsinode: xfsinode.c  AFS_component_version_number.c
	@set -x; case "${SYS_NAME}" in \
		sgi_62 | sgi_64 ) \
		$(CC) -o xfsinode $(AFS_CFLAGS) ${srcdir}/xfsinode.c ${LIBS}
	esac

afs.exp: ${srcdir}/afs4.exp ${srcdir}/afs5.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix[56]* ) \
			cp -p ${srcdir}/afs5.exp afs.exp ;; \
		rs_aix* ) \
			cp -p ${srcdir}/afs4.exp afs.exp ;; \
		* ) \
			touch afs.exp ;; \
	esac

afsl.exp: ${srcdir}/afsl4.exp ${srcdir}/afsl5.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix[56]* ) \
			cp -p ${srcdir}/afsl5.exp afsl.exp ;; \
		rs_aix* ) \
			cp -p ${srcdir}/afsl4.exp afsl.exp ;; \
		* ) \
			touch afsl.exp ;; \
	esac

#
# Installation targets
#
install: libsys.a rmtsysd afssyscalls.h afs.exp xfsattrs.h
	${INSTALL} -d ${DESTDIR}${libdir}/afs
	${INSTALL} -d ${DESTDIR}${sbindir}
	${INSTALL} -d ${DESTDIR}${includedir}/afs
	${INSTALL_DATA} libsys.a ${DESTDIR}${libdir}/afs/libsys.a
	${INSTALL_PROGRAM} rmtsysd ${DESTDIR}${sbindir}/rmtsysd
	${INSTALL_DATA} ${srcdir}/afssyscalls.h ${DESTDIR}${includedir}/afs/afssyscalls.h
	${INSTALL_DATA} ${srcdir}/xfsattrs.h ${DESTDIR}${includedir}/afs/xfsattrs.h
	@set -x; case ${SYS_NAME} in \
	rs_aix*) \
		${INSTALL_DATA} afs.exp ${DESTDIR}${includedir}/afs/afs.exp;; \
	esac

dest: libsys.a rmtsysd afssyscalls.h afs.exp xfsattrs.h
	${INSTALL} -d ${DEST}/lib/afs
	${INSTALL} -d ${DEST}/etc
	${INSTALL} -d ${DEST}/bin
	${INSTALL} -d ${DEST}/include/afs
	${INSTALL_DATA} libsys.a ${DEST}/lib/afs/libsys.a
	${INSTALL_PROGRAM} rmtsysd ${DEST}/etc/rmtsysd
	${INSTALL_DATA} ${srcdir}/afssyscalls.h ${DEST}/include/afs/afssyscalls.h
	${INSTALL_DATA} ${srcdir}/xfsattrs.h ${DEST}/include/afs/xfsattrs.h
	@set -x; case ${SYS_NAME} in \
	rs_aix*) \
		${INSTALL_DATA} afs.exp ${DEST}/include/afs/afs.exp ;; \
	esac

#
# Misc targets
#
include ../config/Makefile.version

clean:
	$(LT_CLEAN)
	$(RM) -f *.o libsys.a xfsinode iinc idec icreate iopen istat core \
	rmtsysc rmtsyss *.o rmtsys.ss.c rmtsys.cs.c rmtsys.xdr.c rmtsys.h \
	rmtsysd AFS_component_version_number.c syscall.c \
	afs.exp afsl.exp libafssetpag.* Krmtsys.cs.c Krmtsys.h Krmtsys.xdr.c
