# -*- mode: makefile -*-
#
default: _complain_

include ./local.uses


SRC_DIR      ?= $(HERE)/openocd
BUILD_SUFFIX ?= $(BUILD_MACHINE)
BUILD_DIR =$(HERE)/openocd.$(BUILD_SUFFIX)

checkout:
	svn co https://svn.berlios.de/svnroot/repos/openocd/trunk openocd

remove.install:
	rm -rf ${INSTALL_DIR}

#========================================
# Win32 Build Permutations
#   none
#   parport
#   ftd2xx - (ftdichip)
#   libftd
CONFIG_OPTIONS_win32_none     = 
CONFIG_OPTIONS_win32_parport  = --enable-parport
CONFIG_OPTIONS_win32_ftd2xx   = --enable-parport --enable-ft2232_ftd2xx --with-ftd2xx-win32-zipdir=$(FTD2XX_WIN32_DIR)

CYGWIN_EASY_PERMUTATIONS += none
CYGWIN_EASY_PERMUTATIONS += parport
CYGWIN_EASY_PERMUTATIONS += ftd2xx

MINGW32_EASY_PERMUTATIONS += none
MINGW32_EASY_PERMUTATIONS += parport
MINGW32_EASY_PERMUTATIONS += ftd2xx


# This is not a possible permutation, it is manual :-(
# Why? Because "libftdi" installs things into install/include
# which would efect the 'ftd2xx' win32 build
CONFIG_OPTIONS_win32_libftdi  = --enable-parport --enable-ft2232_libftdi

# Default build for win32... is the ftd2xx type build.
PERMUTE_win32          ?= ftd2xx
CONFIG_OPTIONS_win32   ?= $(CONFIG_OPTIONS_win32_$(PERMUTE_win32))
CONFIG_OPTIONS_cygwin   = $(CONFIG_OPTIONS_win32)
CONFIG_OPTIONS_mingw32  = $(CONFIG_OPTIONS_win32)

#========================================
# Linux Build Permuatations
#    none
#    parport
#    ft2232_ftd2xx
#    ft2232_libftdi 
CONFIG_OPTIONS_linux_none    =
LINUX_EASY_PERMUTATIONS     += none

CONFIG_OPTIONS_linux_parport = --enable-parport
LINUX_EASY_PERMUTATIONS     += parport

CONFIG_OPTIONS_linux_ft2232_libftdi = --enable-parport --enable-ft2232-libftdi
#this cannot be done as part of the permutations.
#LINUX_EASY_PERMUTATIONS += ft2232_libftdi

CONFIG_OPTIONS_linux_ft2232_ftd2xx_static  = \
	--enable-parport \
	--enable-ft2232-ftd2xx --with-ftd2xx-lib=static --with-ftd2xx-linux-tardir=$(FTD2XX_LINUX_DIR)
LINUX_EASY_PERMUTATIONS += ft2232_ftd2xx_static

# this is not a possible permutation it is manual :-(
# why? because it interfers with the other permutations
# by "installing files" in the $(INSTALL_DIR)
CONFIG_OPTIONS_linux_ftd2xx_installed  = \
	--enable-parport \
	--enable-ft2232-ftd2xx \
	--with-ftd2xx-lib=shared

# The default build permutation is
PERMUTE_linux ?= ft2232_ftd2xx_static
CONFIG_OPTIONS_linux = $(CONFIG_OPTIONS_linux_$(PERMUTE_linux))

CONFIG_OPTIONS_darwin=\
	--enable-ftd2232-libftdi

# Which build are we doing?
CONFIG_OPTIONS := $(CONFIG_OPTIONS_$(BUILD_SYSNAME))

bootstrap:
	cd $(SRC_DIR) && bash ./bootstrap

clean::
	rm -rf $(BUILD_DIR)

ifndef CFLAGS
_CFLAGS=true
else
_CFLAGS=export CFLAGS="${CFLAGS}"
endif


# if this was given... then pass it on 
configure:
	@echo "     Build Sysname: $(BUILD_SYSNAME)"
	@echo "    Config Options: $(CONFIG_OPTIONS)"
	rm -rf $(BUILD_DIR)
	mkdir $(BUILD_DIR)
	${_CFLAGS} && \
	cd $(BUILD_DIR) && \
		$(SRC_DIR)/configure \
			--prefix=$(PREFIX) \
			--exec-prefix=$(EXEC_PREFIX) \
			$(CONFIG_OPTIONS)

build:
	cd $(BUILD_DIR) && $(MAKE)

install:
	cd $(BUILD_DIR) && $(MAKE) install

all: configure build install

.PHONY: install

# The "cygwin.libftdi" requires that libftdi be built
# and installed *PRIOR* to running this target.
# it is not part of the permutations because ... 
# it interfers with the ftd2xx based builds
cygwin.libftdi: 
	$(MAKE) -f Makefile.openocd bootstrap
	$(MAKE) BUILD_SUFFIX=$@ PERMUTE_win32=libftdi -f Makefile.openocd all

cygwin.easy.permutations: remove.install ${CYGWIN_EASY_PERMUTATIONS:%=_cygwin.%}

_cygwin.%:
	@echo ""
	@echo ""
	@echo "========================================"
	@echo "Permutation Build... $@"
	@echo "========================================"
	@echo ""
	@echo ""
	$(MAKE) PERMUTE_win32=$* BUILD_SUFFIX=cygwin.$* -f Makefile.openocd all 
	$(EXEC_PREFIX)/bin/openocd -v   

mingw32.easy.permutations: remove.install ${MINGW32_EASY_PERMUTATIONS:%=_mingw32.%}

# I (duane) build openocd-mingw32 via Cygwin.
# Sadly, the "mingw32" buid for cygwin does not include
# the required "elf.h" header files... so ... 
# we have them in our own private helper place.
_mingw32.%:
	@echo ""
	@echo ""
	@echo "========================================"
	@echo "Permutation Build... $@"
	@echo "========================================"
	@echo ""
	@echo ""
	CFLAGS="-mno-cygwin -I$(HERE)/mingw32_help/include" \
	$(MAKE) -f Makefile.openocd all ;\
	$(EXEC_PREFIX)/bin/openocd -v   

win32.permutations: mingw32.permutations cygwin.permutations


# SMOKE TEST - Build every linux permuation...
# If "openocd -v" does exit(0) we are good enough.

linux.easy.permutations: remove.install ${LINUX_EASY_PERMUTATIONS:%=_linux.%}


_linux.%:
	@echo ""
	@echo ""
	@echo "========================================"
	@echo "Permutation Build... $@"
	@echo "========================================"
	@echo ""
	@echo ""
	$(MAKE) PERMUTE_linux=$* BUILD_SUFFIX=linux.$* -f Makefile.openocd all 
	$(EXEC_PREFIX)/bin/openocd -v   

linux.ftd2xx_installed:
	${MAKE} -f Makefile.openocd _$@

linux.ft2232_libftdi:
	${MAKE} -f Makefile.openocd _$@

_complain_:
	@echo ""
	@echo "Please try one of these targets: bootstrap, clean, configure, build, install"
	@echo " Or read the makefile and learn about the permutation test targets"
	@echo ""
	@exit 1