Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH] tree-sitter-grammar.eclass: Init
Date: Sun, 17 Oct 2021 10:55:51
Message-Id: b9ab4da1f5ede5d17773ef434b7d6d3f77215649.camel@gentoo.org
In Reply to: [gentoo-dev] [PATCH] tree-sitter-grammar.eclass: Init by Matthew Smith
1 On Sun, 2021-10-17 at 11:42 +0100, Matthew Smith wrote:
2 > Hi,
3 >
4 > Please review tree-sitter-grammar.eclass - a new eclass for building
5 > tree-sitter grammars provided by the tree-sitter developers.
6 >
7 > The grammars are shipped as one or two source files and with no build
8 > system. Upstream uses Gyp to build the grammars as a NodeJS module,
9 > but they are useful standalone to other applications such as text
10 > editors.
11 >
12 > Some new packages utilising the eclass can be viewed on GitHub:
13 > https://github.com/gentoo/gentoo/pull/22611
14 >
15 > Thanks,
16 > Matthew
17 >
18 > ---
19 > eclass/tree-sitter-grammar.eclass | 94 +++++++++++++++++++++++++++++++
20 > 1 file changed, 94 insertions(+)
21 > create mode 100644 eclass/tree-sitter-grammar.eclass
22 >
23 > diff --git a/eclass/tree-sitter-grammar.eclass
24 > b/eclass/tree-sitter-grammar.eclass
25 > new file mode 100644
26 > index 00000000000..a107b0b1908
27 > --- /dev/null
28 > +++ b/eclass/tree-sitter-grammar.eclass
29 > @@ -0,0 +1,94 @@
30 > +# Copyright 1999-2021 Gentoo Authors
31 > +# Distributed under the terms of the GNU General Public License v2
32 > +
33 > +# @ECLASS: tree-sitter-grammar.eclass
34 > +# @MAINTAINER:
35 > +# Matthew Smith <matt@×××××××××.uk>
36 > +# Nick Sarnie <sarnex@g.o>
37 > +# @AUTHOR:
38 > +# Matthew Smith <matt@×××××××××.uk>
39 > +# @SUPPORTED_EAPIS: 8
40 > +# @BLURB: Common functions and variables for Tree Sitter grammars
41 > +
42 > +if [[ -z ${_TREE_SITTER_GRAMMAR_ECLASS} ]]; then
43 > +_TREE_SITTER_GRAMMAR_ECLASS=1
44 > +
45 > +case ${EAPI} in
46 > + 8) ;;
47 > + *) die "EAPI=${EAPI:-0} is not supported" ;;
48 > +esac
49 > +
50 > +inherit toolchain-funcs
51 > +
52 > +SRC_URI="https://github.com/tree-sitter/${PN}/archive/${TS_PV:-v${PV}}.tar.gz
53 > -> ${P}.tar.gz"
54
55 TS_PV is not documented. Also, this line asks for wrapping.
56
57 > +S="${WORKDIR}"/${PN}-${TS_PV:-${PV}}/src
58 > +
59 > +# Needed for tree_sitter/parser.h
60 > +DEPEND="dev-libs/tree-sitter"
61 > +
62 > +EXPORT_FUNCTIONS src_compile src_install
63 > +
64 > +# @FUNCTION: get_tsg_abi_ver
65 > +# @DESCRIPTION:
66 > +# This internal function determines the ABI version of a grammar
67 > library based
68
69 Then mark it with @INTERNAL? Also prefixing it with '_' would be a good
70 idea.
71
72 > +# on the package version.
73 > +get_tsg_abi_ver() {
74 > + if ver_test -gt 0.21; then
75 > + die "Grammar too new; unknown ABI version"
76 > + elif ver_test -ge 0.19.0; then
77 > + echo 13
78 > + else
79 > + die "Grammar too old; unknown ABI version"
80 > + fi
81 > +}
82 > +
83 > +# @FUNCTION: tree-sitter-grammar_src_compile
84 > +# @DESCRIPTION:
85 > +# Compiles the Tree Sitter parser as a shared library.
86 > +tree-sitter-grammar_src_compile() {
87 > + debug-print-function $FUNCNAME $*
88
89 Please keep the prologue consistent with our coding style, i.e.:
90
91 debug-print-function ${FUNCNAME} "${@}"
92
93 > +
94 > + # Grammars always contain parser.c, and sometimes a scanner.c,
95 > + # or scanner.cc.
96 > +
97 > + $(tc-getCC) ${CFLAGS} \
98 > + ${CPPFLAGS} \
99 > + -fPIC \
100
101 How about exporting CC etc., and using emake to build them?
102
103 i.e. something like
104
105 tc-export CC
106 export CFLAGS="${CFLAGS} -fPIC"
107 emake parser.o
108
109 (also I think it would be less confusing to use the same directory for
110 output file)
111
112 > + -c "${S}"/parser.c \
113 > + -o "${WORKDIR}"/parser.o || die
114 > +
115 > + local link=$(tc-getCC)
116 > +
117 > + if [[ -f "${S}/scanner.c" ]]; then
118 > + $(tc-getCC) ${CFLAGS} \
119 > + ${CPPFLAGS} \
120 > + -fPIC \
121 > + -c "${S}"/scanner.c \
122 > + -o "${WORKDIR}"/scanner.o || die
123 > + elif [[ -f "${S}/scanner.cc" ]]; then
124 > + $(tc-getCXX) ${CXXFLAGS} \
125 > + ${CPPFLAGS} \
126 > + -fPIC \
127 > + -c "${S}"/scanner.cc \
128 > + -o "${WORKDIR}"/scanner.o || die
129 > + link=$(tc-getCXX)
130 > + fi
131 > +
132 > + local soname=lib${PN}.so.$(get_tsg_abi_ver)
133
134 We've got some helpers like get_libname to make this a little bit more
135 portable, or rather to introduce less obstacles when people want to fix
136 portability issues.
137
138 > + ${link} ${LDFLAGS} \
139 > + -shared \
140 > + "${WORKDIR}"/*.o \
141 > + -Wl,-soname ${soname} \
142 > + -o "${WORKDIR}"/${soname} || die
143 > +}
144 > +
145 > +# @FUNCTION: tree-sitter-grammar_src_install
146 > +# @DESCRIPTION:
147 > +# Installs the Tree Sitter parser library.
148 > +tree-sitter-grammar_src_install() {
149 > + debug-print-function $FUNCNAME $*
150 > +
151 > + dolib.so "${WORKDIR}"/lib${PN}.so.$(get_tsg_abi_ver)
152 > + dosym lib${PN}.so.$(get_tsg_abi_ver) /usr/$(get_libdir)/lib${PN}.so
153 > +}
154 > +fi
155 > --
156 > 2.33.0
157 >
158
159 --
160 Best regards,
161 Michał Górny