From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 0F278158041 for ; Wed, 20 Mar 2024 20:58:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 91FF0E29B5; Wed, 20 Mar 2024 20:57:37 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 14FE6E29B0 for ; Wed, 20 Mar 2024 20:57:37 +0000 (UTC) From: Arthur Zamarin To: gentoo-dev@lists.gentoo.org Cc: Matthew Smith , Nick Sarnie , Arthur Zamarin Subject: [gentoo-dev] [PATCH 1/2] tree-sitter-grammar.eclass: support for new upstream makefile Date: Wed, 20 Mar 2024 22:52:38 +0200 Message-ID: <20240320205659.204635-2-arthurzam@gentoo.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240320205659.204635-1-arthurzam@gentoo.org> References: <20240320205659.204635-1-arthurzam@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archives-Salt: 645b04ef-87ef-4077-b9ad-97ab5ce546cc X-Archives-Hash: 8db0bd768184eeb5f6796db1d47497f3 The build system for tree-sitters now generates a much better Makefile we can use to build the parser and grammar into a good C library. This also matches the build procedure used by upstream, making our reports easier for them to debug (we hit this issue in an old bug report on memory leak with tree-sitter-bash). Signed-off-by: Arthur Zamarin --- eclass/tree-sitter-grammar.eclass | 64 +++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/eclass/tree-sitter-grammar.eclass b/eclass/tree-sitter-grammar.eclass index b2563220cfc..13539daf7e6 100644 --- a/eclass/tree-sitter-grammar.eclass +++ b/eclass/tree-sitter-grammar.eclass @@ -1,10 +1,11 @@ -# Copyright 1999-2023 Gentoo Authors +# Copyright 1999-2024 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: tree-sitter-grammar.eclass # @MAINTAINER: # Matthew Smith # Nick Sarnie +# Arthur Zamarin # @AUTHOR: # Matthew Smith # @SUPPORTED_EAPIS: 8 @@ -22,7 +23,7 @@ inherit edo multilib toolchain-funcs SRC_URI="https://github.com/tree-sitter/${PN}/archive/${TS_PV:-v${PV}}.tar.gz -> ${P}.tar.gz" -S="${WORKDIR}"/${PN}-${TS_PV:-${PV}}/src +S="${WORKDIR}"/${PN}-${TS_PV:-${PV}} BDEPEND+=" test? ( dev-util/tree-sitter-cli )" IUSE+=" test" @@ -44,15 +45,16 @@ _get_tsg_abi_ver() { # This sed script finds ABI definition string in parser source file, # substitutes all the string until the ABI number, and prints remains # (the ABI number itself) - sed -n 's/#define LANGUAGE_VERSION //p' "${S}"/parser.c || + sed -n 's/#define LANGUAGE_VERSION //p' "${S}"/src/parser.c || die "Unable to extract ABI version for this grammar" } -# @FUNCTION: tree-sitter-grammar_src_compile +# @FUNCTION: _tree-sitter-grammar_legacy_compile +# @INTERNAL # @DESCRIPTION: -# Compiles the Tree Sitter parser as a shared library. -tree-sitter-grammar_src_compile() { - debug-print-function ${FUNCNAME} "${@}" +# Compiles the Tree Sitter parser as a shared library, the legacy way. +_tree-sitter-grammar_legacy_compile() { + cd "${S}/src" || die # Grammars always contain parser.c, and sometimes a scanner.c, # or scanner.cc. @@ -60,17 +62,17 @@ tree-sitter-grammar_src_compile() { tc-export CC CXX # We want to use the bundled parser.h, not anything lurking on the system, hence -I # See https://github.com/tree-sitter/tree-sitter-bash/issues/199#issuecomment-1694416505 - export CFLAGS="${CFLAGS} -fPIC -I. -Itree_sitter" - export CXXFLAGS="${CXXFLAGS} -fPIC -I. -Itree_sitter" + local -x CFLAGS="${CFLAGS} -fPIC -I. -Itree_sitter" + local -x CXXFLAGS="${CXXFLAGS} -fPIC -I. -Itree_sitter" local objects=( parser.o ) - if [[ -f "${S}"/scanner.c || -f "${S}"/scanner.cc ]]; then + if [[ -f "${S}"/src/scanner.c || -f "${S}"/src/scanner.cc ]]; then objects+=( scanner.o ) fi emake "${objects[@]}" local link="$(tc-getCC) ${CFLAGS}" - if [[ -f "${S}/scanner.cc" ]]; then + if [[ -f "${S}/src/scanner.cc" ]]; then link="$(tc-getCXX) ${CXXFLAGS}" fi @@ -84,10 +86,24 @@ tree-sitter-grammar_src_compile() { edo ${link} ${LDFLAGS} \ -shared \ *.o \ - ${soname_args} \ + "${soname_args}" \ -o "${WORKDIR}"/${soname} } +tree-sitter-grammar_src_compile() { + debug-print-function ${FUNCNAME} "${@}" + + # legacy grammars don't have a pyproject.toml + if [[ -f "${S}/pyproject.toml" ]]; then + sed -e "/SONAME_MINOR :=/s/:=.*$/:= $(_get_tsg_abi_ver)/" -i "${S}/Makefile" || die + emake \ + PREFIX="${EPREFIX}/usr" \ + LIBDIR="${EPREFIX}/usr/$(get_libdir)" + else + _tree-sitter-grammar_legacy_compile + fi +} + # @FUNCTION: tree-sitter-grammar_src_test # @DESCRIPTION: # Runs the Tree Sitter parser's test suite. @@ -95,20 +111,26 @@ tree-sitter-grammar_src_compile() { tree-sitter-grammar_src_test() { debug-print-function ${FUNCNAME} "${@}" - (cd .. && tree-sitter test) || die "Test suite failed" + tree-sitter test || die "Test suite failed" } -# @FUNCTION: tree-sitter-grammar_src_install -# @DESCRIPTION: -# Installs the Tree Sitter parser library. tree-sitter-grammar_src_install() { debug-print-function ${FUNCNAME} "${@}" - local soname=lib${PN}$(get_libname $(_get_tsg_abi_ver)) - - dolib.so "${WORKDIR}/${soname}" - dosym "${soname}" \ - /usr/$(get_libdir)/lib${PN}$(get_libname) + # legacy grammars don't have a pyproject.toml + if [[ -f "${S}/pyproject.toml" ]]; then + emake \ + PREFIX="${EPREFIX}/usr" \ + LIBDIR="${EPREFIX}/usr/$(get_libdir)" \ + DESTDIR="${D}/" \ + install + find "${D}" -name '*.a' -delete || die "failed to remove static libraries" + else + local soname=lib${PN}$(get_libname $(_get_tsg_abi_ver)) + + dolib.so "${WORKDIR}/${soname}" + dosym "${soname}" /usr/$(get_libdir)/lib${PN}$(get_libname) + fi } fi -- 2.44.0