Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH v2] tree-sitter-grammar.eclass: Init
Date: Sun, 17 Oct 2021 14:14:19
Message-Id: f146be39519c6037d36f78fc937af854394dc2ce.camel@gentoo.org
In Reply to: [gentoo-dev] [PATCH v2] tree-sitter-grammar.eclass: Init by Matthew Smith
1 On Sun, 2021-10-17 at 12:31 +0100, Matthew Smith wrote:
2 > Hi,
3 >
4 > Updated with feedback from mgorny: documenting TS_PV, using emake
5 > to build objects, and using get_libname to fix so/dylib
6 > confusion.
7 >
8 > Thanks again,
9 > Matthew
10 >
11 > ---
12 > eclass/tree-sitter-grammar.eclass | 96 +++++++++++++++++++++++++++++++
13 > 1 file changed, 96 insertions(+)
14 > create mode 100644 eclass/tree-sitter-grammar.eclass
15 >
16 > diff --git a/eclass/tree-sitter-grammar.eclass
17 > b/eclass/tree-sitter-grammar.eclass
18 > new file mode 100644
19 > index 00000000000..fc18ac203e1
20 > --- /dev/null
21 > +++ b/eclass/tree-sitter-grammar.eclass
22 > @@ -0,0 +1,96 @@
23 > +# Copyright 1999-2021 Gentoo Authors
24 > +# Distributed under the terms of the GNU General Public License v2
25 > +
26 > +# @ECLASS: tree-sitter-grammar.eclass
27 > +# @MAINTAINER:
28 > +# Matthew Smith <matt@×××××××××.uk>
29 > +# Nick Sarnie <sarnex@g.o>
30 > +# @AUTHOR:
31 > +# Matthew Smith <matt@×××××××××.uk>
32 > +# @SUPPORTED_EAPIS: 8
33 > +# @BLURB: Common functions and variables for Tree Sitter grammars
34 > +
35 > +if [[ -z ${_TREE_SITTER_GRAMMAR_ECLASS} ]]; then
36 > +_TREE_SITTER_GRAMMAR_ECLASS=1
37 > +
38 > +case ${EAPI} in
39 > + 8) ;;
40 > + *) die "EAPI=${EAPI:-0} is not supported" ;;
41 > +esac
42 > +
43 > +inherit multilib toolchain-funcs
44 > +
45 > +SRC_URI="https://github.com/tree-sitter/${PN}/archive/${TS_PV:-v${PV}}.tar.gz
46 > + -> ${P}.tar.gz"
47 > +S="${WORKDIR}"/${PN}-${TS_PV:-${PV}}/src
48 > +
49 > +# Needed for tree_sitter/parser.h
50 > +DEPEND="dev-libs/tree-sitter"
51 > +
52 > +EXPORT_FUNCTIONS src_compile src_install
53 > +
54 > +# @ECLASS-VARIABLE: TS_PV
55 > +# @PRE_INHERIT
56 > +# @DEFAULT_UNSET
57 > +# @DESCRIPTION:
58 > +# Used to override upstream tag name if tagged differently, e.g. most
59 > releases
60 > +# are v${PV} but some are tagged as rust-${PV}.
61 > +
62 > +# @FUNCTION: _get_tsg_abi_ver
63 > +# @INTERNAL
64 > +# @DESCRIPTION:
65 > +# This internal function determines the ABI version of a grammar
66 > library based
67 > +# on the package version.
68 > +_get_tsg_abi_ver() {
69 > + if ver_test -gt 0.21; then
70 > + die "Grammar too new; unknown ABI version"
71 > + elif ver_test -ge 0.19.0; then
72 > + echo 13
73 > + else
74 > + die "Grammar too old; unknown ABI version"
75 > + fi
76 > +}
77 > +
78 > +# @FUNCTION: tree-sitter-grammar_src_compile
79 > +# @DESCRIPTION:
80 > +# Compiles the Tree Sitter parser as a shared library.
81 > +tree-sitter-grammar_src_compile() {
82 > + debug-print-function ${FUNCNAME} "${@}"
83 > +
84 > + # Grammars always contain parser.c, and sometimes a scanner.c,
85 > + # or scanner.cc.
86 > +
87 > + tc-export CC CXX
88 > + export CFLAGS="${CFLAGS} -fPIC"
89 > + export CXXFLAGS="${CXXFLAGS} -fPIC"
90 > +
91 > + local objects=( parser.o )
92 > + if [[ -f "${S}"/scanner.c || -f "${S}"/scanner.cc ]]; then
93 > + objects+=( scanner.o )
94 > + fi
95 > + emake "${objects[@]}"
96 > +
97 > + local link=$(tc-getCC)
98 > + if [[ -f "${S}/scanner.cc" ]]; then
99 > + link=$(tc-getCXX)
100 > + fi
101 > +
102 > + local soname=lib${PN}$(get_libname $(_get_tsg_abi_ver))
103 > + ${link} ${LDFLAGS} \
104
105 I'm sorry for missing this before -- when linking, you also need to pass
106 CFLAGS or CXXFLAGS.
107
108 > + -shared \
109 > + *.o \
110 > + -Wl,-soname ${soname} \
111 > + -o "${WORKDIR}"/${soname} || die
112 > +}
113 > +
114 > +# @FUNCTION: tree-sitter-grammar_src_install
115 > +# @DESCRIPTION:
116 > +# Installs the Tree Sitter parser library.
117 > +tree-sitter-grammar_src_install() {
118 > + debug-print-function ${FUNCNAME} "${@}"
119 > +
120 > + dolib.so "${WORKDIR}"/lib${PN}$(get_libname $(_get_tsg_abi_ver))
121 > + dosym lib${PN}$(get_libname $(_get_tsg_abi_ver)) \
122 > + /usr/$(get_libdir)/lib${PN}$(get_libname)
123 > +}
124 > +fi
125 > --
126 > 2.33.0
127 >
128
129 --
130 Best regards,
131 Michał Górny