#!/usr/bin/make -f
# -*- makefile -*-
# Build gcc-mingw-w64 using gcc-4.6-source.
# Copyright © 2010 Stephen Kitt <steve@sk2.org>

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

target_version := 4.6

top_dir := $(shell pwd)
gcc_dir := /usr/src/gcc-$(target_version)
upstream_dir := $(top_dir)/src
build_dir := $(top_dir)/build
stampdir := $(top_dir)/stamp
deb_version := $(shell dpkg-query -W -f="\$${Version}\n" gcc-$(target_version)-source)
deb_upstream_version := $(shell echo $(deb_version) | cut -d- -f1)
base_version := $(shell echo $(deb_version) | sed -e 's/\([1-9]\.[0-9]\).*-.*/\1/')

# Include the rules variant - symlink rules.bootstrap to produce the
# minimal gcc required to build mingw-w64, or rules.full to produce
# the full gcc.
include debian/rules.variant

targets := i686-w64-mingw32 x86_64-w64-mingw32

CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
CFLAGS += -Wall
export CFLAGS
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS += -Wl,--as-needed
export LDFLAGS

# Number of jobs to run for build
ifeq ($(USE_NJOBS),no)
  NJOBS :=
  USE_CPUS := 1
else
  # Increase to 192 if building Java
  MEM_PER_CPU = 128
  NUM_CPUS := $(shell if echo $(USE_NJOBS) | grep -q -E '^[0-9]+$$'; \
                        then echo $(USE_NJOBS); \
                        else getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1; fi)
  USE_CPUS := $(shell mt=`awk '/^MemTotal/ { print $$2 }' /proc/meminfo`; \
                        awk -vn=$(NUM_CPUS) -vmt=$$mt -vm=$(MEM_PER_CPU) \
                                'END { mt/=1024; n2 = int(mt/m); print n==1 ? 1 : n2<n+1 ? n2 : n+1}' < /dev/null)
  ifneq (,$(strip $(USE_CPUS)))
    NJOBS := -j $(USE_CPUS)
  endif
endif

# Support parallel=<n> in DEB_BUILD_OPTIONS (see #209008)
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

# Patch targets
patchdir = $(gcc_dir)/debian/patches
series_file = series
unpack_stamp = $(stampdir)/unpack
patch_stamp = $(stampdir)/patch
GFDL_INVARIANT_FREE=yes
srcdir=$(upstream_dir)
include $(gcc_dir)/debian/rules.patch

unpack: $(unpack_stamp)
$(unpack_stamp):
	tar xf $(gcc_dir)/gcc-*.tar.*
	rm -rf $(upstream_dir)
	mv gcc-$(deb_upstream_version) $(upstream_dir)
	# Apply our extra patches here...
	patch -p0 < debian/patches/mingw-include.patch
	patch -p0 < debian/patches/disable-multilib.patch
	mkdir -p $(stampdir)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf $(stampdir) $(build_dir) $(upstream_dir) .pc autotools_files series
	dh_clean

# Configuration constructed as in the gcc package
PF=usr
libdir=lib
libexecdir=$(PF)/$(libdir)

# Standard Debian configuration flags
# TODO Re-enable shared, multilib and linker-build-id
CONFFLAGS = \
	--prefix=/$(PF) \
	--disable-shared \
	--disable-multilib \
	--enable-linker-build-id \
	--with-system-zlib \
	--libexecdir=/$(libexecdir) \
	--without-included-gettext
CONFFLAGS += \
	--libdir=/$(PF)/$(libdir) \
	--enable-libstdcxx-time=yes \
	--with-tune=generic
# MinGW-w64 flags
CONFFLAGS += \
	--enable-version-specific-runtime-libs
# version-specific-runtime-libs puts target-specific libraries in
# /usr/lib/gcc rather than /usr/$(target)
# Languages
CONFFLAGS += \
	--enable-languages=$(languages)
#CONFFLAGS = --disable-shared --disable-multilib --prefix=/usr --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-languages=c,c++,fortran --with-gnu-ld --with-gnu-as --enable-leading-mingw64-underscores
# LTO
CONFFLAGS += \
	--enable-lto \
	--with-plugin-ld
# Dynamic strings (as used upstream)
CONFFLAGS += \
	--enable-fully-dynamic-string

configure: configure-stamp
configure-stamp: $(patch_stamp)
	dh_testdir
	set -e; \
	# Remove the patch building libstdc++_pic.a before configuring
	patch -R -p2 -dsrc < $(gcc_dir)/debian/patches/libstdc++-pic.diff
	for target in $(targets); do \
		dh_auto_configure -B$(build_dir)/$$target -D$(upstream_dir) -- \
			$(CONFFLAGS) \
			--target=$$target \
			--with-gxx-include-dir=/$(PF)/include/c++/$(base_version) \
			--with-as=/usr/bin/$$target-as \
			--with-ld=/usr/bin/$$target-ld; \
	done
	touch $@

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	set -e; \
	for target in $(targets); do \
		cd $(build_dir)/$$target && $(MAKE) $(NJOBS) $(BUILD_TARGET); \
	done
	touch $@

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	set -e; \
	for target in $(targets); do \
		cd $(build_dir)/$$target && $(MAKE) DESTDIR=$(top_dir)/debian/$(package) $(INSTALL_TARGET); \
	done

	# Remove files which conflict with other packages
	# gcc-$(target_version)-locales
	rm -rf $(top_dir)/debian/$(package)/usr/share/locale
	# binutils-dev
	rm -f $(top_dir)/debian/$(package)/usr/lib/libiberty.a
	# libstdc++6-$(target_version)-dbg (potentially)
	rm -rf $(top_dir)/debian/$(package)/usr/share/gcc-*/python
	# libstdc++6-$(target_version)-dev
	ls -1 $(top_dir)/debian/$(package)/usr/include/c++/$(base_version)/|grep -v w64-mingw32|xargs -I{} rm -rf $(top_dir)/debian/$(package)/usr/include/c++/$(base_version)/{}
	# -doc packages
	rm -rf $(top_dir)/debian/$(package)/usr/share/info
	rm -rf $(top_dir)/debian/$(package)/usr/share/man/man7

	# No need to ship empty manpages
	rm -rf $(top_dir)/debian/$(package)/usr/share/man/man1

	# Drop .la files
	find $(top_dir)/debian/$(package) -name \*.la -delete

	# Move libraries to base directories and add version-specific links
	for gnu_upstream_version in $$(basename debian/gcc-mingw-w64/usr/share/gcc-*|cut -d- -f2); do \
		if [ "$$gnu_upstream_version" != "$(base_version)" ]; then \
			[ -d $(top_dir)/debian/$(package)/usr/include/c++ ] && cd $(top_dir)/debian/$(package)/usr/include/c++ && ln -s $(base_version) $$gnu_upstream_version || true; \
			for target in $(targets); do \
				cd $(top_dir)/debian/$(package)/$(PF)/lib/gcc/$$target && \
				mv $$gnu_upstream_version $(base_version) && \
				ln -s $(base_version) $$gnu_upstream_version; \
			done; \
		fi; \
	done

	for i in 1 2; do \
		find $(top_dir)/debian/$(package)/ -type d -empty | while read i; do rmdir $$i; done; \
	done

	touch $@

binary-indep:
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs $(upstream_dir)/ChangeLog
	# Strip the binaries
	dh_strip -Xw64-mingw32/lib -Xw64-mingw32/$(base_version)/lib
	# Special processing for the libraries
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	strip --remove-section=.comment --remove-section=.note --strip-unneeded $(patsubst %,$(top_dir)/debian/$(package)/usr/lib/gcc/%/$(target_version)/liblto_plugin.so.0.0.0,$(targets))
	for target in $(targets); do \
		$$target-strip --strip-unneeded $(top_dir)/debian/$(package)/usr/lib/gcc/$$target/$(target_version)/lib*.a; \
	done
endif
	dh_lintian
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -v$(deb_version) -Vlocal:Version=$(deb_upstream_version)
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: binary-indep binary-arch binary clean build install
