# Get SDE version number information
-include sdeversion

# Some (unsupported) directories have known build failures.
BROKENDIRS := BDMR4102 BDMR4102L METEOR METEORL MIDAS NITRO NITROL TINYSIM

SBDDIRS := $(filter-out SKEL NOSBD $(BROKENDIRS),$(subst /Makefile,,$(wildcard */Makefile)))

NOTFREE := $(subst /.notfree,,$(wildcard */.notfree))

EXCLUDED := $(subst /.exclude,,$(wildcard */.exclude))

ifneq (,$(strip $(filter 6,$(SDE_MAJOR))))
EXCLUDED += $(subst /.exclude-sde6_1_0,,$(wildcard */.exclude-sde6_1_0))
endif

INCLUDED := $(filter-out $(EXCLUDED),$(SBDDIRS))

FREE := $(filter-out $(NOTFREE),$(INCLUDED))

.PHONY: all all-free all-included

all: $(SBDDIRS)

all-free: $(FREE)

all-included: $(INCLUDED)

.PHONY: $(SBDDIRS)

$(SBDDIRS):
	@echo "==== $@ ===="
	@$(MAKE) -C $@ SBD=$@ $(TARGET)

TARGETS = kitall clean clobber reallyclean
TESTS	= test kit-test rtpx-test net-test
.PHONY: $(TARGETS) $(TESTS)

$(TARGETS):
	@$(MAKE) TARGET=$@

# Tests must be built sequentially, not simultaneously in parallel make
$(TESTS):
	@for tgt in $(INCLUDED); do \
	  echo "==== $$tgt ===="; \
	  $(MAKE) -C $$tgt SBD=$$tgt $@; \
	done

.PHONY: test-free
test-free:
	@for tgt in $(FREE); do \
	  echo "==== $$tgt ===="; \
	  $(MAKE) -C $$tgt SBD=$$tgt test; \
	done

.PHONY: test-include
test-included:
	@for tgt in $(INCLUDED); do \
	  echo "==== $$tgt ===="; \
	  $(MAKE) -C $$tgt SBD=$$tgt test \
	done

# Obsolete dummy depend target, this is now handled automagically.
.PHONY: depend
depend:

list:
	@echo "SBDDIRS = $(SBDDIRS)"
	@echo ""
	@echo "EXCLUDED = $(EXCLUDED)"
	@echo ""
	@echo "INCLUDED = $(INCLUDED)"
	@echo ""
	@echo "NOTFREE = $(NOTFREE)"
	@echo ""
	@echo "FREE = $(FREE)"

.PHONY: free
free:
	@$(MAKE) TARGET="clean" all-free
	@$(MAKE) TARGET="free FREEKIT=-DFREE_SDE LOCALDIRS=yes FREEBUILD=yes" all-free

# Only run this in the distribution directory
.PHONY: dist
dist: 
	@$(MAKE) free
