1 |
Hi, |
2 |
|
3 |
Please review tree-sitter-grammar.eclass - a new eclass for building |
4 |
tree-sitter grammars provided by the tree-sitter developers. |
5 |
|
6 |
The grammars are shipped as one or two source files and with no build |
7 |
system. Upstream uses Gyp to build the grammars as a NodeJS module, |
8 |
but they are useful standalone to other applications such as text |
9 |
editors. |
10 |
|
11 |
Some new packages utilising the eclass can be viewed on GitHub: |
12 |
https://github.com/gentoo/gentoo/pull/22611 |
13 |
|
14 |
Thanks, |
15 |
Matthew |
16 |
|
17 |
--- |
18 |
eclass/tree-sitter-grammar.eclass | 94 +++++++++++++++++++++++++++++++ |
19 |
1 file changed, 94 insertions(+) |
20 |
create mode 100644 eclass/tree-sitter-grammar.eclass |
21 |
|
22 |
diff --git a/eclass/tree-sitter-grammar.eclass |
23 |
b/eclass/tree-sitter-grammar.eclass |
24 |
new file mode 100644 |
25 |
index 00000000000..a107b0b1908 |
26 |
--- /dev/null |
27 |
+++ b/eclass/tree-sitter-grammar.eclass |
28 |
@@ -0,0 +1,94 @@ |
29 |
+# Copyright 1999-2021 Gentoo Authors |
30 |
+# Distributed under the terms of the GNU General Public License v2 |
31 |
+ |
32 |
+# @ECLASS: tree-sitter-grammar.eclass |
33 |
+# @MAINTAINER: |
34 |
+# Matthew Smith <matt@×××××××××.uk> |
35 |
+# Nick Sarnie <sarnex@g.o> |
36 |
+# @AUTHOR: |
37 |
+# Matthew Smith <matt@×××××××××.uk> |
38 |
+# @SUPPORTED_EAPIS: 8 |
39 |
+# @BLURB: Common functions and variables for Tree Sitter grammars |
40 |
+ |
41 |
+if [[ -z ${_TREE_SITTER_GRAMMAR_ECLASS} ]]; then |
42 |
+_TREE_SITTER_GRAMMAR_ECLASS=1 |
43 |
+ |
44 |
+case ${EAPI} in |
45 |
+ 8) ;; |
46 |
+ *) die "EAPI=${EAPI:-0} is not supported" ;; |
47 |
+esac |
48 |
+ |
49 |
+inherit toolchain-funcs |
50 |
+ |
51 |
+SRC_URI="https://github.com/tree-sitter/${PN}/archive/${TS_PV:-v${PV}}.tar.gz |
52 |
-> ${P}.tar.gz" |
53 |
+S="${WORKDIR}"/${PN}-${TS_PV:-${PV}}/src |
54 |
+ |
55 |
+# Needed for tree_sitter/parser.h |
56 |
+DEPEND="dev-libs/tree-sitter" |
57 |
+ |
58 |
+EXPORT_FUNCTIONS src_compile src_install |
59 |
+ |
60 |
+# @FUNCTION: get_tsg_abi_ver |
61 |
+# @DESCRIPTION: |
62 |
+# This internal function determines the ABI version of a grammar |
63 |
library based |
64 |
+# on the package version. |
65 |
+get_tsg_abi_ver() { |
66 |
+ if ver_test -gt 0.21; then |
67 |
+ die "Grammar too new; unknown ABI version" |
68 |
+ elif ver_test -ge 0.19.0; then |
69 |
+ echo 13 |
70 |
+ else |
71 |
+ die "Grammar too old; unknown ABI version" |
72 |
+ fi |
73 |
+} |
74 |
+ |
75 |
+# @FUNCTION: tree-sitter-grammar_src_compile |
76 |
+# @DESCRIPTION: |
77 |
+# Compiles the Tree Sitter parser as a shared library. |
78 |
+tree-sitter-grammar_src_compile() { |
79 |
+ debug-print-function $FUNCNAME $* |
80 |
+ |
81 |
+ # Grammars always contain parser.c, and sometimes a scanner.c, |
82 |
+ # or scanner.cc. |
83 |
+ |
84 |
+ $(tc-getCC) ${CFLAGS} \ |
85 |
+ ${CPPFLAGS} \ |
86 |
+ -fPIC \ |
87 |
+ -c "${S}"/parser.c \ |
88 |
+ -o "${WORKDIR}"/parser.o || die |
89 |
+ |
90 |
+ local link=$(tc-getCC) |
91 |
+ |
92 |
+ if [[ -f "${S}/scanner.c" ]]; then |
93 |
+ $(tc-getCC) ${CFLAGS} \ |
94 |
+ ${CPPFLAGS} \ |
95 |
+ -fPIC \ |
96 |
+ -c "${S}"/scanner.c \ |
97 |
+ -o "${WORKDIR}"/scanner.o || die |
98 |
+ elif [[ -f "${S}/scanner.cc" ]]; then |
99 |
+ $(tc-getCXX) ${CXXFLAGS} \ |
100 |
+ ${CPPFLAGS} \ |
101 |
+ -fPIC \ |
102 |
+ -c "${S}"/scanner.cc \ |
103 |
+ -o "${WORKDIR}"/scanner.o || die |
104 |
+ link=$(tc-getCXX) |
105 |
+ fi |
106 |
+ |
107 |
+ local soname=lib${PN}.so.$(get_tsg_abi_ver) |
108 |
+ ${link} ${LDFLAGS} \ |
109 |
+ -shared \ |
110 |
+ "${WORKDIR}"/*.o \ |
111 |
+ -Wl,-soname ${soname} \ |
112 |
+ -o "${WORKDIR}"/${soname} || die |
113 |
+} |
114 |
+ |
115 |
+# @FUNCTION: tree-sitter-grammar_src_install |
116 |
+# @DESCRIPTION: |
117 |
+# Installs the Tree Sitter parser library. |
118 |
+tree-sitter-grammar_src_install() { |
119 |
+ debug-print-function $FUNCNAME $* |
120 |
+ |
121 |
+ dolib.so "${WORKDIR}"/lib${PN}.so.$(get_tsg_abi_ver) |
122 |
+ dosym lib${PN}.so.$(get_tsg_abi_ver) /usr/$(get_libdir)/lib${PN}.so |
123 |
+} |
124 |
+fi |
125 |
-- |
126 |
2.33.0 |