public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Arthur Zamarin <arthurzam@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: Matthew Smith <matthew@gentoo.org>,
	Nick Sarnie <sarnex@gentoo.org>,
	Arthur Zamarin <arthurzam@gentoo.org>
Subject: [gentoo-dev] [PATCH 2/2] tree-sitter-grammar.eclass: support opt in python bindings
Date: Wed, 20 Mar 2024 22:52:39 +0200	[thread overview]
Message-ID: <20240320205659.204635-3-arthurzam@gentoo.org> (raw)
In-Reply-To: <20240320205659.204635-1-arthurzam@gentoo.org>

New tree-sitter cli generated bindings and code around grammars and
parsers now support bulding a python wheel which supply much better
API and library for consumers in python bindings.

Currently I've added only python as a binding languages, even though
rust, swift, and go are also available. We should add them when we
see a request for them. Python will be needed for pkgcheck.

When we opt in into python bindings, we call the matching distutils
phase functions when `use python` is true.

Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
---
 eclass/tree-sitter-grammar.eclass | 85 ++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/eclass/tree-sitter-grammar.eclass b/eclass/tree-sitter-grammar.eclass
index 13539daf7e6..b04d5ee1103 100644
--- a/eclass/tree-sitter-grammar.eclass
+++ b/eclass/tree-sitter-grammar.eclass
@@ -36,6 +36,43 @@ RESTRICT+=" !test? ( test )"
 # Used to override upstream tag name if tagged differently, e.g. most releases
 # are v${PV} but some are tagged as rust-${PV}.
 
+# @ECLASS_VARIABLE: TS_BINDINGS
+# @PRE_INHERIT
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Array of bindings language to build. Currently only "python" is supported.
+
+for _BINDING in "${TS_BINDINGS[@]}"; do
+	case ${_BINDING} in
+		python)
+			DISTUTILS_EXT=1
+			DISTUTILS_OPTIONAL=1
+			DISTUTILS_USE_PEP517=setuptools
+			PYTHON_COMPAT=( python3_{10..12} )
+			inherit distutils-r1
+
+			IUSE+=" python"
+			REQUIRED_USE+=" python? ( ${PYTHON_REQUIRED_USE} )"
+
+			DEPEND+=" python? (
+				${PYTHON_DEPS}
+			)"
+			RDEPEND+=" python? (
+				${PYTHON_DEPS}
+				>=dev-python/tree-sitter-0.21.0[${PYTHON_USEDEP}]
+			)"
+			BDEPEND+=" python? (
+				${DISTUTILS_DEPS}
+				dev-python/wheel[${PYTHON_USEDEP}]
+			)"
+			;;
+		*)
+			die "Unknown binding: ${_BINDING}"
+			;;
+	esac
+done
+unset _BINDING
+
 # @FUNCTION: _get_tsg_abi_ver
 # @INTERNAL
 # @DESCRIPTION:
@@ -49,6 +86,34 @@ _get_tsg_abi_ver() {
 		die "Unable to extract ABI version for this grammar"
 }
 
+tree-sitter-grammar_src_prepare() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	default
+
+	local binding
+	for binding in "${TS_BINDINGS[@]}"; do
+		case ${binding} in
+			python)
+				use python && distutils-r1_src_prepare
+				;;
+		esac
+	done
+}
+
+tree-sitter-grammar_src_configure() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	local binding
+	for binding in "${TS_BINDINGS[@]}"; do
+		case ${binding} in
+			python)
+				use python && distutils-r1_src_configure
+				;;
+		esac
+	done
+}
+
 # @FUNCTION: _tree-sitter-grammar_legacy_compile
 # @INTERNAL
 # @DESCRIPTION:
@@ -102,6 +167,15 @@ tree-sitter-grammar_src_compile() {
 	else
 		_tree-sitter-grammar_legacy_compile
 	fi
+
+	local binding
+	for binding in "${TS_BINDINGS[@]}"; do
+		case ${binding} in
+			python)
+				use python && distutils-r1_src_compile
+				;;
+		esac
+	done
 }
 
 # @FUNCTION: tree-sitter-grammar_src_test
@@ -131,8 +205,17 @@ tree-sitter-grammar_src_install() {
 		dolib.so "${WORKDIR}/${soname}"
 		dosym "${soname}" /usr/$(get_libdir)/lib${PN}$(get_libname)
 	fi
+
+	local binding
+	for binding in "${TS_BINDINGS[@]}"; do
+		case ${binding} in
+			python)
+				use python && distutils-r1_src_install
+				;;
+		esac
+	done
 }
 
 fi
 
-EXPORT_FUNCTIONS src_compile src_test src_install
+EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
-- 
2.44.0



      parent reply	other threads:[~2024-03-20 20:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-20 20:52 [gentoo-dev] tree-sitter-grammar.eclass: support new upstream bindings Arthur Zamarin
2024-03-20 20:52 ` [gentoo-dev] [PATCH 1/2] tree-sitter-grammar.eclass: support for new upstream makefile Arthur Zamarin
2024-03-20 20:52 ` Arthur Zamarin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240320205659.204635-3-arthurzam@gentoo.org \
    --to=arthurzam@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    --cc=matthew@gentoo.org \
    --cc=sarnex@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox