#
# Makefile for root file system
#
# (Corner DE core file)

export TOPDIR := $(shell cd ..; pwd)
include makefile.envars
include makefile.config

#

.PHONY: all rootfs main

all rootfs: main


ifdef FULL_COPY
include makefile.corner
else
chkrev:
endif

# function macros

GET_COMPO = $(word 2,$(subst ., ,$(subst /, ,$(1))))

#

TARGET_LIST := init_root init_src mk_compos glue

.PHONY: $(TARGET_LIST) 

main: $(TARGET_LIST) chkrev
	@echo "+------------------------------+"
	@echo "| \$$LOG_COLLECTOR=${LOG_COLLECTOR} \$$RD_LEVEL=${RD_LEVEL} |"
	@echo "+------------------------------+"

init_root: INSTALL.init_root

# increase this number if RDS00047_LAYOUT is changed
RDS00047_LAYOUT_VER := 2

RDS00047_LAYOUT :=
RDS00047_LAYOUT += $(COMPO_BASE)/include
RDS00047_LAYOUT += $(COMPO_BASE)/$(TARGET)-lib
RDS00047_LAYOUT += $(COMPO_BASE)/amit_priv/include
RDS00047_LAYOUT += $(COMPO_BASE)/amit_priv/$(TARGET)-lib

init_src: $(COMPO_BASE)/.tag.prepared.$(RDS00047_LAYOUT_VER)

$(COMPO_BASE)/.tag.prepared.$(RDS00047_LAYOUT_VER):
ifdef FULL_COPY
	@mkdir -p $(RDS00047_LAYOUT)
	@rm -f $(COMPO_BASE)/.tag.prepared*
	@touch $@
endif

#

.PHONY: $(COMPO_LIST)

mk_compos: $(COMPO_LIST)

$(COMPO_LIST):
	@if test "$(EXCLUDE.$(call GET_COMPO,$@))" == "y"; then \
		echo "!!!---->>> $@ <<<--- is excluded"; \
	elif test -f $@/Makefile; then \
		echo "*** Making $@..."; \
		make -C $@ all-but-not-chkrev; \
	else \
		echo "!!!---->>> $@ <<<--- may be removed"; \
	fi

glue:
	make -C $@ all
	@if test -x ~/bin/cde-hook/post-rootfs; then \
		~/bin/cde-hook/post-rootfs $(TOPDIR); \
	fi

#

CLEAN_LIST := $(patsubst %,clean-%,$(COMPO_LIST))

.PHONY: clean $(CLEAN_LIST)

clean: $(CLEAN_LIST)
ifdef FULL_COPY
	@rm -fr $(RDS00047_LAYOUT) # to make sure we have a clean start
	@rm -f $(COMPO_BASE)/.tag.prepared*
endif

$(CLEAN_LIST):
	@if test "$(EXCLUDE.$(call GET_COMPO,$@))" == "y"; then \
		echo "!!!------>>> $(patsubst clean-%,%,$@) <<<--- is excluded"; \
	elif test -f $(patsubst clean-%,%,$@)/Makefile; then \
		echo "*** Cleaning $(patsubst clean-%,%,$@)..."; \
		make -C $(patsubst clean-%,%,$@) check-then-clean; \
	else \
		echo "!!!------>>> $(patsubst clean-%,%,$@) <<<--- may be removed"; \
	fi

#

.PHONY: boot

boot:
	make -C glue boot

#

.PHONY: ui

ui:
	make -C glue ui

#

.PHONY: distclean

distclean:
	rm -fr $(INSTALL_ROOT)
	#rm -fr $(COMPO_BASE)
	rm -rf squashfs.o
	rm -rf root.tmp/

# must be located behind 'all' target. for safety, let it be at bottom

include makefile.install

