# Default target
.DEFAULT_GOAL = all

include ../tools/common.mk

# All user apps (in subdirectory `app*`)
APPS = $(filter app%/, $(wildcard */))

# OOStuBSmI:
#IMGBUILDER = $(BUILDDIR)/imgbuilder
#INITRD = $(BUILDDIR)/initrd.img

# MPStuBSmI:
INITRD = $(BUILDDIR)/initrd.tar

all: $(INITRD)

# recipe for user apps (their Makefile should build a binary named `app.flat`
# or `app.elf` in its $(BUILDDIR) subdirectory, e.g. `app1/.build/app`)
$(APPS):
	$(VERBOSE) $(MAKE) -C $@

# OOStuBSmI: recipe for compiling imgbuilder
#$(IMGBUILDER): imgbuilder.cc $(MAKEFILE_LIST)
#	@echo "CC		$@"
#	@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
#	$(VERBOSE) $(CXX) -std=c++14 -o $@ $<

# recipe for building the final OOStuBSmI boot module image
#$(INITRD): $(APPS) $(IMGBUILDER)
#	@echo "IMGBUILD	$@"
#	@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
#	$(VERBOSE) $(IMGBUILDER) $(addsuffix $(BUILDDIR)/app.flat, $(APPS)) > $@


# recipe for building the final MPStuBSmI boot module image
$(INITRD): $(APPS)
	@echo "TAR	$@"
	$(VERBOSE) mkdir -p $(BUILDDIR)
	$(VERBOSE) tar -cf $@ $(addsuffix $(BUILDDIR)/app.elf, $(APPS))

# We still support recipes `lint` and `tidy`
lint::
	$(VERBOSE) $(foreach APP,$(APPS),$(MAKE) -C $(APP) lint && ) true

tidy::
	$(VERBOSE) $(foreach APP,$(APPS),$(MAKE) -C $(APP) tidy && ) true

# clean all apps additionally to default clean recipe from common.mk
clean::
	$(VERBOSE) $(foreach APP,$(APPS),$(MAKE) -C $(APP) clean && ) true

.PHONY: all lint tidy clean $(APPS)
