# Makefile for a simple busybox/uClibc root filesystem
#
# Copyright (C) 2003-2004 Red Hat, Inc. <dlehman@redhat.com>
# Copyright (C) 2001-2003 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

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

# set to false to build uClibc-based (uClinux) rootfs
USE_GLIBC=false

# enable to build a native gcc toolchain with uclibc support
USE_UCLIBC_TOOLCHAIN:=false

# Enable this to use the uClibc daily snapshot instead of a released
# version.  Daily snapshots may contain new features and bugfixes. Or
# they may not even compile at all, depending on what Erik is doing...
USE_UCLIBC_SNAPSHOT:=false

# Enable this to use the busybox daily snapshot instead of a released
# version.  Daily snapshots may contain new features and bugfixes. Or
# they may not even compile at all....
USE_BUSYBOX_SNAPSHOT:=false

# Enable large file (files > 2 GB) support
BUILD_WITH_LARGEFILE:=true

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

# Optimize toolchain for which type of CPU?
OPTIMIZE_FOR_CPU=$(ARCH)

ifeq ($(USE_GLIBC),false)
UCLINUX_FLAGS=-D__uClinux__
endif

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

# Any additional gcc options you may want to include....
EXTRA_GCC_CONFIG_OPTIONS=

# Enable the following if you want locale/gettext/i18n support.
ENABLE_LOCALE:=false

# If you want multilib enabled, enable this...
MULTILIB:=--enable-multilib

#Install libstdc++?
INSTALL_LIBSTDCPP=false

# For SMP machines some stuff can be run in parallel
#JLEVEL=-j3

#############################################################
#
# The list of stuff to build for the target filesystem
#
#############################################################
USE_GNUPRO_TOOLS:=true
TARGETS:=
#ifeq ($(USE_GNUPRO_TOOLS),true)
#TARGETS+=gnupro
#endif

#TARGETS+=uclibc

##
## CVS stuff
##
USE_CVS:=false

ifeq ($(USE_CVS),true)
CVS:=set -x ; cvs -Q
CVS_REPO:=-d :ext:cvs.ges.redhat.com:/cvs/cvsfiles
CVS_PREFIX:=devo/
CVS_TAG:=-r frv-uclinux-branch
CVS_CLIENT:=-s CVS_RSH=ssh
endif

# The default minimal set
TARGETS+=busybox tinylogin

ifeq ($(USE_GLIBC),true)
TARGETS+=bash initscripts tcp_wrappers pam
endif

# Netkit base, telnetd
TARGETS+=netkitbase netkittelnet

# Netkit
TARGETS+=netkitftp netkitrouted netkittftp linuxftpd

# compression libs,utils
TARGETS+=zlib bzip2

# DHCP server,client
TARGETS+=dhcp

# general utils
TARGETS+=coreutils gawk

# SSL
TARGETS+=openssl

# SSH
TARGETS+=openssh

# NTP
TARGETS+=ntp

# remote debug agent
TARGETS+=rda

# web server, libs
TARGETS+=w3c-libwww xmlrpc-c

# web server
TARGETS+=boa

TARGETS+=netsnmp smtpclient

# network filesystems
TARGETS+=samba

TARGETS+=nfsutils 

# Power management
TARGETS+=acpid

TARGETS+=pcmcia sox

ifeq ($(USE_GLIBC),true)
TARGETS+=pciutils usbutils mgetty modutils

TARGETS+=cpio e2fsprogs file less net-tools procps psmisc rdate \
	rsync sudo sysklogd sysvinit tcpdump traceroute \
	util-linux which findutils grep gzip sed ash \
	iputils

TARGETS+=anacron at autofs berkeleydb bind dhcpcd gdbm lcap \
	libpcap minicom pidentd pppd zebra dump readline \
	console-tools fbset iproute iptables logrotate \
	lprng mingetty netkitntalk netkitrusers netkitrwall netkitrwho \
	pump rarpd rp-pppoe sash setserial statserial tftp time \
	vixie-cron parted modutils xinetd makedev pciutils \
	shadow-utils slang strace sendmail groff quota ipgrab \
	rstatd netkitrsh passwd finger procmail sox sysstat
endif

# Run customize.mk at the very end to add your own special config.
# This is useful for making your own distro within the buildroot
# process.
# TARGETS+=customize

#############################################################
#
# Pick your root filesystem type.
#
#############################################################
#TARGETS+=ext2root

# Must mount cramfs with 'ramdisk_blocksize=4096'
#TARGETS+=cramfsroot

# You may need to edit make/jffs2root.mk to change target 
# endian-ness or similar, but this is sufficient for most
# things as-is...
#TARGETS+=jffs2root

#############################################################
#
# You should probably leave this stuff alone unless you know 
# what you are doing.
#
#############################################################
ifeq ("$(strip $(TARGET_SOFT_FLOAT))","")
ARCH_FPU_SUFFIX:=
else
ARCH_FPU_SUFFIX:=_nofpu
endif

ifeq ($(ENABLE_LOCALE),true)
EXTRA_GCC_CONFIG_OPTIONS += --enable-clocale=gnu
endif

HOSTCC:=gcc
BASE_DIR:=${shell pwd}
SOURCE_DIR:=$(BASE_DIR)/sources
DL_DIR:=$(SOURCE_DIR)/dl
PATCH_DIR=$(SOURCE_DIR)/patches
BUILD_DIR:=$(BASE_DIR)/build_$(ARCH)$(ARCH_FPU_SUFFIX)
TARGET_DIR:=$(BUILD_DIR)/root
STAGING_DIR:=$(BUILD_DIR)/staging_dir
DOC_DIR:=$(BUILD_DIR)/docs
DESTDIR=$(BASE_DIR)/rootfs_$(ARCH)
SUDO:=/usr/bin/sudo
TOOL_BUILD_DIR=$(BASE_DIR)/toolchain_build_$(ARCH)$(ARCH_FPU_SUFFIX)
TARGET_PATH=$$PATH
ifeq ($(USE_GLIBC),false)
TARGET_CROSS=$(ARCH)-uclinux-
else
TARGET_CROSS=$(ARCH)-linux-gnu-
endif
TARGET_CC=$(TARGET_CROSS)gcc$(TARGET_SOFT_FLOAT)
STRIP=$(TARGET_CROSS)strip --remove-section=.comment --remove-section=.note
IMAGE:=$(BASE_DIR)/root_fs_$(ARCH)$(ARCH_FPU_SUFFIX)
GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-unknown-linux-gnu
KERNEL_CROSS=$(TARGET_CROSS)
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$(TARGET_SOFT_FLOAT) \
		GCC=$(TARGET_CROSS)gcc$(TARGET_SOFT_FLOAT) \
		CXX=$(TARGET_CROSS)g++$(TARGET_SOFT_FLOAT) \
		RANLIB=$(TARGET_CROSS)ranlib \
		STRIP=$(TARGET_CROSS)strip
ifeq ($(ENABLE_LOCALE),true)
DISABLE_NLS:=
else
DISABLE_NLS:=--disable-nls
endif


all:   world

TARGETS_CLEAN:=$(patsubst %,%-clean,$(TARGETS))
TARGETS_SOURCE:=$(patsubst %,%-source,$(TARGETS))
TARGETS_DIRCLEAN:=$(patsubst %,%-dirclean,$(TARGETS))
TARGETS_CHECKOUT:=$(patsubst %,%-checkout,$(TARGETS))

world: $(TARGETS) post

prep: $(DL_DIR) $(BUILD_DIR) $(STAGING_DIR) $(TARGET_DIR) gnupro uclibc

$(TARGETS): prep

.PHONY: all world clean dirclean distclean source prep post \
	$(TARGETS) $(TARGETS_CLEAN) $(TARGETS_DIRCLEAN) $(TARGETS_SOURCE)

include make/*.mk

#############################################################
#
# staging and target directories do NOT list these as 
# dependancies anywhere else
#
#############################################################
$(DL_DIR):
	mkdir $(DL_DIR)

$(BUILD_DIR):
	mkdir $(BUILD_DIR)

$(STAGING_DIR):
	rm -rf $(STAGING_DIR)
	mkdir -p $(STAGING_DIR)/lib
	mkdir -p $(STAGING_DIR)/usr
	mkdir -p $(STAGING_DIR)/usr/include
	ln -sf usr/include $(STAGING_DIR)/include
	mkdir -p $(STAGING_DIR)/usr/lib

$(TARGET_DIR):
	rm -rf $(TARGET_DIR)
	zcat $(SOURCE_DIR)/skel.tar.gz | tar -C $(BUILD_DIR) -xf -
	rm -f $(TARGET_DIR)/usr/lib/terminfo
	ln -s ../share/terminfo $(TARGET_DIR)/usr/lib/terminfo
	cp -a $(SOURCE_DIR)/target_skeleton/* $(TARGET_DIR)/
ifeq ($(USE_GLIBC),true)
	@echo -ne "\nWelcome to GNUPro Linux\n" > $(TARGET_DIR)/etc/issue
	@echo "GNUPro Linux release `basename $(GNUPRO_DIR)`-`date +%Y%m%d%H%M`" > $(TARGET_DIR)/etc/redhat-release
	mkdir -p $(TARGET_DIR)/var/subsys
	sed -i -e 's,^tmpfs.*$$,,' $(TARGET_DIR)/etc/fstab
endif
	-find $(TARGET_DIR) -type d -name CVS -exec rm -rf {} \; > /dev/null 2>&1 || :
	-mkdir -p $(TARGET_DIR)/usr/man $(TARGET_DIR)/usr/info
	-for i in `seq 1 9` ; do \
	    mkdir $(TARGET_DIR)/usr/man/man$$i || : ; \
	done
	-mkdir $(TARGET_DIR)/home/default
	for e in $(TARGET_DIR)/var/* ; do \
	    if test -L $$e ; then \
		if [ `readlink $$e` = /tmp ]; then \
		    rm -f $$e ; \
		    mkdir -p $$e ; \
		fi ; \
	    fi ; \
	done

source: $(TARGETS_SOURCE)

post: $(TARGETS)
	-cp -a $(TARGET_DIR)/usr/man $(DOC_DIR)/
	-cp -a $(TARGET_DIR)/usr/info $(DOC_DIR)/
	-find $(TARGET_DIR)/usr/man -type f | xargs rm -f
	-rm -f $(TARGET_DIR)/usr/info/*
	-rm -rf $(TARGET_DIR)/include $(TARGET_DIR)/usr/include
	chmod 1777 $(TARGET_DIR)/tmp
	-find $(TARGET_DIR) -name CVS -type d | xargs rm -rf
	$(SOURCE_DIR)/ldconfig.py $(TARGET_DIR)
ifeq ($(USE_GLIBC),true)
	echo NETWORKING=yes > $(TARGET_DIR)/etc/sysconfig/network
	mkdir $(TARGET_DIR)/var/lock/subsys || :
endif

install:
	@$(SUDO) sh -c \
		'if [ $$UID -ne 0 ]; then \
			echo "You must be root to install filesystems" ; \
			exit 1 ; \
		 fi'
	@if [ $(DESTDIR) = / ]; then \
		@echo "DESTDIR cannot be '/'" ;\
		exit 1 ;\
	fi
	@echo "Installing root filesystem to $(DESTDIR)..."
	[ -d $(DESTDIR) ] || $(SUDO) mkdir -p $(DESTDIR)
	(cd $(TARGET_DIR) ; \
	 find | cpio --quiet -o -H newc) | \
		(cd $(DESTDIR) ; \
		 $(SUDO) cpio -iumd --no-absolute-filenames --quiet)
	$(SUDO) chown -R 0.0 $(DESTDIR)
	$(SUDO) chown -R 1000.1000 $(DESTDIR)/home/default
	@$(SUDO) sh -c '( \
			if [ $$UID -eq 0 ]; then \
				$(SOURCE_DIR)/mkdevnodes $(DESTDIR) ;\
			else \
				echo "You must be root to create the device tree" ; \
			fi \
		)'
ifeq ($(USE_GLIBC),true)
	$(SUDO) chmod 4111 $(DESTDIR)/usr/bin/sudo
	$(SUDO) chown -R 2.2 $(DESTDIR)/var/spool/at
	$(SUDO) chown -R 25.25 $(DESTDIR)/var/run/named
	$(SUDO) chmod 4111 $(DESTDIR)/bin/ping
	$(SUDO) chmod 4111 $(DESTDIR)/bin/ping6
	$(SUDO) chmod 4111 $(DESTDIR)/sbin/arping
	$(SUDO) chown 0.12 $(DESTDIR)/var/spool/mail
	$(SUDO) chown 51.51 $(DESTDIR)/var/spool/clientmqueue
	$(SUDO) chown 0.12 $(DESTDIR)/var/spool/mqueue 
endif

uninstall:
	[ $(DESTDIR) != / ] && $(SUDO) rm -rf $(DESTDIR)

#############################################################
#
# Cleanup and misc junk
#
#############################################################
clean: $(TARGETS_CLEAN)
	rm -rf $(TARGET_DIR) $(STAGING_DIR) $(IMAGE)

dirclean: $(TARGETS_DIRCLEAN)
	rm -rf $(TARGET_DIR) $(STAGING_DIR) $(IMAGE)

checkout: $(DL_DIR) $(BUILD_DIR) $(STAGING_DIR) $(TARGET_DIR) $(TARGETS_CHECKOUT)

distclean:
	rm -rf $(DL_DIR) $(BUILD_DIR) $(LINUX_KERNEL) $(IMAGE)

sourceball: 
	rm -rf $(BUILD_DIR)
	set -e; \
	cd ..; \
	rm -f buildroot.tar.bz2; \
	tar -cvf buildroot.tar buildroot; \
	bzip2 -9 buildroot.tar

