#!/usr/bin/make -f

DEB_BUILDDIR := $(shell pwd)/objdir
DEB_DH_INSTALL_SOURCEDIR := $(shell pwd)/debian/tmp

# This implements the .deb package creation using debhelper.
include /usr/share/cdbs/1/rules/debhelper.mk

# This implements building using a configure script and Makefile.
include /usr/share/cdbs/1/class/autotools.mk

# Quilt-based patches in debian/patches.
include /usr/share/cdbs/1/rules/patchsys-quilt.mk

# Override CDBS's default CFLAGS, which also includes -Wall; gdb
# does not handle -Wunused well with -Werror, but defaults to
# -Werror.
CFLAGS = -g $(DEB_OPT_FLAG)

# Always install into debian/tmp, even if we are only building one package.
DEB_DESTDIR := $(shell pwd)/debian/tmp

install = /usr/bin/install -p

# Rather paranoid than sorry. Make the shell exit with an error if an
# untested command fails.
SHELL += -e

DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_GNU_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# Cross configuration support.  Check for an environment variable
# $GDB_TARGET, or a file debian/target.
ifndef GDB_TARGET
DEBIAN_TARGET_FILE := $(strip $(shell cat debian/target 2>/dev/null))
ifneq ($(DEBIAN_TARGET_FILE),)
GDB_TARGET := $(DEBIAN_TARGET_FILE)
endif
endif

DEB_TARGET_ARCH	:= $(shell dpkg-architecture -f \
		     -a$(GDB_TARGET) -qDEB_HOST_ARCH 2>/dev/null)
DEB_TARGET_GNU_TYPE := $(shell dpkg-architecture -f \
			 -a$(DEB_TARGET_ARCH) -qDEB_HOST_GNU_TYPE 2>/dev/null)
DEB_TARGET_ALIAS ?= $(DEB_TARGET_GNU_TYPE)

ifeq ($(DEB_TARGET_ARCH),)
$(error GDB_TARGET value "$(GDB_TARGET)" is not a valid Debian architecture)
endif

ifdef GDB_TARGET
  DEB_CROSS = yes
  # TP: Target Prefix. Used primarily as a prefix for cross tool
  #     names (e.g. powerpc-linux-gcc).
  # TS: Target Suffix. Used primarily at the end of cross compiler
  #     package names (e.g. gcc-powerpc).
  TP = $(DEB_TARGET_ALIAS)-
  TS = -$(DEB_TARGET_ALIAS)
  CROSS_FORCE = FORCE
else
ifneq (,$(filter $(DEB_HOST_ARCH),i386 powerpc sparc s390))
  build64 = no
  CC64 = gcc -m64
  CFLAGS64 = $(CFLAGS)
  BUILDDIR64 = objdir64
  ifeq ($(DEB_HOST_ARCH),i386)
    HOST64 = x86_64-linux-gnu
  endif
  ifeq ($(DEB_HOST_ARCH),powerpc)
    HOST64 = powerpc64-linux-gnu
  endif
  ifeq ($(DEB_HOST_ARCH),sparc)
    HOST64 = sparc64-linux-gnu
  endif
  ifeq ($(DEB_HOST_ARCH),s390)
    HOST64 = s390x-linux-gnu
  endif
endif
endif

run_tests := yes

ifneq ($(DEB_HOST_GNU_SYSTEM),linux-gnu)
  run_tests := no
endif

ifeq ($(DEB_HOST_GNU_CPU),ia64)
  arch_config_args := --with-libunwind
endif

ifdef GDB_TARGET
  run_tests := no

  arch_config_args += --program-prefix=$(TP) \
    --target=$(DEB_TARGET_ALIAS) --with-sysroot=/usr/$(DEB_TARGET_ALIAS)
endif

INSTALL = $(install)
export INSTALL

# We pass srcdir explicitly to avoid an extra '/.' at the end of it.  That
# causes a harmless, but ugly, testsuite failure in maint.exp.
EXTRA_FLAGS := --disable-gdbtk --disable-shared --disable-tui \
	--srcdir=$(shell pwd) --with-system-readline \
	$(arch_config_args) --build=$(DEB_HOST_GNU_TYPE)

# Debian does not include 64-bit expat packages yet, so --with-expat
# is here rather than in EXTRA_FLAGS.  Similarly Python.
DEB_CONFIGURE_EXTRA_FLAGS := --host=$(DEB_HOST_GNU_TYPE) $(EXTRA_FLAGS) \
	--with-expat --without-python --disable-werror

DEB_MAKE_BUILD_TARGET = all info

post-patches::
	if ! test -f gdb/version.in.backup; then \
	  mv -f gdb/version.in gdb/version.in.backup; \
	  sed -e 's/$$/-debian/' < gdb/version.in.backup > gdb/version.in; \
	fi

# This should probably be common-post-build-arch, but that runs during
# install, under fakeroot, in CDBS 0.4.21.
build/gdb$(TS):: check-stamp
	$(MAKE) -C objdir/gdb/doc refcard.dvi refcard.ps

build/gdb64:: build64-stamp

build64-stamp:
	mkdir -p $(BUILDDIR64)
	cd $(BUILDDIR64) && CC="$(CC64)" CFLAGS="$(CFLAGS64)" AR=ar \
	  ../configure $(EXTRA_FLAGS) --host=$(HOST64)
	cd $(BUILDDIR64) && $(MAKE)
	touch $@

check-stamp:
ifeq ($(run_tests),yes)
	-ulimit -c unlimited; \
	$(MAKE) -C objdir/gdb check \
		2>&1 | tee objdir/check.log
endif
	touch $@

clean::
	rm -f check-stamp build64-stamp
	rm -rf objdir $(BUILDDIR64)

	if test -f gdb/version.in.backup; then \
	  mv -f gdb/version.in.backup gdb/version.in; \
	fi

#	# For snapshots this is appropriate; careful of release tarballs
#	# which include .gmo files.
#	find -type f -name '*.gmo' | xargs rm -f

	# Prevent gratuitous rebuilds of the BFD documentation, since it
	# updates the copy in the source directory.
	find bfd -name bfd.info\* | xargs --no-run-if-empty touch

binary-post-install/gdb$(TS) ::
	if [ -x debian/tmp/usr/bin/run ]; then				\
		mv debian/tmp/usr/bin/run					\
		  debian/gdb$(TS)/usr/bin/$(DEB_TARGET_ALIAS)-run;		\
		mv debian/tmp/usr/share/man/man1/run.1			\
		  debian/gdb$(TS)/usr/share/man/man1/$(DEB_TARGET_ALIAS)-run.1;	\
	fi
ifeq ($(run_tests),yes)
	install -d debian/gdb$(TS)/usr/share/doc/gdb
	install -m 644 objdir/check.log \
		debian/gdb$(TS)/usr/share/doc/gdb/check.log
endif

ifneq ($(DEB_CROSS),yes)
	# Gcore is only useful for native GDB.
	$(INSTALL) -m 755 -o root -g root \
		gdb/gdb_gcore.sh debian/gdb$(TS)/usr/bin/$(TP)gcore
endif

ifeq ($(build64),yes)
	install -d debian/gdb64/usr/bin
	install -s -m 755 $(BUILDDIR64)/gdb/gdb debian/gdb64/usr/bin/gdb64
	dh_link -pgdb64 usr/share/man/man1/gdb.1 usr/share/man/man1/gdb64.1
endif

	rm -rf debian/tmp/usr/include \
		debian/tmp/usr/lib/lib*.* \
		debian/tmp/usr/share/locale

	# Remove the entire info directory; it only contains dummy files,
	# not the real manuals.
	rm -rf debian/tmp/usr/share/info

ifneq ($(DEB_CROSS),yes)
	# libgdb-dev is only required for native GDB.
	install -d debian/tmp/usr/lib
	install -m 644 $(DEB_BUILDDIR)/gdb/libgdb.a debian/tmp/usr/lib/
	if test -e $(DEB_BUILDDIR)/sim/*/libsim.a; then \
	  install -m 644 $(DEB_BUILDDIR)/sim/*/libsim.a debian/tmp/usr/lib/; \
	fi
endif

debian/control:: debian/control.in $(CROSS_FORCE)
	cat debian/control.in \
		| sed "s/@kfreebsd@/`type-handling any kfreebsd`/g" \
		| sed "s/@gnu@/`type-handling any gnu`/g" \
		| sed "s/@TS@/$(TS)/g" \
		> debian/control
ifeq ($(DEB_CROSS),yes)
	sed -i "/Package: gdb64/,\$$ d" debian/control
	cp debian/gdb.install debian/gdb$(TS).install
endif

DEB_INSTALL_DOCS_gdb$(TS) = gdb/NEWS gdb/README gdb/doc/refcard.tex \
		objdir/gdb/doc/refcard.dvi objdir/gdb/doc/refcard.ps
DEB_INSTALL_CHANGELOGS_gdb$(TS) = gdb/ChangeLog
DEB_INSTALL_MANPAGES_gdb$(TS) = gdb/gdb.1
ifneq ($(DEB_CROSS),yes)
DEB_INSTALL_MANPAGES_gdb$(TS) += debian/gcore.1
endif

DEB_INSTALL_DOCS_libgdb-dev = gdb/NEWS gdb/README
DEB_INSTALL_CHANGELOGS_libgdb-dev = gdb/ChangeLog

.PHONY: FORCE
FORCE:
