#
# MOS: main makefile
#

default: all

.PHONY: all all-kernel all-modules docs clean mostlyclean distclean settarget help-target everything raw test todo fixme

# get the default target
include Makefile.CurrentDefault

BUILDROOT=build-$(ARCH)-$(TYPE)
TARGETDIR=$(BUILDROOT)/.directory
DIRECTORIES := $(BUILDROOT)

TESTDIR=build-test
TESTDIRFILE=build-test/.directory

# get the environment configs
include configs/Makefile.host
include configs/Makefile.$(ARCH)-$(TYPE)

# import source tree
include kernel/Makefile
include include/Makefile
include lib/Makefile
include libc/Makefile
include libfb/Makefile
include libudi/Makefile
include libgl/Makefile
include libgfx/Makefile
include libx86emu/Makefile
#include udiref/Makefile
include modules/Makefile
include docs/Makefile

# build quickstarts
all: all-$(ARCH)-$(TYPE)-$(TARGET)
all-kernel: all-kernel-$(ARCH)-$(TYPE)
all-modules: all-modules-$(ARCH)-$(TYPE)
everything: all-$(ARCH)-$(TYPE)-everything
raw: all-$(ARCH)-$(TYPE)-raw
install: install-$(ARCH)-$(TYPE)-$(TARGET)

# help
help:
	@echo MOS MAKEFILE HELP
	@echo Standard Make Targets:
	@echo "  all         - builds the entire OS according to current settings"
	@echo "  clean       - cleans all built files"
	@echo "  mostlyclean - cleans most files, but leaves libraries"
	@echo "  docs        - builds the documentation"
	@echo
	@echo Special Make Targets:
	@echo "  settarget   - change the default target"
	@echo "  everything  - build all targets for this configuration"
	@echo "  raw         - build, but do not compile images"
	@echo "  run, runw, run3w, runqw, runsw - start emulation"
	@echo "  debug, debugw, debugsw         - start debugged emulation"
	@echo
	@echo Quickstart:
	@echo "  type"
	@echo "     make settarget ARCH=ia32 TYPE=pc TARGET=floppy"
	@echo "     make "
	@echo "  to build the OS for a standard desktop pc"
	@echo
	@make help-target --no-print-directory


# makefile construction
# defaults to ia32-pc-floppy
Makefile.CurrentDefault:
	@echo ARCH=ia32 > Makefile.CurrentDefault
	@echo TYPE=pc >> Makefile.CurrentDefault
	@echo TARGET=floppy >> Makefile.CurrentDefault

settarget:
	@echo ARCH=$(ARCH) > Makefile.CurrentDefault
	@echo TYPE=$(TYPE) >> Makefile.CurrentDefault
	@echo TARGET=$(TARGET) >> Makefile.CurrentDefault
	@echo Target set to: $(ARCH)-$(TYPE)/$(TARGET)

$(TARGETDIR): Makefile Makefile.CurrentDefault configs/Makefile.$(ARCH)-$(TYPE)
	@echo [mkdir] $(BUILDROOT)
	@mkdir -p $(DIRECTORIES)
	@touch $@

# cleanup
clean:
	-rm -r -f $(BUILDROOT)

# this should be more intelligent
mostlyclean:
	@cd $(BUILDROOT)
	-rm -r *.o
	-rm -r *.c
	-rm -r *.bin
	-rm -r *.boot
	-rm -r *.elf
	-rm -r *.mos
	@cd ..

$(TESTDIRFILE): Makefile configs/Makefile.host
	@echo [mkdir] $(TESTDIR)
	@mkdir -p $(TESTDIR)
	@touch $(TESTDIRFILE)

test: $(TESTDEPS)
	@echo [done]

todo: $(TESTDIRFILE)
	-@find -iname '*.c' | xargs grep -i -n 'todo:' | tee $(TESTDIR)/todo.txt
	-@find -iname '*.bas' | xargs grep -i -n 'todo:' | tee -a $(TESTDIR)/todo.txt
	-@find -iname '*.asm' | xargs grep -i -n 'todo:' | tee -a $(TESTDIR)/todo.txt
	-@find -iname '*.h' | xargs grep -i -n 'todo:' | tee -a $(TESTDIR)/todo.txt
	-@find -iname '*.bi' | xargs grep -i -n 'todo:' | tee -a $(TESTDIR)/todo.txt
#	-@find -iname '*.s' | xargs grep -i -n 'todo:' | tee -a $(TESTDIR)/todo.txt

fixme: $(TESTDIRFILE)
	-@find -iname '*.c' | xargs grep -i -n 'fixme:' | tee $(TESTDIR)/fixme.txt
	-@find -iname '*.bas' | xargs grep -i -n 'fixme:' | tee -a $(TESTDIR)/fixme.txt
	-@find -iname '*.asm' | xargs grep -i -n 'fixme:' | tee -a $(TESTDIR)/fixme.txt
	-@find -iname '*.h' | xargs grep -i -n 'fixme:' | tee -a $(TESTDIR)/fixme.txt
	-@find -iname '*.bi' | xargs grep -i -n 'fixme:' | tee -a $(TESTDIR)/fixme.txt
#	-@find -iname '*.s' | xargs grep -i -n 'fixme:' | tee -a $(TESTDIR)/fixme.txt
