Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 029c9de

Browse files
Makefile
1 parent fab6802 commit 029c9de

File tree

4 files changed

+137
-25
lines changed

4 files changed

+137
-25
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ node_modules
44
build
55
examples
66
*.log
7+
/*.a
8+
/*.dylib
9+
/*.so*
10+
*.o
11+
/bindings/c/*.h
12+
/bindings/c/tree-sitter-*.pc

Makefile

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,114 @@
1-
TREE_SITTER=tree-sitter
1+
VERSION := 0.0.1
22

3-
all: fmt gen test
3+
# Repository
4+
SRC_DIR := src
45

5-
fmt:
6-
./node_modules/.bin/prettier --write grammar.js
6+
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin )
77

8-
.PHONY: test
9-
test: gen
10-
$(TREE_SITTER) test
8+
ifeq (, $(PARSER_NAME))
9+
PARSER_NAME := $(shell basename $(PARSER_REPO_URL))
10+
PARSER_NAME := $(subst tree-sitter-,,$(PARSER_NAME))
11+
PARSER_NAME := $(subst .git,,$(PARSER_NAME))
12+
endif
1113

12-
.PHONY: debug
13-
debug: gen
14-
$(TREE_SITTER) test -d
14+
ifeq (, $(PARSER_URL))
15+
PARSER_URL := $(subst :,/,$(PARSER_REPO_URL))
16+
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
17+
PARSER_URL := $(subst .git,,$(PARSER_URL))
18+
endif
1519

16-
.PHONY: gen
17-
gen:
18-
$(TREE_SITTER) generate
20+
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z )
1921

20-
.PHONY: deps
21-
deps:
22-
yarn
22+
# install directory layout
23+
PREFIX ?= /usr/local
24+
INCLUDEDIR ?= $(PREFIX)/include
25+
LIBDIR ?= $(PREFIX)/lib
26+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
2327

24-
.PHONY: web
25-
web: wasm
26-
$(TREE_SITTER) web-ui
28+
# collect C++ sources, and link if necessary
29+
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)
2730

28-
.PHONY: wasm
29-
wasm:
30-
$(TREE_SITTER) build-wasm
31+
ifeq (, $(CPPSRC))
32+
ADDITIONALLIBS :=
33+
else
34+
ADDITIONALLIBS := -lc++
35+
endif
3136

32-
.PHONY: publish
33-
publish: all wasm
34-
cp ./tree-sitter-erlang.wasm ./docs
37+
# collect sources
38+
SRC := $(wildcard $(SRC_DIR)/*.c)
39+
SRC += $(CPPSRC)
40+
OBJ := $(addsuffix .o,$(basename $(SRC)))
3541

42+
# ABI versioning
43+
SONAME_MAJOR := 0
44+
SONAME_MINOR := 0
45+
46+
CFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
47+
CXXFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
48+
override CFLAGS += -std=gnu99 -fPIC
49+
override CXXFLAGS += -fPIC
50+
51+
# OS-specific bits
52+
ifeq ($(shell uname),Darwin)
53+
SOEXT = dylib
54+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
55+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
56+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
57+
ifneq ($(ADDITIONALLIBS),)
58+
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS),
59+
endif
60+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/libtree-sitter-$(PARSER_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
61+
else
62+
SOEXT = so
63+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
64+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
65+
LINKSHARED := $(LINKSHARED)-shared -Wl,
66+
ifneq ($(ADDITIONALLIBS),)
67+
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS),
68+
endif
69+
LINKSHARED := $(LINKSHARED)-soname,libtree-sitter-$(PARSER_NAME).so.$(SONAME_MAJOR)
70+
endif
71+
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
72+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
73+
endif
74+
75+
all: libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXTVER) bindings/c/$(PARSER_NAME).h bindings/c/tree-sitter-$(PARSER_NAME).pc
76+
77+
libtree-sitter-$(PARSER_NAME).a: $(OBJ)
78+
$(AR) rcs $@ $^
79+
80+
libtree-sitter-$(PARSER_NAME).$(SOEXTVER): $(OBJ)
81+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
82+
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXT)
83+
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
84+
85+
bindings/c/$(PARSER_NAME).h:
86+
sed -e 's|@UPPER_PARSERNAME@|$(UPPER_PARSER_NAME)|' \
87+
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
88+
bindings/c/tree-sitter.h.in > $@
89+
90+
bindings/c/tree-sitter-$(PARSER_NAME).pc:
91+
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \
92+
-e 's|=$(PREFIX)|=$${prefix}|' \
93+
-e 's|@PREFIX@|$(PREFIX)|' \
94+
-e 's|@ADDITIONALLIBS@|$(ADDITIONALLIBS)|' \
95+
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
96+
-e 's|@PARSERURL@|$(PARSER_URL)|' \
97+
bindings/c/tree-sitter.pc.in > $@
98+
99+
install: all
100+
install -d '$(DESTDIR)$(LIBDIR)'
101+
install -m755 libtree-sitter-$(PARSER_NAME).a '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).a
102+
install -m755 libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER)
103+
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
104+
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXT)
105+
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
106+
install -m644 bindings/c/$(PARSER_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
107+
install -d '$(DESTDIR)$(PCLIBDIR)'
108+
install -m644 bindings/c/tree-sitter-$(PARSER_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/
109+
110+
clean:
111+
rm -f $(OBJ) libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXT) libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR) libtree-sitter-$(PARSER_NAME).$(SOEXTVER)
112+
rm -f bindings/c/$(PARSER_NAME).h bindings/c/tree-sitter-$(PARSER_NAME).pc
113+
114+
.PHONY: all install clean

bindings/c/tree-sitter.h.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_@UPPER_PARSERNAME@_H_
2+
#define TREE_SITTER_@UPPER_PARSERNAME@_H_
3+
4+
#include <tree_sitter/parser.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
extern TSLanguage *tree_sitter_@PARSERNAME@();
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_@UPPER_PARSERNAME@_H_

bindings/c/tree-sitter.pc.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
prefix=@PREFIX@
2+
libdir=@LIBDIR@
3+
includedir=@INCLUDEDIR@
4+
additionallibs=@ADDITIONALLIBS@
5+
6+
Name: tree-sitter-@PARSERNAME@
7+
Description: A tree-sitter grammar for the @PARSERNAME@ programming language.
8+
URL: @PARSERURL@
9+
Version: @VERSION@
10+
Libs: -L${libdir} ${additionallibs} -ltree-sitter-@PARSERNAME@
11+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)