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