Skip to content

Commit 29f53d8

Browse files
Makefile and C bindings (Wilfred#38)
1 parent 161a924 commit 29f53d8

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

.gitignore

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

Makefile

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
VERSION := 0.19.0
2+
3+
# Repository
4+
SRC_DIR := src
5+
6+
PARSER_REPO_URL ?= $(shell git -C $(SRC_DIR) remote get-url origin )
7+
# the # in the sed pattern has to be escaped or it will be interpreted as a comment
8+
PARSER_NAME ?= $(shell basename $(PARSER_REPO_URL) | cut -d '-' -f3 | sed 's\#.git\#\#')
9+
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z )
10+
11+
# install directory layout
12+
PREFIX ?= /usr/local
13+
INCLUDEDIR ?= $(PREFIX)/include
14+
LIBDIR ?= $(PREFIX)/lib
15+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
16+
17+
# collect C++ sources, and link if necessary
18+
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)
19+
20+
ifeq (, $(CPPSRC))
21+
ADDITIONALLIBS :=
22+
else
23+
ADDITIONALLIBS := -lc++
24+
endif
25+
26+
# collect sources
27+
SRC := $(wildcard $(SRC_DIR)/*.c)
28+
SRC += $(CPPSRC)
29+
OBJ := $(addsuffix .o,$(basename $(SRC)))
30+
31+
# ABI versioning
32+
SONAME_MAJOR := 0
33+
SONAME_MINOR := 0
34+
35+
CFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
36+
CXXFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
37+
override CFLAGS += -std=gnu99 -fPIC
38+
override CXXFLAGS += -fPIC
39+
40+
# OS-specific bits
41+
ifeq ($(shell uname),Darwin)
42+
SOEXT = dylib
43+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
44+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
45+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
46+
ifneq ($(ADDITIONALLIBS),)
47+
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS),
48+
endif
49+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/libtree-sitter-$(PARSER_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
50+
else
51+
SOEXT = so
52+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
53+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
54+
LINKSHARED := $(LINKSHARED)-shared -Wl,
55+
ifneq ($(ADDITIONALLIBS),)
56+
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS)
57+
endif
58+
LINKSHARED := $(LINKSHARED)-soname,libtree-sitter-$(PARSER_NAME).so.$(SONAME_MAJOR)
59+
endif
60+
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
61+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
62+
endif
63+
64+
all: libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXTVER) bindings/c/$(PARSER_NAME).h
65+
66+
libtree-sitter-$(PARSER_NAME).a: $(OBJ)
67+
$(AR) rcs $@ $^
68+
69+
libtree-sitter-$(PARSER_NAME).$(SOEXTVER): $(OBJ)
70+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
71+
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXT)
72+
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
73+
74+
bindings/c/$(PARSER_NAME).h:
75+
sed -e 's|@UPPER_PARSERNAME@|$(UPPER_PARSER_NAME)|' \
76+
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
77+
bindings/c/tree-sitter.h.in > $@
78+
79+
install: all
80+
install -d '$(DESTDIR)$(LIBDIR)'
81+
install -m755 libtree-sitter-$(PARSER_NAME).a '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).a
82+
install -m755 libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER)
83+
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
84+
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXT)
85+
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
86+
install -m644 bindings/c/$(PARSER_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
87+
install -d '$(DESTDIR)$(PCLIBDIR)'
88+
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \
89+
-e 's|=$(PREFIX)|=$${prefix}|' \
90+
-e 's|@PREFIX@|$(PREFIX)|' \
91+
-e 's|@ADDITIONALLIBS@|$(ADDITIONALLIBS)|' \
92+
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
93+
-e 's|@PARSERREPOURL@|$(PARSER_REPO_URL)|' \
94+
bindings/c/tree-sitter.pc.in > '$(DESTDIR)$(PCLIBDIR)'/tree-sitter-$(PARSER_NAME).pc
95+
96+
clean:
97+
rm -f $(OBJ) libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXT) libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR) libtree-sitter-$(PARSER_NAME).$(SOEXTVER) bindings/c/$(PARSER_NAME).h
98+
99+
.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: @PARSERREPOURL@
9+
Version: @VERSION@
10+
Libs: -L${libdir} ${additionallibs} -ltree-sitter-@PARSERNAME@
11+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)