Skip to content

Commit 10e0103

Browse files
Makefile (#40)
1 parent 05e3631 commit 10e0103

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ tree-sitter-elixir.wasm
1212
# Files generated by Cargo
1313
/target/
1414
Cargo.lock
15+
16+
# C bindings/binaries
17+
/*.a
18+
/*.dylib
19+
/*.so*
20+
*.o
21+
/bindings/c/*.h
22+
/bindings/c/tree-sitter-*.pc

Makefile

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
VERSION := 0.19.1
2+
3+
# Repository
4+
SRC_DIR := src
5+
6+
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin )
7+
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
13+
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
19+
20+
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z )
21+
22+
# install directory layout
23+
PREFIX ?= /usr/local
24+
INCLUDEDIR ?= $(PREFIX)/include
25+
LIBDIR ?= $(PREFIX)/lib
26+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
27+
28+
# collect C++ sources, and link if necessary
29+
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)
30+
31+
ifeq (, $(CPPSRC))
32+
ADDITIONALLIBS :=
33+
else
34+
ADDITIONALLIBS := -lc++
35+
endif
36+
37+
# collect sources
38+
SRC := $(wildcard $(SRC_DIR)/*.c)
39+
SRC += $(CPPSRC)
40+
OBJ := $(addsuffix .o,$(basename $(SRC)))
41+
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

+16
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

+11
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)