# This is the developer's -*-Makefile-*-, not the user's makefile.
# Do not use it unless you know exactly what you do!

SHELL = /bin/sh
MAKE = make

.PHONY : all configures update-gnulib gnulib-imported build-aux-update \
	check-configures config-h-in woe32-h woe32-c

all : build-aux-update gnulib-imported aclocal.m4 check-configures \
	config-h-in woe32-h woe32-c

GNULIB_CVS_ROOT = :pserver:anonymous@pserver.git.sv.gnu.org:/gnulib.git
GNULIB_CVS_REPOSITORY = HEAD
GNULIB_GIT = git://git.savannah.gnu.org/gnulib.git
GNULIB_CHECKOUT := gnulib
update-gnulib :
	if test -d $(GNULIB_CHECKOUT); then \
	  cd $(GNULIB_CHECKOUT) && \
	  if test -d .git; then git pull; else cvs update -d -P; fi \
	else \
	  if git --version >/dev/null 2>&1; then \
	    git clone "$(GNULIB_GIT)" $(GNULIB_CHECKOUT) ; \
	  else cvs -d "$(GNULIB_CVS_ROOT)" checkout -d $(GNULIB_CHECKOUT) $(GNULIB_CVS_REPOSITORY); fi \
	fi

gnulib-imported : update-gnulib force
	$(RM) configure.ac; ln -sv configure.in configure.ac
	$(GNULIB_CHECKOUT)/gnulib-tool --import --source-base=gllib \
		--m4-base=glm4 --aux-dir=build-aux --no-changelog \
		longlong nocrash host-cpu-c-abi
	$(RM) configure.ac
	rm -fvr build-aux/.cvsignore glm4/.cvsignore gllib/
	cd glm4; \
	for f in *.m4; do \
	  if test -f ../m4/$$f; then \
	    echo == $$f; \
	    if cmp $$f ../m4/$$f; then \
	      cd ../m4; cvs rm -f $$f; cd ../glm4; \
	    fi; \
	  fi; \
	done

# It is important to get ltmain.sh and libtool.m4 from the same version of
# libtool. Don't rely on what's installed in /usr/share or similar.
LIBTOOL_VERSION = 2.2
LIBTOOL_SOURCE_FILE = libtool-$(LIBTOOL_VERSION).tar.gz
LIBTOOL_SOURCE_URL = http://ftp.gnu.org/gnu/libtool/$(LIBTOOL_SOURCE_FILE)
GNULIB_AUX=config.guess config.sub install-sh
# it is important not to have "*.m4" targets with ": force" dependencies
# because otherwise every make will rebuild all configures
build-aux-update : update-gnulib force
	for f in $(GNULIB_AUX); do \
	  cp -afuv $(GNULIB_CHECKOUT)/build-aux/$$f build-aux; \
	done
	test -f $(LIBTOOL_SOURCE_FILE) || wget $(LIBTOOL_SOURCE_URL)
	rm -rf libtool-$(LIBTOOL_VERSION)
	gunzip -c < $(LIBTOOL_SOURCE_FILE) | tar xf -
	cp -afuv libtool-$(LIBTOOL_VERSION)/libltdl/config/ltmain.sh build-aux/ltmain.sh
	cp -afuv libtool-$(LIBTOOL_VERSION)/libltdl/m4/libtool.m4 m4/libtool.m4
	rm -rf libtool-$(LIBTOOL_VERSION) $(LIBTOOL_SOURCE_FILE)

CURDIR=$(shell pwd)

aclocal.m4 : $(wildcard m4/*.m4) $(wildcard glm4/*.m4) $(CONFIGURES_IN)
	{ echo 'AC_INIT'; cat $(CONFIGURES_IN) | grep -v '\(AC_INIT(\|AC_OUTPUT(\|AC_CONFIG_HEADERS(\|AC_CONFIG_FILES(\)'; } > configure.ac
	mv configure.in configure.in-save
	aclocal --output=aclocal.m4 -I `pwd`/m4 -I `pwd`/glm4
	mv configure.in-save configure.in
	rm -f configure.ac

AUTOCONF_FILES = aclocal.m4
AUTOCONF = autoconf
AUTOCONF_CACHE = autom4te.cache
AUTOHEADER = autoheader

SUBDIRS_CONFIG_H = avcall callback trampoline vacall \
	callback/vacall_r callback/trampoline_r
SUBDIRS_CONFIGURE = $(CURDIR) $(SUBDIRS_CONFIG_H)
CONFIGURES = $(patsubst %,%/configure,$(SUBDIRS_CONFIGURE))
CONFIGURES_IN = $(patsubst %,%.in,$(CONFIGURES))

configures : $(CONFIGURES)

$(CONFIGURES) : %/configure : %/configure.in $(AUTOCONF_FILES)
	cd $* && $(AUTOCONF) --include=$(CURDIR)

check-configures : $(CONFIGURES)
	set -e; for f in $(CONFIGURES); do bash -x -n $$f; done
	rm -rf `find . -name $(AUTOCONF_CACHE)`;

CONFIG_H_IN = $(patsubst %,%/config.h.in,$(SUBDIRS_CONFIG_H))

config-h-in : $(CONFIG_H_IN)

$(CONFIG_H_IN) : %/config.h.in : %/configure.in $(AUTOCONF_FILES)
	cd $* && $(AUTOHEADER) --include=$(CURDIR)

woe32-h : avcall/avcall.h.msvc avcall/avcall.h.mingw32 \
	vacall/config.h.msvc vacall/config.h.mingw32 vacall/vacall.h.msvc \
	vacall/vacall.h.mingw32 \
	trampoline/config.h.msvc trampoline/config.h.mingw32 \
	callback/vacall_r/config.h.msvc callback/vacall_r/config.h.mingw32 \
	callback/vacall_r/vacall_r.h.msvc \
	callback/vacall_r/vacall_r.h.mingw32 \
	callback/trampoline_r/config.h.msvc \
	callback/trampoline_r/config.h.mingw32

woe32-c : avcall/avcall-i386-msvc.c avcall/avcall-i386-mingw32.c \
	vacall/vacall-i386-msvc.c vacall/vacall-i386-mingw32.c \
	callback/vacall_r/vacall-i386-msvc.c

avcall/avcall.h.msvc : avcall/avcall.h.in
	cd avcall && $(MAKE) -f Makefile.devel avcall.h.msvc

avcall/avcall.h.mingw32 : avcall/avcall.h.in
	cd avcall && $(MAKE) -f Makefile.devel avcall.h.mingw32

avcall/avcall-i386-msvc.c : avcall/avcall-i386-macro.S avcall/asmi386.sh
	cd avcall && $(MAKE) -f Makefile.devel avcall-i386-msvc.c

avcall/avcall-i386-mingw32.c : avcall/avcall-i386-macro.S avcall/asmi386.sh
	cd avcall && $(MAKE) -f Makefile.devel avcall-i386-mingw32.c

vacall/config.h.msvc : vacall/config.h.in
	cd vacall && $(MAKE) -f Makefile.devel config.h.msvc

vacall/config.h.mingw32 : vacall/config.h.in
	cd vacall && $(MAKE) -f Makefile.devel config.h.mingw32

vacall/vacall.h.msvc : vacall/vacall.h.in VERSION
	cd vacall && $(MAKE) -f Makefile.devel vacall.h.msvc

vacall/vacall.h.mingw32 : vacall/vacall.h.in VERSION
	cd vacall && $(MAKE) -f Makefile.devel vacall.h.mingw32

vacall/vacall-i386-msvc.c : vacall/vacall-i386-macro.S vacall/asmi386.sh
	cd vacall && $(MAKE) -f Makefile.devel vacall-i386-msvc.c

vacall/vacall-i386-mingw32.c : vacall/vacall-i386-macro.S vacall/asmi386.sh
	cd vacall && $(MAKE) -f Makefile.devel vacall-i386-mingw32.c

trampoline/config.h.msvc : trampoline/config.h.in
	cd trampoline && $(MAKE) -f Makefile.devel config.h.msvc

trampoline/config.h.mingw32 : trampoline/config.h.in
	cd trampoline && $(MAKE) -f Makefile.devel config.h.mingw32

callback/vacall_r/config.h.msvc : callback/vacall_r/config.h.in
	cd callback && $(MAKE) -f Makefile.devel vacall_r/config.h.msvc

callback/vacall_r/config.h.mingw32 : callback/vacall_r/config.h.in
	cd callback && $(MAKE) -f Makefile.devel vacall_r/config.h.mingw32

callback/vacall_r/vacall_r.h.msvc : callback/vacall_r/vacall_r.h.in VERSION
	cd callback && $(MAKE) -f Makefile.devel vacall_r/vacall_r.h.msvc

callback/vacall_r/vacall_r.h.mingw32 : callback/vacall_r/vacall_r.h.in VERSION
	cd callback && $(MAKE) -f Makefile.devel vacall_r/vacall_r.h.mingw32

callback/vacall_r/vacall-i386-msvc.c : callback/vacall_r/vacall-i386-macro.S callback/vacall_r/asmi386.sh
	cd callback && $(MAKE) -f Makefile.devel vacall_r/vacall-i386-msvc.c

callback/vacall_r/vacall-i386-mingw32.c : callback/vacall_r/vacall-i386-macro.S callback/vacall_r/asmi386.sh
	cd callback && $(MAKE) -f Makefile.devel vacall_r/vacall-i386-mingw32.c

callback/trampoline_r/config.h.msvc : callback/trampoline_r/config.h.in
	cd callback && $(MAKE) -f Makefile.devel trampoline_r/config.h.msvc

callback/trampoline_r/config.h.mingw32 : callback/trampoline_r/config.h.in
	cd callback && $(MAKE) -f Makefile.devel trampoline_r/config.h.mingw32

COMPILERS="cc -O" "gcc -freg-struct-return" "gcc -fpcc-struct-return"

multibuild : force
	for compiler in $(COMPILERS) ; do \
	  dir=build-`echo $$compiler | sed -e 's, ,,g' -e 's,/,_,g'`; \
	  mkdir $$dir; cd $$dir; \
	  CC="$$compiler" ../configure --srcdir=.. && make && make check; \
	  cd ..; \
	done

force :

