diff options
author | Matt Smith <matt@offtopica.uk> | 2021-10-16 20:39:16 +0100 |
---|---|---|
committer | Nick Sarnie <sarnex@gentoo.org> | 2021-10-21 19:49:23 -0400 |
commit | 9afc675ca6a16d345f48224d14c0cf6301d32607 (patch) | |
tree | 3b419d950d16ee29fa0a5fefb9bf4190ac658a2c /eclass | |
parent | dev-lang/rust: base 1.56.0 on llvm-13 (diff) | |
download | gentoo-9afc675ca6a16d345f48224d14c0cf6301d32607.tar.gz gentoo-9afc675ca6a16d345f48224d14c0cf6301d32607.tar.bz2 gentoo-9afc675ca6a16d345f48224d14c0cf6301d32607.zip |
tree-sitter-grammar.eclass: Init
Signed-off-by: Matt Smith <matt@offtopica.uk>
Signed-off-by: Nick Sarnie <sarnex@gentoo.org>
Diffstat (limited to 'eclass')
-rw-r--r-- | eclass/tree-sitter-grammar.eclass | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/eclass/tree-sitter-grammar.eclass b/eclass/tree-sitter-grammar.eclass new file mode 100644 index 000000000000..46573027f96f --- /dev/null +++ b/eclass/tree-sitter-grammar.eclass @@ -0,0 +1,96 @@ +# Copyright 1999-2021 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: tree-sitter-grammar.eclass +# @MAINTAINER: +# Matthew Smith <matt@offtopica.uk> +# Nick Sarnie <sarnex@gentoo.org> +# @AUTHOR: +# Matthew Smith <matt@offtopica.uk> +# @SUPPORTED_EAPIS: 8 +# @BLURB: Common functions and variables for Tree Sitter grammars + +if [[ -z ${_TREE_SITTER_GRAMMAR_ECLASS} ]]; then +_TREE_SITTER_GRAMMAR_ECLASS=1 + +case ${EAPI} in + 8) ;; + *) die "EAPI=${EAPI:-0} is not supported" ;; +esac + +inherit 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 + +# Needed for tree_sitter/parser.h +DEPEND="dev-libs/tree-sitter" + +EXPORT_FUNCTIONS src_compile src_install + +# @ECLASS-VARIABLE: TS_PV +# @PRE_INHERIT +# @DEFAULT_UNSET +# @DESCRIPTION: +# Used to override upstream tag name if tagged differently, e.g. most releases +# are v${PV} but some are tagged as rust-${PV}. + +# @FUNCTION: _get_tsg_abi_ver +# @INTERNAL +# @DESCRIPTION: +# This internal function determines the ABI version of a grammar library based +# on the package version. +_get_tsg_abi_ver() { + if ver_test -gt 0.21; then + die "Grammar too new; unknown ABI version" + elif ver_test -ge 0.19.0; then + echo 13 + else + die "Grammar too old; unknown ABI version" + fi +} + +# @FUNCTION: tree-sitter-grammar_src_compile +# @DESCRIPTION: +# Compiles the Tree Sitter parser as a shared library. +tree-sitter-grammar_src_compile() { + debug-print-function ${FUNCNAME} "${@}" + + # Grammars always contain parser.c, and sometimes a scanner.c, + # or scanner.cc. + + tc-export CC CXX + export CFLAGS="${CFLAGS} -fPIC" + export CXXFLAGS="${CXXFLAGS} -fPIC" + + local objects=( parser.o ) + if [[ -f "${S}"/scanner.c || -f "${S}"/scanner.cc ]]; then + objects+=( scanner.o ) + fi + emake "${objects[@]}" + + local link="$(tc-getCC) ${CFLAGS}" + if [[ -f "${S}/scanner.cc" ]]; then + link="$(tc-getCXX) ${CXXFLAGS}" + fi + + local soname=lib${PN}$(get_libname $(_get_tsg_abi_ver)) + ${link} ${LDFLAGS} \ + -shared \ + *.o \ + -Wl,-soname ${soname} \ + -o "${WORKDIR}"/${soname} || die +} + +# @FUNCTION: tree-sitter-grammar_src_install +# @DESCRIPTION: +# Installs the Tree Sitter parser library. +tree-sitter-grammar_src_install() { + debug-print-function ${FUNCNAME} "${@}" + + dolib.so "${WORKDIR}"/lib${PN}$(get_libname $(_get_tsg_abi_ver)) + dosym lib${PN}$(get_libname $(_get_tsg_abi_ver)) \ + /usr/$(get_libdir)/lib${PN}$(get_libname) +} +fi |