#
#  Makefile for the Xinu operating system.
#
#  $Id: Makefile 1577 2008-10-03 18:19:26Z mschul $

include mipsVars

CONFIG    = xinu.conf

BOOTIMAGE = xinu.boot
KERNARC   = kernel.a
MAIN_SRC  = ../system/main.c

LDFLAGS   = -static --gc-sections -T ld.script

TOPDIR    = ..
LIBDIR    = ${TOPDIR}/lib

# Default make target
all: ${BOOTIMAGE}

###########################
# Source file information #
###########################

# Major system components to build into Xinu
# Each component is a directory in the source tree and should include a
# Makerules specifying what files to include
# (The loader should always be first, ensuring the image starts with it.)
COMPS     = loader system device/uart device/tty shell mailbox device/nvram device/backplane device/ether device/loopback device/switch test

# Location of the conf.c file defining system devices
CONFC     = ../system/conf.c

# Libraries to build into Xinu
# All libraries should exist as archive files in the lib directory
LIBS      = libxc

# Include expected files, each should add its part to the compile source
COMP_SRC :=
include ${COMPS:%=${TOPDIR}/%/Makerules}

COMP_OBJ = $(patsubst %.S,%.o,$(filter %.S,$(COMP_SRC))) \
           $(patsubst %.c,%.o,$(filter %.c,$(COMP_SRC)))

CONF_OBJ = ${CONFC:%.c=%.o}

LIB_ARC  = ${LIBS:%=${LIBDIR}/%.a}

MAIN_OBJ = ${MAIN_SRC:%.c=%.o}

###########################
# Documentation Variables #
###########################
DOCGEN_CONFIG = Doxyfile
DOCS := $(shell grep ^OUTPUT_DIRECTORY ${DOCGEN_CONFIG} | awk '{ print $$3 }')

INDENT = indent
INDENT_TYPES = -T uchar -T ushort -T uint -T ulong -T bool -T message    \
               -T irqmask -T semaphore -T mailbox -T qid_type -T tid_typ \
               -T syscall -T devcall -T shellcmd -T thread -T interrupt
INDENT_ARGS = -npro -kr -bl -blf -bls -bli0 -nce -ncs -nlps -l74 -lc74 -nut
INDENT_FILES = $(shell find ${TOPDIR} -name '*.[ch]')

# Export variables for recursive make calls (such as the library)
export
################
# Make targets #
################
${BOOTIMAGE}: ${COMP_OBJ} ${CONF_OBJ} ${MAIN_OBJ} ${LIB_ARC} ld.script
	${LD} -o $@ ${LDFLAGS} $^

${COMP_OBJ}: ${CONF_OBJ}

${KERNARC}: ${COMP_OBJ} ../system/unistd.o
	rm -f ${KERNARC}
	${AR} cr ${KERNARC} $^

objects: ${COMP_OBJ}

clean:
	rm -f *.o ${COMP_OBJ} ${MAIN_OBJ} ${BOOTIMAGE}
	rm -rf ${DOCS}

depend:
	$(MAKEDEP) -Y ${INCLUDE} ${COMP_SRC}

indent:
	${INDENT} ${INDENT_ARGS} ${INDENT_TYPES} ${INDENT_FILES}
	rm -f ${INDENT_FILES:%=%~}

docs:
	${DOCGEN} ${DOCGEN_CONFIG}

debug: 
	$(MAKE) DEBUG="${BUGFLAG}" ${BOOTIMAGE}

libclean: ${LIBS:%=%-clean}

confclean:
	$(MAKE) -C config/ clean

depclean:
	$(MAKEDEP)

realclean: clean depclean libclean confclean
	rm -f ../include/conf.h
	rm -f ../system/conf.c
	rm -f vn version
	rm -f Makefile.bak

###################
# Special targets #
###################
${MAIN_OBJ}: ${CONF_OBJ} ${MAIN_SRC}

${COMPS}:
	$(MAKE) COMPS="$@" objects

${CONFC}: config/config
	./config/config ${CONFIG}

config/config:
	$(MAKE) -C config/

${LIBS}: ${COMP_OBJ}
	$(MAKE) -C ${LIBDIR}/$@

${LIB_ARC}: ${COMP_OBJ}
	$(MAKE) -C ${@:%.a=%} install

${LIBS:%=%-clean}:
	$(MAKE) -C ${LIBDIR}/${@:%-clean=%} clean

# DO NOT DELETE THIS LINE -- make depend depends on it.
