# -*- makefile -*-
# rules to patch the unpacked files in the source directory
# ---------------------------------------------------------------------------
# various rules to unpack addons and (un)apply patches.
# 	- patch / apply-patches
#	- unpatch / reverse-patches

patchdir	= /usr/src/toolchain/patches/gcc/

# which patches should be applied?
debian_patches += gcc-names gcc-version fastjar-doc
debian_patches += libstdc++-incdir libstdc++-codecvt libstdc++-pic
debian_patches += libstdc++-doclink m68k-bootstrap gcc-line-numbers

ifeq ($(with_java),yes)
  debian_patches += gcj-debian-policy gcj-names gcj-without-rpath \
        gcj-manpage gij-classpath backport-java-6865 gcj-manpage
endif

ifeq ($(with_ffi),yes)
  debian_patches += libffi-install
  ifneq ($(with_java),yes)
    debian_patches += libffi-without-libgcj
  endif
endif

ifeq ($(with_proto),yes)
  debian_patches += deb-protoize
endif

ifeq ($(with_objc),yes)
  ifeq ($(with_check),yes)
    debian_patches += #objc-testsuite
  endif
  debian_patches += libobjc
endif

ifeq ($(with_fortran),yes)
  debian_patches += g77-names
endif

ifeq ($(with_ada),yes)
  debian_patches += ada-missing-lib ada-link-lib ada-gcc-name #ada-names
endif

ifeq ($(with_pascal),yes)
  debian_patches += gpc-3.x gpc-names libgpc-shared
endif

ifeq ($(TARGET_ARCH),alpha)
  debian_patches += 
endif
ifeq ($(TARGET_ARCH),arm)
  debian_patches += arm-libstdc++ arm-tune arm-const-double
#  debian_patches += arm-interwork
#  debian_patches += arm-rdp
endif
ifeq ($(TARGET_ARCH),hppa)
#  debian_patches += hppa-build hppa-align  libstdc++-hppa
  debian_patches +=
endif
ifeq ($(TARGET_ARCH),hurd-i386)
  debian_patches += 
endif
ifeq ($(TARGET_ARCH),ia64)
#  debian_patches += gcc-ia64-NaT
endif
ifeq ($(TARGET_ARCH),m68k)
  debian_patches += m68k-java-build gcc-m68k-config
endif
ifeq ($(DEB_HOST_ARCH),s390)
  debian_patches += s390-java s390-ada s390-biarch s390-config-ml
endif
ifeq ($(TARGET_ARCH),powerpc)
#  debian_patches += libgcc-powerpc
endif
#ifeq ($(TARGET_ARCH),sparc)
#  debian_patches += subreg-byte sparc64-build gcc-sparc
#endif
ifeq ($(with_sparc64),yes)
  debian_patches += sparc64-build
endif
ifeq ($(TARGET_ARCH),avr)
#  debian_patches += avr-64bit-workaround
#debian_patches += avr-no-PC-fix
endif
ifeq ($(DEB_HOST_ARCH),freebsd-i386)
  debian_patches += freebsd-gnu
endif
ifeq ($(DEB_HOST_ARCH),netbsd-i386) 
  debian_patches += netbsd-dynlinker netbsd-gcc-config 
endif 

debian_patches += reporting # applied after gcc-cvs-updates

debian_patches += autoreconf # applied last

patch: $(patch_stamp)
$(patch_stamp): $(unpack_stamp) \
    $(foreach p,$(debian_patches),$(patch_stamp)-$(p))
	echo -e "\nPatches applied in this version:" > pxxx
	for i in $(debian_patches); do \
	  echo -e "\n$$i:" >> pxxx; \
	  sed -n 's/^# *DP: */  /p' $(patchdir)/$$i.dpatch >> pxxx; \
	done
	mv -f pxxx $@

unpatch:
	for stamp in none `ls -1t $(patch_stamp)-*`; do \
	  case "$$stamp" in none|patched-stamp|patched-\*) continue; esac; \
	  patch=`echo $$stamp | sed -e 's,$(patch_stamp)-,,'`; \
	  echo "trying to revert patch $$patch ..."; \
	  if [ -x $(patchdir)/$$patch.dpatch ]; then true; else \
	    chmod +x $(patchdir)/$$patch.dpatch; fi; \
	  if $(patchdir)/$$patch.dpatch -unpatch -d $(srcdir); then \
	    echo "reverted $$patch patch."; \
	    rm -f $$stamp; \
	  else \
	    echo "error in reverting $$patch patch."; \
	    exit 1; \
	  fi; \
	done
	rm -f patched-stamp

$(patch_stamp)-%: $(patchdir)/%.dpatch
	if [ -x $< ]; then true; else chmod +x $<; fi
	if [ -f $@ ]; then \
	  echo "$* patches already applied."; exit 1; \
	fi
	$< -patch -d $(srcdir)
	echo "$* patches applied." > $@
