#!/usr/bin/make -f
# -*- makefile -*-

export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

d = debian/gcc-arm-linux-androideabi
DESTDIR = $(CURDIR)/tmp

include /usr/share/dpkg/default.mk

TARGET	= arm-linux-androideabi
TARGET_TOOLS_PATH = $(DESTDIR)/usr
TARGET_TOOLS_PREFIX = $(TARGET_TOOLS_PATH)/bin/$(TARGET)-

SYSROOT=$(CURDIR)/sys-root

SET_PATH = PATH=$(DESTDIR)/usr/bin:$(PATH)

CFLAGS_FOR_TARGET = -O2 -Os -g -DTARGET_POSIX_IO -fno-short-enums -isystem $(CURDIR)/android/bionic/libc/include -isystem $(CURDIR)/android/bionic/libc/kernel/common -isystem $(CURDIR)/android/bionic/libc/kernel/arch-arm -isystem $(CURDIR)/android/bionic/libc/arch-arm/include -isystem $(CURDIR)/android/bionic/libm/include -isystem $(CURDIR)/android/bionic/libm/include/arm -isystem $(CURDIR)/android/bionic/libm/arm -include $(CURDIR)/android/bionic/libc/include/sys/limits.h

CFLAGS=-O2

stamps/binutils-prepare:
	rm -rf binutils
	mkdir -p binutils
	tar -C binutils -x -f /usr/src/binutils/binutils-*.tar.*
	cd binutils && ln -sf binutils-* binutils
	#cd binutils/binutils && QUILT_PATCHES=$(CURDIR)/debian/patches/binutils quilt --quiltrc - push -a
	mkdir -p stamps && touch $@

stamps/gcc-prepare:
	rm -rf gcc
	cp -a /usr/src/gcc-4.7 gcc
	mkdir -p gcc/stamps
	cd gcc && debian/rules series
	cp debian/patches/gcc/* gcc/debian/patches/
	cd gcc && debian/rules patch
	mkdir -p stamps && touch $@

stamps/binutils1: stamps/binutils-prepare
	mkdir -p binutils/build1
	cd binutils/build1 && \
	  ../binutils/configure \
		--prefix=/usr \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_BUILD_GNU_TYPE) \
		--target=$(TARGET) \
		--disable-docs \
		--disable-nls \
		--disable-werror \
		--disable-ppl-version-check \
		--enable-gold=default \
                --with-cloog \
		--disable-cloog-version-check \
		--enable-cloog-backend=ppl \
		--enable-plugins \
		--enable-threads \
		--disable-shared
	dh_auto_build --parallel -Bbinutils/build1
	dh_auto_build --parallel -Bbinutils/build1 -- install DESTDIR=$(DESTDIR)
	mkdir -p stamps && touch $@

stamps/gcc-stage1: stamps/binutils1 stamps/gcc-prepare
	rm -rf gcc/stage1
	mkdir -p gcc/stage1
	cd gcc/stage1 && \
	  $(SET_PATH) CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET)" CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" ../src/configure \
		--prefix=$(DESTDIR)/usr \
		--libexecdir=$(DESTDIR)/usr/lib \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_BUILD_GNU_TYPE) \
		--target=$(TARGET) \
		--disable-multilib \
		--with-gnu-as \
		--with-gnu-ld \
		--enable-languages=c,c++ \
                --with-cloog \
		--disable-ppl-version-check \
		--disable-cloog-version-check \
		--enable-cloog-backend=ppl \
		--disable-libssp --disable-nls \
		--disable-libmudflap \
		--disable-shared \
		--disable-sjlj-exceptions \
		--disable-libquadmath \
		--disable-libitm \
		--disable-libatomic \
		--with-float=soft \
		--with-fpu=vfp \
		--with-arch=armv5te \
		--enable-target-optspace \
		--disable-docs \
		--disable-nls \
		--enable-threads \
		--enable-tls \
		--disable-libgomp \
		--disable-libstdc__-v3 \
		--disable-libitm \
		--disable-libatomic
	$(SET_PATH) dh_auto_build --parallel -Bgcc/stage1 -- CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET)" CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET)"
	$(SET_PATH) dh_auto_build --parallel -Bgcc/stage1 -- install
	touch $@

stamps/bionic1: stamps/gcc-stage1
	ONE_SHOT_MAKEFILE=build/libs/host/Android.mk dh_auto_build --parallel -B$(CURDIR)/android -- all_modules TARGET_TOOLS_PREFIX=$(TARGET_TOOLS_PREFIX) TARGET_PRODUCT=pandaboard showcommands
	ONE_SHOT_MAKEFILE=build/tools/acp/Android.mk dh_auto_build --parallel -B$(CURDIR)/android -- all_modules TARGET_TOOLS_PREFIX=$(TARGET_TOOLS_PREFIX) TARGET_PRODUCT=pandaboard showcommands
	ONE_SHOT_MAKEFILE=bionic/Android.mk dh_auto_build --parallel -B$(CURDIR)/android -- all_modules out/target/product/pandaboard/obj/lib/crtbegin_dynamic.o  TARGET_TOOLS_PREFIX=$(TARGET_TOOLS_PREFIX) TARGET_PRODUCT=pandaboard showcommands
	./debian/build-sysroot.sh $(CURDIR)/android/out/target/product/pandaboard/ $(SYSROOT)
	touch dummy.c
	$(SET_PATH) $(TARGET)-gcc -O2 -o dummy.o -c dummy.c
	$(SET_PATH) $(TARGET)-ar cru $(SYSROOT)/usr/lib/libpthread.a dummy.o
	$(SET_PATH) $(TARGET)-ranlib $(SYSROOT)/usr/lib/libpthread.a
	rm -f dummy.[co]

	touch $@

stamps/gcc-stage2: stamps/binutils1 stamps/bionic1
	rm -rf gcc/stage2
	mkdir -p gcc/stage2
	# FIXME should use --prefix=/usr & --with-build-sysroot ?
	cd gcc/stage2 && \
	  $(SET_PATH) CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET) -fexceptions" CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET) -frtti" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" ../src/configure \
		--prefix=/usr \
		--libexecdir=/usr/lib \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_BUILD_GNU_TYPE) \
		--target=$(TARGET) \
		--with-build-sysroot=$(SYSROOT) \
		--disable-multilib \
		--with-gnu-as \
		--with-gnu-ld \
		--enable-languages=c,c++ \
		--disable-ppl-version-check \
		--disable-cloog-version-check \
		--enable-cloog-backend=isl \
		--disable-libssp \
		--disable-nls \
		--disable-libmudflap \
		--disable-shared \
		--disable-sjlj-exceptions \
		--disable-libquadmath \
		--disable-libitm \
		--disable-libatomic \
		--with-float=soft \
		--with-fpu=vfp \
		--with-arch=armv5te \
		--enable-target-optspace \
		--disable-docs \
		--disable-nls \
		--enable-threads \
		--enable-tls \
		--enable-libgomp \
		--disable-libitm \
		--disable-libatomic \
		--disable-libstdc__-v3
	$(SET_PATH) dh_auto_build --parallel -Bgcc/stage2 -- CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET) -fexceptions" CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET) -frtti"
	$(SET_PATH) dh_auto_build --parallel -Bgcc/stage2 -- install DESTDIR=$(DESTDIR)
	touch $@

# Build shared binutils2
stamps/binutils2: stamps/binutils-prepare
	mkdir -p binutils/build2
	cd binutils/build2 && \
	  ../binutils/configure \
		--prefix=/usr \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_BUILD_GNU_TYPE) \
		--target=$(TARGET) \
		--disable-docs \
		--disable-nls \
		--disable-werror \
		--disable-ppl-version-check \
		--enable-gold=default \
		--disable-cloog-version-check \
		--enable-cloog-backend=isl \
		--enable-plugins \
		--enable-threads \
		--enable-shared
	dh_auto_build --parallel -Bbinutils/build2
	dh_auto_build --parallel -Bbinutils/build2 -- install DESTDIR=$(DESTDIR)
	mkdir -p stamps && touch $@

build: build-arch build-indep

build-arch: stamps/gcc-stage2 stamps/binutils2
build-indep:

clean: 
	dh_testdir
	dh_testroot
	rm -rf stamps
	rm -rf binutils gcc android/out tmp $(SYSROOT)
	dh_clean 

install: install-indep install-arch
install-indep:

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s 
	dh_installdirs -s

	tar -C $(DESTDIR) -c -f - . | tar -C $(d) -x -f -

	cp -a $(SYSROOT)/usr/lib/* $(d)/usr/$(TARGET)/lib/
	cp -a $(SYSROOT)/usr/include $(d)/usr/$(TARGET)
	find $(d) -name "*.la" -delete
	rm -rf $(d)/usr/bin/$(TARGET)-gcc-*
	rm -rf $(d)/usr/lib/libiberty.a
	rm -rf $(d)/usr/share/locale
	rm -rf $(d)/usr/share/man/man7
	rm -rf $(d)/usr/share/info

binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot

binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	mkdir -p debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)
	cp android/bionic/libm/NOTICE debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)/copyright.libm
	cp android/bionic/libc/NOTICE debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)/copyright.libc
	cp android/frameworks/native/NOTICE debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)/copyright.frameworks.native	
	dh_installchangelogs -s
	dh_installdocs -s
	dh_installexamples -s
	dh_installman -s
	dh_strip -s
	dh_compress -s
	dh_fixperms -s
	dh_installdeb -s
	dh_shlibdeps -s -X.so
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s -- -Zxz

get-orig-source:
	fakeroot ./debian/rules clean
	-quilt pop -a -f
	tar --exclude-vcs --transform='s,^,$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/,S' -caf ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz android

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