#!/usr/bin/make -f
# Makefile for a simple busybox/uClibc root filesystem
#
# 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

# This makefile is a modified Makefile from the openwrt buildroot,
# which in its turn is a modified uClibc Makefile.
# The purpose of this Makefile is to serve as ipkg/rules file
# so an ipkg package can be created using ipkg-build.
# ipkg-buildroot expects stuff in /tmp/package-name, so that's
# where $DESTDIR is set to.

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

# Path to buildroot
OPENWRTBR:=/data/pw/WRT54G/buildroot

# Package information
PACKAGE:=mawk
DESTDIR:=/tmp/$(PACKAGE)

# What sortof target system shall we compile this for?
ARCH:=mipsel

# 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

HOSTCC:=gcc

# Set up useful directories
BASE_DIR:=${shell pwd}
BUILD_DIR:=$(BASE_DIR)/ipkg
STAGING_DIR=$(OPENWRTBR)/build_$(ARCH)$(ARCH_FPU_SUFFIX)/staging_dir
INCLUDE_DIR=$(OPENWRTBR)/../packages/build_$(ARCH)$(ARCH_FPU_SUFFIX)/usr/include
LIB_DIR=$(OPENWRTBR)/../packages/build_$(ARCH)$(ARCH_FPU_SUFFIX)/usr/lib

TARGET_PATH=$(STAGING_DIR)/bin:/bin:/sbin:/usr/bin:/usr/sbin

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

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

# Package specific flags
PKG_LDFLAGS= \
PKG_CFLAGS= \
PKG_CONFIG= \
#	--sharedstatedir=/var \
#	--disable-ipv6 \
#	--with-user=root

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

.PHONY: build install clean

all: install

build: build-stamp

build-stamp: $(PACKAGE)/.configured

$(PACKAGE)/.configured: $(BASE_DIR)/.patched
	rm -rf config.cache; \
	$(TARGET_CONFIGURE_OPTS) \
	CFLAGS="$(TARGET_CFLAGS)" \
	MATHLIB="$(STAGING_DIR)/mipsel-linux-uclibc/lib/libm.a" \
	./configure \
	--target=$(GNU_TARGET_NAME) \
	--host=$(GNU_TARGET_NAME) \
	--build=$(GNU_HOST_NAME) \
	--prefix=/usr \
	$(PKG_CONFIG)
	touch  .configured

$(BASE_DIR)/.patched:
	@for p in $(BUILD_DIR)/patches/*;do	patch -p0 < $$p;done
	touch .patched

install: build
	mkdir -p $(DESTDIR)/usr/bin
	mkdir -p $(DESTDIR)/usr/man/man1
	$(MAKE) $(TARGET_CONFIGURE_OPTS) DESTDIR=$(DESTDIR) STRIP=$(STRIP) install
	rm -rf $(DESTDIR)/usr/man
	for f in `find $(DESTDIR) -type f`; do (if file $$f | grep MIPS-II > /dev/null; then ( $(STRIP) --strip-unneeded $$f); fi); done
	
clean:
	rm -f build-stamp
	rm -rf $(DESTDIR)
	$(MAKE) clean
