# SPDX-License-Identifier: GPL-2.0-or-later
# Ngie Cooper, June 2010

# Makefiles that are considered critical to execution; if they don't exist
# all of the Makefiles will be rebuilt by default.
CRITICAL_MAKEFILE=	conformance/interfaces/timer_settime/Makefile

top_srcdir?=		.

include include/mk/env.mk

# The default logfile for the tests.
LOGFILE?=		logfile
# Subdirectories to traverse down.
SUBDIRS=		conformance functional stress

MAKE_ENV=		LOGFILE=`if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@

BUILD_MAKE_ENV=		"CFLAGS=$(CFLAGS)" "LDFLAGS=$(LDFLAGS)"
BUILD_MAKE_ENV+=	"LDLIBS=$(LDLIBS)" $(MAKE_ENV)

TEST_MAKE_ENV=		$(MAKE_ENV)

BUILD_MAKE=		env $(BUILD_MAKE_ENV) $(MAKE)

TEST_MAKE=		env $(TEST_MAKE_ENV) $(MAKE) -k

all: conformance-all functional-all stress-all tools-all

AUTOGENERATED_FILES = include/mk/config.mk

.PHONY: ac-clean
ac-clean: clean

.PHONY: clean
clean:
	$(RM) -f $(LOGFILE)*
	$(RM) -f config.log config.status
	@for dir in $(SUBDIRS) tools; do \
		$(MAKE) -C $$dir clean >/dev/null; \
	done

.PHONY: distclean
distclean: clean distclean-makefiles
	@rm -f $(AUTOGENERATED_FILES)

# Clean out all of the generated Makefiles.
.PHONY: distclean-makefiles
distclean-makefiles:
	@for dir in $(SUBDIRS); do \
		$(MAKE) -C $$dir $@; \
	done

$(AUTOGENERATED_FILES): $(top_builddir)/config.status
	$(SHELL) $^

.PHONY: autotools
autotools: configure

configure: configure.ac
	autoconf

.PHONY: generate-makefiles
generate-makefiles: distclean-makefiles
	@env top_srcdir=$(top_srcdir) \
		$(top_srcdir)/scripts/generate-makefiles.sh

.PHONY: install
install: bin-install conformance-install functional-install stress-install

.PHONY: test
test: conformance-test functional-test stress-test

# Test build and execution targets.
.PHONY: conformance-all conformance-install conformance-test
conformance-all: $(CRITICAL_MAKEFILE)
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(BUILD_MAKE) -C conformance -j1 all

conformance-install:
	@$(MAKE) -C conformance install

conformance-test:
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(TEST_MAKE) -C conformance test

.PHONY: functional-all functional-install functional-test
functional-all: $(CRITICAL_MAKEFILE)
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(BUILD_MAKE) -C functional -j1 all

functional-install:
	@$(MAKE) -C functional install

functional-test:
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(TEST_MAKE) -C functional test

.PHONY: stress-all stress-install stress-test
stress-all: $(CRITICAL_MAKEFILE)
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(BUILD_MAKE) -C stress -j1 all

stress-install:
	@$(MAKE) -C stress install

stress-test:
	@rm -f `if echo "$(LOGFILE)" | grep -q '^/'; then echo "$(LOGFILE)"; else echo "\`pwd\`/$(LOGFILE)"; fi`.$@
	@$(TEST_MAKE) -C stress test

# Tools build and install targets.
.PHONY: bin-install
bin-install:
	@$(MAKE) -C bin install

.PHONY: tools-all
tools-all:
	@$(MAKE) -C tools all

$(CRITICAL_MAKEFILE): $(top_srcdir)/scripts/generate-makefiles.sh
	@$(MAKE) generate-makefiles

.PHONY: check
check:
	@echo "Checker not yet supported by Open POSIX testsuite"
