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