-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMakefile.common
More file actions
83 lines (77 loc) · 3.3 KB
/
Copy pathMakefile.common
File metadata and controls
83 lines (77 loc) · 3.3 KB
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
#
# Build the upstream and semgrep-extended grammars under
# lang/semgrep-grammars/src/. Each grammar is generated using the tree-sitter
# version pinned for it in lang/languages-<version> or
# lang/language-variants-<version>. There is no globally-selected tree-sitter
# binary here.
#
PROJECT_ROOT = $(shell git rev-parse --show-toplevel)
LANG_SCRIPTS = $(PROJECT_ROOT)/lang/scripts
NODE_PATH := $(PROJECT_ROOT)/lang/semgrep-grammars/src:$(NODE_PATH)
export NODE_PATH
# Per-grammar build. The tree-sitter binary comes from the version directory
# that owns the grammar. ABI selection:
# - ABI 15 only when the grammar ships a tree-sitter.json AND ABI 15 is
# enabled via the SEMGREP_ENABLE_ABI15 environment variable (see the
# "tree-sitter ABI 15" section of the repository-root README). This is
# off by default: we are not yet ready to ship ABI 15 parsers.
# - ABI 14 otherwise, for tree-sitter >= 0.24.0 (which introduced --abi).
# - --no-bindings for older tree-sitter.
.PHONY: build
build:
$(MAKE) prep
set -eu; \
for dir in $$(find . -name grammar.js | xargs -I{} dirname {}); do \
ts_version=$$($(LANG_SCRIPTS)/ts-version-for-grammar-dir "$$dir"); \
ts_bindir=$(PROJECT_ROOT)/core/tree-sitter-$$ts_version/bin; \
if [ ! -x "$$ts_bindir/tree-sitter" ]; then \
echo "Error: tree-sitter $$ts_version is not installed at $$ts_bindir." >&2; \
echo "Install it with:" >&2; \
echo " cd $(PROJECT_ROOT)/core && ./scripts/switch-tree-sitter-version $$ts_version && ./scripts/install-tree-sitter-cli && ./scripts/install-tree-sitter-lib" >&2; \
exit 1; \
fi; \
echo "Generate parser in $$(pwd)/$$dir (tree-sitter $$ts_version)"; \
if ! [ -e "$$dir/src/parser.c" ] \
|| [ "$$dir/grammar.js" -nt "$$dir/src/parser.c" ]; then \
gen_args=$$($(LANG_SCRIPTS)/ts-generate-abi-args "$$dir" "$$ts_version"); \
echo " Running in $$dir: tree-sitter generate $$gen_args"; \
(cd "$$dir" \
&& PATH="$$ts_bindir:$$PATH" \
NODE_PATH=$(PROJECT_ROOT)/lang/semgrep-grammars/src \
tree-sitter generate $$gen_args); \
else \
echo "$$dir/src/parser.c is up to date."; \
fi; \
done
# Import, rename, patch etc. in a custom manner, as needed.
#
.PHONY: prep
prep:
if [ -x prep ]; then ./prep; fi
.PHONY: test
test:
# 'tree-sitter test' can emit a large number of repetitive error
# messages when compiling 'parser.c'.
set -eu; \
for dir in $$(find . | grep 'grammar.js$$' | xargs -L1 dirname); do \
ts_version=$$($(LANG_SCRIPTS)/ts-version-for-grammar-dir "$$dir"); \
ts_bindir=$(PROJECT_ROOT)/core/tree-sitter-$$ts_version/bin; \
if [ ! -x "$$ts_bindir/tree-sitter" ]; then \
echo "Error: tree-sitter $$ts_version is not installed at $$ts_bindir." >&2; \
echo "Install it with:" >&2; \
echo " cd $(PROJECT_ROOT)/core && ./scripts/switch-tree-sitter-version $$ts_version && ./scripts/install-tree-sitter-cli && ./scripts/install-tree-sitter-lib" >&2; \
exit 1; \
fi; \
echo "Test $$(pwd)/$$dir (tree-sitter $$ts_version)"; \
(cd "$$dir"; \
show_log() { echo "..."; tail -n 1000 test.log; }; \
if PATH="$$ts_bindir:$$PATH" tree-sitter test > test.log 2>&1; then \
show_log; \
else \
show_log; \
exit 1; \
fi); \
done
.PHONY: clean
clean:
git clean -dfX