Skip to content

Commit af498bc

Browse files
committed
Add a simple Makefile-based build system.
This produces static/shared libraries as well as a pkg-config file, and installs them to the standard Unix hierarchy. GNU Make is assumed as features like $(wildcard ...) or $(shell uname) may not work elsewhere, but it should otherwise build on most/all Unix platforms. Note that we assume the following POSIX default rules/macros exist, so we don't bother redefining them: - pattern rules for compiling .c -> .o - $(CC) - $(AR) Special note on the .pc file generation: we do most variable replacements in one go, but delay @Prefix@ so that we can first find existing substring instances of the prefix value (if libdir/includedir reside within the prefix), and update them to use a literal pkg-config variable '${prefix}'. This is fairly compact (one single sed) while still letting us produce pkg-config files that support runtime redefinition à la cross-compilation.
1 parent 1635aab commit af498bc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Makefile

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
VERSION := 0.6.3
2+
3+
# install directory layout
4+
PREFIX ?= /usr/local
5+
INCLUDEDIR ?= $(PREFIX)/include
6+
LIBDIR ?= $(PREFIX)/lib
7+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
8+
9+
# collect sources
10+
ifneq ($(AMALGAMATED),1)
11+
SRC := $(wildcard lib/src/*.c)
12+
# do not double-include amalgamation
13+
SRC := $(filter-out lib/src/lib.c,$(SRC))
14+
else
15+
# use amalgamated build
16+
SRC := lib/src/lib.c
17+
endif
18+
OBJ := $(SRC:.c=.o)
19+
20+
# define default flags, and override to append mandatory flags
21+
CFLAGS ?= -O3
22+
override CFLAGS += -std=gnu99 -fPIC -Ilib/src -Ilib/include
23+
24+
# ABI versioning
25+
SONAME_MAJOR := 0
26+
SONAME_MINOR := 0
27+
28+
# OS-specific bits
29+
ifeq ($(shell uname),Darwin)
30+
SOEXT = dylib
31+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
32+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
33+
LINKSHARED += -dynamiclib -Wl,-install_name,$(LIBDIR)/libtree-sitter.$(SONAME_MAJOR).dylib
34+
else
35+
SOEXT = so
36+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
37+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
38+
LINKSHARED += -shared -Wl,-soname,libtree-sitter.so.$(SONAME_MAJOR)
39+
endif
40+
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
41+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
42+
endif
43+
44+
all: libtree-sitter.a libtree-sitter.$(SOEXTVER)
45+
46+
libtree-sitter.a: $(OBJ)
47+
$(AR) rcs $@ $^
48+
49+
libtree-sitter.$(SOEXTVER): $(OBJ)
50+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
51+
ln -sf $@ libtree-sitter.$(SOEXT)
52+
ln -sf $@ libtree-sitter.$(SOEXTVER_MAJOR)
53+
54+
install: all
55+
install -d '$(DESTDIR)$(LIBDIR)'
56+
install -m755 libtree-sitter.a '$(DESTDIR)$(LIBDIR)'/libtree-sitter.a
57+
install -m755 libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXTVER)
58+
ln -sf libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXTVER_MAJOR)
59+
ln -sf libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXT)
60+
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
61+
install -m644 lib/include/tree_sitter/*.h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
62+
install -d '$(DESTDIR)$(PCLIBDIR)'
63+
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \
64+
-e 's|=$(PREFIX)|=$${prefix}|' \
65+
-e 's|@PREFIX@|$(PREFIX)|' \
66+
tree-sitter.pc.in > '$(DESTDIR)$(PCLIBDIR)'/tree-sitter.pc
67+
68+
clean:
69+
rm -f lib/src/*.o libtree-sitter.a libtree-sitter.$(SOEXT) libtree-sitter.$(SOEXTVER_MAJOR) libtree-sitter.$(SOEXTVER)
70+
71+
.PHONY: all install clean

tree-sitter.pc.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@PREFIX@
2+
libdir=@LIBDIR@
3+
includedir=@INCLUDEDIR@
4+
5+
Name: tree-sitter
6+
Description: An incremental parsing system for programming tools
7+
URL: https://tree-sitter.github.io/
8+
Version: @VERSION@
9+
Libs: -L${libdir} -ltree-sitter
10+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)