build(bindings): add CMakeLists.txt file
This commit is contained in:
parent
03313dbbf0
commit
939e61c58d
4 changed files with 83 additions and 40 deletions
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
54
cli/src/templates/cmakelists.txt
Normal file
54
cli/src/templates/cmakelists.txt
Normal file
|
|
@ -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:
|
||||
|
|
@ -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 $^
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue