# $Id: Makefile,v 1.16 2001/07/31 05:42:58 cfl Exp $

.SUFFIXES: .done .config .mlz .sh
.PHONY: images root install install_new upgrade clean cleanimage \
	cleankernel veryclean distclean prereq check checkdevice checkwarning \
	webconfig oldconfig import docs

export MIDORI_VERSION=$(shell cat VERSION)

-include .config.mk

# if the above include failed we need to 
# set some variables anyway, make will abort otherwise
ifeq "$(TOPDIR)" ""
TOPDIR=.
SCRIPTS=cache/scripts
TOOLS=cache/tools
BUILD=cache/build
ROOT=cache/root
endif

ifeq "$(CONFIG)" ""
CONFIG=$(shell if [ ! -f .config ] ; then echo config ; else  cat .config ; fi )
endif
OLDCONFIG=$(shell cat .config ; echo $(CONFIG) > .config)

# Now lets see if the config file name changed, in which case we
# have to run a clean. We do this by touching $(CONFIG)
ifneq "$(CONFIG)" "$(OLDCONFIG)"
dummy=$(shell touch $(CONFIG))
endif

# export desired environment variables 
export SCRIPTS		# saves lots of repeating myself!
export GCC_CPP_TIME
export GCC_CPP_BASENAME
export TZ

# unexport evil environment variables (must also be in $(SCRIPTS)/functions.sh)
unexport LC_COLLATE
unexport LC_CTYPE
unexport LC_MONETARY
unexport LC_NUMERIC
unexport LC_TIME
unexport LC_ALL
unexport LANG
unexport LANGUAGE
unexport LOCPATH
unexport NLSPATH
unexport TZDIR

RM=rm -f		# In case something odd comes in from the env.

# kernel defines
KERNEL=$(BUILD)/linux/arch/i386/boot/bzImage


# Get list of applications to install (used to tigger .config.sh creation)
APPLICATIONS=$(shell for dir in $(APPS); do \
			for i in $$dir/*.mlz ; do \
				[ ! -f $$i ] || echo $$i ; \
			done ; \
		done)
APPSHELLS=$(shell for dir in $(APPS); do \
			for i in $$dir/*.sh ; do \
				[ ! -f $$i ] || echo $$i ; \
			done ; \
		done)

PACKAGES=$(PACKAGE_LIST:.mlz=.config) # run all installconfig functions first.

# Don't change the order of the first few entries (up to and 
# including compilation of gcc the second time)
PACKAGES+=$(BINUTILS_SRC_PACKAGE:.mlz=.done) 	# linker
PACKAGES+=$(FINDUTILS_SRC_PACKAGE:.mlz=.done)	# Locally-patched xargs
PACKAGES+=$(TOOLS)/bin/strip			# BSD-compat install script
PACKAGES+=$(BUILD)/linux/include/asm		# symbolic links, configure
PACKAGES+=$(TOOLS)/include/linux		# header links
PACKAGES+=$(GCC_SRC_PACKAGE:.mlz=.init)		# GCC, THE compiler
PACKAGES+=$(GLIBC_SRC_PACKAGE:.mlz=.done)	# GLIBC
PACKAGES+=$(GCC_SRC_PACKAGE:.mlz=.done)		# GCC, THE compiler, this time with g++
PACKAGES+=$(ROOT)/dev/console                   # all necessary devices not created elsewhere

PACKAGES+=$(PACKAGE_LIST:.mlz=.done)   # special target for additional applications (compilation and installation)
PACKAGES+=$(APPSHELLS:.sh=.done)       # special target for additional applications

PACKAGES+=$(KERNEL)                  # kernel
PACKAGES+=$(ROOT)/etc/ld.so.cache    # must be last package so that all libraries are picked up correctly

IMAGES=$(IMAGE)/.done 
UPGRADES=$(IMAGE)/upgrades/index.html 

## TARGETS START HERE ##

# create the images, but don't burn anything
images all::	prereq checkwarning $(ROOT) $(IMAGES)
	@touch $(IMAGES)

# handy target to create root directory
root::	prereq checkwarning $(ROOT) $(PACKAGES)

# Extract the documentation
docs::	prereq checkwarning $(PACKAGE_LIST:.mlz=.config)

# create a disk image
disk: prereq $(IMAGES) $(TOOLS)/bin/write_partition
	$(SCRIPTS)/mlz mlinstall install_disk

# burn a flash
install:: prereq checkwarning checkdevice $(IMAGES) $(TOOLS)/bin/write_partition
	ID=$(INSTALL_DEVICE) $(SCRIPTS)/mlz mlinstall install_device

# burn a flash after zeroing device$(TOOLS)/bin/write_partition
install_zero:: prereq checkwarning checkdevice $(IMAGES)
	ID=$(INSTALL_DEVICE) $(SCRIPTS)/mlz mlinstall install_zero

# upgrade a flash
install_new:: prereq checkwarning checkdevice $(IMAGES) $(TOOLS)/bin/make_active $(TOOLS)/bin/write_partition $(TOOLS)/bin/write_partition
	ID=$(INSTALL_DEVICE) $(SCRIPTS)/mlz mlinstall install_new 

$(TOOLS)/bin/write_partition: 
	$(SCRIPTS)/mlz mlinstall

$(TARGET)/write_partition: 
	$(SCRIPTS)/mlz mlinstall


# create the upgrade scripts
upgrade:: prereq $(UPGRADES) $(TARGET)/write_partition $(TOOLS)/bin/write_partition
	@echo "********************************************************************************"
	@echo "* you are now ready to upgrade using `hostname`:$(UPGRADE_PORT)"
	@echo "********************************************************************************"
	@$(TOOLS)/bin/httpd $(UPGRADE_PORT) $(IMAGE)/upgrades

$(UPGRADES): $(IMAGES) $(TARGET)/make_upgrade
	$(TARGET)/make_upgrade


# this works only if you installed the mltest package
ifneq "$(TEST_SERVER_SRC)" "no"
test-server:: prereq $(UPGRADES)
	$(TOOLS)/bin/test_server
endif


# use clean to create a new root tree
clean:: prereq
	$(RM) -r .config.sh .config.mk
	$(RM) -r $(ROOT)
	$(RM) -r $(ROOT).work
	$(RM) -r $(CACHE)/runtime
	$(RM) -r $(shell for dir in $(APPS) ; do echo $$dir/*.done $$dir/*.config $$dir/*.init ; done)
	$(RM) -r $(TOOLS)/.*installed*
	$(RM) -f $(TOPDIR)/configurations/*.*
	$(RM) -f $(TOPDIR)/docs/packages/*.*
	$(RM) $(KERNEL) $(BUILD)/$(LINUX_SRC)/.compile $(BUILD)/linux

# clean root tree and remove images to force creation of new images
cleanimage:: prereq clean
	$(RM) -r $(PARTS)
	$(RM) -r $(IMAGE)

# clean the kernel 
cleankernel:: prereq cleanimage
	$(SCRIPTS)/mlz $(LINUX_SRC) clean

# clean everything except the config file,
# but including the compiler and all libraries. Use with care.
# Note: top level directories in $(CACHE) containing a file .precious are not
# deleted!
veryclean:: prereq clean
	@for i in $(BUILD)/* ; do echo removing $$i ; [ -f $$i/.precious ] || $(RM) -rf $$i ; done
	$(RM) $(BUILD)/*/.installlocal
	$(RM) -r $(TOOLS)
	$(RM) -r $(TARGET)
	$(RM) .config.mk  .config.sh
	@a=`echo $(BUILD)/*/.precious` ; \
		if [ "$$a" !=  '$(BUILD)/*/.precious' ] ; then \
			echo ; echo '*************************************************************' ; \
			echo the following directories have not been deleted: ; \
			echo $(BUILD)/*/.precious | sed 's/\(.*\).precious$$/- \1/' ; \
			echo ; \
			exit 1 ; \
		fi
	-[ ! -d $(CACHE)/src ] && $(RM) -r $(CACHE)

# clean everything, including the file that remembers the name of the config file
distclean:: prereq veryclean
	$(RM) .config 

# run the configuration program
webconfig:: $(SCRIPTS)/configurator .config.sh
	CONFIG=$(CONFIG) $(SCRIPTS)/configurator configurator

# update the config file with new packages
oldconfig:: $(SCRIPTS)/configurator
	CONFIG=$(CONFIG) $(SCRIPTS)/configurator oldconfig

# import some other config files into the current one
# You need to define the names of the new ones by setting the variable IMPORT
import:: $(SCRIPTS)/configurator
	CONFIG=$(CONFIG) $(SCRIPTS)/configurator import $(IMPORT)
	$(RM) .config.mk .config.sh
	BUILD=$(BUILD) TOPDIR=$(TOPDIR) CONFIG=$(CONFIG) $(SCRIPTS)/configurator dot_files

prereq:: check $(CONFIG) 

checkwarning::
	@if [ -f .warning ] ; then echo "There are some configuration problems:" ; \
				   echo ; \
				   cat .warning ; \
				   echo ; \
				   echo "Please run the configurator with 'make webconfig'" ; \
				   echo "or edit the config file ${CONFIG}" ; \
				   exit 1 ; \
		fi

$(IMAGES): $(ROOT) .config.sh $(PACKAGES)
	$(SCRIPTS)/mlz mlinstall make_image
	-@$(SCRIPTS)/need_cleanimage || true # store new options

# create the main directory structure.
$(ROOT): .config.sh Makefile .config.mk
	if $(SCRIPTS)/need_cleanimage ; then $(MAKE) cleanimage ; else $(MAKE) clean ; fi
	mkdir -p $(ROOT) $(BUILD) $(TOOLS) $(TARGET) $(IMAGE) 
	touch $(IMAGE)/kernel.modules
	touch $(IMAGE)/kernel.config
	cd $(ROOT) ; mkdir -p $(DIRS)
	CONFIG=$(CONFIG) make .config.sh

# we only create the config file if it does not exist
$(CONFIG): $(SCRIPTS)/configurator configurations/DEFAULT.config
	@if [ ! -f $(CONFIG) ] ; then TOPDIR=$(TOPDIR) BUILD=$(BUILD) CONFIG=$(CONFIG) $(SCRIPTS)/configurator oldconfig ; \
		TOPDIR=$(TOPDIR) BUILD=$(BUILD) CONFIG=$(CONFIG) $(SCRIPTS)/configurator import + DEFAULT ; fi

# extract the configurator package (don't use CONFIGURATOR_SRC_PACKAGE, its not defined yet!)
$(SCRIPTS)/configurator: $(shell echo apps/configurator-*.mlz)
	mkdir -p cache/build
	@if [ ! -f "`echo apps/configurator-*.mlz`" ] ; then echo "The build process needs exactly ONE apps/configurator-*.mlz file" ; exit 1 ; fi 
	rm -rf cache/build/configurator-*
	cd cache/build ; tar xzf ../../apps/configurator-*.mlz
	cp cache/build/configurator-*/build $(SCRIPTS)/configurator

configurations/DEFAULT.config: $(SCRIPTS)/configurator
	 TOPDIR=$(TOPDIR) BUILD=$(BUILD) $(SCRIPTS)/configurator defaults

# we cannot use $SCRIPTS for cache/scripts/Makefile as the rule for that
# makefile does not use $SCRIPTS either  (its in midori/Makefile)
.config.mk .config.sh: $(CONFIG) Makefile cache/scripts/Makefile $(SCRIPTS)/configurator $(APPLICATIONS)
	BUILD=$(BUILD) TOPDIR=$(TOPDIR) CONFIG=$(CONFIG) $(SCRIPTS)/configurator dot_files

check:
	@if ! id -nu | fgrep root > /dev/null ; then echo "You need to be root to build this system!"; exit 1 ; fi
	@if ls -ald . .. cache | egrep -qi '^......s' ; then echo "You must not build using setgid directories!"; exit 1 ; fi
	@mkdir -p $(CACHE)
	@( echo ; echo `date`: 'NEW BUILD STARTED' ) >> $(CACHE)/log

checkdevice::
	@if [ "$(INSTALL_DEVICE)" = "" ] ; then echo "You need to define INSTALL_DEVICE in $(CONFIG)," ; echo "or use 'make INSTALL_DEVICE=... install'." ; echo ; exit 1 ; fi

# Infrastrucure tools that we don't maintain locally.

# BSD-compat install script
$(TOOLS)/bin/strip:	$(SCRIPTS)/strip.sh $(TOOLS)/bin/$(TARGETARCH)-strip
	cp -f $< $@

$(TOOLS)/bin/$(TARGETARCH)-strip:
	ln -s ../$(TARGETARCH)/bin/strip $@

# gcc, first version without c++
$(GCC_SRC_PACKAGE:.mlz=.init): $(GCC_SRC_PACKAGE)
	$(SCRIPTS)/mlz $(GCC_SRC) upto installlocal
	touch $(GCC_SRC_PACKAGE:.mlz=.init)

# The linux kernel
$(KERNEL): $(IMAGE)/kernel.config $(IMAGE)/kernel.modules $(LINUX_SRC_PACKAGE)
	MOPTS="$(KERNEL_OPTIONS)" MMODS="$(KERNEL_MODULES)" $(SCRIPTS)/mlz $(LINUX_SRC)
	touch $(KERNEL)

# the kernel, configure only
$(BUILD)/linux/include/asm:
	CONFIG=$(CONFIG) $(SCRIPTS)/mlz $(LINUX_SRC) upto configure

# kernel header links
$(TOOLS)/include/linux:
	$(SCRIPTS)/mlz $(LINUX_SRC) upto kernel_links

# ldconfig and friends (part of glibc package)
$(ROOT)/etc/ld.so.cache:
	$(BUILD)/$(GLIBC_SRC)/build_ld

# devices
$(ROOT)/dev/console:
	$(SCRIPTS)/make_devices

# the applications files and scripts (shell scripts are deprecated)
$(CONFIGURATIONS:.mlz=.config): .config.sh
$(APPSHELLS:.sh=.done): .config.sh

.mlz.config:
	$(SCRIPTS)/mlz $*.mlz upto installconfig
	@touch $*.config

.config.done:
	$(SCRIPTS)/mlz $*.mlz
	@touch $*.done

.sh.done:
	$*.sh $*
	@touch $*.done
