Subversion Repositories avr32-toolchain

Rev

Rev 13 | Rev 17 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

# AVR32 Toolchain Makefile
#
# Copyright (C) 2011 by James Snyder <jbsnyder@fanplastic.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

#### PRIMARY TOOLCHAIN VERSIONS #####

GCC_VERSION       = 4.4.3
GDB_VERSION_DLOAD = 6.7.1a
GDB_VERSION       = 6.7.1
BINUTILS_VERSION  = 2.20.1
NEWLIB_VERSION    = 1.16.0
DFU_VERSION       = 0.5.4
AVR_PATCH_REV     = 3.2.3.261
AVR_HEADER_REV    = 3.2.3.258


#### PATHS AND ENVIRONMENT VARIABLES #####

SHELL       = /bin/bash
TARGET      = avr32

TODAY = $(shell date "+%Y%m%d")
GIT_REV     = $(shell git rev-parse --verify HEAD --short 2> /dev/null)
SVN_REV     = $(shell head -1 ReleaseNotes 2> /dev/null | cut -f 1 -d " ")

ifeq ($(strip $(GIT_REV)),)
ifeq ($(strip $(SVN_REV)),)
PREFIX     ?= $(HOME)/avr32-tools-$(TODAY)
TOOL_REV   = $(TODAY)
else
PREFIX     ?= $(HOME)/avr32-tools-$(SVN_REV)
TOOL_REV   = $(SVN_REV)
BUG_URL = jasmin@anw.at
endif
else
PREFIX     ?= $(HOME)/avr32-tools-$(GIT_REV)
TOOL_REV   = $(GIT_REV)
endif

ifeq ($(UNAME), Linux)
PROCS  ?= $(shell grep -c ^processor /proc/cpuinfo)
else ifeq ($(UNAME), Darwin)
PROCS  ?= $(shell sysctl hw.ncpu | awk '{print $$2}')
else
PROCS  ?= 2
endif

SUPP_PREFIX = $(CURDIR)/supp
PATH       := ${PREFIX}/bin:${SUPP_PREFIX}/bin:${PATH}
AUTOCONF    = $(SUPP_PREFIX)/bin/autoconf
AUTOMAKE    = $(SUPP_PREFIX)/bin/automake

STAMP_DIR  := $(CURDIR)/stamps
DOWNLOAD_DIR := $(CURDIR)/downloads
BUILD_DIR := $(CURDIR)/build

ifeq ($(strip $(BUG_URL)),)
BUG_URL = https://github.com/jsnyder/avr32-toolchain
endif
PKG_VERSION = "AVR 32 bit GNU Toolchain-$(AVR_PATCH_REV)-$(TOOL_REV)"


#### PRIMARY TOOLCHAIN URLS #####

GCC_ARCHIVE = gcc-$(GCC_VERSION).tar.bz2
GCC_URL = http://mirror.anl.gov/pub/gnu/gcc/gcc-$(GCC_VERSION)/$(GCC_ARCHIVE)
GCC_MD5 = fe1ca818fc6d2caeffc9051fe67ff103

GDB_ARCHIVE = gdb-$(GDB_VERSION_DLOAD).tar.bz2
GDB_URL = http://mirror.anl.gov/pub/gnu/gdb/$(GDB_ARCHIVE)
GDB_MD5 = 3564f308f3e4d4f2750891bc2ce9b214

BINUTILS_ARCHIVE = binutils-$(BINUTILS_VERSION).tar.bz2
BINUTILS_URL = http://mirror.anl.gov/pub/gnu/binutils/$(BINUTILS_ARCHIVE)
BINUTILS_MD5 = 2b9dc8f2b7dbd5ec5992c6e29de0b764

NEWLIB_ARCHIVE = newlib-$(NEWLIB_VERSION).tar.gz
NEWLIB_URL = ftp://sources.redhat.com/pub/newlib/$(NEWLIB_ARCHIVE)
NEWLIB_MD5 = bf8f1f9e3ca83d732c00a79a6ef29bc4

AVR32PATCHES_ARCHIVE = avr32-gnu-toolchain-$(AVR_PATCH_REV)-source.zip
AVR32PATCHES_URL=http://www.atmel.com/dyn/resources/prod_documents/$(AVR32PATCHES_ARCHIVE)
AVR32PATCHES_MD5 = 69a03828a328068f25d457cfd8341857
install_dir_avr32patches := source/avr32

AVR32HEADERS_ARCHIVE = avr32-headers-$(AVR_HEADER_REV).zip
AVR32HEADERS_URL=http://www.atmel.com/dyn/resources/prod_documents/$(AVR32HEADERS_ARCHIVE)
AVR32HEADERS_MD5 = 3293d70a46e460d342e1f939b8e0d228

DFU_ARCHIVE = dfu-programmer-$(DFU_VERSION).tar.gz
DFU_URL = http://surfnet.dl.sourceforge.net/project/dfu-programmer/dfu-programmer/$(DFU_VERSION)/$(DFU_ARCHIVE)
DFU_MD5 = 707dcd0f957a74e92456ea6919faa772


##### SUPPORT TOOLS VERSIONS / URLS ######
AUTOCONF_VERSION = 2.64
AUTOMAKE_VERSION = 1.11
MPC_VERSION = 0.8.1

AUTOCONF_ARCHIVE = autoconf-$(AUTOCONF_VERSION).tar.bz2
AUTOCONF_URL = http://mirror.anl.gov/pub/gnu/autoconf/$(AUTOCONF_ARCHIVE)
AUTOCONF_MD5 = ef400d672005e0be21e0d20648169074

AUTOMAKE_ARCHIVE = automake-$(AUTOMAKE_VERSION).tar.bz2
AUTOMAKE_URL = http://mirror.anl.gov/pub/gnu/automake/$(AUTOMAKE_ARCHIVE)
AUTOMAKE_MD5 = 4db4efe027e26b33930a7e151de19d0f


ALL_TOOLS := gcc gdb binutils newlib avr32patches avr32headers dfu autoconf automake



.PHONY: install-tools
install-tools: install-binutils install-final-gcc install-newlib install-headers

.PHONY: install-cross
install-cross: install-tools install-note


.PHONY: sudomode
sudomode:
ifneq ($(USER),root)
        @echo Please run this target with sudo!
        @echo e.g.: sudo make targetname
        @exit 1
endif


.PHONY: tst
tst:
        @echo "PREFIX=$(PREFIX)"
        @echo "PKG_VERSION=$(PKG_VERSION)"
        @echo "BUG_URL=$(BUG_URL)"


############ MACROS for the lazy people ;-) ############

## in silent mode (make -s),, print whats ging on
ifeq ($(strip $(MAKEFLAGS)),s)
define quiet_text
        @echo "$1"
endef
else
define quiet_text
endef
endif

## the stamp name
define name_stamp
$(STAMP_DIR)/$(1)
endef

## remove a stamp
define rm_stamp
        rm -f $(STAMP_DIR)/$(1)
endef

## remove all stamps
define rm_stamps
        rm -f $(STAMP_DIR)/*-$(1)
endef

## create the stamp directory if not existing and the stamp
define make_stamp
        @[ -d $(STAMP_DIR) ] || mkdir $(STAMP_DIR)
        date > $(STAMP_DIR)/$(1)
endef

## delete empty directories
define del_empty-directories
        @find . -type d -empty | xargs rm -rf
endef

## delete all empty directories
## need to do this several times. to remove empty sub directories
define del_all_empty-directories
        $(call del_empty-directories)
        $(call del_empty-directories)
        $(call del_empty-directories)
endef


## to make the following macros more simply, we need the variables
## with the tool name in lowercase. I could have changed the original
## definition, but I don't want to break the make convention of upper
## case definitions for such user defined variables.

define LOWER_TOOL_VARS
UP_$(1) := $(shell echo "$(1)" | tr '[a-z]' '[A-Z]')
$(1)_VERSION := $$($$(UP_$(1))_VERSION)
$(1)_ARCHIVE := $$($$(UP_$(1))_ARCHIVE)
$(1)_URL := $$($$(UP_$(1))_URL)
$(1)_MD5 := $$($$(UP_$(1))_MD5)
endef

$(foreach tool,$(ALL_TOOLS),$(eval $(call LOWER_TOOL_VARS,$(tool))))


## create the download rule and force download rule
define DOWNLOAD_template
download-$(1): $(DOWNLOAD_DIR)/$$($(1)_ARCHIVE)
download-$(1)-f $(DOWNLOAD_DIR)/$$($(1)_ARCHIVE):
        [ -d $(DOWNLOAD_DIR) ] || mkdir -p $(DOWNLOAD_DIR)
        cd $(DOWNLOAD_DIR) && curl -LO $$($(1)_URL)

download-$(1)_TEXT := "Removing $(DOWNLOAD_DIR)/$$($(1)_ARCHIVE)"
download-$(1)-remove:
        $(call quiet_text,$$(download-$(1)_TEXT))
        rm -f $(DOWNLOAD_DIR)/$$($(1)_ARCHIVE)

PHONY += download-$(1) download-$(1)-f download-$(1)-remove
endef

## extract commands
EXT_BZ2 := tar -jxf
EXT_TGZ := tar -xf
EXT_ZIP := unzip -o

## create the extract rule and force extract rule
define EXTRACT_template
extract-$(1)_TEXT := "Extracting $$($(1)_ARCHIVE)"

extract-$(1): $(STAMP_DIR)/extract-$(1)
extract-$(1)-f $(STAMP_DIR)/extract-$(1): $(DOWNLOAD_DIR)/$$($(1)_ARCHIVE)
        @(rm -rf $(1)-*; \
        t1=`openssl md5 $$< | cut -f 2 -d " " -` && \
        [ "$$$$t1" = "$$($(1)_MD5)" ] || \
        ( echo "Bad Checksum! Please remove the following file and retry: $$<" && false ))
        $(call quiet_text,$$(extract-$(1)_TEXT))
        $($(2)) $$< $(3)
        $(call make_stamp,extract-$(1))

extract-$(1)-remove:
        rm -rf $(1)-*
        $(call rm_stamp,extract-$(1))

PHONY += extract-$(1) extract-$(1)-f extract-$(1)-remove
endef

## create the patch rule
define PATCH_template
patch-$(1)_TEXT := "Patching $$($(1)_ARCHIVE)"
patch-$(1): $(STAMP_DIR)/patch-$(1)
$(STAMP_DIR)/patch-$(1): $(STAMP_DIR)/extract-$(1) $(STAMP_DIR)/extract-avr32patches
        $(call quiet_text,$$(patch-$(1)_TEXT))
        @(pushd $(1)-$$($(1)_VERSION) ; \
        for f in ../$(install_dir_avr32patches)/$(1)/*.patch; do \
        patch -N -p0 < $$$${f} ; \
        done ; \
        popd)
        $(call make_stamp,patch-$(1))

patch-$(1)-remove: extract-$(1)-remove
        $(call rm_stamp,patch-$(1))

PHONY += patch-$(1) patch-$(1)-remove
endef


#.PHONY: regen-binutils regen-binutils-force
#regen-binutils: stamps/regen-binutils
#regen-binutils-force stamps/regen-binutils: stamps/patch-binutils stamps/install-supp-tools
#       pushd binutils-$(BINUTILS_VERSION) ; \
#       "$(SUPP_PREFIX)/bin/aclocal" -I config ; \
#       "$(SUPP_PREFIX)/bin/autoconf" ; \
#       "$(SUPP_PREFIX)/bin/automake" ; \
#       "$(SUPP_PREFIX)/bin/autoheader" ; \
#       for dir in bfd opcodes binutils gas ld; do \
#         pushd $$dir ; \
#         "$(SUPP_PREFIX)/bin/autoconf"; \
#         "$(SUPP_PREFIX)/bin/automake"; \
#         "$(SUPP_PREFIX)/bin/autoheader"; \
#         popd ; \
#       done; \
#       popd; \
#       [ -d stamps ] || mkdir stamps ;
#       touch stamps/regen-binutils;


## create the configure rule and force configure rule
define CONF_template
conf-$(1)_TEXT := "Configuring $(1) $$($(1)_VERSION)"
conf-$(1): $(STAMP_DIR)/conf-$(1)
conf-$(1)-f $(STAMP_DIR)/conf-$(1): $(STAMP_DIR)/$(2)-$(1) $(BUILD_DIR)/$(1)
        @rm -rf $(BUILD_DIR)/$(1)/*
        $(call quiet_text,$$(conf-$(1)_TEXT))
        cd $(BUILD_DIR)/$(1) && \
        ../../$(1)-$$($(1)_VERSION)/configure $$($(1)_conf_opts)
        $(call make_stamp,conf-$(1))

conf-$(1)-remove:
        $(call rm_stamp,conf-$(1))

PHONY += conf-$(1) conf-$(1)-f conf-$(1)-remove
endef

## create the build rule and force build rule
define BUILD_template
build-$(1)_TEXT := "Building $(1) $$($(1)_VERSION)"
build-$(1): $(STAMP_DIR)/build-$(1)
build-$(1)-f $(STAMP_DIR)/build-$(1): $(STAMP_DIR)/conf-$(1)
        $(call quiet_text,$$(build-$(1)_TEXT))
        cd $(BUILD_DIR)/$(1) && \
        $(MAKE) clean && $(MAKE) -j$(PROCS)
        $(call make_stamp,build-$(1))

build-$(1)-remove:
        rm -rf $(BUILD_DIR)/$(1)
        $(call rm_stamp,build-$(1))

PHONY += build-$(1) build-$(1)-f build-$(1)-remove
endef

## create the install rule and force install rule
define INSTALL_template
install-$(1)_TEXT := "Installing $(1) $$($(1)_VERSION)"
install-$(1): $(STAMP_DIR)/install-$(1)
install-$(1)-f $(STAMP_DIR)/install-$(1): $(STAMP_DIR)/build-$(1)
        $(call quiet_text,$$(install-$(1)_TEXT))
        cd $(BUILD_DIR)/$(1) && \
        $(MAKE) install
        $(call make_stamp,install-$(1))

uninstall-$(1)_TEXT := "Uninstalling $(1) $$($(1)_VERSION)"
uninstall-$(1):
        $(call quiet_text,$$(uninstall-$(1)_TEXT))
        [ -f $(BUILD_DIR)/$(1)/Makefile ] && \
        [ -f $(call name_stamp,install-$(1)) ] && \
        cd $(BUILD_DIR)/$(1) && \
        $(MAKE) uninstall || true
        $(call rm_stamp,install-$(1))

PHONY += install-$(1) install-$(1)-f uninstall-$(1)
endef

## create the clean rule and realclean rule
define CLEAN_template
clean-$(1)_TEXT := "Cleaning $(1) $$($(1)_VERSION)"
clean-$(1)-text:
        $(call quiet_text,$$(clean-$(1)_TEXT))

clean-$(1): clean-$(1)-text uninstall-$(1) extract-$(1)-remove patch-$(1)-remove \
            build-$(1)-remove conf-$(1)-remove
        $(call del_all_empty-directories)

realclean-$(1): clean-$(1) download-$(1)-remove
        $(call del_all_empty-directories)

PHONY += clean-$(1) realclean-$(1) clean-$(1)-text
endef

## create some dummy rules for the clean rule
define DUMMY_patch_remove
patch-$(1)-remove:
PHONY += patch-$(1)-remove
endef
define DUMMY_conf_remove
conf-$(1)-remove:
PHONY += conf-$(1)-remove
endef
define DUMMY_build_remove
build-$(1)-remove:
PHONY += build-$(1)-remove
endef


$(BUILD_DIR)/%:
        mkdir -p $@


############# SUPP: AUTOCONF ############

$(eval $(call DOWNLOAD_template,autoconf))
$(eval $(call EXTRACT_template,autoconf,EXT_BZ2))
autoconf_conf_opts = --prefix="$(SUPP_PREFIX)"
$(eval $(call CONF_template,autoconf,extract))
$(eval $(call BUILD_template,autoconf))
$(eval $(call INSTALL_template,autoconf))
$(eval $(call CLEAN_template,autoconf))


############ SUPP: AUTOMAKE ############

$(eval $(call DOWNLOAD_template,automake))
$(eval $(call EXTRACT_template,automake,EXT_BZ2))
automake_conf_opts = --prefix="$(SUPP_PREFIX)"
$(eval $(call CONF_template,automake,extract))
$(eval $(call BUILD_template,automake))
$(eval $(call INSTALL_template,automake))
$(eval $(call CLEAN_template,automake))


############# AVR32 PATCHES ############

$(eval $(call DOWNLOAD_template,avr32patches))
$(eval $(call EXTRACT_template,avr32patches,EXT_ZIP))

## use the uninstall-XXX rule to remove the patches directory
PHONY += uninstall-avr32patches
uninstall-avr32patches:
        rm -rf $(install_dir_avr32patches)

$(eval $(call CLEAN_template,avr32patches))


############# AVR32 HEADERS ############
$(eval $(call DOWNLOAD_template,avr32headers))

###### FIXME: Need to add a dependency to "stamps/install-final-gcc"
install_path_avr32headers := $(PREFIX)/$(TARGET)/include/
avr32headers_unzip_opt := -d "$(install_path_avr32headers)"
$(eval $(call EXTRACT_template,avr32headers,EXT_ZIP,$(avr32headers_unzip_opt)))

## use the uninstall-XXX rule to remove the patches directory
install_dir_avr32headers := $(install_path_avr32headers)/avr32
PHONY += uninstall-avr32headers
uninstall-avr32headers:
        rm -rf $(install_dir_avr32headers)

$(eval $(call CLEAN_template,avr32headers))


################ BINUTILS ################

$(eval $(call DOWNLOAD_template,binutils))
$(eval $(call EXTRACT_template,binutils,EXT_BZ2))
$(eval $(call PATCH_template,binutils))
#$(eval $(call REGEN_template,binutils))
binutils_conf_opts = \
--enable-maintainer-mode \
--prefix="$(PREFIX)" --target=$(TARGET) --disable-nls \
--disable-shared --disable-werror \
--with-sysroot="$(PREFIX)/$(TARGET)" --with-bugurl=$(BUG_URL)
$(eval $(call CONF_template,binutils,extract))
$(eval $(call BUILD_template,binutils))
$(eval $(call INSTALL_template,binutils))
$(eval $(call CLEAN_template,binutils))


# we need to satisfy the rules, generated by the CLEAN_template, if they not exist
patch_remove_dummy_tools := autoconf automake avr32patches avr32headers
$(foreach tool,$(patch_remove_dummy_tools),$(eval $(call DUMMY_patch_remove,$(tool))))
conf_remove_dummy_tools := avr32patches avr32headers
$(foreach tool,$(conf_remove_dummy_tools),$(eval $(call DUMMY_conf_remove,$(tool))))
build_remove_dummy_tools := avr32patches avr32headers
$(foreach tool,$(build_remove_dummy_tools),$(eval $(call DUMMY_build_remove,$(tool))))


.PHONY: $(PHONY)


.PHONY: tst2 tst3
tst2:
        $(call make_stamp,JES)
tst3:
        $(call rm_stamp,JES)


.PHONY: help
help:
        @echo ""
        @echo "The following targets are available:"
        @echo ""
        @echo "download-XXX     : download the required package"
        @echo "extract-XXX      : extract the downloaded package"
        @echo "conf-XXX : configure the extracted package"
        @echo "build-XXX        : build the configured package"
        @echo "install-XXX      : install the build package"
        @echo "uninstall-XXX    : uninstall the installed package"
        @echo "clean-XXX        : clean the package source and build tree"
        @echo "realclean-XXX    : same as clean-XXX, but remove the downloaded"
        @echo "           package, too"
        @echo ""
        @echo "XXX can be one of the following: "
        @echo "$(ALL_TOOLS)"
        @echo ""
        @echo "The above targets will be executed in the right order and only if"
        @echo "they need to be be really built. If you want to rebuild one of then"
        @echo "manualy, you can use the aaaa-XXX-f target (e.g.: build-autoconf-f,"
        @echo "download-automake-f). Please note, that not all steps are available for"
        @echo "all tools (e.g.: install-avr32patches is not available)."
        @echo ""
        @echo ""


.PHONY: install-note
install-note: install-tools
        @echo
        @echo ====== INSTALLATION NOTE ======
        @echo Your tools have now been installed at the following prefix:
        @echo $(PREFIX)
        @echo
        @echo Please be sure to add something similar to the following to your .bash_profile, .zshrc, etc:
        @echo export PATH=$(PREFIX)/bin:'$$PATH'


.PHONY: install-supp-tools install-supp-tools-force
install-supp-tools: stamps/install-supp-tools
install-supp-tools-force stamps/install-supp-tools: stamps/install-autoconf stamps/install-automake
        [ -d stamps ] || mkdir stamps ;
        touch stamps/install-supp-tools;

.PHONY: clean-supp-tools
clean-supp-tools: clean-autoconf clean-automake
        rm stamps/install-supp-tools;

.PHONY: realclean-supp-tools
realclean-supp-tools: realclean-autoconf realclean-automake
        rm stamps/install-supp-tools;





############ SUPP: AUTOMAKE ############

##.PHONY: download-automake download-automake-force
##download-automake: downloads/$(AUTOMAKE_ARCHIVE)
##download-automake-force downloads/$(AUTOMAKE_ARCHIVE):
##      [ -d downloads ] || mkdir downloads ;
##      cd downloads && curl -LO $(AUTOMAKE_URL)

##.PHONY: extract-automake extract-automake-force
##extract-automake: stamps/extract-automake
##extract-automake-force stamps/extract-automake: downloads/$(AUTOMAKE_ARCHIVE)
##      @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
##      [ "$$t1" = "$(AUTOMAKE_MD5)" ] || \
##      ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
##      tar -jxf $< ;
##      [ -d stamps ] || mkdir stamps ;
##      touch stamps/extract-automake;

##.PHONY: build-automake build-automake-force
##build-automake: stamps/build-automake
##build-automake-force stamps/build-automake: stamps/extract-automake stamps/install-autoconf
##      mkdir -p build/automake && cd build/automake && \
##      ../../automake-$(AUTOMAKE_VERSION)/configure --prefix="$(SUPP_PREFIX)" && \
##      $(MAKE) -j$(PROCS)
##      [ -d stamps ] || mkdir stamps
##      touch stamps/build-automake;

##.PHONY: install-automake install-automake-force
##install-automake: stamps/install-automake
##install-automake-force stamps/install-automake: stamps/build-automake
##      cd build/automake && \
##      $(MAKE) install
##      [ -d stamps ] || mkdir stamps
##      touch stamps/install-automake;

##.PHONY: clean-automake
##clean-automake:
##      rm -rf build/automake stamps/*-automake automake-*

##.PHONY: realclean-automake
##realclean-automake: clean-automake
##      rm downloads/$(AUTOMAKE_ARCHIVE)


############# AVR32 PATCHES ############

##.PHONY: download-avr32patches download-avr32patches-force
##download-avr32patches: downloads/$(AVR32PATCHES_ARCHIVE)
##download-avr32patches-force downloads/$(AVR32PATCHES_ARCHIVE):
##      cd downloads && curl -LO $(AVR32PATCHES_URL)

##.PHONY: extract-avr32patches extract-avr32patches-force
##extract-avr32patches: stamps/extract-avr32patches
##extract-avr32patches-force stamps/extract-avr32patches: downloads/$(AVR32PATCHES_ARCHIVE)
##      @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
##      [ "$$t1" = "$(AVR32PATCHES_MD5)" ] || \
##      ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
##      unzip -o $<
##      [ -d stamps ] || mkdir stamps
##      touch stamps/extract-avr32patches;

##.PHONY: clean-avr32patches
##clean-avr32patches:
##      rm -rf stamps/*-avr32patches source

##.PHONY: realclean-avr32patches
##realclean-avr32patches: clean-avr32patches
##      rm downloads/$(AVR32PATCHES_ARCHIVE)


############# AVR32 HEADERS ############

##.PHONY: download-avr32headers download-avr32headers-force
##download-avr32headers: downloads/$(AVR32HEADERS_ARCHIVE)
##download-avr32headers-force downloads/$(AVR32HEADERS_ARCHIVE):
##      cd downloads && curl -LO $(AVR32HEADERS_URL)

##.PHONY: install-headers install-headers-force
##install-headers: stamps/install-headers
##install-headers-force stamps/install-headers: downloads/$(AVR32HEADERS_ARCHIVE) stamps/install-final-gcc
##      @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
##      [ "$$t1" = "$(AVR32HEADERS_MD5)" ] || \
##      ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
##      unzip -o $< -d "$(PREFIX)/$(TARGET)/include/" && \
##      [ -d stamps ] || mkdir stamps
##      touch stamps/install-headers;

##.PHONY: clean-headers
##clean-headers:
##      rm -rf stamps/*-headers $(PREFIX)/$(TARGET)/include/avr32

##.PHONY: realclean-headers
##realclean-headers: clean-headers
##      rm downloads/$(AVR32HEADERS_ARCHIVE)


################ NEWLIB ################

.PHONY: download-newlib download-newlib-force
download-newlib: downloads/$(NEWLIB_ARCHIVE)
download-newlib-force downloads/$(NEWLIB_ARCHIVE):
        [ -d downloads ] || mkdir downloads ;
        cd downloads && curl -LO $(NEWLIB_URL)

.PHONY: extract-newlib extract-newlib-force
extract-newlib: stamps/extract-newlib
extract-newlib-force stamps/extract-newlib : downloads/$(NEWLIB_ARCHIVE)
        @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
        [ "$$t1" = "$(NEWLIB_MD5)" ] || \
        ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
        tar -xf $<
        [ -d stamps ] || mkdir stamps
        touch stamps/extract-newlib;


.PHONY: patch-newlib patch-newlib-force
patch-newlib: stamps/patch-newlib
patch-newlib-force stamps/patch-newlib: stamps/extract-newlib stamps/extract-avr32patches
        pushd newlib-$(NEWLIB_VERSION) ; \
        for f in ../source/avr32/newlib/*.patch; do \
        patch -N -p0 <$${f} ; \
        done ; \
        popd ;
        [ -d stamps ] || mkdir stamps
        touch stamps/patch-newlib;

.PHONY: regen-newlib regen-newlib-force
regen-newlib: stamps/regen-newlib
regen-newlib-force stamps/regen-newlib: stamps/patch-newlib stamps/install-supp-tools
        pushd newlib-$(NEWLIB_VERSION) ; \
        "$(SUPP_PREFIX)/bin/autoconf"; "$(SUPP_PREFIX)/bin/automake" ; \
        for dir in newlib/libc/machine/avr32 newlib/libc/machine newlib/libc/sys/avr32 newlib/libc/sys; do \
          pushd $$dir ; \
          "$(SUPP_PREFIX)/bin/aclocal" -I ../.. -I ../../.. -I ../../../.. ; \
          "$(SUPP_PREFIX)/bin/autoconf"; "$(SUPP_PREFIX)/bin/automake"; \
          popd ; \
        done; \
        popd;
        [ -d stamps ] || mkdir stamps
        touch stamps/regen-newlib;

NEWLIB_FLAGS="-ffunction-sections -fdata-sections       \
-DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -g -Os     \
-fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=128 \
-DSMALL_MEMORY"

.PHONY: build-newlib build-newlib-force
build-newlib: stamps/build-newlib
build-newlib-force stamps/build-newlib: stamps/regen-newlib stamps/install-binutils stamps/install-gcc
        mkdir -p build/newlib && cd build/newlib && \
        pushd ../../newlib-$(NEWLIB_VERSION) ; \
        make clean ; \
        popd ; \
        ../../newlib-$(NEWLIB_VERSION)/configure --prefix=$(PREFIX)     \
        --with-build-time-tools=$(PREFIX)                               \
        --target=$(TARGET) --disable-newlib-supplied-syscalls           \
        --disable-libgloss --disable-nls --disable-shared               \
        --enable-newlib-io-long-long --enable-newlib-io-long-double     \
        --enable-target-optspace --enable-newlib-io-pos-args            \
        --enable-newlib-reent-small  && \
        $(MAKE) -j$(PROCS) CFLAGS_FOR_TARGET=$(NEWLIB_FLAGS) CCASFLAGS=$(NEWLIB_FLAGS) && \
        [ -d stamps ] || mkdir stamps
        touch stamps/build-newlib;

.PHONY: install-newlib install-newlib-force
install-newlib: stamps/install-newlib
install-newlib-force stamps/install-newlib: stamps/build-newlib
        cd build/newlib && \
        $(MAKE) install
        [ -d stamps ] || mkdir stamps
        touch stamps/install-newlib;

.PHONY: clean-newlib
clean-newlib:
        rm -rf build/newlib stamps/*-newlib newlib-*

.PHONY: realclean-newlib
realclean-newlib: clean-newlib
        rm downloads/$(NEWLIB_ARCHIVE)


################ BINUTILS ################

##.PHONY: download-binutils download-binutils-force
##download-binutils: downloads/$(BINUTILS_ARCHIVE)
##download-binutils-force downloads/$(BINUTILS_ARCHIVE):
##      [ -d downloads ] || mkdir downloads ;
##      cd downloads && curl -LO $(BINUTILS_URL)

##.PHONY: extract-binutils extract-binutils-force
##extract-binutils: stamps/extract-binutils
##extract-binutils-force stamps/extract-binutils: downloads/$(BINUTILS_ARCHIVE)
##      @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
##      [ "$$t1" = "$(BINUTILS_MD5)" ] || \
##      ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
##      tar -jxf $< ;
##      [ -d stamps ] || mkdir stamps ;
##      touch stamps/extract-binutils;

##.PHONY: patch-binutils patch-binutils-force
##patch-binutils: stamps/patch-binutils
##patch-binutils-force stamps/patch-binutils: stamps/extract-binutils stamps/extract-avr32patches
##      pushd binutils-$(BINUTILS_VERSION) ; \
##      for f in ../source/avr32/binutils/*.patch; do \
##      patch -N -p0 <$${f} ; \
##      done ; \
##      popd ; \
##      [ -d stamps ] || mkdir stamps
##      touch stamps/patch-binutils;

##.PHONY: regen-binutils regen-binutils-force
##regen-binutils: stamps/regen-binutils
##regen-binutils-force stamps/regen-binutils: stamps/patch-binutils stamps/install-supp-tools
##      pushd binutils-$(BINUTILS_VERSION) ; \
##      "$(SUPP_PREFIX)/bin/aclocal" -I config ; \
##      "$(SUPP_PREFIX)/bin/autoconf" ; \
##      "$(SUPP_PREFIX)/bin/automake" ; \
##      "$(SUPP_PREFIX)/bin/autoheader" ; \
##      for dir in bfd opcodes binutils gas ld; do \
##        pushd $$dir ; \
##        "$(SUPP_PREFIX)/bin/autoconf"; \
##        "$(SUPP_PREFIX)/bin/automake"; \
##        "$(SUPP_PREFIX)/bin/autoheader"; \
##        popd ; \
##      done; \
##      popd; \
##      [ -d stamps ] || mkdir stamps ;
##      touch stamps/regen-binutils;

##.PHONY: build-binutils build-binutils-force
##build-binutils: stamps/build-binutils
##build-binutils-force stamps/build-binutils: stamps/regen-binutils stamps/install-supp-tools
##      cd binutils-$(BINUTILS_VERSION) ; \
##      ./configure   --enable-maintainer-mode          \
##      --prefix="$(PREFIX)" --target=$(TARGET) --disable-nls           \
##      --disable-shared --disable-werror                               \
##      --with-sysroot="$(PREFIX)/$(TARGET)" --with-bugurl=$(BUG_URL) &&        \
##      $(MAKE) all-bfd TARGET-bfd=headers; \
##      rm bfd/Makefile; \
##      make configure-bfd; \
##      $(MAKE)
##      [ -d stamps ] || mkdir stamps ;
##      touch stamps/build-binutils;

##.PHONY: install-binutils install-binutils-force
##install-binutils: stamps/install-binutils
##install-binutils-force stamps/install-binutils: stamps/build-binutils
##      cd binutils-$(BINUTILS_VERSION) && \
##      $(MAKE) installdirs install-host install-target
##      [ -d stamps ] || mkdir stamps ;
##      touch stamps/install-binutils;


##.PHONY: clean-binutils
##clean-binutils:
##      rm -rf build/binutils stamps/*-binutils binutils-*

##.PHONY: realclean-binutils
##realclean-binutils: clean-binutils
##      rm downloads/$(BINUTILS_ARCHIVE)


########## DFU PROGRAMMER ###########

.PHONY: download-dfu download-dfu-force
download-dfu: downloads/$(DFU_ARCHIVE)
download-dfu-force downloads/$(DFU_ARCHIVE):
        [ -d downloads ] || mkdir downloads ;
        cd downloads && curl -LO $(DFU_URL)

.PHONY: extract-dfu extract-dfu-force
extract-dfu: stamps/extract-dfu
extract-dfu-force stamps/extract-dfu: downloads/$(DFU_ARCHIVE)
        @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
        [ "$$t1" = "$(DFU_MD5)" ] || \
        ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
        tar -zxf $< ;
        [ -d stamps ] || mkdir stamps ;
        touch stamps/extract-dfu;

.PHONY: build-dfu build-dfu-force
build-dfu: stamps/build-dfu
build-dfu-force stamps/build-dfu: stamps/extract-dfu
        mkdir -p build/dfu-programmer && cd build/dfu-programmer && \
        ../../dfu-programmer-$(DFU_VERSION)/configure --prefix="$(PREFIX)" && \
        $(MAKE) -j$(PROCS)
        [ -d stamps ] || mkdir stamps
        touch stamps/build-dfu;

.PHONY: install-dfu install-dfu-force
install-dfu: stamps/install-dfu
install-dfu-force stamps/install-dfu: stamps/build-dfu
        cd build/dfu-programmer && \
        $(MAKE) install
        [ -d stamps ] || mkdir stamps
        touch stamps/install-dfu;

.PHONY: clean-dfu
clean-dfu:
        rm -rf build/dfu stamps/*-dfu dfu-*

.PHONY: realclean-dfu
realclean-dfu: clean-dfu
        rm downloads/$(DFU_ARCHIVE)


################ Bootstrap GCC ################

.PHONY: download-gcc download-gcc-force
download-gcc: downloads/$(GCC_ARCHIVE)
download-gcc-force downloads/$(GCC_ARCHIVE):
        [ -d downloads ] || mkdir downloads ;
        cd downloads && curl -LO $(GCC_URL)

.PHONY: extract-gcc extract-gcc-force
extract-gcc: stamps/extract-gcc
extract-gcc-force stamps/extract-gcc: downloads/$(GCC_ARCHIVE)
        @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
        [ "$$t1" = "$(GCC_MD5)" ] || \
        ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
        tar -jxf $< ;
        [ -d stamps ] || mkdir stamps ;
        touch stamps/extract-gcc;

.PHONY: patch-gcc patch-gcc-force
patch-gcc: stamps/patch-gcc
patch-gcc-force stamps/patch-gcc: stamps/extract-gcc stamps/extract-avr32patches
        pushd gcc-$(GCC_VERSION) ; \
        for f in ../source/avr32/gcc/*.patch; do \
        patch -N -p0 <$${f} ; \
        done ; \
        popd ;
        [ -d stamps ] || mkdir stamps
        touch stamps/patch-gcc;

CFLAGS_FOR_TARGET="-ffunction-sections -fdata-sections                  \
-fomit-frame-pointer -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -g    \
-Os -fno-unroll-loops"

.PHONY: build-gcc build-gcc-force
build-gcc: stamps/build-gcc
build-gcc-force stamps/build-gcc: stamps/install-binutils stamps/patch-gcc
        mkdir -p build/gcc && cd build/gcc && \
        pushd ../../gcc-$(GCC_VERSION) ; \
        make clean ; \
        popd ; \
        ../../gcc-$(GCC_VERSION)/configure --prefix="$(PREFIX)"         \
        --target=$(TARGET) --enable-languages="c" --with-gnu-ld         \
        --with-gnu-as --with-newlib --disable-nls --disable-libssp      \
        --with-dwarf2 --enable-sjlj-exceptions                          \
        --enable-version-specific-runtime-libs --disable-libstdcxx-pch  \
        --disable-shared                                                \
        --with-build-time-tools="$(PREFIX)/$(TARGET)/bin"               \
        --enable-cxx-flags=$(CFLAGS_FOR_TARGET)                         \
        --with-sysroot="$(PREFIX)/$(TARGET)"                            \
        --with-build-sysroot="$(PREFIX)/$(TARGET)"                      \
        --with-build-time-tools="$(PREFIX)/$(TARGET)/bin"               \
        CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)                          \
        LDFLAGS_FOR_TARGET="--sysroot=\"$(PREFIX)/$(TARGET)\""          \
        CPPFLAGS_FOR_TARGET="--sysroot=\"$(PREFIX)/$(TARGET)\""         \
        --with-bugurl=$(BUG_URL) \
        --with-pkgversion=$(PKG_VERSION) && \
        $(MAKE) -j$(PROCS)
        [ -d stamps ] || mkdir stamps
        touch stamps/build-gcc;

.PHONY: install-gcc install-gcc-force
install-gcc: stamps/install-gcc
install-gcc-force stamps/install-gcc: stamps/build-gcc
        cd build/gcc && \
        $(MAKE) installdirs install-target && \
        $(MAKE) -C gcc install-common install-cpp install- install-driver install-headers install-man
        [ -d stamps ] || mkdir stamps
        touch stamps/install-gcc;

.PHONY: clean-gcc
clean-gcc:
        rm -rf build/gcc stamps/*-gcc gcc-*

.PHONY: realclean-gcc
realclean-gcc: clean-gcc
        rm downloads/$(GCC_ARCHIVE)


################ Final GCC ################

.PHONY: build-final-gcc build-final-gcc-force
build-final-gcc: stamps/build-final-gcc
build-final-gcc-force stamps/build-final-gcc: stamps/install-binutils stamps/install-gcc stamps/install-newlib stamps/patch-gcc
        mkdir -p build/final-gcc && cd build/final-gcc && \
        pushd ../../gcc-$(GCC_VERSION) ; \
        make clean ; \
        popd ; \
        ../../gcc-$(GCC_VERSION)/configure --prefix=$(PREFIX) \
        --target=$(TARGET) $(DEPENDENCIES) --enable-languages="c,c++" --with-gnu-ld \
        --with-gnu-as --with-newlib --disable-nls --disable-libssp \
        --with-dwarf2 --enable-sjlj-exceptions \
        --enable-version-specific-runtime-libs --disable-libstdcxx-pch \
        --disable-shared --enable-__cxa_atexit \
        --with-build-time-tools=$(PREFIX)/$(TARGET)/bin \
        --enable-cxx-flags=$(CFLAGS_FOR_TARGET) \
        --with-sysroot=$(PREFIX)/$(TARGET) \
        --with-build-sysroot=$(PREFIX)/$(TARGET) \
        --with-build-time-tools=$(PREFIX)/$(TARGET)/bin \
        CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET) \
        LDFLAGS_FOR_TARGET="--sysroot=$(PREFIX)/$(TARGET)" \
        CPPFLAGS_FOR_TARGET="--sysroot=$(PREFIX)/$(TARGET)" \
        --with-bugurl=$(BUG_URL) \
        --with-pkgversion=$(PKG_VERSION) && \
        $(MAKE) -j$(PROCS)
        [ -d stamps ] || mkdir stamps
        touch stamps/build-final-gcc;

.PHONY: install-final-gcc install-final-gcc-force
install-final-gcc: stamps/install-final-gcc
install-final-gcc-force stamps/install-final-gcc: stamps/build-final-gcc
        cd build/final-gcc && \
        $(MAKE) installdirs install-target && \
        $(MAKE) -C gcc install-common install-cpp install- install-driver install-headers install-man
        [ -d stamps ] || mkdir stamps
        touch stamps/install-final-gcc;

.PHONY: clean-final-gcc
clean-final-gcc: clean-gcc
        rm -rf build/final-gcc stamps/*-final-gcc

.PHONY: realclean-final-gcc
realclean-final-gcc: realclean-gcc


########## GDB ###########

.PHONY: download-gdb download-gdb-force
download-gdb: downloads/$(GDB_ARCHIVE)
download-gdb-force downloads/$(GDB_ARCHIVE):
        [ -d downloads ] || mkdir downloads ;
        cd downloads && curl -LO $(GDB_URL)

.PHONY: extract-gdb extract-gdb-force
extract-gdb: stamps/extract-gdb
extract-gdb-force stamps/extract-gdb: downloads/$(GDB_ARCHIVE)
        @(t1=`openssl md5 $< | cut -f 2 -d " " -` && \
        [ "$$t1" = "$(GDB_MD5)" ] || \
        ( echo "Bad Checksum! Please remove the following file and retry: $<" && false ))
        tar -jxf $< ;
        [ -d stamps ] || mkdir stamps ;
        touch stamps/extract-gdb;

.PHONY: patch-gdb patch-gdb-force
patch-gdb: stamps/patch-gdb
patch-gdb-force stamps/patch-gdb: stamps/extract-gdb stamps/extract-avr32patches
        pushd gdb-$(GDB_VERSION) ; \
        for f in ../source/avr32/gdb/*.patch; do \
        patch -N -p0 <$${f} ; \
        done ; \
        popd ; \
        [ -d stamps ] || mkdir stamps
        touch stamps/patch-gdb;

.PHONY: regen-gdb regen-gdb-force
regen-gdb: stamps/regen-gdb
regen-gdb-force stamps/regen-gdb: stamps/patch-gdb stamps/install-supp-tools
        pushd gdb-$(GDB_VERSION) ; \
        "$(SUPP_PREFIX)/bin/aclocal" -I config ; \
        "$(SUPP_PREFIX)/bin/autoconf" ; \
        "$(SUPP_PREFIX)/bin/automake" ; \
        "$(SUPP_PREFIX)/bin/autoheader" ; \
        for dir in bfd opcodes gdb; do \
          pushd $$dir ; \
          echo "JESS 1: $$dir" >&2 ; \
          "$(SUPP_PREFIX)/bin/autoconf"; \
          "$(SUPP_PREFIX)/bin/automake"; \
          "$(SUPP_PREFIX)/bin/autoheader"; \
          echo "JESS 2: $$dir" >&2 ; \
          popd ; \
        done; \
        popd; \
        [ -d stamps ] || mkdir stamps ;
        touch stamps/regen-gdb;

.PHONY: build-gdb build-gdb-force
build-gdb: stamps/build-gdb
build-gdb-force stamps/build-gdb: stamps/regen-gdb stamps/install-supp-tools
        echo "JESS 3" >&2 ; \
        mkdir -p build/gdb && cd build/gdb && \
        ../../gdb-$(GDB_VERSION)/configure --prefix="$(PREFIX)" --target=$(TARGET) --disable-werror && \
        $(MAKE) -j$(PROCS)
        $(MAKE) installdirs install-host install-target
        [ -d stamps ] || mkdir stamps
        touch stamps/build-gdb;

.PHONY: install-gdb install-gdb-force
install-gdb: stamps/install-gdb
install-gdb-force stamps/install-gdb: stamps/build-gdb
        cd build/gdb && \
        $(MAKE) install
        [ -d stamps ] || mkdir stamps
        touch stamps/install-gdb;

.PHONY: clean-gdb
clean-gdb:
        rm -rf build/gdb stamps/*-gdb gdb-*

.PHONY: realclean-gdb
realclean-gdb: clean-gdb
        rm downloads/$(GDB_ARCHIVE)

################ NON-WORKING/NON-ADJUSTED TARGETS ################


gcc-optsize-patch: gcc-$(GCC_VERSION)-$(CS_BASE)/
        pushd gcc-$(GCC_VERSION)-$(CS_BASE) ; \
        patch -N -p1 < ../patches/gcc-optsize.patch ; \
        popd ;

gmp: gmp-$(CS_BASE)/ sudomode
        sudo -u $(SUDO_USER) mkdir -p build/gmp && cd build/gmp ; \
        pushd ../../gmp-$(CS_BASE) ; \
        make clean ; \
        popd ; \
        sudo -u $(SUDO_USER) ../../gmp-$(CS_BASE)/configure --disable-shared && \
        sudo -u $(SUDO_USER) $(MAKE) -j$(PROCS) all && \
        $(MAKE) install

mpc: mpc-$(MPC_VERSION)/ sudomode
        sudo -u $(SUDO_USER) mkdir -p build/gmp && cd build/gmp ; \
        pushd ../../mpc-$(MPC_VERSION) ; \
        make clean ; \
        popd ; \
        sudo -u $(SUDO_USER) ../../mpc-$(MPC_VERSION)/configure --disable-shared && \
        sudo -u $(SUDO_USER) $(MAKE) -j$(PROCS) all && \
        $(MAKE) install

mpc-$(MPC_VERSION) : $(LOCAL_BASE)/mpc-$(CS_VERSION).tar.bz2
ifeq ($(USER),root)
        sudo -u $(SUDO_USER) tar -jxf $<
else
        tar -jxf $<
endif

mpfr: gmp mpfr-$(CS_BASE)/ sudomode
        sudo -u $(SUDO_USER) mkdir -p build/mpfr && cd build/mpfr && \
        pushd ../../mpfr-$(CS_BASE) ; \
        make clean ; \
        popd ; \
        sudo -u $(SUDO_USER) ../../mpfr-$(CS_BASE)/configure LDFLAGS="-Wl,-search_paths_first" --disable-shared && \
        sudo -u $(SUDO_USER) $(MAKE) -j$(PROCS) all && \
        $(MAKE) install

cross-gdb: gdb-$(CS_BASE)/
        mkdir -p build/gdb && cd build/gdb && \
        pushd ../../gdb-$(CS_BASE) ; \
        make clean ; \
        popd ; \
        ../../gdb-$(CS_BASE)/configure --prefix="$(PREFIX)" --target=$(TARGET) --disable-werror && \
        $(MAKE) -j$(PROCS) && \
        $(MAKE) installdirs install-host install-target && \
        mkdir -p "$(PREFIX)/man/man1" && \
        cp ../../gdb-$(CS_BASE)/gdb/gdb.1 "$(PREFIX)/man/man1/arm-none-eabi-gdb.1"

.PHONY : clean
clean:
        rm -rf build *-$(CS_BASE) binutils-* gcc-* gdb-* newlib-* mpc-* $(LOCAL_BASE) dfu-programmer-* autoconf-* automake-* stamps/* source supp