-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabuild-tree-sitter
executable file
·104 lines (92 loc) · 2.37 KB
/
abuild-tree-sitter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
#---help---
# Usage:
# $0 build [options]
# $0 install [options]
# $0 (-h | --help)
#
# Compile and install tree-sitter grammar as a .so library. This script should
# be used in all tree-sitter grammar aports to simplify maintenance.
#
# Options:
# -q <querydir> Location of directory with queries (.scm files) to be
# installed (defaults to $PWD/queries).
#
# -s <srcdir> Location of the source directory with grammar.json and C/C++
# sources (defaults to $PWD/src).
#
# -n <name> Name of the grammar (defaults to name specified in
# grammar.json converted to kebab-case).
#
# Environment variables:
# CC, CXX, LD, CFLAGS, CXXFLAGS, LDFLAGS, DESTDIR, PREFIX
#---help---
set -eu
PROGNAME='abuild-tree-sitter'
: ${CC:="cc"}
: ${CFLAGS:=}
: ${CXX:="c++"}
: ${CXXFLAGS:=}
: ${LD:="ld"}
: ${LDFLAGS:=}
: ${DESTDIR:=}
: ${PREFIX:="/usr"}
: ${CFLAGS_BASE:="-fPIC -Wall -std=c99"}
: ${CXXFLAGS_BASE:="-fPIC -Wall -fno-exceptions"}
: ${LDFLAGS_BASE:="-shared"}
help() {
local tag='#---help---'
sed -n "/^$tag/,/^$tag/{/^$tag/d; s/^# \\?//; s/\$0/$PROGNAME/; p}" "$0"
}
die() {
printf "$PROGNAME: %s\n" "$@" >&2
exit 1
}
subcmd=
case "${1:-}" in
'' | -h | --help) help; exit 0;;
-*) die 'missing subcommand';;
*) subcmd=$1; shift;;
esac
querydir='./queries'
srcdir='./src'
destdir=
name=
while getopts ':q:s:D:h' OPT; do
case "$OPT" in
q) querydir=$OPTARG;;
s) srcdir=$OPTARG;;
D) destdir=$OPTARG;;
n) name=$OPTARG;;
h) help; exit 0;;
\?) die "unknown option: -$OPTARG";;
esac
done
shift $((OPTIND - 1))
[ "$name" ] || name=$(jq -re '.name | gsub("_"; "-")' "$srcdir"/grammar.json)
case "$subcmd" in
build)
cd "$srcdir"
set -x
$CC $CFLAGS_BASE $CFLAGS -c ./*.c
if find -name '*.cc' | grep -q .; then
$CXX $CXXFLAGS_BASE $CXXFLAGS -c ./*.cc
$CXX $LDFLAGS_BASE $LDFLAGS -o $name.so ./*.o
else
$CC $LDFLAGS_BASE $LDFLAGS -o $name.so ./*.o
fi
;;
install)
set -x
# Some programs expect grammar libs as <lang>.so in a specific
# directory, some expect libtree-sitter-<lang>.so on the library path.
install -D -m755 "$srcdir"/*.so -t "$DESTDIR$PREFIX"/lib/tree-sitter/
ln -s tree-sitter/$name.so "$DESTDIR$PREFIX"/lib/libtree-sitter-$name.so
if [ -d "$querydir" ]; then
install -D -m755 "$querydir"/*.scm -t "$DESTDIR$PREFIX"/share/tree-sitter/queries/$name/
fi
;;
*)
die "invalid subcommand: $subcmd"
;;
esac