From 939e61c58de00342d4fd56df7a1c60948a03ca3f Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Mon, 30 Sep 2024 18:28:33 +0300 Subject: [PATCH] build(bindings): add CMakeLists.txt file --- cli/src/init.rs | 9 +++++ cli/src/templates/PARSER_NAME.pc.in | 16 ++++----- cli/src/templates/cmakelists.txt | 54 +++++++++++++++++++++++++++++ cli/src/templates/makefile | 44 +++++++---------------- 4 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 cli/src/templates/cmakelists.txt diff --git a/cli/src/init.rs b/cli/src/init.rs index 80341cc2..484e2835 100644 --- a/cli/src/init.rs +++ b/cli/src/init.rs @@ -22,6 +22,9 @@ use url::Url; const CLI_VERSION: &str = env!("CARGO_PKG_VERSION"); const CLI_VERSION_PLACEHOLDER: &str = "CLI_VERSION"; +const ABI_VERSION_MAX: usize = tree_sitter::LANGUAGE_VERSION; +const ABI_VERSION_MAX_PLACEHOLDER: &str = "ABI_VERSION_MAX"; + const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME"; const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME"; const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME"; @@ -74,6 +77,7 @@ const BINDING_GYP_TEMPLATE: &str = include_str!("./templates/binding.gyp"); const BINDING_TEST_JS_TEMPLATE: &str = include_str!("./templates/binding_test.js"); const MAKEFILE_TEMPLATE: &str = include_str!("./templates/makefile"); +const CMAKELISTS_TXT_TEMPLATE: &str = include_str!("./templates/cmakelists.txt"); const PARSER_NAME_H_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.h"); const PARSER_NAME_PC_IN_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.pc.in"); @@ -746,6 +750,10 @@ pub fn generate_grammar_files( generate_file(path, MAKEFILE_TEMPLATE, language_name, &generate_opts) })?; + missing_path(repo_path.join("CMakeLists.txt"), |path| { + generate_file(path, CMAKELISTS_TXT_TEMPLATE, language_name, &generate_opts) + })?; + Ok(()) })?; } @@ -954,6 +962,7 @@ fn generate_file( .replace(PARSER_NAME_PLACEHOLDER, language_name) .replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION) .replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION) + .replace(ABI_VERSION_MAX_PLACEHOLDER, &ABI_VERSION_MAX.to_string()) .replace( PARSER_VERSION_PLACEHOLDER, &generate_opts.version.to_string(), diff --git a/cli/src/templates/PARSER_NAME.pc.in b/cli/src/templates/PARSER_NAME.pc.in index e0dbee0b..8567e9e2 100644 --- a/cli/src/templates/PARSER_NAME.pc.in +++ b/cli/src/templates/PARSER_NAME.pc.in @@ -1,11 +1,11 @@ -prefix=@PREFIX@ -libdir=@LIBDIR@ -includedir=@INCLUDEDIR@ +prefix=@CMAKE_INSTALL_PREFIX@ +libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ Name: tree-sitter-PARSER_NAME -Description: PARSER_DESCRIPTION -URL: @URL@ -Version: @VERSION@ -Requires: @REQUIRES@ -Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-PARSER_NAME +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Requires: @TS_REQUIRES@ +Libs: -L${libdir} -ltree-sitter-PARSER_NAME Cflags: -I${includedir} diff --git a/cli/src/templates/cmakelists.txt b/cli/src/templates/cmakelists.txt new file mode 100644 index 00000000..0a671697 --- /dev/null +++ b/cli/src/templates/cmakelists.txt @@ -0,0 +1,54 @@ +cmake_minimum_required(VERSION 3.13) + +project(tree-sitter-PARSER_NAME + VERSION "0.0.1" + DESCRIPTION "CAMEL_PARSER_NAME grammar for tree-sitter" + HOMEPAGE_URL "https://github.com/tree-sitter/tree-sitter-PARSER_NAME" + LANGUAGES C) + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +set(TREE_SITTER_ABI_VERSION ABI_VERSION_MAX CACHE STRING "Tree-sitter ABI version") +if(NOT ${TREE_SITTER_ABI_VERSION} MATCHES "^[0-9]+$") + unset(TREE_SITTER_ABI_VERSION CACHE) + message(FATAL_ERROR "TREE_SITTER_ABI_VERSION must be an integer") +endif() + +find_program(TREE_SITTER_CLI tree-sitter DOC "Tree-sitter CLI") + +add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json" + COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json + --abi=${TREE_SITTER_ABI_VERSION} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Generating parser.c") + +add_library(tree-sitter-PARSER_NAME src/parser.c) +if(EXISTS src/scanner.c) + target_sources(tree-sitter-PARSER_NAME PRIVATE src/scanner.c) +endif() +target_include_directories(tree-sitter-PARSER_NAME PRIVATE src) + +set_target_properties(tree-sitter-PARSER_NAME + PROPERTIES + C_STANDARD 11 + POSITION_INDEPENDENT_CODE ON + SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}") + +configure_file(bindings/c/tree-sitter-PARSER_NAME.pc.in + "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc" @ONLY) + +include(GNUInstallDirs) + +install(FILES bindings/c/tree-sitter-PARSER_NAME.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") +install(TARGETS tree-sitter-PARSER_NAME + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") + +add_custom_target(test "${TREE_SITTER_CLI}" test + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "tree-sitter test") + +# vim:ft=cmake: diff --git a/cli/src/templates/makefile b/cli/src/templates/makefile index 18ee4c8c..c9bb86bc 100644 --- a/cli/src/templates/makefile +++ b/cli/src/templates/makefile @@ -2,23 +2,13 @@ ifeq ($(OS),Windows_NT) $(error Windows is not supported) endif -VERSION := PARSER_VERSION - LANGUAGE_NAME := tree-sitter-PARSER_NAME +HOMEPAGE_URL := PARSER_URL +VERSION := PARSER_VERSION # repository SRC_DIR := src -PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null) - -ifeq ($(PARSER_URL),) - PARSER_URL := $(subst .git,,$(PARSER_REPO_URL)) -ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),) - PARSER_URL := $(subst :,/,$(PARSER_URL)) - PARSER_URL := $(subst git@,https://,$(PARSER_URL)) -endif -endif - TS ?= tree-sitter # install directory layout @@ -37,28 +27,20 @@ ARFLAGS ?= rcs override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC # ABI versioning -SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION))) -SONAME_MINOR := $(shell sed -n 's/#define LANGUAGE_VERSION //p' $(PARSER)) +SONAME_MAJOR = $(shell sed -n 's/\#define LANGUAGE_VERSION //p' $(PARSER)) +SONAME_MINOR = $(word 1,$(subst ., ,$(VERSION))) # OS-specific bits ifeq ($(shell uname),Darwin) SOEXT = dylib SOEXTVER_MAJOR = $(SONAME_MAJOR).$(SOEXT) SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).$(SOEXT) - LINKSHARED := $(LINKSHARED)-dynamiclib -Wl, - ifneq ($(ADDITIONAL_LIBS),) - LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS), - endif - LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks + LINKSHARED = -dynamiclib -Wl,-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks else SOEXT = so SOEXTVER_MAJOR = $(SOEXT).$(SONAME_MAJOR) SOEXTVER = $(SOEXT).$(SONAME_MAJOR).$(SONAME_MINOR) - LINKSHARED := $(LINKSHARED)-shared -Wl, - ifneq ($(ADDITIONAL_LIBS),) - LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS) - endif - LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) + LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) endif ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),) PCLIBDIR := $(PREFIX)/libdata/pkgconfig @@ -76,14 +58,12 @@ ifneq ($(STRIP),) endif $(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in - sed -e 's|@URL@|$(PARSER_URL)|' \ - -e 's|@VERSION@|$(VERSION)|' \ - -e 's|@LIBDIR@|$(LIBDIR)|' \ - -e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \ - -e 's|@REQUIRES@|$(REQUIRES)|' \ - -e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \ - -e 's|=$(PREFIX)|=$${prefix}|' \ - -e 's|@PREFIX@|$(PREFIX)|' $< > $@ + sed -e 's|@PROJECT_VERSION@|$(VERSION)|' \ + -e 's|@CMAKE_INSTALL_LIBDIR@|$(LIBDIR)|' \ + -e 's|@CMAKE_INSTALL_INCLUDEDIR@|$(INCLUDEDIR)|' \ + -e 's|@PROJECT_DESCRIPTION@|$(DESCRIPTION)|' \ + -e 's|@PROJECT_HOMEPAGE_URL@|$(HOMEPAGE_URL)|' \ + -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|' $< > $@ $(PARSER): $(SRC_DIR)/grammar.json $(TS) generate $^