# -*- 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

.NOTPARALLEL:

patchdir	?= debian/patches
series_file	?= $(patchdir)/series

# which patches should be applied?

debian_patches = \
	$(if $(with_linaro_branch),gcc-linaro) \
	$(if $(with_linaro_branch),gcc-linaro-revert-106905) \
	svn-updates$(if $(with_linaro_branch),-linaro) \

ifeq ($(with_java),yes)
#  debian_patches += \
#	svn-class-updates
endif

ifneq ($(GFDL_INVARIANT_FREE),yes)
  debian_patches += \
	rename-info-files \
	$(if $(with_linaro_branch),gcc-linaro-doc) \
	$(if $(with_linaro_branch),gcc-linaro-revert-106905-doc) \

#	$(if $(with_linaro_branch),,svn-doc-updates) \

else
endif
debian_patches += \
	gcc-gfdl-build

#	boehm-gc-nocheck: seems to work on the buildds \

debian_patches += \
	gcc-textdomain \
	gcc-driver-extra-langs

ifeq ($(distribution),Ubuntu)
  ifneq (,$(filter $(distrelease),dapper hardy intrepid jaunty karmic lucid))
    debian_patches += gcc-hash-style-both
  else
    debian_patches += gcc-hash-style-gnu
  endif
else
  debian_patches += gcc-hash-style-both
endif

debian_patches += \
	libstdc++-pic \
	libstdc++-doclink \
	libstdc++-man-3cxx \
	libstdc++-test-installed \
	libjava-stacktrace \
	libjava-jnipath \
	libjava-sjlj \
	libjava-disable-plugin \
	alpha-no-ev4-directive \
	boehm-gc-getnprocs \
	note-gnu-stack \
	libgomp-omp_h-multilib \
	sparc-force-cpu \
	pr24619 \
	gccgo-version \
	no_fpr_in_libgcc \
	pr48226 \
	pr47487 \
	mudflap-varargs \
	mudflapth-link \
	pr45078 \
	pr43804 \
	pr47818 \
	$(if $(with_linaro_branch),,pr48830) \
	$(if $(with_linaro_branch),,pr49169) \
	pr49756 \
	pr49940 \
	pr49944 \
	gcc-cloog-dl \
	libffi-kfreebsd \
	libffi-m68k \
	gcc-base-version \
	pr49696 \
	libffi-powerpc-sf \
	libffi-powerpc-sysv-without-string-ops \
	$(if $(with_linaro_branch),,pr49030) \
	$(if $(with_linaro_branch),,pr50193) \
	pr50114 \
	$(if $(with_linaro_branch),,pr50946) \
	pr47955 \
	m68k-fp-cmp-zero \
	$(if $(with_linaro_branch),,gcc-arm-abi-conformance) \
	sh4-enable-ieee \
	gcc-gengtype-fix1 \
	gcc-gengtype-fix2 \

#	$(if $(filter yes, $(DEB_CROSS)),,gcc-print-file-name) \
#	libstdc++-nothumb-check \
# TODO: update ...
#	libjava-rpath \

hardening_patches =
ifneq ($(distribution),Debian)
  ifneq (,$(findstring gcc-4, $(PKGSOURCE)))
    hardening_patches += gcc-default-format-security \
	gcc-default-fortify-source \
	gcc-default-relro \
	testsuite-hardening-format \
	testsuite-hardening-printf-types
  endif
endif
ifeq ($(with_ssp)-$(with_ssp_default),yes-yes)
  ifeq ($(distribution),Ubuntu)
    hardening_patches += gcc-default-ssp$(if $(filter ppc64,$(DEB_TARGET_ARCH)),-ppc64)
  else
    hardening_patches += gcc-default-ssp
  endif
endif

# FIXME 4.5: Drop and adjust symbols files
ifneq (,$(findstring 4.4, $(PKGSOURCE)))
  debian_patches += pr39491
endif

ifeq ($(with_ada),yes)
  debian_patches += \
	ada-driver-check \
	ada-gcc-name \
	ada-default-project-path \
	ada-symbolic-tracebacks \
	ada-library-project-files-soname \
	ada-ppc64

  ifeq ($(biarch64),yes)
    debian_patches += \
	ada-nobiarch-check
  endif

  ifeq ($(with_libgnat),yes)
    debian_patches += \
	ada-link-lib \
	ada-libgnatvsn \
	ada-libgnatprj \
	ada-acats
    ifeq ($(with_gnat_zcx)-$(with_gnat_sjlj),yes-yes)
      debian_patches += \
	ada-sjlj
    endif
  endif

  ifneq (,$(findstring $(DEB_TARGET_ARCH_OS),kfreebsd hurd))
    debian_patches += ada-kfreebsd-gnu \
	ada-bug564232
  endif

  ifeq ($(DEB_TARGET_ARCH_OS),hurd)
    debian_patches += \
        gcc_ada_gcc-interface_Makefile.in \
	ada-s-osinte-gnu.adb \
	ada-s-osinte-gnu.ads \
	ada-s-taprop-gnu.adb
  endif

endif

ifeq ($(with_d),yes)
  debian_patches += \
       gdc-4.6 \
       gdc-libphobos-math \
       gdc-driver-zlib

  ifeq ($(with_libphobos),yes)
    debian_patches += gdc-libphobos-build
  else
    debian_patches += gdc-driver-nophobos
  endif

else
  debian_patches += gcc-d-lang
endif

ifeq ($(DEB_TARGET_ARCH_OS),hurd)
  debian_patches += hurd-pthread
endif

ifeq ($(DEB_TARGET_ARCH),alpha)
  debian_patches += alpha-ieee mudflap-nocheck
  ifneq ($(GFDL_INVARIANT_FREE),yes)
    debian_patches += alpha-ieee-doc
  endif
endif

ifneq (,$(findstring $(DEB_TARGET_ARCH),arm armel armhf))
  debian_patches += libjava-armel-unwind
endif

ifeq ($(DEB_TARGET_ARCH),m68k)
  debian_patches += pr47612
  debian_patches += m68k-revert-pr45144
  debian_patches += m68k-ada
endif

ifeq ($(DEB_TARGET_ARCH),powerpcspe)
  debian_patches += powerpc_remove_many
endif

ifeq ($(DEB_TARGET_ARCH_OS),kfreebsd)
  debian_patches += kbsd-gnu
endif

ifeq ($(DEB_TARGET_ARCH),ppc64)
  ifeq ($(distribution),Ubuntu)
    debian_patches += ibm-branch
  endif
endif

#spu_patches = cell-branch
#ifneq ($(GFDL_INVARIANT_FREE),yes)
#  spu_patches += cell-branch-doc
#endif

#debian_patches += link-libs

debian_patches += libjava-disable-static

# all patches below this line are applied for gcc-snapshot builds as well

ifeq ($(single_package),yes)
  spu_patches =
  debian_patches =
endif

ifneq ($(trunk_build),yes)
  debian_patches += s390-biarch
  debian_patches += mips-triarch
endif

debian_patches += $(if $(with_linaro_branch),,arm-dynamic-linker)
ifeq ($(with_softfloat),yes)
  debian_patches += arm-multilib-soft-float
else ifeq ($(multilib),yes)
  ifneq (,$(filter $(distrelease),lucid maverick natty oneiric precise))
    debian_patches += arm-multilib-softfp
  else
    debian_patches += arm-multilib-soft
  endif
endif
debian_patches += arm-multilib-defaults

ifeq ($(DEB_CROSS),yes)
  ifeq ($(trunk_build),yes)
    debian_patches += cross-include-trunk cross-fixes-trunk
  else
    debian_patches += cross-include cross-fixes
  endif
endif

ifeq ($(DEB_TARGET_ARCH_OS),hurd)
  debian_patches += hurd-changes hurd-fixes
endif

debian_patches += gcc-ice-hack gcc-ice-apport
#FIXME
#debian_patches += gold-and-ld

debian_patches += libjava-fixed-symlinks
debian_patches += libstdc++-arm-wno-abi

ifneq (,$(filter $(DEB_TARGET_ARCH), armel mipsel))
  # timeouts on the buildd's, both Debian and Ubuntu
  #debian_patches += libstdc++-no-testsuite
endif
debian_patches += ada-mips
debian_patches += libffi-ro-eh_frame_sect

ifneq ($(trunk_build),yes)
debian_patches += gcc-multiarch
ifneq ($(GFDL_INVARIANT_FREE),yes)
  debian_patches += gcc-multiarch-doc
endif
ifeq ($(with_multiarch_lib),yes)
  debian_patches += libjava-multiarch
endif
endif # gcc-snapshot
debian_patches += libjava-nobiarch-check
debian_patches += config-ml
ifeq ($(biarch64),yes)
  ifeq ($(DEB_CROSS),yes)
    debian_patches += cross-biarch
  endif
endif
ifeq ($(trunk_build),yes)
  debian_patches += gcc-powerpc-nof-trunk
else
  debian_patches += gcc-powerpc-nof
endif
debian_patches += gcc-powerpc-undef
ifneq (,$(findstring /$(DEB_TARGET_ARCH)/,$(biarch32archs)))
  ifeq ($(with_multiarch_lib),yes)
    ifeq ($(trunk_build),yes)
      debian_patches += gcc-multilib64-multiarch-trunk
    else
      debian_patches += gcc-multilib64-multiarch
    endif
  else
    debian_patches += gcc-multilib64
  endif
endif

ifeq (,$(filter $(distrelease),lenny squeeze dapper hardy intrepid jaunty karmic lucid maverick))
  debian_patches += gcc-no-add-needed
endif
ifeq (,$(filter $(distrelease),lenny squeeze wheezy sid experimental dapper hardy intrepid jaunty karmic lucid maverick))
  debian_patches += gcc-as-needed
endif
debian_patches += mips-fix-loongson2f-nop
ifeq ($(distribution),Ubuntu)
  ifeq ($(DEB_TARGET_ARCH),ppc64)
    debian_patches += gcc-ppc64-O3
  endif
endif
debian_patches += gcc-system-root
debian_patches += libgomp-kfreebsd-testsuite

ifeq ($(trunk_build),yes)
  debian_patches += armhf-triplet-trunk
else
  debian_patches += armhf-triplet
endif

#debian_patches += address-clauses-timed-entry-calls

series_stamp = $(stampdir)/02-series-stamp
series: $(series_stamp)
$(series_stamp):
	echo $(strip $(addsuffix .diff,$(debian_patches))) \
	  | sed -r 's/  +/ /g' | tr " " "\n" > $(series_file)
ifneq (,$(strip $(hardening_patches)))
  ifneq ($(trunk_build),yes)
	echo $(strip $(addsuffix .diff,$(hardening_patches))) \
	  | sed -r 's/  +/ /g' | tr " " "\n" >> $(series_file)
  endif
endif
	sed -r 's/(.)$$/\1 -p1/' -i $(series_file)
	touch $@

autotools_files := $(addprefix ./,$(foreach file,$(shell lsdiff --no-filename \
	$(foreach patch,$(debian_patches),$(patchdir)/$(patch).diff) \
	| sed -r 's/[ab]\/src\//src\//' | sort | uniq),$(shell echo $(file) \
	| egrep 'configure\.(ac|in)|Makefile\.(am|in)|acinclude.m4')))

autoconf_version = 2.64
ifeq ($(trunk_build),yes)
  # The actual version depends on the build-dependencies set by
  # variable AUTO_BUILD_DEP in rules.conf.  Here, we assume the
  # correct version is installed.
  #autoconf_version =
endif

# FIXME: the auto* stuff is done every time for every subdir, which
# leads to build errors. Idea: record the auto* calls in the patch
# files (AUTO <dir> <auto-command with options>) and run them separately,
# maybe only once per directory).
$(patch_stamp): $(unpack_stamp) $(series_stamp)
	QUILT_PATCHES=$(patchdir) \
	  quilt --quiltrc /dev/null push -a || test $$? = 2

ifneq (,$(filter svn-updates, $(debian_patches)))
	awk '/^EOF/ {exit} p==1 {print} /EOF$$/ {p=1}' \
	  $(patchdir)/svn-updates.diff > src/LAST_UPDATED
endif

	if ! test -f ./autotools_files ; then touch ./autotools_files ; fi

	cd $(srcdir)/fixincludes && ./genfixes

	for f in $(autotools_files) ; \
	  do case $$f in \
	    */classpath/m4/acinclude.m4) \
	        : ;; \
	    */configure.*|*/acinclude.m4) \
		if grep ^"$$(md5sum $$f)"$$ ./autotools_files >/dev/null ; \
		  then echo "Skipping already regenerated file $$f." ; \
		else \
		  echo "Running autoconf$(autoconf_version) in $$(dirname $$f)..." ; \
		  dir="$(CURDIR)"; cd $(CURDIR)/$$(dirname $$f) \
		    && AUTOM4TE=/usr/bin/autom4te$(autoconf_version) autoconf$(autoconf_version) \
		    && cd $$dir \
		    && echo "$$(md5sum $$f)" >> ./autotools_files ; \
		fi ;; \
	    */Makefile.*) ;; \
	    *) echo "Unknown file: $$f"; false; \
	  esac; \
	done

	for i in $(debian_patches); do \
	  echo -e "\n$$i:" >> pxxx; \
	  sed -n 's/^# *DP: */  /p' $(patchdir)/$$i.diff >> pxxx; \
	done
#	-$(srcdir)/move-if-change pxxx $@
	mv pxxx $@

unpatch:
	QUILT_PATCHES=$(patchdir) \
	  quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc
	for f in $(autotools_files); do \
	  rm -f $$(echo $$f | sed -r 's/\.(ac|am|in)$$//'); \
	done

$(src_spu_stamp): $(patch_stamp)
	rm -rf src-spu
ifeq (,$(strip $(hardening_patches)))
	ln -s src src-spu
else
	cp -a src src-spu
	set -e; \
	for p in $(hardening_patches); do \
	  list="$$p $$list"; \
	done; \
	for p in $$list; do \
	  echo "Revert for spu build: $$p"; \
	  patch -d src-spu -p2 -R < debian/patches/$$p.diff; \
	done
endif
	set -e; \
	for p in $(spu_patches); do \
	  echo "Apply for spu build: $$p"; \
	  patch -d src-spu -p2 < debian/patches/$$p.diff; \
	done
	touch $@

update-patches: $(series_stamp)
	export QUILT_PATCHES=$(patchdir); \
	export QUILT_REFRESH_ARGS="--no-timestamps --no-index -pab"; \
	export QUILT_DIFF_ARGS="--no-timestamps --no-index -pab"; \
	while quilt push; do quilt refresh; done

patch: $(patch_stamp)
.PHONY: patch series quilt autotools
