# This Makefile fragment is used to build GASNet conduits 
# it is not meant to be used directly 
# @configure_input@

.PHONY: do-error do-make-fragment libs force clean-local        \
        do-clean-local do-install-data-local do-uninstall-local \
        do-libgasnet-seq do-libgasnet-par do-libgasnet-parsync  \
	do-libgasnet do-run-testexit do-error do-pthreads-error \
	test tests tests-seq tests-par tests-parsync tests-clean 

thread_defines = @GASNET_THREAD_DEFINES@

# header files to be installed
# Conduit-specific headers will overwrite any conflicts in extended-ref
headers = $(top_srcdir)/extended-ref/*.h $(srcdir)/*.h $(CONDUIT_EXTRAHEADERS)

makefile_fragments =                \
        $(CONDUIT_NAME)-seq.mak     \
@HAVE_PTHREAD_TRUE@  $(CONDUIT_NAME)-parsync.mak $(CONDUIT_NAME)-par.mak

@GASNET_SEGMENT_FAST_TRUE@GASNET_SEGMENT_STR="FAST"
@GASNET_SEGMENT_LARGE_TRUE@GASNET_SEGMENT_STR="LARGE"
@GASNET_SEGMENT_EVERYTHING_TRUE@GASNET_SEGMENT_STR="EVERYTHING"

VPATH=$(srcdir)

LIBINCLUDES =             \
        -I$(srcdir)       \
        -I$(top_srcdir)   \
        -I$(top_builddir) \
        -I$(top_srcdir)/other \
        -I$(top_srcdir)/extended-ref  

LIBDEFINES =                        \
        $(LIBGASNET_THREAD_DEFINES) \
        -DGASNET_$(THREAD_MODEL)    \
        @BUILDCONFIG_DEFINES@       \
        @GASNET_MACHINE_DEFINES@ 

LIBCFLAGS =                       \
        @CFLAGS@                  \
        @MISC_CFLAGS@             \
        $(LIBDEFINES)             \
        $(CONDUIT_EXTRALIBCFLAGS) \
	$(LIBINCLUDES)		  \
	$(MANUAL_LIBCFLAGS)

libgasnet_sources =           \
	$(CONDUIT_SOURCELIST) \
	$(top_srcdir)/gasnet_internal.c	  \
	$(top_srcdir)/gasnet_trace.c	  \
	$(top_srcdir)/gasnet_mmap.c	  

libgasnet_objects = \
	`for file in $(libgasnet_sources) ; do echo \`basename $$file .c\`.o ; done` \
	$(CONDUIT_SPECIAL_OBJS)

libgasnet_dependencies =                  \
        $(CONFIG_HEADER)                  \
        $(srcdir)/*.[ch]                  \
        $(top_srcdir)/*.[ch]              \
        $(top_srcdir)/extended-ref/*.[ch] \
	$(top_srcdir)/other/*.h           \
	$(CONDUIT_SOURCELIST)             \
	$(CONDUIT_EXTRAHEADERS)           \
	$(CONDUIT_EXTRADEPS)

EXTRA_DIST = $(CONDUIT_FILELIST) README

docdir = $(datadir)/doc/gasnet

# library targets 
THREAD_MODEL=SEQ
THREAD_MODEL_LC=`echo "$(THREAD_MODEL)" | @AWK@ '{print tolower($$0)}'`
do-libgasnet: $(CONDUIT_SPECIAL_OBJS)
	@mkdir -p .$(THREAD_MODEL)
	@libgasnet_objects="$(libgasnet_objects)" ; libgasnet_objects=`echo $$libgasnet_objects` ; \
	pwd=`@PWD_PROG@` ; cmd="$(CC) $(LIBCFLAGS) -c $(libgasnet_sources) && \
	$(AR) cru $$pwd/libgasnet-$(CONDUIT_NAME)-$(THREAD_MODEL_LC).a $$libgasnet_objects && \
	$(RANLIB) $$pwd/libgasnet-$(CONDUIT_NAME)-$(THREAD_MODEL_LC).a && \
	rm -f $$libgasnet_objects" ; \
	echo " --- BUILDING libgasnet-$(CONDUIT_NAME)-$(THREAD_MODEL_LC).a --- " ; \
        echo $$cmd ; cd .$(THREAD_MODEL) ; eval $$cmd
	@rm -Rf .$(THREAD_MODEL)

do-libgasnet-seq: $(libgasnet_dependencies) $(CONDUIT_SEQ_HOOK)
	@$(MAKE) THREAD_MODEL=SEQ LIBGASNET_THREAD_DEFINES= \
          top_srcdir=`cd $(top_srcdir); @PWD_PROG@`         \
          top_builddir=`cd $(top_builddir); @PWD_PROG@`     \
          srcdir=`cd $(srcdir); @PWD_PROG@`                 \
          builddir=`@PWD_PROG@`                             \
          do-libgasnet

do-libgasnet-par: $(libgasnet_dependencies) $(CONDUIT_PAR_HOOK)
	@@HAVE_PTHREAD_FALSE@$(MAKE) do-pthreads-error
	@$(MAKE) THREAD_MODEL=PAR                           \
          LIBGASNET_THREAD_DEFINES="$(thread_defines)"      \
          top_srcdir=`cd $(top_srcdir); @PWD_PROG@`         \
          top_builddir=`cd $(top_builddir); @PWD_PROG@`     \
          srcdir=`cd $(srcdir); @PWD_PROG@`                 \
          builddir=`@PWD_PROG@`                             \
          do-libgasnet

do-libgasnet-parsync: $(libgasnet_dependencies) $(CONDUIT_PARSYNC_HOOK)
	@@HAVE_PTHREAD_FALSE@$(MAKE) do-pthreads-error
	@$(MAKE) THREAD_MODEL=PARSYNC                       \
          LIBGASNET_THREAD_DEFINES="$(thread_defines)"      \
          top_srcdir=`cd $(top_srcdir); @PWD_PROG@`         \
          top_builddir=`cd $(top_builddir); @PWD_PROG@`     \
          srcdir=`cd $(srcdir); @PWD_PROG@`                 \
          builddir=`@PWD_PROG@`                             \
          do-libgasnet

# test targets 

tests: force
	@echo You must specify one of tests-seq, tests-par or tests-parsync
	@exit 1

test: tests

TESTINSTANCE=$${PPID:-xxxx}
TESTLOG=`pwd`/.test-results
TESTLOG_INHERIT=0

do-begin-tests do-end-tests:
	@if test "$(TESTLOG_INHERIT)" = "0"; then \
          ( cd "$(top_builddir)" && $(MAKE) TESTINSTANCE=$(TESTINSTANCE) TESTLOG=$(TESTLOG) $@ ) || exit $$? ; \
        fi

tests-seq: force libgasnet-$(CONDUIT_NAME)-seq.a $(CONDUIT_NAME)-seq.mak $(CONDUIT_SEQ_HOOK)
	@echo "*** Building all GASNet tests in SEQ/$(GASNET_SEGMENT_STR) mode ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile tests-seq \
	  configfile="$(CONDUIT_NAME)-seq.mak" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

tests-parsync: force libgasnet-$(CONDUIT_NAME)-parsync.a $(CONDUIT_NAME)-parsync.mak $(CONDUIT_PARSYNC_HOOK)
	@echo "*** Building all GASNet tests in PARSYNC/$(GASNET_SEGMENT_STR) mode ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile tests-parsync \
	  configfile="$(CONDUIT_NAME)-parsync.mak" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

tests-par: force libgasnet-$(CONDUIT_NAME)-par.a $(CONDUIT_NAME)-par.mak $(CONDUIT_PAR_HOOK)
	@echo "*** Building all GASNet tests in PAR/$(GASNET_SEGMENT_STR) mode ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile tests-par \
	  configfile="$(CONDUIT_NAME)-par.mak" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

tests-mpi: force 
	@echo "*** Building all MPI tests ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile tests-mpi \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

tests-mpi2: force 
	@echo "*** Building all MPI2 tests ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile tests-mpi2 \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

test%: $(top_srcdir)/tests/test%.c force 
	@$(MAKE) libgasnet-$(CONDUIT_NAME)-$(THREAD_MODEL_LC).a $(CONDUIT_NAME)-$(THREAD_MODEL_LC).mak
	@echo "*** Building $@ in $(THREAD_MODEL)/$(GASNET_SEGMENT_STR) mode ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile $@   \
	  configfile="$(CONDUIT_NAME)-$(THREAD_MODEL_LC).mak" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

test%: $(top_srcdir)/tests/test%.cc force 
	@$(MAKE) libgasnet-$(CONDUIT_NAME)-$(THREAD_MODEL_LC).a $(CONDUIT_NAME)-$(THREAD_MODEL_LC).mak
	@echo "*** Building $@ in $(THREAD_MODEL)/$(GASNET_SEGMENT_STR) mode ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile $@   \
	  configfile="$(CONDUIT_NAME)-$(THREAD_MODEL_LC).mak" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

test%: $(top_srcdir)/tests/mpi/test%.c force 
	@echo "*** Building MPI test $@ ***"
	@$(MAKE) -f $(top_builddir)/tests/Makefile $@   \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

test%-seq: force 
	@$(MAKE) `basename $@ -seq` THREAD_MODEL=SEQ
test%-parsync: force 
	@$(MAKE) `basename $@ -parsync` THREAD_MODEL=PARSYNC
test%-par: force 
	@$(MAKE) `basename $@ -par` THREAD_MODEL=PAR

tests-clean: 
	@$(MAKE) -f $(top_builddir)/tests/Makefile clean \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)

check-run-tests-deps: force
	@if test "$(SUBDIRS)" != "" ; then 	\
	   for dir in ""$(SUBDIRS) ; do 	\
	   	( cd "$$dir" && $(MAKE) ) || exit $$?;	\
	   done ;				\
	 fi

run-tests do-run-testexit: check-run-tests-deps
	@$(MAKE) TESTINSTANCE=$(TESTINSTANCE) TESTLOG=$(TESTLOG) do-begin-tests
	@$(MAKE) -f $(top_builddir)/tests/Makefile $@ \
	  TESTINSTANCE=$(TESTINSTANCE) TESTLOG=$(TESTLOG) \
	  CONDUIT_NAME=$(CONDUIT_NAME) \
	  CONDUIT_RUNCMD="$(CONDUIT_RUNCMD)" \
	  CONDUIT_TESTS="$(CONDUIT_TESTS)" $(CONDUIT_TEST_MAKEARGS)
	@$(MAKE) TESTINSTANCE=$(TESTINSTANCE) TESTLOG=$(TESTLOG) do-end-tests

# these are separate make commands to ensure they can be composed 
# and still get multiple run-tests invocations
run-tests-seq: tests-clean
	$(MAKE) tests-seq run-tests
run-tests-par: tests-clean
	@$(MAKE) tests-par run-tests
run-tests-parsync: tests-clean 
	@$(MAKE) tests-parsync run-tests
run-tests-mpi: tests-clean 
	@$(MAKE) tests-mpi run-tests
run-tests-mpi2: tests-clean 
	@$(MAKE) tests-mpi2 run-tests

.PRECIOUS: run-testexit
run-testexit: force
	@if test ! -x testexit ; then \
	  $(MAKE) testexit-par || exit $$? ; \
	fi
	@$(MAKE) do-run-testexit	

@GNU_NM_TRUE@check-exports: $(libraries)
@GNU_NM_TRUE@	@echo Checking libgasnet exports...
@GNU_NM_TRUE@	@for lib in $(libraries) ; do                                               \
@GNU_NM_TRUE@	  failed=0 ;                                                                \
@GNU_NM_TRUE@	  echo ;                                                                    \
@GNU_NM_TRUE@	  echo $$lib: ;                                                             \
@GNU_NM_TRUE@	  @NM@ --defined-only $$lib |                                               \
@GNU_NM_TRUE@	    grep -v -e ' [\._]*gasnetc_' -e ' [\._]*gasneti_' -e ' [\._]*gasnete_'  \
@GNU_NM_TRUE@		    -e ' [\._]*gasnet_' -e ' [\._]*gasnett_'                        \
@GNU_NM_TRUE@		    -e ' [\._]*fh_' -e ' [\._]*firehose_'                           \
@GNU_NM_TRUE@		    -e ' [\._]*fhi_' -e ' [\._]*fhc_' -e ' [\._]*fhsmp_' -e ' [\._]*fhuni_' \
@GNU_NM_TRUE@		    -e __FUNCTION__ -e __PRETTY_FUNCTION__ -e ' [\._]*DWinfo'       \
@GNU_NM_TRUE@               -e ' [\._]*debug_info_seg' -e ' [\._]*debug_abbrev_seg'         \
@GNU_NM_TRUE@               -e ' [\._]*debug_frame_seg' -e ' [\._]*debug_line_seg' |        \
@GNU_NM_TRUE@	    @PERL@ -n -e 'print if /^[0-9a-fA-F]+\s+[A-Z]\s+/' > .$$lib.exp;        \
@GNU_NM_TRUE@	  if test -s .$$lib.exp ; then                                              \
@GNU_NM_TRUE@	    cat .$$lib.exp ;                                                        \
@GNU_NM_TRUE@	    echo FAILED ;                                                           \
@GNU_NM_TRUE@	    failed=1 ;                                                              \
@GNU_NM_TRUE@	  else                                                                      \
@GNU_NM_TRUE@	    echo PASSED ;                                                           \
@GNU_NM_TRUE@	  fi ;                                                                      \
@GNU_NM_TRUE@	  rm -f .$$lib.exp ;                                                        \
@GNU_NM_TRUE@	done ; exit $$failed

@GNU_NM_FALSE@check-exports: $(libraries)
@GNU_NM_FALSE@	@echo check-exports test SKIPPED

check: check-exports tests-par

force:

do-clean-local: tests-clean
	@echo rm -f $(makefile_fragments) $(libgasnet_objects) $(libraries) *.o core .test-results
	@rm -f $(makefile_fragments) $(libgasnet_objects) $(libraries) *.o core .test-results
	@rm -Rf .SEQ .PAR .PARSYNC


do-install-data-local: force $(headers) $(makefile_fragments)
	$(mkinstalldirs) $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit
	@list='$(headers)'; for p in $$list; do \
	  if test -f $$p; then \
	    filename=`basename $$p`; \
            if test "$$filename" = "`basename $$p _internal.h`" ; then \
	    echo " $(INSTALL_DATA) $$p $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/$$filename"; \
	    $(INSTALL_DATA) $$p $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/$$filename || exit $$? ; \
	    else :; fi; \
	  else :; fi; \
	done
	@list='$(makefile_fragments)'; for p in $$list; do \
	  if test -f $$p; then \
	    filename=`basename $$p`; \
	    echo "@GASNET_INSTALL_CMD@ < $$p > $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/$$filename"; \
	    @GASNET_INSTALL_CMD@ < $$p > $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/$$filename || exit $$? ; \
	  else :; fi; \
	done
	$(mkinstalldirs) $(DESTDIR)$(docdir)
	cp -f $(srcdir)/README $(DESTDIR)$(docdir)/README-$(CONDUIT_NAME)
	if test -f $(srcdir)/license.txt ; then \
	  cp -f $(srcdir)/license.txt $(DESTDIR)$(docdir)/license-$(CONDUIT_NAME).txt || exit $$? ; \
        fi

do-uninstall-local:
	rm -f $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/*.h $(DESTDIR)$(includedir)/$(CONDUIT_NAME)-conduit/*.mak
	rm -Rf $(DESTDIR)$(docdir)

do-error: 
	@echo "ERROR: $(CONDUIT_NAME)-conduit support was not detected at configure time"
	@echo "       try re-running configure with --enable-$(CONDUIT_NAME)"
	@exit 1

do-pthreads-error: 
	@echo "ERROR: pthreads support was not detected at configure time"
	@echo "       try re-running configure with --enable-pthreads"
	@exit 1

$(top_builddir)/other/Makefile-conduit.mak: $(top_srcdir)/other/Makefile-conduit.mak.in
	$(MAKE) -C $(top_builddir)/other Makefile-conduit.mak

$(top_builddir)/other/fragment-body.mak: $(top_srcdir)/other/fragment-body.mak.in
	$(MAKE) -C $(top_builddir)/other fragment-body.mak

$(top_builddir)/other/fragment-head.mak: $(top_srcdir)/other/fragment-head.mak.in
	$(MAKE) -C $(top_builddir)/other fragment-head.mak

$(top_builddir)/tests/Makefile: $(top_srcdir)/tests/Makefile.in
	$(MAKE) -C $(top_builddir)/tests Makefile

Makefile: $(srcdir)/Makefile.am $(top_builddir)/other/Makefile-conduit.mak $(top_builddir)/tests/Makefile


$(CONDUIT_NAME)-seq.mak: conduit.mak $(top_builddir)/other/fragment-head.mak $(top_builddir)/other/fragment-body.mak 
	$(MAKE) do-make-fragment thread_model=seq THREAD_MODEL=SEQ

$(CONDUIT_NAME)-par.mak: conduit.mak $(top_builddir)/other/fragment-head.mak $(top_builddir)/other/fragment-body.mak 
	@@HAVE_PTHREAD_FALSE@$(MAKE) do-pthreads-error
	$(MAKE) do-make-fragment thread_model=par THREAD_MODEL=PAR

$(CONDUIT_NAME)-parsync.mak: conduit.mak $(top_builddir)/other/fragment-head.mak $(top_builddir)/other/fragment-body.mak 
	@@HAVE_PTHREAD_FALSE@$(MAKE) do-pthreads-error
	$(MAKE) do-make-fragment thread_model=parsync THREAD_MODEL=PARSYNC

do-make-fragment: force
	rm -f $(CONDUIT_NAME)-$(thread_model).mak
	@echo Building $(CONDUIT_NAME)-$(thread_model).mak... ;                             \
	AUTOGENMSG='WARNING: This file is automatically generated - do NOT edit directly' ; \
	cat $(top_builddir)/other/fragment-head.mak                                         \
	    conduit.mak                                                                     \
	    $(top_builddir)/other/fragment-body.mak |                                       \
	sed -e 's@#conduit_name#@$(CONDUIT_NAME)@g'                                         \
	    -e 's@#THREAD_MODEL#@$(THREAD_MODEL)@g'                                         \
	    -e 's@#thread_model#@$(thread_model)@g'                                         \
	    -e '/#INSTRUCTIONS#/d'                                                          \
	    -e "s@#AUTOGEN#@$${AUTOGENMSG}@g"                                               \
	> $(CONDUIT_NAME)-$(thread_model).mak || exit 1
