#
#----------------------------------------------------------------------
# (C) Copyright 2000
# Sysgo Real-Time Solutions GmbH.
# Klein-Winternheim, Germany
# All rights reserved.
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# Project:     Elinos -- Embedded Linux OS
# Description: BusyBox Demo
# This file:   Makefile
# Purpose:     Build the demo
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# CVS info:
#
# $Author: pba $
# $Date: 2002/06/21 10:15:00 $
# $Revision: 1.22 $
# $Id: Makefile,v 1.22 2002/06/21 10:15:00 pba Exp $
# $Source: /home/elinos/CVS/sysgo/elinos/demo-empty/Makefile,v $
#----------------------------------------------------------------------

# only allow ourselves to run when ELINOS.sh is sourced:
ifeq "undefined" "$(origin ELINOS_PREFIX)"
.DEFAULT: all ; @$(MAKE) all
all: ; @echo "*** ELINOS_PREFIX is not set, please source ELINOS.sh first!"
else

KDIR       = $(ELINOS_PROJECT)/linux
KCONFIG    = $(KDIR)/.config
MKEFS      = $(ELINOS_PREFIX)/bin/mkefs
SRC        = src
SUBDIRS    = $(wildcard $(SRC)/*)
AUTONODE   = autonode.sh 
DEPMOD     = $(ELINOS_PREFIX)/bin/depmod.pl
elinos-make = Makefile.elinos

# export the Kernel-Release
KVERSION = $(shell sed -n '/^VERSION  *=/s/^.*= *//p' $(KDIR)/Makefile)
KPATCHLEVEL = $(shell sed -n '/^PATCHLEVEL  *=/s/^.*= *//p' $(KDIR)/Makefile)
KSUBLEVEL = $(shell sed -n '/^SUBLEVEL  *=/s/^.*= *//p' $(KDIR)/Makefile)
KEXTRAVERSION = $(shell sed -n '/^EXTRAVERSION  *=/s/^.*= *//p' $(KDIR)/Makefile)
export KERNELRELEASE=$(KVERSION).$(KPATCHLEVEL).$(KSUBLEVEL)$(KEXTRAVERSION)

# test for modules and devfs
ifeq ($(shell sed -n '/^CONFIG_MODULES/p' $(KCONFIG)),)
  have-modules = "no"
else
  have-modules = "yes"
  module-path = $(ELINOS_PROJECT)/kernel.rootfs/lib/modules/$(KERNELRELEASE)
endif

ifeq ($(shell sed -n '/^CONFIG_DEVFS_FS/p' $(KCONFIG)),)
  have-devfs = "no"
else
  have-devfs = "yes"
endif

all: install-subdirs

clean: clean-subdirs
	rm -f $(ELINOS_DOSNAME).tgz
	rm -rf app.rootfs/*

cleaner: clean
	rm -rf boot/*
	rm -rf kernel.rootfs

distclean: cleaner distclean-subdirs distclean-kernel
	rm -f banner syslinux.cfg

install-subdirs:
ifeq ($(filter %$(elinos-make), $(SUBDIRS)),)
ifneq ($(SUBDIRS),)
	(for dir in $(SUBDIRS); do   \
		if [ -e $$dir/$(elinos-make) ]; then \
		    $(MAKE) -C $$dir -f $(elinos-make) install || exit 1; \
		fi;\
	done)
endif
else
	$(MAKE) -C $(SRC) -f $(elinos-make) install
endif

clean-subdirs:
ifeq ($(filter %$(elinos-make), $(SUBDIRS)),)
ifneq ($(SUBDIRS),)
	(for dir in $(SUBDIRS); do   \
		if [ -e $$dir/$(elinos-make) ]; then \
		    $(MAKE) -C $$dir -f $(elinos-make) clean || exit 1; \
		fi; \
	done)
endif
else
	$(MAKE) -C $(SRC) -f $(elinos-make) clean
endif

distclean-subdirs:
ifeq ($(filter %$(elinos-make), $(SUBDIRS)),)
ifneq ($(SUBDIRS),)
	(for dir in $(SUBDIRS); do   \
		if [ -e $$dir/$(elinos-make) ]; then \
		    $(MAKE) -C $$dir -f $(elinos-make) distclean || exit 1; \
		fi; \
	done)
endif
else
	$(MAKE) -C $(SRC) -f $(elinos-make) distclean
endif

distclean-kernel:
	mv $(KCONFIG) save.config
	$(MAKE) -C $(KDIR) distclean
	mv save.config $(KCONFIG)
	rm -rf kernel.rootfs/*
	rm -rf boot/*

.PHONY: boot
boot: $(KDIR)/vmlinux kernel.rootfs/lib all $(ELINOS_DOSNAME).tgz
	./makeboot $(ELINOS_DOSNAME).tgz

kernel.rootfs/lib: $(KCONFIG)
ifeq ($(have-modules), "yes")
	$(MAKE) -C $(KDIR) modules modules_install;
endif

$(KDIR)/vmlinux: $(KCONFIG)
	$(MAKE) -C $(KDIR) symlinks dep install

.PHONY: modules.dep
modules.dep:
ifeq ($(have-modules), "yes")
	$(MAKE) -C $(KDIR) _modinst_post
endif

$(ELINOS_DOSNAME).tgz: check-app-rootfs check-kernel-rootfs modules.dep
	@echo -n "Checking device node type on target file system ... "
ifeq ($(have-devfs), "no")
	@echo "device nodes"
	(                           \
	cat $(ELINOS_DOSNAME).mkefs;\
	CONFIG_ELINOS_NR_PTYS=10    \
	CONFIG_ELINOS_NR_CONSOLES=3 \
	$(AUTONODE) $(KCONFIG) \
	) | grep -v ELKAUTO | $(MKEFS) -a -s -o $@
else
	@echo "device file system"
	$(MKEFS) -a -s -o $@ -f $(ELINOS_DOSNAME).mkefs
endif

.PHONY: check-kernel-rootfs
check-kernel-rootfs:
	@mkdir -p kernel.rootfs

.PHONY: check-app-rootfs
check-app-rootfs:
	@mkdir -p app.rootfs

endif
