# 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 = forward.c 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- += pthread_mutex_getprioceiling.c pthread_mutex_setprioceiling.c # XXX: delete those or use them!
libpthread-routines-$(UCLIBC_HAS_RESOLVER_SUPPORT) += res.c
libpthread-routines-$(UCLIBC_SUSV4_LEGACY) += pthread_getconcurrency.c \
	pthread_setconcurrency
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)

ifeq ($(UCLIBC_CTOR_DTOR),y)
START_FILE-libpthread.so := $(top_builddir)libpthread/nptl/sysdeps/pthread/pt-crti.o
END_FILE-libpthread.so := $(top_builddir)libpthread/nptl/sysdeps/pthread/pt-crtn.o
LDFLAGS-libpthread.so += -nostartfiles
$(top_builddir)lib/libpthread.so: | $(START_FILE-libpthread.so) $(END_FILE-libpthread.so)
endif

libpthread_FULL_NAME := libpthread-$(VERSION).so
lib-a-$(UCLIBC_HAS_THREADS) += $(top_builddir)lib/libpthread.a
lib-so-$(UCLIBC_HAS_THREADS) += $(top_builddir)lib/libpthread.so

$(top_builddir)lib/libpthread.so: $(libpthread_OUT)/libpthread_so.a $(libc.depend) $(libdl.depend) $(top_builddir)lib/libpthread_nonshared.a
	$(call link.so,$(libpthread_FULL_NAME),$(ABI_VERSION))
	$(Q)cat $(top_srcdir)extra/scripts/format.lds > $@.tmp
	$(Q)echo "GROUP ( $(notdir $@).$(ABI_VERSION) libpthread_nonshared.a )" >> $@.tmp
	$(Q)mv $@.tmp $@

ifeq ($(PTHREADS_DEBUG_SUPPORT),y)
$(libpthread_OUT)/libpthread_so.a: STRIP_FLAGS:=$(STRIP_FLAGS:-x=-X --strip-debug)
endif
$(libpthread_OUT)/libpthread_so.a: $(libpthread-so-y)
	$(Q)$(RM) $@
	$(do_ar)

$(top_builddir)lib/libpthread.a: $(libpthread-a-y)
	$(Q)$(INSTALL) -d $(dir $@)
	$(Q)$(RM) $@
	$(do_ar)

#
# Create 'pthread-errnos.h' header file.
#
CFLAGS-gen_pthread-errnos.c = -S

$(libpthread_OUT)/gen_pthread-errnos.c: $(libpthread_DIR)/pthread-errnos.sym | $(libpthread_OUT)
	$(do_awk) $(top_srcdir)extra/scripts/gen-as-const.awk $< > $@

$(libpthread_OUT)/gen_pthread-errnos.s: $(libpthread_OUT)/gen_pthread-errnos.c | headers
	$(compile.c)
libpthread-generated-y += $(libpthread_OUT)/gen_pthread-errnos.s
$(libpthread_OUT)/pthread-errnos.h: $(libpthread_OUT)/gen_pthread-errnos.s
	$(do_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) \
	      $(addprefix $(libpthread_OUT)/gen_pthread-errnos., c s) \
	      $(libpthread_OUT)/pthread-errnos.h

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

ifeq ($(PTHREADS_DEBUG_SUPPORT),y)
LDFLAGS-libpthread.so += $(LDFLAGS_NOSTRIP) -Wl,-z,defs
else
LDFLAGS-libpthread.so += $(LDFLAGS)
endif

LDFLAGS-libpthread.so += $(top_builddir)lib/$(UCLIBC_LDSO_NAME)-$(VERSION).so $(top_builddir)lib/libdl-$(VERSION).so \
	-Wl,-z,nodelete,-z,initfirst,-init=__pthread_initialize_minimal_internal

LIBS-libpthread.so := $(LIBS)

CFLAGS-nptl = -DNOT_IN_libc -DIS_IN_libpthread $(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

# Among others, __pthread_unwind is forwarded.  This function must handle
# exceptions.
CFLAGS-forward.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-fcntl.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-lockf.c = -fexceptions
CFLAGS-pread.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pread64.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pwrite.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-pwrite64.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-wait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-waitpid.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-sigwait.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-msgrcv.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-msgsnd.c = -fexceptions -fasynchronous-unwind-tables
CFLAGS-tcdrain.c = -fexceptions -fasynchronous-unwind-tables

CFLAGS-pt-system.c = -fexceptions -I$(top_srcdir)libc/stdlib

#
# 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-nptl)
CFLAGS-OMIT-forward.c = $(CFLAGS-nptl)
CFLAGS-OMIT-libc-lowlevelock.c = $(CFLAGS-nptl)
CFLAGS-OMIT-libc-cancellation.c = $(CFLAGS-nptl)