# @configure_input@
# $Id$
#
# Makefile template for MinGW.org composite System Libraries Packages

PACKAGE_TARNAME := @PACKAGE_TARNAME@
PACKAGE_VERSION := @PACKAGE_VERSION@

# Written by Keith Marshall <keithmarshall@users.sourceforge.net>
# Copyright (C) 2016, 2019, MinGW.org Project
#
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.


# The default target is "all"; declare it early, to avoid any possible
# inadvertent override from any included file.
#
all:

# Offer users the opportunity to assign boolean build-time options, from
# the command line, in the form 'option=NAME', or 'options="NAME ..."'
#
$(foreach opt,$(option) $(options),$(eval $(opt) = true))


# Configuration
# -------------
#
srcdir = @srcdir@
top_srcdir = @top_srcdir@

# Keep the configuration in a consistent state.  Note that we assume
# that configure has been run initially, (otherwise we wouldn't have
# a Makefile to begin with); since this also creates config.status,
# we may normally assume that it is already available ...
#
vpath configure ${top_srcdir}
config.status: configure
	$(SHELL) config.status --recheck

# ... in which case, updating Makefile should be a simple matter of
# running config.status ...
#
vpath Makefile.in ${top_srcdir}
requires_existing = $(if $(wildcard $1),$1,$1.missing)
Makefile: Makefile.in configure $(call requires_existing,config.status)
	$(SHELL) config.status

.PHONY: config.status.missing
# ... but, in the event that this may be missing, (e.g. because it
# has been manually removed, or removed by "make distclean"), suggest
# running configure, and bail out.
#
config.status.missing:
	$(warning *** cannot execute config.status)
	$(error please run ${top_srcdir}/configure to regenerate it.)

# If configure itself needs to be updated, we must run autoconf in the
# top level source directory.
#
vpath %.m4 ${top_srcdir}
vpath configure.ac ${top_srcdir}
configure: configure.ac aclocal.m4 VERSION.m4
	cd ${top_srcdir}; autoconf


# Rules to Build and Install Both Sub-Packages
# --------------------------------------------
#
all install install-headers install-strip uninstall: %: %-subdirs

subdirs = @subdirs@
%-subdirs:; $(foreach dir,${subdirs},$(MAKE) -C ${dir} $*;)


# Test Suite
# ----------
#
.PHONY: check-recursive
check test tests: check-recursive
check-recursive: %: %-subdirs


# Distribution
# ------------
#
MKDIR_P = @MKDIR_P@
LN = @LN@

dist-prepare:
	$(RM) -r $@; $(MKDIR_P) $@

dist: dist-prepare dist-subdirs dist-extra
	for file in $(addsuffix /$@/*,${subdirs}); do \
	  test -f $$file && $(LN) $$file $@; done

LUA = @LUA@
LUACMD = $(if $(LUA),$(LUA) -e,false)

WSL_TMPDIR = $(CURDIR)/dist/var/lib/wsl
FEATURES_CFG = wsl-features-$(TODAY)-$(ISSUE)-mingw32-cfg.tar.xz

TODAY = $(shell date +%Y%m%d)
ISSUE = $(if $(wildcard $@),$(shell awk '\
  { if( $$1 == $(TODAY) ) print $$2 + 1; else print 1; exit; \
  }' $@),1)

dist-extra: $(if $(FEATURES),features.stamp)
features.unsupported:
	$(warning Lua does not appear to be installed on your system;)
	$(warning it is required to support the FEATURES distribution option.)

vpath features.lua ${srcdir}/var
features.stamp: features.$(if $(LUA),lua,unsupported)
	$(RM) -r dist/var dist/include
	$(MKDIR_P) dist/include $(WSL_TMPDIR)/include && $(LN) $^ $(WSL_TMPDIR)
	MINGW32_SYSROOT=$(WSL_TMPDIR) $(LUACMD) '$(FEATURES_H_INITIALIZE)'
	cd dist && tar cf - var include | xz -c > $(FEATURES_CFG)
	echo $(TODAY) $(ISSUE) > $@
	$(RM) -r dist/var dist/include

FEATURES_H_INITIALIZE = \
  sysroot = os.getenv( "MINGW32_SYSROOT" ) \
  if sysroot then \
     package.path = sysroot.."/?.lua;"..package.path \
     have_features, config = pcall( require, "features" ) \
     if have_features then \
	config.initialize( io.open( config.pathname(), "w" )) \
     end \
  end

# Clean-up Rules
# --------------
#
clean mostlyclean distclean: %: %-subdirs
maintainer-clean: maintainer-clean-warning maintainer-clean-local

maintainer-clean-warning:
	$(warning $(MAKE) $(@:%-warning=%))
	$(warning This command should be used by package maintainers only;)
	$(warning it deletes files which may require special tools to rebuild.)

maintainer-clean-local: %: %-subdirs
	$(RM) -r ${srcdir}/autom4te.cache

# $RCSfile$: end of file
