################################################################################
#
# waf.cygclass - functions for building Waf-based packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# 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 <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/waf.cygclass
#  SYNOPSIS
#  inherit waf
#  DESCRIPTION
#  Waf is a general-purpose build system written in Python used by XMMS2, a
#  few GTK+ programs, and some other projects.  The build system is provided
#  by a bundled 'waf' script in the top source directory, and is driven
#  by a 'wscript' file in the top source directory and 'wscript_build' files
#  in subdirectories, both of which are also written in Python.
#  NOTE
#  Previous versions uses to allow use of a system installed waf.  This is no
#  longer supported, as waf does not have a stable API and is therefore not
#  feasibly used in this way.  Fortunately, by now most packages do include
#  a recent enough waf to build properly with at most only patches to 'wscript'
#  and/or 'wscript_build' files.
#  REQUIRES
#  python
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

#****d* waf.cygclass/WAF
#  DESCRIPTION
#  Path to the Waf command.
#****
WAF="./waf"


#****C* waf.cygclass/waf_compile
#  SYNOPSIS
#  waf_compile [OPTIONS]
#  DESCRIPTION
#  Runs 'waf configure' to configure the package, followed by 'waf build' to
#  compile it.  WAF_CONFIGURE_FLAGS and WAF_BUILD_FLAGS can be defined to pass
#  additional arguments to the configure and build commands, respectively.
#  Any arguments to waf_compile are passed as configure flags to the 'waf
#  configure' command.  waf_compile must be run from the directory containing
#  'wscript'.
#****
waf_compile() {
	if [ ! -e wscript ]
	then
		error "waf_compile: wscript: file not found"
	fi

#****v* waf_compile/WAF_CONFIGURE_FLAGS
#  DESCRIPTION
#  String containing additional arguments to pass to 'waf configure'.
#****
	${WAF} configure \
		--prefix=/usr \
		${WAF_CONFIGURE_FLAGS} ${@} \
		|| error "waf configure failed"

#****v* waf_compile/WAF_BUILD_FLAGS
#  DESCRIPTION
#  String containing additional arguments to pass to 'waf build'.
#****
	${WAF} build ${WAF_BUILD_FLAGS} || error "waf build failed"
}

#****T* waf.cygclass/waf_test
#  DESCRIPTION
#  Runs the package's test suite with 'waf check'.  WAF_TEST_FLAGS can be
#  defined to pass additional arguments to the check command.  Any arguments
#  to waf_test are passed as well. waf_test must be run from the directory
#  containing 'wscript'.
#****
waf_test() {
	if [ ! -e wscript ]
	then
		error "waf_install: wscript: file not found"
	fi

#****v* waf_test/WAF_TEST_FLAGS
#  DESCRIPTION
#  String containing additional arguments to pass to 'waf check'.
#****
	${WAF} check -k ${WAF_TEST_FLAGS} ${@} || true
}

#****I* waf.cygclass/waf_install
#  DESCRIPTION
#  Installs the package into $D with 'waf install'.  WAF_INSTALL_FLAGS can be
#  defined to pass additional arguments to the install command.  Any arguments
#  to waf_install are passed as well. waf_install must be run from the directory
#  containing 'wscript'.
#****
waf_install() {
	if [ ! -e wscript ]
	then
		error "waf_install: wscript: file not found"
	fi

#****v* waf_install/WAF_INSTALL_FLAGS
#  DESCRIPTION
#  String containing additional arguments to pass to 'waf install'.
#****
	${WAF} install --destdir=${D} ${WAF_INSTALL_FLAGS} ${@} || error "waf install failed"
}

#****o* waf.cygclass/src_compile (waf)
#  DEFINITION
src_compile() {
	lndirs
	cd ${B}
	waf_compile
}
#****

#****o* waf.cygclass/src_install (waf)
#  DEFINITION
src_install() {
	cd ${B}
	waf_install
}
#****

readonly -f waf_compile waf_test waf_install
