Skip to content

Commit

Permalink
Add a simple Makefile-based build system.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eli-schwartz committed Apr 22, 2020
1 parent 1635aab commit af498bc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
VERSION := 0.6.3

# install directory layout
PREFIX ?= /usr/local
INCLUDEDIR ?= $(PREFIX)/include
LIBDIR ?= $(PREFIX)/lib
PCLIBDIR ?= $(LIBDIR)/pkgconfig

# collect sources
ifneq ($(AMALGAMATED),1)
SRC := $(wildcard lib/src/*.c)
# do not double-include amalgamation
SRC := $(filter-out lib/src/lib.c,$(SRC))
else
# use amalgamated build
SRC := lib/src/lib.c
endif
OBJ := $(SRC:.c=.o)

# define default flags, and override to append mandatory flags
CFLAGS ?= -O3
override CFLAGS += -std=gnu99 -fPIC -Ilib/src -Ilib/include

# ABI versioning
SONAME_MAJOR := 0
SONAME_MINOR := 0

# OS-specific bits
ifeq ($(shell uname),Darwin)
SOEXT = dylib
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
LINKSHARED += -dynamiclib -Wl,-install_name,$(LIBDIR)/libtree-sitter.$(SONAME_MAJOR).dylib
else
SOEXT = so
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
LINKSHARED += -shared -Wl,-soname,libtree-sitter.so.$(SONAME_MAJOR)
endif
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
endif

all: libtree-sitter.a libtree-sitter.$(SOEXTVER)

libtree-sitter.a: $(OBJ)
$(AR) rcs $@ $^

libtree-sitter.$(SOEXTVER): $(OBJ)
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
ln -sf $@ libtree-sitter.$(SOEXT)
ln -sf $@ libtree-sitter.$(SOEXTVER_MAJOR)

install: all
install -d '$(DESTDIR)$(LIBDIR)'
install -m755 libtree-sitter.a '$(DESTDIR)$(LIBDIR)'/libtree-sitter.a
install -m755 libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXTVER)
ln -sf libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXTVER_MAJOR)
ln -sf libtree-sitter.$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter.$(SOEXT)
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
install -m644 lib/include/tree_sitter/*.h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
install -d '$(DESTDIR)$(PCLIBDIR)'
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \
-e 's|=$(PREFIX)|=$${prefix}|' \
-e 's|@PREFIX@|$(PREFIX)|' \
tree-sitter.pc.in > '$(DESTDIR)$(PCLIBDIR)'/tree-sitter.pc

clean:
rm -f lib/src/*.o libtree-sitter.a libtree-sitter.$(SOEXT) libtree-sitter.$(SOEXTVER_MAJOR) libtree-sitter.$(SOEXTVER)

.PHONY: all install clean
10 changes: 10 additions & 0 deletions tree-sitter.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@

Name: tree-sitter
Description: An incremental parsing system for programming tools
URL: https://tree-sitter.github.io/
Version: @VERSION@
Libs: -L${libdir} -ltree-sitter
Cflags: -I${includedir}

1 comment on commit af498bc

@Grumpy2869

This comment was marked as spam.

Please sign in to comment.