# [Makefile wk 19.06.97] for the Mingw32/CPD system
#

.PHONY: all install clean distclean dist libs make-binutils make-gcc \
	copy-api-headers

-include config.mk

gcclibdir := $(PREFIX)/lib/gcc-lib/${TARGET}/${GCCVER}
target_dir := $(PREFIX)/$(TARGET)

all: config.okay make-gcc libs

config.okay:
	@./Configure
	@echo
	@echo "You should now re-run make"
	@echo "(Please ignore the error, it is used to stop the make run)"
	exit 1

copy-api-headers:
	( set -e; \
	  srcdir="$(WINDOWS32API_SRC_PATH)/Headers" ; \
	  dstdir="$(target_dir)/include"            ; \
	  if [ ! -f "$$srcdir/windows.h" ]; then       \
	      echo "windows.h not found in $srcdir" ; \
	      exit 1				    ; \
	  fi					    ; \
	  if [ ! -d "$$srcdir/Windows32" ]; then       \
	      echo "Windows32 directory not in $srcdir" ; \
	      exit 1				    ; \
	  fi					    ; \
	  cp -a $$srcdir/* $$dstdir	  ; \
	  sed -f patches/Functions.h.sed \
	    $$srcdir/Windows32/Functions.h > $$dstdir/Windows32/Functions.h \
	)

libs:	copy-api-headers
	$(MAKE) -C src

install: all
	$(MAKE) -C src install

clean:
	$(MAKE) -C src clean
	-rm -f *.tmp *~

distclean:  clean
	-rm -f config.okay  config.mk config.cache config.new

dist:
	@set -e; \
	version=$$(sed -ne '/^CPDVER=/s/.*=\([0-9.]*\)/\1/p' config.in); \
	echo "Making mingw32-cpd-$$version.tar.gz"; \
	cvs -q diff || exit 1; \
	test -d ../temp || mkdir ../temp; \
	cd ../temp || exit 2; \
	rm -rf mingw32-cpd-$$version; \
	mkdir mingw32-cpd-$$version || exit 2; \
	cvs -Q checkout -d mingw32-cpd-$$version mingw32-cpd ; \
	cd mingw32-cpd-$$version || exit 2; \
	tagver=V-$$(echo $$version| sed -e 's/\./-/g'); \
	echo "Tagging the CVS archives with $$tagver"; \
	cvs -Q tag $$tagver || exit 2; \
	rm -r $$(find . -name CVS -type d); \
	cd .. || exit 2; \
	echo "Creating tar file"; \
	tar czf mingw32-cpd-$$version.tar.gz mingw32-cpd-$$version





make-binutils:
	make -C $(BINUTILS_SRC_PATH)
	make -C $(BINUTILS_SRC_PATH) install
	set -e; cd $(PREFIX)/bin; ln -sf ../$(TARGET)/bin/dlltool .

make-gcc: make-binutils
	-mkdir $(PREFIX)/$(TARGET)/include
	cp -a src/include/* $(PREFIX)/${TARGET}/include
	make -C $(GCC_SRC_PATH) LANGUAGES="c"
	make -C $(GCC_SRC_PATH) LANGUAGES="c" install
	rm -rf $(gcclibdir)/include
	sed -e "s!@@TARGET_INC@@!$(target_dir)!" patches/specs > $(gcclibdir)/specs
	-strip $(PREFIX)/bin/$(TARGET)-*
	-strip $(PREFIX)/$(TARGET)/bin/*

