#!/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-i686-linux-android
DESTDIR = $(CURDIR)/tmp

include /usr/share/dpkg/default.mk

TARGET	= i686-linux-android
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-x86 -isystem $(CURDIR)/android/bionic/libc/arch-x86/include -isystem $(CURDIR)/android/bionic/libm/include -isystem $(CURDIR)/android/bionic/libm/include/i387 -isystem $(CURDIR)/android/bionic/libm/i387 -isystem $(CURDIR)/android/bionic/libm/i386 -include $(CURDIR)/android/bionic/libc/include/sys/limits.h

CFLAGS=-O2

# Needed when landing via CI Train
#LANDING_URL = http://ppa.launchpad.net/ci-train-ppa-service/landing-004/ubuntu
ifeq ($(wildcard /CurrentlyBuilding),)
PORTS_URL = http://archive.ubuntu.com/ubuntu
else
PORTS_URL = http://ftpmaster.internal/ubuntu
endif
SRC ?= android
dist:=$(DEB_DISTRIBUTION)

stamps/chdist:
	rm -rf repo
	chdist --data-dir repo --arch i386 create $(dist)
	echo "deb-src $(PORTS_URL) $(dist) main universe restricted multiverse" >> repo/$(dist)/etc/apt/sources.list
	echo "deb-src $(PORTS_URL) $(dist)-updates main universe restricted multiverse" >> repo/$(dist)/etc/apt/sources.list
	echo "deb-src $(PORTS_URL) $(dist)-proposed main universe restricted multiverse" >> repo/$(dist)/etc/apt/sources.list
	echo "deb-src $(PORTS_URL) $(dist)-security main universe restricted multiverse" >> repo/$(dist)/etc/apt/sources.list
	[ -z "$(LANDING_URL)" ] || echo "deb-src $(LANDING_URL) $(dist) main" >> repo/$(dist)/etc/apt/sources.list
	chdist --data-dir $(CURDIR)/repo --arch i386 apt-get $(dist) update
	cd repo/$(dist) && chdist --data-dir $(CURDIR)/repo --arch i386 apt-get $(dist) source $(SRC)
	mv repo/$(dist)/android-* android
	rm -f android/bionic/tests/Android.mk
	cd android/bionic && patch -p1 < $(CURDIR)/debian/patches/android/undefined.patch
	mkdir -p stamps && touch $@


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
	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-multilib \
		--disable-docs \
		--disable-nls \
		--disable-werror \
                --with-cloog \
		--disable-ppl-version-check \
		--enable-gold=default \
		--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 stamps/chdist
	#rm -rf gcc/stage1
	mkdir -p gcc/stage1
	cd gcc/stage1 && \
	  $(SET_PATH) CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET)" CXXFLAGS_FOR_TARGET="$(CFLAGS_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-arch=i686 \
		--with-tune=atom \
		--with-fpmath=sse \
                --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 \
		--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="$(CFLAGS_FOR_TARGET)"
	$(SET_PATH) dh_auto_build --parallel -Bgcc/stage1 -- install
	touch $@

stamps/bionic1: SHELL=/bin/bash

stamps/bionic1: stamps/gcc-stage1
	cd android; . ./build/envsetup.sh; lunch aosp_x86-userdebug; cd ../; \
	dh_auto_build --parallel -B$(CURDIR)/android -- \
	libc libm libstdc++ libthread_db \
	$(CURDIR)/android/out/target/product/generic_x86/obj/lib/crtbegin_dynamic.o \
	$(CURDIR)/android/out/target/product/generic_x86/obj/lib/crtbegin_static.o \
	$(CURDIR)/android/out/target/product/generic_x86/obj/lib/crtend_android.o \
	showcommands TARGET_TOOLS_PREFIX=$(TARGET_TOOLS_PREFIX)
	./debian/build-sysroot.sh $(CURDIR)/android/out/target/product/generic_x86/ $(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="$(CFLAGS_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++ \
		--with-arch=i686 \
		--with-tune=atom \
		--with-fpmath=sse \
		--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 \
		--enable-target-optspace \
		--disable-docs \
		--disable-nls \
		--enable-threads \
		--enable-tls \
		--disable-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="$(CFLAGS_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 tmp $(SYSROOT) repo
	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/$(DEB_BUILD_GNU_TYPE)/$(TARGET)/lib/
	cp -a $(SYSROOT)/usr/include $(d)/usr/$(DEB_BUILD_GNU_TYPE)/$(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
	rm -rf $(d)/usr/lib/ldscripts

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
