################################################################################
#
# meson.cygclass - functions for building Meson-based packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/meson.cygclass
#  SYNOPSIS
#  inherit meson
#  DESCRIPTION
#  Meson is a general-purpose build system written in Python being adopted as
#  a replacement for autotools by the GNOME project. The build is defined
#  by 'meson.build' files in the top source directory and in subdirectories.
#  Meson runs configuration tests, then generates the files needed for ninja
#  to perform the actual build.  In this sense, meson replaces autoconf and
#  configure, and ninja replaces automake and make.
#  INHERITS
#  ninja.cygclass
#  REQUIRES
#  meson
#****

inherit ninja

#****C* meson.cygclass/cygmeson
#  SYNOPSIS
#  cygmeson [OPTIONS]
#  DESCRIPTION
#  Runs meson to configure the package.  cygmeson passes meson the flags
#  necessary to install the package into the standard prefix and according to the
#  |html <a href="https://www.pathname.com/fhs/">Filesystem Hierarchy Standard</a> and the
#  |html <a href="https://cygwin.com/packaging-package-files.html#package_contents">Cygwin package guidelines</a>.
#  In addition, all arguments to cygmeson are passed to meson, followed by
#  CYGMESON_ARGS, if set.
#  NOTE
#  cygmeson should be run in the directory containing the top-level meson.build.
#****
cygmeson() {
	local crossargs mdir meson_version

	if [ ! -e meson.build ]
	then
		error "cygmeson: meson directory not found";
	elif [ -e ../meson.build ]
	then
		error "cygmeson: must be run from the top-level directory";
	fi

	if cross_compiling
	then
		if [ -f /usr/share/meson/cross/${CHOST} ]
		then
			crossargs="--cross-file=/usr/share/meson/cross/${CHOST}"
		else
			error "meson.cygclass: no cross-file for ${CHOST}"
		fi
	fi

	check_prog_req meson

	meson_version=$(meson --version)
	if ! __version_at_least 0.47 ${meson_version}
	then
	    error "meson.cygclass: minimum supported meson version is 0.47"
	fi

#****v* cygmeson/CYGMESON_ARGS
#  DESCRIPTION
#  Additional flags to pass to meson, as a string.
#  NOTE
#  Flags in CYGMESON_ARGS follow, and therefore override, flags passed by
#  default to cygmeson.
#****
	PYTHONUNBUFFERED=1 meson setup \
		--prefix=$(__host_prefix) \
		--localstatedir=$(__host_localstatedir) \
		--sysconfdir=$(__host_sysconfdir) \
		--reconfigure \
		--buildtype=plain \
		--wrap-mode=nodownload \
		--auto-features=enabled \
		${crossargs} ${CYGMESON_ARGS} "${@}" _build.${CHOST} \
		|| error "meson failed"
}

#****C* meson.cygclass/meson_compile
#  SYNOPSIS
#  meson_compile [OPTIONS]
#  DESCRIPTION
#  Runs meson to configure the package, followed by ninja to compile it.  Any
#  options are passed to meson as option flags.
#  NOTE
#  meson_compile should be run in the directory containing the top-level meson.build.
#****
meson_compile() {
	cygmeson ${@}
	cygninja -C _build.${CHOST}
}

#****T* meson.cygclass/meson_test
#  SYNOPSIS
#  meson_test
#  DESCRIPTION
#  Calls ninja test to run the package's testsuite.
#  NOTE
#  meson_test should be run in the directory containing the top-level meson.build.
#****

#****v* cygmeson/CYGMESON_TEST_ARGS
#  DESCRIPTION
#  Additional flags to pass to meson test, as a string.
#  DEFINITION
CYGMESON_TEST_ARGS=${CYGMESON_TEST_ARGS:-"--timeout-multiplier=2"}
#  EXAMPLE
#    CYGMESON_TEST_ARGS="--timeout-multiplier=10"
#****
meson_test() {
	meson test -C _build.${CHOST} ${CYGMESON_TEST_ARGS}
}

#****I* meson.cygclass/meson_install
#  SYNOPSIS
#  meson_install
#  DESCRIPTION
#  Runs ninja install to install the package into D.
#  NOTE
#  meson_install should be run in the directory containing the top-level meson.build.
#****
meson_install() {
	ninja_install -C _build.${CHOST}
}

#****o* meson.cygclass/src_compile (meson)
#  DEFINITION
src_compile() {
	cd ${S}
	meson_compile
}
#****

#****o* meson.cygclass/src_test (meson)
#  DEFINITION
src_test() {
	cd ${S}
	meson_test
}
#****

#****o* meson.cygclass/src_install (meson)
#  DEFINITION
src_install() {
	cd ${S}
	meson_install
}
#****

readonly -f cygmeson meson_compile meson_test meson_install
