NODEBUGCHECK = nodebug
TSTCHECK = alloctag basic location cf continued dc demangler dlopen do filter find_alloc flush leak lockable_auto_ptr magic marker strdup test_delete type_info
THREADSCHECK = keys threads

srcdir = @srcdir@
top_builddir = ..
distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
subdir = testsuite
VPATH = @srcdir@
SHELL = @SHELL@

CXX = @CXX@
DEFS = -DHAVE_CONFIG_H -DCWDEBUG
CXXFLAGS = @CXXFLAGS@
CXXFLAGSNODEBUG := $(shell echo "$$CXXFLAGS" | sed -e 's/-g[^ ]*//')
CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LDFLAGS@
RPATH_OPTION = @RPATH_OPTION@
RPATH := $(shell cd $(top_builddir)/.libs; pwd)
SHAREDLIBS = -L$(top_builddir)/.libs -lcwd @LIBS@ $(RPATH_OPTION)$(RPATH)
STATICLIBS = -static -L$(top_builddir)/.libs -lcwd @LIBS@
THREADS_SHAREDLIBS = -L$(top_builddir)/.libs -lcwd_r @LIBS@ @LIB_THREADS@ $(RPATH_OPTION)$(RPATH)
THREADS_STATICLIBS = -static -L$(top_builddir)/.libs -lcwd_r @LIBS@ @LIB_THREADS@
enable_shared = @enable_shared@
enable_static = @enable_static@
enable_threading = @libcwd_config_threading@
enable_nonthreading = @libcwd_config_nonthreading@
INCLUDES = -I$(top_builddir) -I$(srcdir)/libcwd.tst -I$(top_builddir)/include -I$(srcdir)/../include
PACKAGE = @PACKAGE@
VERSION = @VERSION@
DISTFILES = Makefile.in module.cc config lib libcwd.tst libcwd.nodebug libcwd.threads

ifeq ($(enable_shared), yes)
SHAREDTSTTARGETS := $(patsubst %,tst_%_shared,$(TSTCHECK))
SHAREDNODEBUGTARGETS := $(patsubst %,nodebug_%_shared,$(NODEBUGCHECK))
SHAREDTHREADSTARGETS := $(patsubst %,threads_%_shared,$(THREADSCHECK))
else
SHAREDTSTTARGETS :=
SHAREDNODEBUGTARGETS :=
SHAREDTHREADSTARGETS :=
endif

ifeq ($(enable_static), yes)
STATICTSTTARGETS := $(patsubst %,tst_%_static,$(TSTCHECK))
STATICNODEBUGTARGETS := $(patsubst %,nodebug_%_static,$(NODEBUGCHECK))
STATICTHREADSTARGETS := $(patsubst %,threads_%_static,$(THREADSCHECK))
else
STATICTSTTARGETS :=
STATICNODEBUGTARGETS :=
STATICTHREADSTARGETS :=
endif

SHAREDTARGETS =
STATICTARGETS =
EXPECT_SCRIPTS =
MODULETARGETS_THREADS =
MODULETARGETS =
ifeq ($(enable_nonthreading), yes)
SHAREDTARGETS += $(SHAREDTSTTARGETS) $(SHAREDNODEBUGTARGETS)
STATICTARGETS += $(STATICTSTTARGETS) $(STATICNODEBUGTARGETS)
EXPECT_SCRIPTS += nodebug.exp tst.exp
ifeq ($(enable_shared), yes)
MODULETARGETS += module.so
endif
endif
ifeq ($(enable_threading), yes)
SHAREDTARGETS += $(SHAREDTHREADSTARGETS)
STATICTARGETS += $(STATICTHREADSTARGETS)
EXPECT_SCRIPTS += threads.exp
ifeq ($(enable_shared), yes)
MODULETARGETS_THREADS += module_r.so
endif
endif
MODULETARGETS += $(MODULETARGETS_THREADS)

CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)
CXXCOMPILENODEBUG = $(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGSNODEBUG)
CXXLINK = $(CXX) $(CXXFLAGS) $(LDFLAGS)

RUNTEST = runtest
RUNTESTFLAGS = -a
FLAGS_TO_PASS =

all:
info install-info clean-info dvi install etags tags installcheck:

site.exp: Makefile
	@echo "Making a new config file..."
	-@rm -f ./tmp?
	@touch site.exp
	-@mv site.exp site.bak
	@echo "## these variables are automatically generated by make ##" > ./tmp0
	@echo "# Do not edit here. If you wish to override these values" >> ./tmp0
	@echo "# add them to the last section" >> ./tmp0
	@echo "set tool libcwd" >> ./tmp0
	@echo "set srcdir $(srcdir)" >> ./tmp0
	@test "$(enable_static)" != "yes" || echo "set STATIC yes" >> ./tmp0
	@test "$(enable_static)" != "no" || echo "set STATIC no" >> ./tmp0
	@test "$(enable_shared)" != "yes" || echo "set SHARED yes" >> ./tmp0
	@test "$(enable_shared)" != "no" || echo "set SHARED no" >> ./tmp0
	@echo "## All variables above are generated by configure. Do Not Edit ##" >> ./tmp0
	@cat ./tmp0 > site.exp
	@cat site.bak | sed -e '1,/^## All variables above are.*##/ d' >> site.exp
	-@rm -f ./tmp?

check: site.exp $(MODULETARGETS)
	LD_LIBRARY_PATH=".:$$LD_LIBRARY_PATH" $(RUNTEST) $(RUNTESTFLAGS) $(FLAGS_TO_PASS) $(EXPECT_SCRIPTS)

ifeq ($(enable_nonthreading), yes)
ifeq ($(enable_shared), yes)
module.so: module.cc $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILE) -shared -fPIC -DPIC -w $< -o $@
endif
endif

ifeq ($(enable_threading), yes)
ifeq ($(enable_shared), yes)
module_r.so: module.cc $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILE) -DLIBCWD_THREAD_SAFE -D_REENTRANT -pthread -shared -fPIC -DPIC -w $< -o $@
endif
endif

# Provide rules for manual compilation:
.SECONDARY: $(patsubst %,tst_%.o,$(TSTCHECK)) $(patsubst %,threads_%.o,$(THREADSCHECK)) $(patsubst %,nodebug_%.o,$(NODEBUGCHECK))

ifeq ($(enable_nonthreading), yes)
ifeq ($(enable_static), yes)
tst_%_static.o: libcwd.tst/%.cc Makefile $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILE) -DSTATIC -c $< -o $@
endif

tst_%.o: libcwd.tst/%.cc Makefile $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILE) -c $< -o $@

nodebug_%.o: libcwd.nodebug/%.cc Makefile $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILENODEBUG) -c $< -o $@
endif

ifeq ($(enable_threading), yes)
threads_%.o: libcwd.threads/%.cc Makefile $(top_builddir)/include/libcwd/*.h
	$(CXXCOMPILE) -DLIBCWD_THREAD_SAFE -D_REENTRANT -pthread -c $< -o $@
endif

ifeq ($(enable_shared), yes)
ifeq ($(enable_threading), yes)
threads_threads_shared: threads_threads.o $(top_builddir)/.libs/libcwd_r.so.* module_r.so
	$(CXXLINK) -pthread $< -o $@ $(THREADS_SHAREDLIBS)

threads_%_shared: threads_%.o $(top_builddir)/.libs/libcwd_r.so.*
	$(CXXLINK) -pthread $< -o $@ $(THREADS_SHAREDLIBS)
endif
ifeq ($(enable_nonthreading), yes)
tst_dlopen_shared: tst_dlopen.o $(top_builddir)/.libs/libcwd.so.* module.so
	$(CXXLINK) $< -o $@ $(SHAREDLIBS) @LIBS@

tst_filter_shared: tst_filter.o $(top_builddir)/.libs/libcwd.so.* module.so
	$(CXXLINK) $< -o $@ $(SHAREDLIBS)

tst_%_shared: tst_%.o $(top_builddir)/.libs/libcwd.so.*
	$(CXXLINK) $< -o $@ $(SHAREDLIBS)

nodebug_%_shared: nodebug_%.o $(top_builddir)/.libs/libcwd.so.*
	$(CXXLINK) $< -o $@ $(SHAREDLIBS)
endif
endif

ifeq ($(enable_static), yes)
ifeq ($(enable_threading), yes)
threads_threads_static: threads_threads.o $(top_builddir)/.libs/libcwd_r.a
	$(CXXLINK) -pthread $< -o $@ $(THREADS_STATICLIBS)

threads_%_static: threads_%.o $(top_builddir)/.libs/libcwd_r.a
	$(CXXLINK) -pthread $< -o $@ $(THREADS_STATICLIBS)
endif
ifeq ($(enable_nonthreading), yes)
tst_dlopen_static: tst_dlopen_static.o $(top_builddir)/.libs/libcwd.a
	$(CXXLINK) $< -o $@ $(STATICLIBS) @LIBS@

tst_filter_static: tst_filter_static.o $(top_builddir)/.libs/libcwd.a
	$(CXXLINK) $< -o $@ $(STATICLIBS)

tst_%_static: tst_%.o $(top_builddir)/.libs/libcwd.a
	$(CXXLINK) $< -o $@ $(STATICLIBS)

nodebug_%_static: nodebug_%.o $(top_builddir)/.libs/libcwd.a
	$(CXXLINK) $< -o $@ $(STATICLIBS)
endif
endif

shared: $(SHAREDTARGETS)
static: $(STATICTARGETS)

run: shared static $(MODULETARGETS)
	@for i in $(SHAREDTARGETS) $(STATICTARGETS); do \
	  echo -n "$$i: "; if (LD_LIBRARY_PATH=".:$$LD_LIBRARY_PATH" ./$$i 2>/dev/null >/dev/null); then echo "OK"; else echo "ERROR"; fi ; done

# Clean rules
mostlyclean:
	rm -f site.bak core core.* *.core *.o
clean: mostlyclean
	rm -f site.exp *.log *.sum *_*_shared *_*_static module*.so .\#* */.\#*
distclean: clean
	rm -f Makefile

# Uninstall rule
uninstall:

# Maintainer rules
maintainer-clean realclean: distdir-clean distclean

distdir-clean:
	(\
	  svndirs="`find $(srcdir) -type d -name .svn`"; \
	  dirs="`for d in $$svndirs; do echo $$d | sed -e 's%/\.svn$$%%'; done`"; \
	  for d in $$dirs; do \
	    rmfiles="$$(for entry in \
	        $$(svn stat --verbose --no-ignore --non-recursive --non-interactive --ignore-externals $$d | \
	          grep '^[?I]' | sed -re 's%.*[[:space:]]([^[:space:]]*)$$%\1%' | grep -v '^Makefile$$'); do \
		      test -f $$entry -o -L $$entry && echo -n "$$entry "; done)"; \
	    echo "distclean in $$d"; \
	    (test -z "$$rmfiles" || (echo "  removing $$rmfiles"; rm -f $$rmfiles)); \
	  done; \
	);

distdir: distdir-clean $(DISTFILES)
	distdir=`cd $(distdir) && pwd`;
	@for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    mkdir -p $(distdir)/$$file; \
	    find $$d/$$file -maxdepth 1 -type f -exec cp -p {} $(distdir)/$$file \;; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) && CONFIG_FILES=testsuite/Makefile CONFIG_HEADERS= $(SHELL) ./config.status
