# Makefile for uClibc NPTL
#
# Copyright (C) 2005-2006 Steven J. Hill <sjhill@realitydiluted.com>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

subdirs += libpthread/nptl

libpthread_DIR = $(top_srcdir)libpthread/nptl
libpthread_OUT = $(top_builddir)libpthread/nptl

include $(libpthread_DIR)/sysdeps/Makefile.in

libc-shared-routines-y = libc-cancellation.c
libc-static-routines-y = alloca_cutoff.c libc-cancellation.c
libpthread-shared-only-routines-y = version.c
libpthread-static-only-routines-y = pthread_atfork.c
libpthread-routines- += $(notdir $(wildcard $(libpthread_DIR)/gen_*.c)) # dummy generated files
libpthread-routines- += allocatestack.c # dummy included by pthread_create.c
libpthread-routines-$(UCLIBC_SUSV4_LEGACY) += pthread_getconcurrency.c \
	pthread_setconcurrency.c
libpthread_CSRC = $(filter-out $(libpthread-routines-) \
		  		$(libc-shared-routines-y) \
		  		$(libc-static-routines-y) \
				$(libpthread-shared-only-routines-y) \
				$(libpthread-static-only-routines-y) \
				$(notdir $(libpthread_OBJS:.o=.c)), \
		  $(notdir $(wildcard $(libpthread_DIR)/*.c)))

libpthread_OBJS += $(addprefix $(libpthread_OUT)/,$(libpthread_CSRC:.c=.o))
libpthread-so-y += $(addprefix $(libpthread_OUT)/,$(libpthread-shared-only-routines-y:.c=.oS))
libpthread-so-y += $(libpthread_OBJS:.o=.oS)
libpthread-nonshared-y := $(addprefix $(libpthread_OUT)/,$(libpthread-static-only-routines-y:.c=.oS))
libpthread-static-y    := $(addprefix $(libpthread_OUT)/,$(libpthread-static-only-routines-y:.c=.o))
libpthread-static-y    += $(libpthread_OBJS)
ifeq ($(DOPIC),y)
libpthread-a-y := $(libpthread-static-y:.o=.os)
else
libpthread-a-y := $(libpthread-static-y)
endif

libc-shared-routines-y := $(filter-out $(notdir $(libpthread_libc_OBJS:.o=.c)), $(libc-shared-routines-y))
libc-static-routines-y := $(filter-out $(notdir $(libpthread_libc_OBJS:.o=.c)), $(libc-static-routines-y))
libc-shared-routines-y := $(addprefix $(libpthread_OUT)/,$(libc-shared-routines-y:.c=.oS))
libc-static-routines-y := $(addprefix $(libpthread_OUT)/,$(libc-static-routines-y:.c=.o))
libc-shared-y += $(libc-shared-routines-y) $(libpthread_libc_OBJS:.o=.oS)
ifeq ($(DOPIC),y)
libc-static-y += $(libc-static-routines-y:.o=.os) $(libpthread_libc_a_OBJS:.o=.os) $(libpthread_ld_tls_COBJ:.o=.os)
else
libc-static-y += $(libc-static-routines-y) $(libpthread_libc_a_OBJS) $(libpthread_ld_tls_COBJ)
endif

librt-pt-routines-y := $(patsubst %.c,$(libpthread_pthread_OUT)/%.o,$(filter-out $(notdir $(libpthread_librt_OBJS:.o=.c)), $(librt-pt-routines-y)))
librt-pt-shared-only-routines-y := $(patsubst %.c,$(libpthread_pthread_OUT)/%.o,$(filter-out $(notdir $(libpthread_librt_OBJS:.o=.c)), $(librt-pt-shared-only-routines-y)))
librt_OBJS = $(libpthread_librt_OBJS) $(librt-pt-routines-y)
ifeq ($(DOPIC),y)
librt-a-y += $(librt_OBJS:.o=.os)
else
librt-a-y += $(librt_OBJS)
endif
librt-so-y += $(librt_OBJS:.o=.oS) $(librt-pt-shared-only-routines-y:.o=.oS)

$(libpthread_OUT)/pthread-errnos.h: $(top_srcdir)extra/scripts/gen-as-const.awk
$(libpthread_OUT)/pthread-errnos.h: $(libpthread_DIR)/pthread-errnos.sym
	@$(disp_gen)
	$(do_awk) $(top_srcdir)extra/scripts/gen-as-const.awk $< \
	| $(CC) $(CFLAGS) -x c - -S -o - \
	| $(SED) $(PTHREAD_GENERATE_MANGLE) > $@
	@if test ! -s $@ ; then rm -f $@ ; false ; fi

pregen-headers-$(UCLIBC_HAS_THREADS_NATIVE) += $(libpthread_OUT)/pthread-errnos.h

headers-$(UCLIBC_HAS_THREADS_NATIVE) += $(nptl_headers_bootstrap)

libpthread_H := $(addprefix $(top_builddir)include/,semaphore.h)
libpthread_include_H := $(addprefix $(top_builddir)include/,pthread.h)
libpthread_include_BITS_H := $(addprefix $(top_builddir)include/bits/,libc-lock.h stdio-lock.h)
libpthread_include_STD_IMPL_OS_ARCH_BITS_H := $(addprefix $(top_builddir)include/bits/,pthreadtypes.h semaphore.h)

$(libpthread_include_STD_IMPL_OS_ARCH_BITS_H): $(top_builddir)include/bits/%:
	$(do_ln) $(call rel_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux/$(TARGET_ARCH)/bits/$(@F) $@
$(libpthread_include_BITS_H): $(top_builddir)include/bits/%:
	$(do_ln) $(call rel_srcdir)$(PTDIR)/sysdeps/pthread/bits/$(@F) $@
$(libpthread_include_H): $(top_builddir)include/%:
	$(do_ln) $(call rel_srcdir)$(PTDIR)/sysdeps/pthread/$(@F) $@
$(libpthread_H): $(top_builddir)include/%:
	$(do_ln) $(call rel_srcdir)$(PTDIR)/$(@F) $@

nptl_headers_bootstrap = $(libpthread_H) $(libpthread_include_H) $(libpthread_include_BITS_H) $(libpthread_include_STD_IMPL_OS_ARCH_BITS_H)

objclean-y += CLEAN_libpthread/nptl
headers_clean-y += HEADERCLEAN_libpthread/nptl

HEADERCLEAN_libpthread/nptl:
	$(do_rm) $(nptl_headers_bootstrap) \
	      $(libpthread_OUT)/pthread-errnos.h

CLEAN_libpthread/nptl:
	$(do_rm) $(addprefix $(libpthread_OUT)/*., o os oS a)

CFLAGS-dir_nptl := -DNOT_IN_libc -DIS_IN_libpthread
CFLAGS-libpthread/nptl := $(CFLAGS-dir_nptl) $(SSP_ALL_CFLAGS)

# Since cancellation handling is in large parts handled using exceptions
# we have to compile some files with exception handling enabled, some
# even with asynchronous unwind tables.

# init.c contains sigcancel_handler().
CFLAGS-init.c = -fexceptions -fasynchronous-unwind-tables
# The unwind code itself,
CFLAGS-unwind.c = -fexceptions
CFLAGS-unwind-forcedunwind.c = -fexceptions -fasynchronous-unwind-tables

# The following three functions must be async-cancel safe.
CFLAGS-pthread_cancel.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pthread_setcancelstate.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pthread_setcanceltype.c = -fexceptions -fasynchronous-unwind-tables

# These are internal functions which similar functionality as setcancelstate
# and setcanceltype.
CFLAGS-cancellation.c = -fasynchronous-unwind-tables
CFLAGS-libc-cancellation.c = -fasynchronous-unwind-tables

# Calling pthread_exit() must cause the registered cancel handlers to
# be executed.  Therefore exceptions have to be thrown through this
# function.
CFLAGS-pthread_exit.c = -fexceptions

# The following are cancellation points.  Some of the functions can
# block and therefore temporarily enable asynchronous cancellation.
# Those must be compiled asynchronous unwind tables.
CFLAGS-pthread_testcancel.c = -fexceptions
CFLAGS-pthread_join.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pthread_timedjoin.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pthread_once.c = $(uses-callbacks) -fexceptions \
			-fasynchronous-unwind-tables
CFLAGS-pthread_cond_wait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pthread_cond_timedwait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sem_wait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sem_timedwait.c = -fexceptions -fasynchronous-unwind-tables

# These are the function wrappers we have to duplicate here.
CFLAGS-accept.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-clock_nanosleep.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-close.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-connect.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-creat.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-creat64.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-fdatasync.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-fsync.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-lockf.c = -fexceptions
CFLAGS-lockf64.c = -fexceptions
CFLAGS-msgrcv.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-msgsnd.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-msync.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-nanosleep.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-open64.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-open.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-openat.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pause.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-poll.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-ppoll.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pread_write.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pselect.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-read.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-readv.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-recv.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-recvfrom.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-recvmsg.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-recvmmsg.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-__rt_sigtimedwait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-__rt_sigwaitinfo.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-select.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-send.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sendmsg.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sendmmsg.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sendto.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sigpause.c = -fexceptions
CFLAGS-sigsuspend.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sigtimedwait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sigwait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sigwaitinfo.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-__syscall_fcntl64.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-__syscall_fcntl.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-tcdrain.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-usleep.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-wait4.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-wait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-waitid.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-waitpid.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-write.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-writev.c = -fexceptions -fasynchronous-unwind-tables

#
# The rest of this file is uClibc specific.
#
CFLAGS-pthread_barrier_init.c = -D_GNU_SOURCE
CFLAGS-pthread_barrier_destroy.c = -D_GNU_SOURCE
CFLAGS-pthread_barrierattr_init.c = -D_GNU_SOURCE
CFLAGS-pthread_barrierattr_destroy.c = -D_GNU_SOURCE
CFLAGS-pthread_barrierattr_getpshared.c = -D_GNU_SOURCE
CFLAGS-pthread_barrierattr_setpshared.c = -D_GNU_SOURCE
CFLAGS-sem_open.c = -D_GNU_SOURCE

CFLAGS-OMIT-alloca_cutoff.c = $(CFLAGS-dir_nptl)
CFLAGS-OMIT-libc-lowlevelock.c = $(CFLAGS-dir_nptl)
CFLAGS-OMIT-libc-cancellation.c = $(CFLAGS-dir_nptl)
