# SPDX-License-Identifier: GPL-2.0-or-later
# Autotools include Makefile.
# Copyright (c) Linux Test Project, 2010-2024
# Copyright (C) 2009, Cisco Systems Inc.
# Ngie Cooper, July 2009

# Override these variables to use non-system available tools.
ACLOCAL		?= aclocal
AUTOCONF	?= autoconf
AUTOHEADER	?= autoheader
AUTOMAKE	?= automake

AUTOCONFED_SUBDIRS	= \
			testcases/open_posix_testsuite

CLEAN_SUBDIRS = $(AUTOCONFED_SUBDIRS) include

# We want to run this every single time to ensure that all of the prereq files
# are there.
.PHONY: testcases/open_posix_testsuite/configure
testcases/open_posix_testsuite/configure:
	$(MAKE) -C $(@D) autotools

.PHONY: autotools
autotools: aclocal autoconf autoheader automake $(addsuffix /configure,$(AUTOCONFED_SUBDIRS))

.PHONY: aclocal
aclocal: aclocal.m4

aclocal.m4: $(wildcard m4/*.m4) m4/ltp-version.m4
	$(ACLOCAL) -I m4

.PHONY: autoconf
autoconf: configure

configure: configure.ac aclocal.m4
	$(AUTOCONF)

.PHONY: autoheader
autoheader: configure.ac $(wildcard m4/*.m4) m4/ltp-version.m4 aclocal.m4
	$(AUTOHEADER)

include:
	mkdir -p "$@"

m4/ltp-version.m4: VERSION
	sed -n '1{s:LTP-:m4_define([LTP_VERSION],[:;s:$$:]):;p;q}' $< > $@

.PHONY: automake
AUTOMAKE_FILES := compile config.guess config.sub install-sh missing
automake: aclocal $(AUTOMAKE_FILES)
$(AUTOMAKE_FILES): m4/Makefile.in
m4/Makefile.in: m4/Makefile.am aclocal.m4
	$(AUTOMAKE) -c -a

.PHONY: ac-clean ac-distclean ac-maintainer-clean
ac-clean::
	$(RM) -rf autom4te.cache
	$(RM) -f config.log config.status
	$(RM) -f m4/Makefile m4/ltp-version.m4
	for d in $(CLEAN_SUBDIRS); do \
	    $(MAKE) -C "$(top_srcdir)/$$d" $@; \
	done

ac-distclean:: ac-clean
ac-maintainer-clean:: ac-distclean
	for d in $(CLEAN_SUBDIRS); do \
	    $(MAKE) -C "$(top_srcdir)/$$d" $@; \
	done
	$(RM) -f aclocal.m4 configure $(AUTOMAKE_FILES) m4/Makefile.in

# Don't include config.h, or make will (rightfully) whine about overriding
# rules.
#
# This list should match the files in configure.ac.
#
AUTOGENERATED_FILES	= \
			include/mk/config.mk	\
			include/mk/config-openposix.mk \
			include/mk/features.mk	\
			lib/ltp.pc		\
			m4/Makefile

distclean:: %: clean ac-distclean
	for d in $(CLEAN_SUBDIRS); do \
	    $(MAKE) -C "$(top_srcdir)/$$d" $@; \
	done
	$(RM) -f $(AUTOGENERATED_FILES)

maintainer-clean:: distclean ac-maintainer-clean

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

# This variable is automatically changed from help to all once someone has
# run configure, or the equivalent steps manually, as described in INSTALL.
$(abs_top_builddir)/include/mk/config.mk \
$(abs_top_builddir)/include/mk/features.mk:
	$(MAKE) -C $(top_srcdir) help; false