# -*- 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 = \
	git-updates \
	$(if $(with_linaro_branch),gcc-linaro) \
	$(if $(with_linaro_branch),gcc-linaro-no-macros) \

#	git-updates \

ifneq ($(GFDL_INVARIANT_FREE),yes)
  debian_patches += \
	git-doc-updates \
	$(if $(with_linaro_branch),gcc-linaro-doc) \
	rename-info-files \
	pr97250-doc \
	pr97250-follow-up-doc \

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

else
endif
debian_patches += \
	gcc-gfdl-build

debian_patches += \
	gcc-textdomain \
	gcc-distro-specs \
	gcc-driver-extra-langs \
	gcc-hash-style-gnu \
	libstdc++-pic \
	libstdc++-doclink \
	libstdc++-man-3cxx \
	libstdc++-test-installed \
	alpha-no-ev4-directive \
	note-gnu-stack \
	libgomp-omp_h-multilib \
	libgo-testsuite \
	libgo-cleanfiles \
	gcc-target-include-asm \
	libgo-revert-timeout-exp \
	libgo-setcontext-config \
	gcc-auto-build \
	libitm-no-fortify-source \
	sparc64-biarch-long-double-128 \
	pr66368 \
	pr67590 \
	libjit-ldflags \
	libffi-pax \
	libffi-race-condition \
	cuda-float128 \
	libffi-mipsen-r6 \
	t-libunwind-elf-Wl-z-defs \
	gcc-force-cross-layout \
	gcc-search-prefixed-as-ld \
	kfreebsd-decimal-float \
	pr87808 \
	libgomp-no-werror \
	gdc-cross-build \
	pr94253 \
	aarch64-offload \
	pr97250-0 \
	pr97250-1 \
	pr97250-2 \
	pr97250-3 \
	pr97250-follow-up \
	pr98274 \
	pr97714 \
	pr98920 \
	pr99773 \
	musl-ssp \
	pr99264-follow-up \

ifneq (,$(filter $(distrelease), jessie stretch buster precise trusty xenial bionic))
  debian_patches += pr85678-revert
  ifneq ($(GFDL_INVARIANT_FREE),yes)
    debian_patches += pr85678-doc-revert
  endif
endif

#TODO: still necessary?
#	verbose-lto-linker \

# TODO:
#	pr81829 \

#	$(if $(filter yes, $(DEB_CROSS)),,gcc-print-file-name) \
#	libstdc++-nothumb-check \

hardening_patches =
ifneq (,$(filter $(derivative),Ubuntu))
  ifneq (,$(findstring gcc-10, $(PKGSOURCE)))
    hardening_patches += \
	gcc-distro-specs-ubuntu-doc \
	testsuite-hardening-format \
	testsuite-hardening-printf-types \
	testsuite-hardening-updates \
	testsuite-glibc-warnings
  endif
else ifneq (,$(filter $(derivative),Debian))
  ifneq (,$(findstring gcc-10, $(PKGSOURCE)))
#    ifneq ($(with_pie),yes)
#      hardening_patches += \
#        ignore-pie-specs-when-not-enabled
#    endif
  endif
endif

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

# Patches for non-core languages.

debian_patches += gm2 gm2-texinfo gm2-bootstrap-compare
# This seems to work now ...
#debian_patches += gm2-no-lto
debian_patches += gm2-jit-def
debian_patches += gm2-link

# Most of the time, it would be safe to apply them whether the
# language is selected or not. But when working on a new GCC version,
# it is convenient to concentrate on core languages, and refresh them
# later when working on the specific language.
ifeq ($(with_ada),yes)
    debian_patches += ada-gcc-name
    debian_patches += ada-verbose
  ifeq ($(biarch64),yes)
    debian_patches += ada-nobiarch-check
  endif
    debian_patches += ada-link-lib
    debian_patches += ada-libgnat_util
    debian_patches += ada-gnattools-cross
    ifeq ($(with_gnatsjlj),yes)
      debian_patches += ada-sjlj
    endif
    debian_patches += ada-lib-info-source-date-epoch
    debian_patches += ada-armel-libatomic
    debian_patches += ada-kfreebsd
    debian_patches += ada-749574
    debian_patches += ada-perl-shebang
endif

# FIXME: still relevant?
#	gdc-updates \
#	gdc-multiarch

ifeq ($(GFDL_INVARIANT_FREE),yes)
  debian_patches += gdc-texinfo
endif
ifneq ($(with_phobos),yes)
  debian_patches += gdc-driver-nophobos
endif
ifeq (,$(filter $(DEB_TARGET_ARCH),amd64 i386 armhf))
  debian_patches += disable-gdc-tests
endif

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

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

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

debian_patches += \
	sys-auxv-header \
	gdc-dynamic-link-phobos \
	ia64-disable-selective-scheduling \
	gcc-foffload-default \
	libstdc++-pythondir \

ifeq ($(with_ibm_branch),yes)
  debian_patches += ibm-branch
endif

ifeq ($(with_softfloat),yes)
  debian_patches += arm-multilib-soft-float
else ifeq ($(multilib),yes)
  ifneq (,$(biarchhfarchs)$(biarchsfarchs))
    ifneq (,$(filter $(distrelease), precise))
      debian_patches += arm-multilib-softfp$(if $(filter yes,$(DEB_CROSS)),-cross)
    else
      debian_patches += arm-multilib-soft$(if $(filter yes,$(DEB_CROSS)),-cross)
    endif
  endif
endif
ifneq (,$(filter $(distrelease), precise trusty xenial bionic focal groovy hirsute))
  debian_patches += arm-multilib-defaults
endif

ifeq ($(DEB_CROSS),yes)
  debian_patches += cross-fixes
  debian_patches += cross-install-location
  ifeq ($(with_m2),yes)
    debian_patches += cross-install-location-gm2
  endif
endif

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

debian_patches += gcc-ice-dump
debian_patches += gcc-ice-apport
debian_patches += skip-bootstrap-multilib
debian_patches += libffi-ro-eh_frame_sect
debian_patches += libffi-mips

# sigaction on sparc changed between glibc 2.19 and 2.21
ifeq (,$(filter 2.1%, $(shell dpkg-query -l libc-bin | awk '/^.i/ {print $$3}')))
  # keep it, gets remove in GCC from time to time
  #debian_patches += pr67899
endif

debian_patches += gcc-multiarch
debian_patches += config-ml
ifneq ($(single_package),yes)
  ifeq ($(with_multiarch_cxxheaders),yes)
    debian_patches += g++-multiarch-incdir
    debian_patches += canonical-cpppath
  endif
endif
ifneq (,$(filter $(build_type), build-cross cross-build-cross))
  debian_patches += cross-no-locale-include
  debian_patches += cross-biarch
endif
debian_patches += gcc-multilib-multiarch

ifneq ($(trunk_build),yes)
ifneq (,$(filter $(derivative),Ubuntu))
  debian_patches += gcc-as-needed
  ifeq (,$(filter $(distrelease), precise trusty xenial))
    debian_patches += gcc-as-needed-gold
  endif
else # Debian
  ifeq (,$(filter $(distrelease), jessie stretch))
    debian_patches += gcc-as-needed gcc-as-needed-gold
  endif
endif
endif

debian_patches += libgomp-kfreebsd-testsuite
debian_patches += go-testsuite

# don't remove, this is regularly overwritten, see PR sanitizer/63958.
#debian_patches += libasan-sparc

# Has to be refreshed manually as described in the header.
# Mostly for Ada, but not only (bootstrap-no-unneeded-libs).
debian_patches += ada-changes-in-autogen-output

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 $@

autoconf_files = $(shell lsdiff --no-filename $(foreach patch,$(debian_patches),$(patchdir)/$(patch).diff) \
	    | sed -rn '/(configure\.ac|acinclude.m4)$$/s:[^/]+/src/:src/:p' | sort -u)
autoconf_dirs = $(sort $(dir $(autoconf_files)))

automake_files = $(addprefix ./, $(filter-out none, \
	  $(shell lsdiff --no-filename $(foreach patch,$(debian_patches),$(patchdir)/$(patch).diff) \
	    | sed -rn '/Makefile\.(am|in)$$/s:[^/]+/src/:src/:p' | sort -u)))

autoconf_version = 2.69
ifneq (,$(filter $(distrelease),stretch buster trusty xenial bionic focal groovy))
  autoconf_version =
endif
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)
	sync
	QUILT_PATCHES=$(patchdir) QUILT_PATCH_OPTS='-E' \
	  quilt --quiltrc /dev/null push -a || test $$? = 2

	: # only needed when we have changes, and currently fails with autogen 5.18
	: #cd $(srcdir)/fixincludes && ./genfixes

	sync
	echo -n $(autoconf_dirs) | xargs -d ' ' -L 1 -P $(USE_CPUS) -I{} \
	  sh -c 'echo "Running autoconf$(autoconf_version) in {}..." ; \
	  cd $(CURDIR)/{} && rm -f configure && \
	  AUTOM4TE=/usr/bin/autom4te$(autoconf_version) autoconf$(autoconf_version)'

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

	: # generate the distro-defaults.h header
	rm -f $(srcdir)/gcc/distro-defaults.h
	( \
	  echo '/* distro specific configuration injected by the distro build.  */'; \
	  echo ''; \
	  echo '#ifndef ACCEL_COMPILER' \
	) >> $(srcdir)/gcc/distro-defaults.h
ifeq ($(with_async_unwind),yes)
	echo '#define DIST_DEFAULT_ASYNC_UNWIND 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
endif
ifeq ($(with_ssp)-$(with_ssp_default),yes-yes)
	echo '#define DIST_DEFAULT_SSP 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
  ifeq (,$(filter $(distrelease), precise trusty))
	echo '#define DIST_DEFAULT_SSP_STRONG 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
  endif
	echo '#define DIST_DEFAULT_FORMAT_SECURITY 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
endif
ifneq (,$(filter $(derivative),Ubuntu))
	echo '#define DIST_DEFAULT_FORTIFY_SOURCE 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
	echo '#define DIST_DEFAULT_RELRO 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
	echo '#define DIST_DEFAULT_BIND_NOW 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
  ifneq (,$(findstring gcc-10, $(PKGSOURCE)))
# FIXME: this is directly patched
#	echo '#define DIST_DEFAULT_FORTIFY_SOURCE 1' \
#	     >> $(srcdir)/gcc/distro-defaults.h
  endif
  ifeq ($(with_stack_clash),yes)
	echo '#define DIST_DEFAULT_STACK_CLASH 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
  endif
  ifeq ($(with_cf_protection),yes)
	echo '#define DIST_DEFAULT_CF_PROTECTION 1' \
	     >> $(srcdir)/gcc/distro-defaults.h
  endif
else ifneq (,$(filter $(derivative),Debian))
  ifneq (,$(findstring gcc-10, $(PKGSOURCE)))
  endif
endif
	  echo '#endif' \
	     >> $(srcdir)/gcc/distro-defaults.h

	mv pxxx $@

unpatch:
	QUILT_PATCHES=$(patchdir) \
	  quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc

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
