# 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

DBG=${LWP_DBG}
OPTMZ=${LWP_OPTMZ}

LIBOBJS=lwp.o process.o iomgr.o timer.o \
	threadname.o AFS_component_version_number.o

LT_objs = waitkey.lo fasttime.lo lock.lo
LT_deps = $(top_builddir)/src/opr/liboafs_opr.la
LT_libs = $(MT_LIBS)

all: liboafs_lwpcompat.la liblwpcompat_pic.la \
	${TOP_LIBDIR}/liblwp.a depinstall

depinstall: ${TOP_INCDIR}/lock.h ${TOP_INCDIR}/lwp.h ${TOP_INCDIR}/timer.h

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

${TOP_INCDIR}/lock.h: lock.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/lwp.h: lwp.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/timer.h: timer.h
	${INSTALL_DATA} $? $@

#
# Build rules
#
liblwp.a: ${LIBOBJS} $(LT_objs)
	-$(RM) -f $@
	$(AR) crv $@ ${LIBOBJS} .lwp/waitkey.o .lwp/fasttime.o .lwp/lock.o
	$(RANLIB) $@

liboafs_lwpcompat.la: liboafs_lwpcompat.la.sym $(LT_objs) $(LT_deps)
	$(LT_LDLIB_shlib) $(LT_objs) $(LT_deps) $(LT_libs)

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

# This is required so sgi_64+ does not try to build this file.
process.default.s:
	@echo Nothing to be done for process.default.s

# Making process.o for $(SYS_NAME)
# The files "process.s" and "process.S" may be used internally,
# but will be removed after building the .o
CFLAGS_process.o = @CFLAGS_NODANGLING_POINTER@
process.o	: process.default.s process.i386.s process.amd64.s process.c lwp.o
	@set -x; case "$(SYS_NAME)" in \
	sun4c_51 | sun4c_52 | sun4m_51 | sun4m_52 | sun4c_53 | sun4m_53  | sun4_53 | sun4_52 | sun4_54 | sun4c_54 | sun4m_54 | sun4x_5* | arm_linux* ) \
		$(PATH_CPP) -P -I${TOP_INCDIR} ${srcdir}/process.default.s > process.s; \
		${AS} process.s -o process.o; \
			$(RM) process.s ;; \
	sgi_5* ) \
		cp ${srcdir}/process.default.s process.s ; \
		$(CCOBJ) ${LWP_DBG} ${LWP_OPTMZ} -c -I${TOP_INCDIR} -KPIC -G0  process.s; \
		$(RM) -f process.s ;;\
	sgi_61 | sgi_62 | sgi_63 ) \
		cp ${srcdir}/process.default.s process.s ; \
		$(CCOBJ) ${LWP_DBG} ${LWP_OPTMZ} -c ${XCFLAGS} ${ARCHFLAGS} -I${TOP_INCDIR} -KPIC -G0  process.s; \
		$(RM) -f process.s ;;\
	*_darwin* ) \
		$(RM) -f process.o ; \
		CURRENTBUILDARCH="${ARCHFLAGS}"; \
		if [ -z "$${CURRENTBUILDARCH}" ] ; then \
		    CURRENTBUILDARCH=`$(CC) -dM -E -xc /dev/null | grep 'ppc\|i386\|x86_64\|arm'`; \
		fi; \
		ppc=`echo $${CURRENTBUILDARCH} | grep -q ppc && echo process.ppc.o` ; \
		i386=`echo $${CURRENTBUILDARCH} | grep -q i386 && echo process.i386.o` ; \
		amd64=`echo $${CURRENTBUILDARCH} | grep -q x86_64 && echo process.amd64.o` ; \
		armv6=`echo $${CURRENTBUILDARCH} | grep -q armv6 && echo process.armv6.o` ; \
		armv7=`echo $${CURRENTBUILDARCH} | grep -q armv7 && echo process.armv7.o` ; \
		arm64=`echo $${CURRENTBUILDARCH} | grep -q arm64 && echo process.arm64.o` ; \
		if [ -z "$${ppc}" -a -z "$${i386}" -a -z "$${amd64}" -a -z "$${armv6}" -a -z "$${armv7}" -a -z "$${arm64}" ] ; then \
			if [ `arch` = ppc ] ; then \
				ppc=yes ; \
			else \
				i386=yes ; \
			fi ; \
		fi ; \
		if [ -n "$${armv6}" ] ; then \
			$(CC) -x assembler-with-cpp -c -arch armv6 -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.default.s -o "$${armv6}" ; \
			if [ -z "$${i386}" -a -z "$${amd64}" -a -z "$${ppc}" -a -z "$${armv7}" -a -z "$${arm64}" ] ; then \
				${MV} "$${armv6}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${armv7}" ] ; then \
			$(CC) -x assembler-with-cpp -c -arch armv7 -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.default.s -o "$${armv7}" ; \
			if [ -z "$${i386}" -a -z "$${amd64}" -a -z "$${ppc}" -a -z "$${armv6}" -a -z "$${arm64}" ] ; then \
				${MV} "$${armv7}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${ppc}" ] ; then \
			$(CC) ${LWP_DBG} ${LWP_OPTMZ} -c -arch ppc -I${TOP_INCDIR} ${srcdir}/process.default.s -o "$${ppc}" ; \
			if [ -z "$${i386}" -a -z "$${amd64}" -a -z "$${armv6}" -a -z "$${armv7}" -a -z "$${arm64}" ] ; then \
				${MV} "$${ppc}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${i386}" ] ; then \
			$(CC) -x assembler-with-cpp -c -arch i386 -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.i386.s -o "$${i386}" ; \
			if [ -z "$${ppc}" -a -z "$${amd64}" -a -z "$${armv6}" -a -z "$${armv7}" -a -z "$${arm64}" ] ; then \
				${MV} "$${i386}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${amd64}" ] ; then \
			$(CC) -x assembler-with-cpp -c -arch x86_64 -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.amd64.s -o "$${amd64}" ; \
			if [ -z "$${ppc}" -a -z "$${i386}" -a -z "$${armv6}" -a -z "$${armv7}" -a -z "$${arm64}" ] ; then \
				${MV} "$${amd64}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${arm64}" ] ; then \
			$(CC) ${LWP_DBG} ${LWP_OPTMZ} -c -arch arm64 -I${TOP_INCDIR} -I${srcdir} -I. -I${TOP_OBJDIR}/src/config ${srcdir}/process.c -o "$${arm64}" ; \
			if [ -z "$${ppc}" -a -z "$${i386}" -a -z "$${armv6}" -a -z "$${armv7}" -a -z "$${amd64}" ] ; then \
				${MV} "$${arm64}" process.o ; \
			fi ; \
		fi ; \
		if [ ! -f process.o ] ; then \
			lipo -create $${amd64} $${i386} $${ppc} $${armv6} $${armv7} $${arm64} -output process.o ;\
		fi ;; \
	i386_*bsd*) \
		cp ${srcdir}/process.i386.s process.S ; \
		${CCOBJ} -DIGNORE_STDS_H -E -I${srcdir} -I${TOP_INCDIR} process.S > process.s ; \
		${AS} process.s -o process.o ;  \
		$(RM) -f process.s process.S ;;\
	amd64_fbsd_1??) \
		$(AFS_CCRULE_NOQ) -c ${srcdir}/process.c;; \
	amd64_*bsd*) \
		cp ${srcdir}/process.amd64.s process.S ; \
		${CCOBJ} -DIGNORE_STDS_H -E -I${srcdir} -I${TOP_INCDIR} process.S > process.s ; \
		${AS} process.s -o process.o ;  \
		$(RM) -f process.s process.S ;;\
	ncrx86_* | sunx86_58 | sunx86_59 ) \
		$(PATH_CPP) -P -D__sun -D__i386 -DIGNORE_STDS_H -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.i386.s > process.s; \
		${AS} -o process.o process.s; \
		$(RM) process.s ;; \
	sunx86_510 | sunx86_511) \
		if [ "${ASFLAGS}" = "-m64" ] ; then \
			CURRENTBUILDARCH=amd64 ; \
		else \
			XCPPFLAGS="-D__i386" ; \
			CURRENTBUILDARCH=i386 ; \
		fi ; \
		$(PATH_CPP) $${XCPPFLAGS} -P -D__sun -DIGNORE_STDS_H -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.$${CURRENTBUILDARCH}.s > process.s ; \
		${AS} ${ASFLAGS} -o process.o process.s ; \
		$(RM) process.s ;; \
	alpha_nbsd* ) \
		$(PATH_CPP) -DASSEMBLER -P -I${TOP_INCDIR} ${srcdir}/process.default.s > process.s; \
		${AS} -o process.o process.s; \
		$(RM) process.s ;; \
	hp* | *_linux* | *_umlinux* | sgi_64 | sgi_65 ) \
		$(AFS_CCRULE_NOQ) -c ${srcdir}/process.c;; \
	ia64_hpux11* | hp_ux11* ) \
		$(AFS_CCRULE_NOQ) -c ${srcdir}/process.c;; \
	i386_*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.i386.s > process.s; \
		${AS} process.s -o process.o; \
		$(RM) process.s ;; \
	*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} ${srcdir}/process.default.s > process.s; \
		${AS} process.s -o process.o; \
		$(RM) process.s ;; \
	esac

iomgr.o		: iomgr.c lwp.h timer.h
lwp.o		: lwp.c lwp.h
	case "$(SYS_NAME)" in \
		rs_aix*) \
			$(CCOBJ) ${LWP_DBG} ${LWP_OPTMZ} -I${srcdir} -I. -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR} -c ${srcdir}/lwp.c;; \
		*)  \
			$(AFS_CCRULE_NOQ) ${srcdir}/lwp.c;; \
		esac
lock.lo		: lock.c lock.h lwp.h
timer.o		: timer.c
fasttime.lo	: fasttime.c
threadname.o    : threadname.c

exc_handling.o	: exc_handling.c

#
# tests
#
rw: rw.o liblwp.a $(TOP_LIBDIR)/util.a $(TOP_LIBDIR)/libopr.a
	$(AFS_LDRULE) rw.o liblwp.a $(TOP_LIBDIR)/util.a $(TOP_LIBDIR)/libopr.a

#
# Install targets
#
install: liblwp.a lock.h lwp.h timer.h
	${INSTALL} -d ${DESTDIR}${libdir}
	${INSTALL} -d ${DESTDIR}${includedir}
	${INSTALL_DATA} liblwp.a ${DESTDIR}${libdir}/liblwp.a
	${INSTALL_DATA} ${srcdir}/lock.h ${DESTDIR}${includedir}/lock.h
	${INSTALL_DATA} ${srcdir}/lwp.h ${DESTDIR}${includedir}/lwp.h
	${INSTALL_DATA} ${srcdir}/timer.h ${DESTDIR}${includedir}/timer.h

dest: liblwp.a lock.h lwp.h timer.h
	${INSTALL} -d ${DEST}/lib
	${INSTALL} -d ${DEST}/include
	${INSTALL_DATA} liblwp.a ${DEST}/lib/liblwp.a
	${INSTALL_DATA} ${srcdir}/lock.h ${DEST}/include/lock.h
	${INSTALL_DATA} ${srcdir}/lwp.h ${DEST}/include/lwp.h
	${INSTALL_DATA} ${srcdir}/timer.h ${DEST}/include/timer.h

buildtools: ${TOP_LIBDIR}/liblwp.a depinstall

#
# Misc. targets
#
clean:
	$(LT_CLEAN)
	$(RM) -f *.o *.a core AFS_component_version_number.c

include ../config/Makefile.version
