From af498bc0c37e212a3823d10c2cd87d64aa06d434 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 21 Apr 2020 00:05:01 -0400 Subject: [PATCH] Add a simple Makefile-based build system. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Makefile | 71 +++++++++++++++++++++++++++++++++++++++++++++++ tree-sitter.pc.in | 10 +++++++ 2 files changed, 81 insertions(+) create mode 100644 Makefile create mode 100644 tree-sitter.pc.in diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..95f53b9a --- /dev/null +++ b/Makefile @@ -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 diff --git a/tree-sitter.pc.in b/tree-sitter.pc.in new file mode 100644 index 00000000..f98816cb --- /dev/null +++ b/tree-sitter.pc.in @@ -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}