# 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
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

CFLAGS=${LWP_DBG} ${LWP_OPTMZ} ${XCFLAGS} ${ARCHFLAGS} ${COMMON_INCL}

LIBOBJS=lwp.o process.o lock.o iomgr.o timer.o fasttime.o preempt.o \
	waitkey.o threadname.o AFS_component_version_number.o

all: ${TOP_LIBDIR}/liblwp.a depinstall

depinstall: ${TOP_INCDIR}/lock.h ${TOP_INCDIR}/lwp.h ${TOP_INCDIR}/preempt.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}/preempt.h: preempt.h
	${INSTALL_DATA} $? $@

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

#
# Build rules
#
liblwp.a: ${LIBOBJS}
	-$(RM) -f $@
	$(AR) crv $@ ${LIBOBJS}
	$(RANLIB) $@

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

# Making process.o for $(SYS_NAME)
process.o	: process.s process.i386.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.s > process.ss; \
		${AS} process.ss -o process.o; \
			$(RM) process.ss ;; \
	sgi_5* ) \
		$(CCOBJ) ${LWP_DBG} ${LWP_OPTMZ} -c -I${TOP_INCDIR} -KPIC -G0  ${srcdir}/process.s;; \
	sgi_61 | sgi_62 | sgi_63 ) \
		$(CCOBJ) ${LWP_DBG} ${LWP_OPTMZ} -c ${XCFLAGS} ${ARCHFLAGS} -I${TOP_INCDIR} -KPIC -G0  ${srcdir}/process.s;; \
	s390_*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} ${srcdir}/process.s390.s >process.ss; \
		${AS} -ahlns process.ss -o process.o >process.lst; \
		$(RM) process.ss ;; \
	s390x_*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} ${srcdir}/process.s390x.s >process.ss; \
		${AS} -ahlns process.ss -o process.o >process.lst; \
		$(RM) process.ss ;; \
	*_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` ; \
		arm=`echo $${CURRENTBUILDARCH} | grep -q arm && echo process.arm.o` ; \
		if [ -z "$${ppc}" -a -z "$${i386}" -a -z "$${amd64}" -a -z "$${arm}" ] ; then \
			if [ `arch` = ppc ] ; then \
				ppc=yes ; \
			else \
				i386=yes ; \
			fi ; \
		fi ; \
		if [ -n "$${arm}" ] ; then \
			$(CC) ${LWP_DBG} ${LWP_OPTMZ} -c -arch armv6 -I${TOP_INCDIR} ${srcdir}/process.c -o "$${arm}" ; \
			if [ -z "$${i386}" -a -z "$${amd64}" -a -z "$${ppc}" ] ; then \
				${MV} "$${arm}" process.o ; \
			fi ; \
		fi ; \
		if [ -n "$${ppc}" ] ; then \
			$(CC) ${LWP_DBG} ${LWP_OPTMZ} -c -arch ppc -I${TOP_INCDIR} ${srcdir}/process.s -o "$${ppc}" ; \
			if [ -z "$${i386}" -a -z "$${amd64}" -a -z "$${arm}" ] ; 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 "$${arm}" ] ; 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 "$${arm}" ] ; then \
				${MV} "$${amd64}" process.o ; \
			fi ; \
		fi ; \
		if [ ! -f process.o ] ; then \
			lipo -create $${amd64} $${i386} $${ppc} $${arm} -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.ss ; \
		${AS} process.ss -o process.o ;  \
		$(RM) -f process.S ;;\
	amd64_*bsd*) \
		cp ${srcdir}/process.amd64.s process.S ; \
		${CCOBJ} -DIGNORE_STDS_H -E -I${srcdir} -I${TOP_INCDIR} process.S > process.ss ; \
		${AS} process.ss -o process.o ;  \
		$(RM) -f process.S ;;\
	ncrx86_* | sunx86_*) \
		$(PATH_CPP) -P -D__sun -D__i386 -DIGNORE_STDS_H -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.i386.s > process.ss; \
		${AS} -o process.o process.ss; \
		$(RM) process.ss ;; \
	alpha_nbsd* ) \
		$(PATH_CPP) -DASSEMBLER -P -I${TOP_INCDIR} ${srcdir}/process.s > process.ss; \
		${AS} -o process.o process.ss; \
		$(RM) process.ss ;; \
	hp* | *_linux* | *_umlinux* | sgi_64 | sgi_65 ) \
		${CCOBJ} ${CFLAGS} -c ${srcdir}/process.c;; \
	alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c | alpha_dux?? ) \
		${AS} -v  -P -DOSF -I${TOP_INCDIR} -DASSEMBLER ${srcdir}/process.s; \
		${AS} -v process.i -o process.o;; \
	ia64_hpux11* | hp_ux11* ) \
		${CC} ${CFLAGS} -c ${srcdir}/process.c;; \
	i386_*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} -I${srcdir} ${srcdir}/process.i386.s > process.ss; \
		${AS} process.ss -o process.o; \
		$(RM) process.ss ;; \
	*) \
		$(PATH_CPP) -P -I${TOP_INCDIR} ${srcdir}/process.s > process.ss; \
		${AS} process.ss -o process.o; \
		$(RM) process.ss ;; \
	esac

iomgr.o		: iomgr.c lwp.h timer.h
		${CCOBJ} -c ${CFLAGS} ${srcdir}/iomgr.c

preempt.o	: preempt.c lwp.h preempt.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;; \
		*)  \
			$(CCOBJ) ${CFLAGS} -c ${srcdir}/lwp.c;; \
		esac
lock.o		: lock.c lock.h lwp.h
timer.o		: timer.c
fasttime.o	: fasttime.c
threadname.o    : threadname.c

exc_handling.o	: exc_handling.c

#
# tests
#
rw: rw.o liblwp.a $(TOP_LIBDIR)/util.a
	${CC} ${CFLAGS} -o rw rw.o liblwp.a $(TOP_LIBDIR)/util.a

rw.o: rw.c
	${CC} ${CFLAGS} -c ${srcdir}/rw.c


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

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

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

include ../config/Makefile.version
