# Makefile that builds a cross compiler against an existing system.
#
# This version fetches the Maverick libc and headers and builds a
# compiler against that.  To build for a different system,
# replace the sysroot.stamp rule with one that installs the headers
# and libc for your target.
#
# Tested on Ubuntu Maverick.  It should work on any Linux
# operating that has 'dpkg' including Fedora.  Note that .deb files are
# 'ar' archives so you could extract them manually.
#
# Copyright (c) 2010 Linaro
# Contributors:
#   Michael Hope - initial implementation
#

# Basics:

# Target machine triplet
TARGET ?= arm-linux-gnueabi

CURDIR := $(shell pwd)

# Where to install resulting tarball. Does not have to be writable.
PREFIX = $(HOME)/opt/cross-gcc-linaro

# Configure flags to pass to GCC.  Sets the default system that GCC
# builds for.  Note that the calling convention must be the same as
# the pre-built sysroot
GCCFLAGS = --with-arch=armv7-a --with-tune=cortex-a8 --with-float=softfp --with-fpu=neon --enable-threads=posix --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-languages=c,c++ --enable-shared --enable-multiarch --with-system-zlib --disable-nls --without-included-gettext --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-symvers=gnu --enable-__cxa_atexit --disable-libgomp --enable-linker-build-id

# Sysroot:

# Where the headers and libc will end up.  Check out the GCC
# --with-build-sysroot configure option
SYSROOT = $(PREFIX)/$(TARGET)
BUILD_SYSROOT = $(CURDIR)/build/sysroot

# Where to fetch the packages from
MIRROR = http://ports.ubuntu.com/

# Set the pre-built headers and libc to build against.
LIBC6_VER = 2.12.1-0ubuntu6
LINUX_VER = 2.6.35-1022.33

LIBC6        = pool/main/e/eglibc/libc6_${LIBC6_VER}_armel.deb
LIBC6DEV     = pool/main/e/eglibc/libc6-dev_${LIBC6_VER}_armel.deb
LINUXLIBCDEV = pool/main/l/linux/linux-libc-dev_${LINUX_VER}_armel.deb

# All of the packages that go into the sysroot
PACKAGES = $(LIBC6) $(LIBC6DEV) $(LINUXLIBCDEV)

# Sources:

# The binutils version to build
BINUTILSSRC  = src/binutils_2.21.0.20110327.orig.tar.gz

# The Linaro GCC version to build.  Check out
# https://launchpad.net/gcc-linaro/+download for the latest link.
GCCURL = http://launchpad.net/gcc-linaro/4.5/4.5-2011.05-0/+download/gcc-linaro-4.5-2011.05-0.tar.bz2
GCCSRC = src/$(notdir $(GCCURL))

# Options:

# Build in parallel
PARALLEL =
# Tell curl to be quiet, follow links, and resume
CURLFLAGS = -L -C -

# Directory to put all stamp files
stamp = stamp/

-include local.mk

all: tarball

# Make the final binary tarball
tarball: binutils gcc
	strip $(BUILD_SYSROOT)$(SYSROOT)/bin/*
	echo Install into $(PREFIX) > $(BUILD_SYSROOT)$(PREFIX)/INSTALL
	tar caf cross-$(TARGET)-$(notdir $(GCCURL)) --numeric-owner -C $(dir $(BUILD_SYSROOT)$(PREFIX)) .

$(stamp)init-dirs:
	install -d src $(stamp) build
	touch $@

# Binutils
binutils: $(stamp)binutils-install

# Convenience target to fetch the binutils source
binutils-src: $(BINUTILSSRC)

# Fetch the binutils tarball from Ubuntu archive
$(BINUTILSSRC): $(stamp)init-dirs
	curl $(CURLFLAGS) -o $@ http://archive.ubuntu.com/ubuntu/pool/main/b/binutils/$(@F)

# Extract
$(stamp)binutils-extract: $(BINUTILSSRC)
	tar xaf $< -C src
	touch $@

# Configure
$(stamp)binutils-configure: $(stamp)binutils-extract
	install -d build/binutils
	cd build/binutils && ../../src/binutils*/configure \
	--prefix=$(PREFIX) \
	--target=$(TARGET) \
	--disable-nls \
	--disable-werror \
	--enable-lto \
	--enable-gold \
	--with-sysroot=$(SYSROOT) \
	--with-build-sysroot=$(BUILD_SYSROOT)
	touch $@

# Build and install
$(stamp)binutils-install: $(stamp)binutils-configure
	make -C build/binutils $(PARALLEL) 
	make -C build/binutils $(PARALLEL) install DESTDIR=$(BUILD_SYSROOT)
	touch $@

# Fetch and extract the sysroot.  Unfortunately the packages include
# absolute links in sysroot/usr/lib.  Fix these up to point to the
# sysroot instead.
$(stamp)sysroot-extract: $(stamp)init-dirs
	cd src && curl $(CURLFLAGS) $(PACKAGES:%=-O $(MIRROR)%)
	install -d $(BUILD_SYSROOT)$(SYSROOT)
	for i in src/*.deb; do \
		rm -f data.*; \
		(cd src && ar x `basename $$i`); \
		tar xaf src/data.tar.* -C $(BUILD_SYSROOT)$(SYSROOT); \
	done
	for i in $(BUILD_SYSROOT)$(SYSROOT)/usr/lib/*; do \
		if [ -L $$i ] && (readlink $$i | grep -q ^/); then \
			x=`readlink $$i`; rm $$i; ln -s $(BUILD_SYSROOT)$(SYSROOT)$$x $$i; \
	        fi; \
	done	
	touch $@

# Build GCC
gcc: $(stamp)gcc-install

# Convenience target to fetch the GCC source
gcc-src: $(GCCSRC)

# Fetch
$(GCCSRC): $(stamp)init-dirs
	curl $(CURLFLAGS) -o $@ $(GCCURL)

# Extract
$(stamp)gcc-extract: $(GCCSRC) $(stamp)init-dirs
	tar xaf $< -C src
	touch $@

# Configure
$(stamp)gcc-configure: $(stamp)binutils-install $(stamp)sysroot-extract $(stamp)gcc-extract
	install -d build/gcc 
	export PATH=$(BUILD_SYSROOT)$(SYSROOT)/../bin:$(PATH); \
	cd build/gcc && ../../src/gcc-*/configure \
	--enable-languages=c,c++ \
	--prefix=$(PREFIX) \
	--target=$(TARGET) \
	--with-sysroot=$(SYSROOT) \
	--with-build-sysroot=$(BUILD_SYSROOT)$(SYSROOT) \
	$(GCCFLAGS)
	touch $@

# Build and install
$(stamp)gcc-install: $(stamp)gcc-configure
	export PATH=$(BUILD_SYSROOT)$(SYSROOT)/../bin:$(PATH); \
	make -C build/gcc $(PARALLEL); \
	make -C build/gcc $(PARALLEL) install DESTDIR=$(BUILD_SYSROOT)
	touch $@

# Remove all temporary files
clean:
	rm -rf $(stamp) build src
