SUMMARY="Foreign Function Interface for Python calling C code"
DESCRIPTION="CFFI is a Python module which allows to call C code."
HOMEPAGE="https://cffi.readthedocs.io/
	https://pypi.org/project/cffi/"
COPYRIGHT="2012-2018 Armin Rigo, Maciej Fijalkowski"
LICENSE="MIT"
REVISION="3"
SOURCE_URI="https://files.pythonhosted.org/packages/source/c/cffi/cffi-$portVersion.tar.gz"
CHECKSUM_SHA256="599a1e8ff057ac530c9ad1778293c665cb81a791421f46922d80a86473c13346"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	cffi$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libffi$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:pkg_config$secondaryArchSuffix
	"

PYTHON_PACKAGES=()
PYTHON_VERSIONS=()
PYTHON_LIBSUFFIXES=()
# We don't have python2 for secondaryArch
if [ -z "$secondaryArchSuffix" ]; then
	PYTHON_PACKAGES+=(python)
	PYTHON_VERSIONS+=(2.7)
	PYTHON_LIBSUFFIXES+=("")
	BUILD_REQUIRES+="
		setuptools_python
		pycparser_python
		"
	BUILD_PREREQUIRES+="
		cmd:python2
		"
fi
# gcc2 does not support the flags passed by python3
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	PYTHON_PACKAGES+=(python3)
	PYTHON_VERSIONS+=(3.7)
	PYTHON_LIBSUFFIXES+=(m)
	BUILD_REQUIRES+="
		setuptools_python3
		pycparser_python3
		"
	BUILD_PREREQUIRES+="
		cmd:python3
		"
fi
for i in "${!PYTHON_PACKAGES[@]}"; do
	pythonPackage=${PYTHON_PACKAGES[i]}
	pythonVersion=${PYTHON_VERSIONS[$i]}
	pythonLibSuffix=${PYTHON_LIBSUFFIXES[$i]}

	eval "PROVIDES_$pythonPackage=\"
		${portName}_$pythonPackage = $portVersion
		\""
	eval "REQUIRES_$pythonPackage=\"
		haiku$secondaryArchSuffix
		pycparser_$pythonPackage
		lib:libffi$secondaryArchSuffix
		lib:libpython$pythonVersion$pythonLibSuffix$secondaryArchSuffix
		\""

	TEST_REQUIRES+="
		${portName}_$pythonPackage
		py_$pythonPackage
		pycparser_$pythonPackage
		pytest_$pythonPackage
		"
done
if [ "$targetArchitecture" = x86_gcc2 -a -n "$secondaryArchSuffix" ]; then
	PROVIDES_python3+="
		cffi_python3 = $portVersion
		"
fi

BUILD()
{
	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		rm -rf "$sourceDir"-$pythonPackage
		cp -a "$sourceDir" "$sourceDir"-$pythonPackage
		cd "$sourceDir"-$pythonPackage

		python=python$pythonVersion
		$python setup.py build
	done
}

INSTALL()
{
	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		cd "$sourceDir"-$pythonPackage

		python=python$pythonVersion
		installLocation=$prefix/lib/$python/vendor-packages
		export PYTHONPATH=$installLocation
		mkdir -p "$installLocation"
		$python setup.py install \
			--optimize=1 \
			--root=/ --prefix="$prefix"

		packageEntries $pythonPackage \
			"$prefix"/lib/$python
	done

	cd "$sourceDir"
	install -m 755 -d "$docDir"
	install -m 644 -t "$docDir" README.md
}

TEST()
{
	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		cd "$sourceDir"-$pythonPackage

		python=python$pythonVersion
		echo import cffi | $python
		$python setup.py test
		cd testing
		$python support.py
	done
}
