#!/usr/bin/make -f
# Makefile for an ipkg-buildpackage automated iPKG build process
# Heavily based on the toplevel Makefile of OpenWRT's buildroot.
# This should appear as ipkg/rules in the toplevel directory of 
# your openswan source package, and set executable.
#
# Modifications (C) 2004 Ferdinand O. Tempel <ftempel@linuxops.net>
#
# Copyright (C) 2001-2004 Erik Andersen <andersen@codepoet.org>
# Copyright (C) 2002 by Tim Riker <Tim@Rikers.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA


#############################################################
#
# EDIT this stuff to suit your system and preferences
#
# Use := when possible to get precomputation, thereby
# speeding up the build process.
#
#############################################################

# Change this variable to set the package name.
PACKAGE:=openswan

# Point this variable to your (prebuild!) openwrt buildroot
OPENWRTBR:=/home/polarwolf/WRT54G/buildroot

# What sortof target system shall we compile this for?
#ARCH:=i386
#ARCH:=arm
#ARCH:=mips
ARCH:=mipsel
#ARCH:=powerpc
#ARCH:=sh4
# Busybox link failing due to needing libgcc functions that are statics.
#ARCH:=cris

# The following currently fail to build since no shared lib support.
#ARCH:=sh64
#ARCH:=m68k
#ARCH:=v850
#ARCH:=sparc
#ARCH:=whatever

# Command used to download source code
WGET:=wget --passive-ftp

# Optimize toolchain for which type of CPU?
OPTIMIZE_FOR_CPU=$(ARCH)
#OPTIMIZE_FOR_CPU=i686
# Note... gcc 2.95 does not seem to like anything higher than i586.
#OPTIMIZE_FOR_CPU=i586
#OPTIMIZE_FOR_CPU=whatever

TARGET_OPTIMIZATION=-Os
TARGET_DEBUGGING= #-g
TARGET_CFLAGS=$(TARGET_OPTIMIZATION) $(TARGET_DEBUGGING)

HOSTCC:=gcc
BASE_DIR:=${shell pwd}
BUILD_DIR:=$(BASE_DIR)/ipkg
DESTDIR:=/tmp/$(PACKAGE)
STAGING_DIR=$(OPENWRTBR)/build_$(ARCH)$(ARCH_FPU_SUFFIX)/staging_dir
TARGET_PATH=$(STAGING_DIR)/bin:/bin:/sbin:/usr/bin:/usr/sbin
REAL_GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-linux-uclibc
GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-linux
KERNEL_CROSS=$(STAGING_DIR)/bin/$(OPTIMIZE_FOR_CPU)-linux-uclibc-
TARGET_CROSS=$(STAGING_DIR)/bin/$(OPTIMIZE_FOR_CPU)-linux-uclibc-
TARGET_CC=$(TARGET_CROSS)gcc
STRIP=$(TARGET_CROSS)strip

HOST_ARCH:=$(shell $(HOSTCC) -dumpmachine | sed -e s'/-.*//' \
	-e 's/sparc.*/sparc/' \
	-e 's/arm.*/arm/g' \
	-e 's/m68k.*/m68k/' \
	-e 's/ppc/powerpc/g' \
	-e 's/v850.*/v850/g' \
	-e 's/sh[234]/sh/' \
	-e 's/mips-.*/mips/' \
	-e 's/mipsel-.*/mipsel/' \
	-e 's/cris.*/cris/' \
	-e 's/i[3-9]86/i386/' \
	)

GNU_HOST_NAME:=$(HOST_ARCH)-pc-linux-gnu
TARGET_CONFIGURE_OPTS=PATH=$(TARGET_PATH) \
		AR=$(TARGET_CROSS)ar \
		AS=$(TARGET_CROSS)as \
		LD=$(TARGET_CROSS)ld \
		NM=$(TARGET_CROSS)nm \
		CC=$(TARGET_CROSS)gcc \
		GCC=$(TARGET_CROSS)gcc \
		CXX=$(TARGET_CROSS)g++ \
		RANLIB=$(TARGET_CROSS)ranlib

ifeq ($(ENABLE_LOCALE),true)
DISABLE_NLS:=
else
DISABLE_NLS:=--disable-nls
endif

# This assumes mipsel compiled libraries and headers for gmp in /tmp/gmp!
# ToDo: Check for this, download and build if necessary

USERCOMPILE:=$(TARGET_CFLAGS) -I/usr/local/openwrt/buildroot/build_mipsel/root/include -I/tmp/gmp.build/usr/include 
LDFLAGS="-L/usr/local/openwrt/buildroot/build_mipsel/root/lib/ -L/tmp/gmp.build/usr/lib"

KERNELSRC=$(OPENWRTBR)/build_mipsel/linux

.PHONY: build install clean

all: install

build: build-stamp

build-stamp: $(BASE_DIR)/.patched
	$(MAKE) $(TARGET_CONFIGURE_OPTS) KERNELSRC=$(KERNELSRC) ARCH=mips module
	$(MAKE) DESTDIR=$(DESTDIR) $(TARGET_CONFIGURE_OPTS) USERCOMPILE="$(USERCOMPILE)" programs
	
install: build
	$(MAKE) DESTDIR=$(DESTDIR) $(TARGET_CONFIGURE_OPTS) USERCOMPILE="$(USERCOMPILE)" install
	$(MAKE) DESTDIR=$(DESTDIR) $(TARGET_CONFIGURE_OPTS) STRIP=$(STRIP) USERCOMPILE="$(USERCOMPILE)" ipkg_strip
	cp -a $(DESTDIR) $(DESTDIR).sav
	
$(BASE_DIR)/.patched:
	@for p in $(BUILD_DIR)/patches/*;do	patch -p1 < $$p;done
	$(MAKE) DESTDIR=$(DESTDIR) $(TARGET_CONFIGURE_OPTS) USERCOMPILE="$(USERCOMPILE)" KERNELSRC=$(KERNELSRC) 
	touch .patched

clean:
	rm -f build-stamp
