#***********************************************************************
#
#  Copyright (c) 2007  Broadcom Corporation
#  All Rights Reserved
#
#***********************************************************************/

# GNUMAKEFLAGS := --output-sync=target

all: autobuild_dirs

CURR_DIR := $(shell pwd)
BUILD_DIR:=$(subst /userspace, /userspace,$(CURR_DIR))
BUILD_DIR:=$(word 1, $(BUILD_DIR))
export BUILD_DIR
include $(BUILD_DIR)/make.common
# export INSTALL from make.common to implicitly affect all Makefiles/configures below
export INSTALL

ifeq ($(wildcard ../targets/config.in),)
CONSUMER_BUILD=1
endif


MODSW_GLOBAL=1

# Check to see if any autodetect files are newer than the autogenerated makefile
DUMMY := $(shell if [ `ls -tr */*/*/autodetect makefile.modsw.autogen 2>/dev/null| tail -1` != "makefile.modsw.autogen" ] ; then \
	   rm -f makefile.modsw.autogen makefile.modsw.autogen.mini; \
	fi )

include $(BUILD_DIR)/make.modsw


#
# The build sequence of userspace is:
# public-include, circular_dep_check, autobuild_dirs
# circular_dep_check will also fail if it detects non-existent targets, see
# ouptput.txt for full output.
#
public-include: $(BUILD_DIR)/cms_dmp_flags.h
	mkdir -p $(BCM_FSBUILD_DIR)/public/include
	mkdir -p $(BCM_FSBUILD_DIR)/public/lib
	$(MAKE) -C public/include

CIRC.MAKE   := makefile.circ
CIRC.OUTPUT := circ_output.txt

circular_dep_check: public-include
	@echo "include makefile.modsw.autogen.mini" > $(CIRC.MAKE)
	@echo "" >> $(CIRC.MAKE)
	@echo '$$(AUTODIRS) :' >> $(CIRC.MAKE)
	@echo "" >> $(CIRC.MAKE)
	@echo 'circ_check: $$(AUTODIRS)' >> $(CIRC.MAKE)
	make -f $(CIRC.MAKE) circ_check > $(CIRC.OUTPUT) 2>&1
	@if grep Circular $(CIRC.OUTPUT) ; then \
		echo "ERROR!!  Circular dependency detected!  Abort build."; \
		exit 1; \
	fi

$(AUTODIRS) : circular_dep_check

autobuild_dirs: $(AUTODIRS)

.PHONY: public-include circular_dep_check autobuild_dirs


# Clean related targets
# Do special cleaning for multiarch autodirs first, then regular autodirs
AUTODIRS_CLEAN := $(addprefix _clean_,$(wildcard $(AUTODIRS)))
MULTIARCH_AUTODIRS_CLEAN := $(addprefix _multiarchclean_,$(wildcard $(MULTIARCH_AUTODIRS)))

$(MULTIARCH_AUTODIRS_CLEAN):
	-$(MAKE) $(patsubst _multiarchclean_%,%,$@/multiarchclean)

multiarch_autodirs_clean: $(MULTIARCH_AUTODIRS_CLEAN)

$(AUTODIRS_CLEAN) : multiarch_autodirs_clean
	-$(MAKE) $(if $(wildcard $(patsubst _clean_%,%/Bcmbuild.mk,$@)),-f Bcmbuild.mk) -C $(patsubst _clean_%,%,$@) clean

clean: $(AUTODIRS_CLEAN)
	@rm -f $(CIRC.MAKE) $(CIRC.OUTPUT)
	@rm -f makefile.modsw.autogen makefile.modsw.autogen.mini
	@rm -f $(BUILD_DIR)/cms_dmp_flags.h
	@$(MAKE) -C public/include clean

.PHONY: $(AUTODIRS_CLEAN)
.PHONY: $(MULTIARCH_AUTODIRS_CLEAN)
