#	$NetBSD: Makefile,v 1.40 2007/03/02 18:53:51 ad Exp $
#

WARNS=	4

# Define PT_FIXEDSTACKSIZE_LG to set a fixed stacksize
#CPPFLAGS+=-DPT_FIXEDSTACKSIZE_LG=18

.include <bsd.own.mk>

.if exists(${.CURDIR}/arch/${MACHINE_ARCH})
ARCHSUBDIR=	${MACHINE_ARCH}
.elif exists(${.CURDIR}/arch/${MACHINE_CPU}) 
ARCHSUBDIR=	${MACHINE_CPU}
.else
.BEGIN:
	@echo "WARNING: no ARCHSUBDIR for ${MACHINE_ARCH}/${MACHINE_CPU}; skipping..."
.endif

INCS=	pthread.h pthread_types.h pthread_queue.h
INCSDIR=/usr/include

.if defined(ARCHSUBDIR)

C_ARCHDIR=	${.CURDIR}/../libc/arch/${ARCHSUBDIR}
ARCHDIR=	${.CURDIR}/arch/${ARCHSUBDIR}
.PATH:	${ARCHDIR}

CPPFLAGS+=	-I${ARCHDIR} -I${.CURDIR} -I${.OBJDIR} -D_LIBC
CPPFLAGS+=	-D__LIBPTHREAD_SOURCE__

# XXX: This crappy poke at libc's internals needs to be fixed.
# We need to put this *after our own includes, so that our "assym.h"
# gets picked, instead of the libc one
CPPFLAGS+=-I${NETBSDSRCDIR}/sys -I${.CURDIR}/../libc -I${C_ARCHDIR}

DPSRCS+=	assym.h
CLEANFILES+=	assym.h

assym.h: ${ARCHDIR}/genassym.cf pthread.h pthread_int.h pthread_md.h
	${_MKTARGET_CREATE}
	${TOOL_GENASSYM} -- ${CC} ${CFLAGS} ${CPPFLAGS} ${PROF} \
		< ${ARCHDIR}/genassym.cf > assym.h.tmp && \
	mv -f assym.h.tmp assym.h

LIB=	pthread
LIBDIR=	/usr/lib/post-sa
MKLINT=	no

ASM=		_lwp_park.S _lwp_unpark.S _lwp_unpark_all.S _lwp_detach.S _lwp_kill.S
WEAKASM=	sched_yield.S
SRCS+=		${ASM} ${WEAKASM}

ASMDEPS=	${.CURDIR}/Makefile ${C_ARCHDIR}/SYS.h ${DESTDIR}/usr/include/sys/syscall.h

${ASM}: ${ASMDEPS}
	${_MKTARGET_CREATE}
	printf '#include "syscall-local.h"\n#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' >${.TARGET}

${WEAKASM}: ${ASMDEPS}
	${_MKTARGET_CREATE}
	printf '#include "syscall-local.h"\n#include "SYS.h"\nWSYSCALL(${.PREFIX},_sys_${.PREFIX})\nWEAK_ALIAS(_${.PREFIX},_sys_${.PREFIX})\n' >${.TARGET}

#
# NOTE: When you create a new file for libpthread, make sure that pthread.c
# gets a reference to a symbol in that file.  Otherwise, Unix's stupid static
# library semantics will end up discarding potentially important objects.
#
SRCS+=	pthread.c 
SRCS+=	pthread_attr.c
SRCS+=	pthread_barrier.c
SRCS+=	pthread_cancelstub.c
SRCS+=	pthread_cond.c
SRCS+=	pthread_lock.c 
SRCS+=	pthread_misc.c
SRCS+=	pthread_mutex.c
SRCS+=	pthread_rwlock.c
SRCS+=	pthread_specific.c
SRCS+=  pthread_stack.c
SRCS+=	pthread_tsd.c
SRCS+=  pthread_debug.c
SRCS+=	res_state.c
SRCS+=	sched.c
SRCS+=	sem.c
# Architecture-dependent files
SRCS+=	_context_u.S
.if exists(${ARCHDIR}/pthread_md.c)
SRCS+= pthread_md.c
.endif

# The TSD routines are used in the implementation of profiling, and so
# can't be profiled themselves.
pthread_specific.po: pthread_specific.o
	${_MKTARGET_CREATE}
	cp pthread_specific.o pthread_specific.po

_context_u.po: _context_u.o
	${_MKTARGET_CREATE}
	cp _context_u.o _context_u.po

pthread_switch.S _context_u.S: assym.h

debuglog: debuglog.o
	$(CC) -o debuglog debuglog.o -lpthread

.include <bsd.lib.mk>

.else

.include <bsd.files.mk>
.include <bsd.inc.mk>

.endif

# WARNS=2 sets -Wcast-qual. This causes problems for one of
# pthread_setspecific() and pthread_getspecific(), since the constness
# of the argument to setspecific() has to be discarded *somewhere*
# before returning it from getspecific().
CFLAGS+= -Wno-cast-qual
