Skip to content

Commit 939e61c

Browse files
ObserverOfTimeamaanq
authored andcommitted
build(bindings): add CMakeLists.txt file
1 parent 03313db commit 939e61c

File tree

4 files changed

+83
-40
lines changed

4 files changed

+83
-40
lines changed

cli/src/init.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use url::Url;
2222
const CLI_VERSION: &str = env!("CARGO_PKG_VERSION");
2323
const CLI_VERSION_PLACEHOLDER: &str = "CLI_VERSION";
2424

25+
const ABI_VERSION_MAX: usize = tree_sitter::LANGUAGE_VERSION;
26+
const ABI_VERSION_MAX_PLACEHOLDER: &str = "ABI_VERSION_MAX";
27+
2528
const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME";
2629
const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME";
2730
const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME";
@@ -74,6 +77,7 @@ const BINDING_GYP_TEMPLATE: &str = include_str!("./templates/binding.gyp");
7477
const BINDING_TEST_JS_TEMPLATE: &str = include_str!("./templates/binding_test.js");
7578

7679
const MAKEFILE_TEMPLATE: &str = include_str!("./templates/makefile");
80+
const CMAKELISTS_TXT_TEMPLATE: &str = include_str!("./templates/cmakelists.txt");
7781
const PARSER_NAME_H_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.h");
7882
const PARSER_NAME_PC_IN_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.pc.in");
7983

@@ -746,6 +750,10 @@ pub fn generate_grammar_files(
746750
generate_file(path, MAKEFILE_TEMPLATE, language_name, &generate_opts)
747751
})?;
748752

753+
missing_path(repo_path.join("CMakeLists.txt"), |path| {
754+
generate_file(path, CMAKELISTS_TXT_TEMPLATE, language_name, &generate_opts)
755+
})?;
756+
749757
Ok(())
750758
})?;
751759
}
@@ -954,6 +962,7 @@ fn generate_file(
954962
.replace(PARSER_NAME_PLACEHOLDER, language_name)
955963
.replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION)
956964
.replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION)
965+
.replace(ABI_VERSION_MAX_PLACEHOLDER, &ABI_VERSION_MAX.to_string())
957966
.replace(
958967
PARSER_VERSION_PLACEHOLDER,
959968
&generate_opts.version.to_string(),
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
prefix=@PREFIX@
2-
libdir=@LIBDIR@
3-
includedir=@INCLUDEDIR@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
3+
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
44

55
Name: tree-sitter-PARSER_NAME
6-
Description: PARSER_DESCRIPTION
7-
URL: @URL@
8-
Version: @VERSION@
9-
Requires: @REQUIRES@
10-
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-PARSER_NAME
6+
Description: @PROJECT_DESCRIPTION@
7+
URL: @PROJECT_HOMEPAGE_URL@
8+
Version: @PROJECT_VERSION@
9+
Requires: @TS_REQUIRES@
10+
Libs: -L${libdir} -ltree-sitter-PARSER_NAME
1111
Cflags: -I${includedir}

cli/src/templates/cmakelists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(tree-sitter-PARSER_NAME
4+
VERSION "0.0.1"
5+
DESCRIPTION "CAMEL_PARSER_NAME grammar for tree-sitter"
6+
HOMEPAGE_URL "https://github.com/tree-sitter/tree-sitter-PARSER_NAME"
7+
LANGUAGES C)
8+
9+
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
10+
11+
set(TREE_SITTER_ABI_VERSION ABI_VERSION_MAX CACHE STRING "Tree-sitter ABI version")
12+
if(NOT ${TREE_SITTER_ABI_VERSION} MATCHES "^[0-9]+$")
13+
unset(TREE_SITTER_ABI_VERSION CACHE)
14+
message(FATAL_ERROR "TREE_SITTER_ABI_VERSION must be an integer")
15+
endif()
16+
17+
find_program(TREE_SITTER_CLI tree-sitter DOC "Tree-sitter CLI")
18+
19+
add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c"
20+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json"
21+
COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json
22+
--abi=${TREE_SITTER_ABI_VERSION}
23+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
24+
COMMENT "Generating parser.c")
25+
26+
add_library(tree-sitter-PARSER_NAME src/parser.c)
27+
if(EXISTS src/scanner.c)
28+
target_sources(tree-sitter-PARSER_NAME PRIVATE src/scanner.c)
29+
endif()
30+
target_include_directories(tree-sitter-PARSER_NAME PRIVATE src)
31+
32+
set_target_properties(tree-sitter-PARSER_NAME
33+
PROPERTIES
34+
C_STANDARD 11
35+
POSITION_INDEPENDENT_CODE ON
36+
SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}")
37+
38+
configure_file(bindings/c/tree-sitter-PARSER_NAME.pc.in
39+
"${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc" @ONLY)
40+
41+
include(GNUInstallDirs)
42+
43+
install(FILES bindings/c/tree-sitter-PARSER_NAME.h
44+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter")
45+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc"
46+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
47+
install(TARGETS tree-sitter-PARSER_NAME
48+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
49+
50+
add_custom_target(test "${TREE_SITTER_CLI}" test
51+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
52+
COMMENT "tree-sitter test")
53+
54+
# vim:ft=cmake:

cli/src/templates/makefile

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ ifeq ($(OS),Windows_NT)
22
$(error Windows is not supported)
33
endif
44

5-
VERSION := PARSER_VERSION
6-
75
LANGUAGE_NAME := tree-sitter-PARSER_NAME
6+
HOMEPAGE_URL := PARSER_URL
7+
VERSION := PARSER_VERSION
88

99
# repository
1010
SRC_DIR := src
1111

12-
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
13-
14-
ifeq ($(PARSER_URL),)
15-
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
16-
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
17-
PARSER_URL := $(subst :,/,$(PARSER_URL))
18-
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
19-
endif
20-
endif
21-
2212
TS ?= tree-sitter
2313

2414
# install directory layout
@@ -37,28 +27,20 @@ ARFLAGS ?= rcs
3727
override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC
3828

3929
# ABI versioning
40-
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
41-
SONAME_MINOR := $(shell sed -n 's/#define LANGUAGE_VERSION //p' $(PARSER))
30+
SONAME_MAJOR = $(shell sed -n 's/\#define LANGUAGE_VERSION //p' $(PARSER))
31+
SONAME_MINOR = $(word 1,$(subst ., ,$(VERSION)))
4232

4333
# OS-specific bits
4434
ifeq ($(shell uname),Darwin)
4535
SOEXT = dylib
4636
SOEXTVER_MAJOR = $(SONAME_MAJOR).$(SOEXT)
4737
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).$(SOEXT)
48-
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
49-
ifneq ($(ADDITIONAL_LIBS),)
50-
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
51-
endif
52-
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks
38+
LINKSHARED = -dynamiclib -Wl,-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks
5339
else
5440
SOEXT = so
5541
SOEXTVER_MAJOR = $(SOEXT).$(SONAME_MAJOR)
5642
SOEXTVER = $(SOEXT).$(SONAME_MAJOR).$(SONAME_MINOR)
57-
LINKSHARED := $(LINKSHARED)-shared -Wl,
58-
ifneq ($(ADDITIONAL_LIBS),)
59-
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
60-
endif
61-
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).$(SOEXTVER)
43+
LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER)
6244
endif
6345
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
6446
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
@@ -76,14 +58,12 @@ ifneq ($(STRIP),)
7658
endif
7759

7860
$(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
79-
sed -e 's|@URL@|$(PARSER_URL)|' \
80-
-e 's|@VERSION@|$(VERSION)|' \
81-
-e 's|@LIBDIR@|$(LIBDIR)|' \
82-
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \
83-
-e 's|@REQUIRES@|$(REQUIRES)|' \
84-
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \
85-
-e 's|=$(PREFIX)|=$${prefix}|' \
86-
-e 's|@PREFIX@|$(PREFIX)|' $< > $@
61+
sed -e 's|@PROJECT_VERSION@|$(VERSION)|' \
62+
-e 's|@CMAKE_INSTALL_LIBDIR@|$(LIBDIR)|' \
63+
-e 's|@CMAKE_INSTALL_INCLUDEDIR@|$(INCLUDEDIR)|' \
64+
-e 's|@PROJECT_DESCRIPTION@|$(DESCRIPTION)|' \
65+
-e 's|@PROJECT_HOMEPAGE_URL@|$(HOMEPAGE_URL)|' \
66+
-e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|' $< > $@
8767

8868
$(PARSER): $(SRC_DIR)/grammar.json
8969
$(TS) generate $^

0 commit comments

Comments
 (0)