|
| 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 |
0 commit comments