#!/usr/bin/make -f

# prefix for builds - without leading slash
PF ?= usr

# Linaro toolchain backports PPA support
PPA := false

# if debian/ppa is present then we do PPA
ifneq ($(wildcard debian/ppa),)
  PPA := true
endif

DISTRO ?= $(shell lsb_release -is)

EXTRA_DEPS :=

MIN_VER_BINUTILS     = 2.24-2~

ifeq ($(PPA),false)

  VER_BINUTILS     := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-'|cut -d '.' -f1-3)
  PKG_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-')
  DEB_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' ')

  BINUTILS_DIR  := /usr/src/binutils

else # Linaro toolchain backport PPA

  EXTRA_DEPS       :=
  VER_BINUTILS     := 2.24

  PKG_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils-${VER_BINUTILS}/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-')
  DEB_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils-${VER_BINUTILS}/debian/changelog | egrep '^Version:' | cut -f 2 -d ' ')

  BINUTILS_DIR  := /usr/src/binutils-${VER_BINUTILS}

endif

DEB_NAME_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Source: \(.*\)/\1/p')
DEB_VER_ACT      := $(shell dpkg-parsechangelog| sed -n 's/^Version: \(.*\)/\1/p')

CROSS_ARCH       := $(shell dpkg-parsechangelog| sed -n 's/^Source: .*-\(.*\)-.*/\1/p')
CROSS_GNU_TYPE   := $(shell dpkg-architecture -a${CROSS_ARCH} -qDEB_HOST_GNU_TYPE -f)
HOST_ARCH        := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
HOST_GNU_TYPE    := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
stamp            := stamp-dir/

# taken from gcc packaging
define unpack_tarball
	tar -x -f $1
endef

info:
	@echo "Binutils version:         ${VER_BINUTILS}	${DEB_VER_BINUTILS}	${PKG_VER_BINUTILS}"
	@echo
	@echo "Target architecture:      ${CROSS_ARCH}"
	@echo "Target GNU type:          ${CROSS_GNU_TYPE}"
	@echo "Host architecture:        ${HOST_ARCH}"
	@echo "Host GNU type:            ${HOST_GNU_TYPE}"

$(stamp)init-dirs:
	mkdir debian/tmp -p
	touch $@

init: $(stamp)init-binutils

define init_binutils
	$(call unpack_tarball, ${BINUTILS_DIR}/binutils-${VER_BINUTILS}.tar.xz)
	cd binutils-${PKG_VER_BINUTILS} ;\
	cp -a ${BINUTILS_DIR}/debian/ . ;\
	if [ -n "$$(grep -v '^\#' ${CURDIR}/debian/patches/binutils/series)" ]; then \
	  QUILT_PATCHES=${CURDIR}/debian/patches/binutils quilt push -a; \
	fi; \
	echo ${CROSS_ARCH} > debian/target;
endef

$(stamp)init-binutils:
	$(call init_binutils)
	touch $@

$(stamp)build-binutils:
	$(call init_binutils)
	cd binutils-${PKG_VER_BINUTILS} \
	  && DEB_TARGET_ARCH=$(CROSS_ARCH) \
	     WITH_SYSROOT=/ \
	     PATCHED_SOURCES=yes \
	     PKG_IGNORE_CURRENTLY_BUILDING=1 \
	     BACKPORT=false \
		dpkg-buildpackage -b -uc -us -nc -d
	# binutils wants dejagnu to generate docs which are not generate during cross build
	touch $@

clean:
	rm -rf binutils-${PKG_VER_BINUTILS}
	rm -rf debian/tmp debian/files
	rm -f *.*deb *.changes
	rm -rf repackfiles tmp
	rm -rf $(stamp)
	mkdir $(stamp)

DEB_LIST_ARCH = $(wildcard *_${HOST_ARCH}.deb)

#
# Repack resulting packages to make some changes;
# - I want libgcc1 to contain changelog etc which normally are in gcc-${VER_GCC_BASE}-*-base package
# - libgcc1(-dbg) should depend on proper version libgcc1 (epoch hardcoded)
# - libgcc1-dbg needs to have doc symlink to libgcc1
# - linux-libc-dev gets copyright and changelog
# - libc6 gets copyright and changelog
# - libc6-dev-arm(el|hf)-cross provides libc6-dev-arm(hf|el)-arm(hf|el)-cross
# - libc6-dev-armhf-cross ships symlink to /usr/arm-linux-gnueabi/lib/hf for multilib cross compiler
define repack_debs
	@for deb in $1; \
	do \
	    rm -rf tmp/*; \
	    dpkg-deb -x $$deb tmp; \
	    dpkg-deb -e $$deb tmp/DEBIAN; \
	    pkgname=`echo $$deb | cut -d'_' -f1`; \
	    sed -i -e's/^Source:.*/Source: ${DEB_NAME_ACT} (${DEB_VER_ACT})/' tmp/DEBIAN/control; \
	    sed -i -e "s/${DEB_VER_BINUTILS}/${DEB_VER_BINUTILS}cross${DEB_VER_ACT}/g" tmp/DEBIAN/control ; \
	    sed -i -e "s/cross${DEB_VER_ACT}cross${DEB_VER_ACT}/cross${DEB_VER_ACT}/g" tmp/DEBIAN/control ; \
	    if [ -e debian/overrides/$$pkgname ]; then \
		  mkdir -p tmp/usr/share/lintian/overrides; \
		  cp debian/overrides/$$pkgname tmp/usr/share/lintian/overrides/; \
	    fi; \
	    (cd tmp && md5sum `find usr -type f` >>DEBIAN/md5sums);\
	    newdeb=`echo $$deb|sed -e "s/\(.*\)_\(.*\)_\(.*\)/\1_\2cross${DEB_VER_ACT}_\3/g"`; \
	    PKG_IGNORE_CURRENTLY_BUILDING=1 dpkg-deb -b tmp/ ../$$newdeb; \
	    rm -rf tmp/*; \
	done
endef

repack-debs-arch: build
	$(call repack_debs, ${DEB_LIST_ARCH})

build-arch: build
build-indep: build
build: $(stamp)build-binutils
	# we do not need those packages
	rm -f cpp-*_${HOST_ARCH}.*deb
	rm -f gcc-*_${HOST_ARCH}.*deb
	rm -f linux-headers*.*deb
	rm -f *-doc*.*deb
	rm -f *-source*.*deb
	rm -f linux-tools-common_*.*deb
	rm -f nscd*deb

binary: binary-arch binary-indep
binary-arch:  build mangle-debian-files-arch
binary-indep: 

DUMMYNAME = 
ifeq ($(CROSS_ARCH),armel)
	DUMMYNAME = armel-armhf
else ifeq ($(CROSS_ARCH),armhf)
	DUMMYNAME = armhf-armel
endif

$(stamp)debian-files-base:
	rm -f debian/files
	touch $@

define generate_debian_files
	@for deb in $1; \
	do \
		echo -n "`basename $$deb` " >>debian/files; \
		dpkg-deb -I $$deb | grep Section  | cut -d ' ' -f 3 | tr "\n" ' ' >>debian/files; \
		dpkg-deb -I $$deb | grep Priority | cut -d ' ' -f 3 | tr -d "\n" >>debian/files; \
		echo "" >>debian/files; \
	done
endef

mangle-debian-files-arch: $(stamp)debian-files-base repack-debs-arch
	$(call generate_debian_files,   ${DEB_LIST_ARCH})
	sed -i -e "s/_all.deb/cross${DEB_VER_ACT}_all.deb/g" debian/files
	sed -i -e "s/_${HOST_ARCH}.deb/cross${DEB_VER_ACT}_${HOST_ARCH}.deb/g" debian/files
	sed -i -e "s/cross${DEB_VER_ACT}cross${DEB_VER_ACT}/cross${DEB_VER_ACT}/g" debian/files


control:
	sed -e "s/CROSS_ARCH/${CROSS_ARCH}/g" \
	    -e "s/CROSS_GNU_TYPE/${CROSS_GNU_TYPE}/g" \
	    -e "s/MIN_VER_EGLIBC/${MIN_VER_EGLIBC}/g" \
	    -e "s/MIN_VER_LINUX/${MIN_VER_LINUX}/g" \
	    -e "s/VER_LINUX/${VER_LINUX}/g" \
	    -e "s/MIN_VER_GCC/${MIN_VER_GCC}/g" \
	    -e "s/VER_GCC_BASE/${VER_GCC_BASE}/g" \
	    -e "s/MIN_VER_BINUTILS/${MIN_VER_BINUTILS}/g" \
	    -e "s/EXTRA_DEPS/${EXTRA_DEPS}/g" \
	    $(CURDIR)/debian/control.in >$(CURDIR)/debian/control
ifeq ($(PPA),true)
	sed  \
	    -e "s/binutils-source/binutils-${VER_BINUTILS}-source/g" \
	    -e "s/eglibc-source/eglibc-${VER_EGLIBC}-source/g" \
	    -e "s/linux-source/linux-source-${VER_LINUX}/g" \
	    -e "s/1.16.0~ubuntu6/1.15.5/g" \
	    -e "s/gcc-${VER_GCC_BASE}-source/gcc-${VER_GCC_BASE}-${VER_GCC}-source/g" \
	    -i $(CURDIR)/debian/control
endif
