# -*- mode: makefile -*-
# NOTE: Also includes bsd.port.pre.mk

PKGNAMEPREFIX=	${FLAVOR}-
DISTFILES=

.if !defined(LICENSE)
LICENSE=	MIT PSFL
LICENSE_COMB=	multi
.endif

PYPY_DEFAULT_DISTVERSION?=	7.3.14

BUILD_DEPENDS+=	${PYPY_PYTHON_IMPL}:lang/${FLAVOR}
RUN_DEPENDS+=	${PYPY_PYTHON_IMPL}:lang/${FLAVOR}

CFFI_MODULE?=	_${PORTNAME}

FLAVORS=	pypy39 pypy310
FLAVOR?=	pypy310

.if ${FLAVOR}==pypy39
PYPY_PYTHON_IMPL=	pypy3.9
.elif ${FLAVOR}==pypy310
PYPY_PYTHON_IMPL=	pypy3.10
.else
.endif
PYPY_PORTVERSION=	${DISTVERSION}
PYPY_CMD=	${LOCALBASE}/bin/${PYPY_PYTHON_IMPL}

PYPY_DIR=	pypy3-${DISTVERSION:C|([0-9]+)\.([0-9]+).*|\1.\2|}
PYPY_CFFI_VER=	${FLAVOR}-pp${DISTVERSION:S|.||:C|\..*||}-${OPSYS:tl}${OSREL:C|\..*||}

PLIST_FILES+=	${PYPY_DIR}/lib/${PYPY_PYTHON_IMPL}/${CFFI_MODULE}_cffi.${PYPY_CFFI_VER}.so

.include <bsd.port.pre.mk>

do-build:
	${CP} ${LOCALBASE}/${PYPY_DIR}/lib/${PYPY_PYTHON_IMPL}/${CFFI_MODULE}_build.py ${WRKDIR}/${PORTNAME}.py
	(cd ${WRKDIR}; \
		${PYPY_CMD} -c "from ${PORTNAME} import ${FFI_MODULE_NAME} as ffi; ffi.compile('${WRKDIR}');")

do-install:
	${MKDIR} ${STAGEDIR}${PREFIX}/${PYPY_DIR}/lib/${PYPY_PYTHON_IMPL}/`dirname ${CFFI_MODULE}`/
	${INSTALL_LIB} ${WRKDIR}/${CFFI_MODULE}_cffi.${PYPY_CFFI_VER}.so ${STAGEDIR}${PREFIX}/${PYPY_DIR}/lib/${PYPY_PYTHON_IMPL}/`dirname ${CFFI_MODULE}`/
