From 853d0712c3a822884c8885d03cc4e73d447bf6fe Mon Sep 17 00:00:00 2001 From: toge Date: Wed, 6 Nov 2024 00:04:05 +0900 Subject: [PATCH] tree-sitter-sql: add recipe (#25690) Co-authored-by: Uilian Ries --- recipes/tree-sitter-sql/all/CMakeLists.txt | 30 ++++++++ recipes/tree-sitter-sql/all/conandata.yml | 7 ++ recipes/tree-sitter-sql/all/conanfile.py | 71 +++++++++++++++++++ .../all/test_package/CMakeLists.txt | 8 +++ .../all/test_package/conanfile.py | 30 ++++++++ .../all/test_package/test_package.c | 26 +++++++ recipes/tree-sitter-sql/config.yml | 3 + 7 files changed, 175 insertions(+) create mode 100644 recipes/tree-sitter-sql/all/CMakeLists.txt create mode 100644 recipes/tree-sitter-sql/all/conandata.yml create mode 100644 recipes/tree-sitter-sql/all/conanfile.py create mode 100644 recipes/tree-sitter-sql/all/test_package/CMakeLists.txt create mode 100644 recipes/tree-sitter-sql/all/test_package/conanfile.py create mode 100644 recipes/tree-sitter-sql/all/test_package/test_package.c create mode 100644 recipes/tree-sitter-sql/config.yml diff --git a/recipes/tree-sitter-sql/all/CMakeLists.txt b/recipes/tree-sitter-sql/all/CMakeLists.txt new file mode 100644 index 0000000000000..2c51456eb9a4e --- /dev/null +++ b/recipes/tree-sitter-sql/all/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.15) +project(tree-sitter-sql LANGUAGES C) + +find_package(tree-sitter REQUIRED CONFIG) + +add_library(${PROJECT_NAME} + ${TREE_SITTER_SQL_SRC_DIR}/src/scanner.c + ${TREE_SITTER_SQL_SRC_DIR}/src/parser.c +) +target_link_libraries(${PROJECT_NAME} + PUBLIC + tree-sitter::tree-sitter +) +target_include_directories(${PROJECT_NAME} + PRIVATE + $ +) +set_target_properties(${PROJECT_NAME} + PROPERTIES + C_STANDARD 99 + PUBLIC_HEADER "${TREE_SITTER_SQL_SRC_DIR}/bindings/c/tree-sitter-sql.h" +) + +include(GNUInstallDirs) +install(TARGETS ${PROJECT_NAME} + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) diff --git a/recipes/tree-sitter-sql/all/conandata.yml b/recipes/tree-sitter-sql/all/conandata.yml new file mode 100644 index 0000000000000..89e867a3cdb74 --- /dev/null +++ b/recipes/tree-sitter-sql/all/conandata.yml @@ -0,0 +1,7 @@ +sources: + # As per the official README, the release tarballs don't include the generated codes. + # > We don't commit the generated parser files to the main branch. Instead, you can find them on the gh-pages branch. + # We have to obtain the corresponding snapshot of the github-pages branch. + "0.3.5": + url: "https://github.com/DerekStride/tree-sitter-sql/archive/c67ecbd37d8d12f22e4cc7138afd14bc20253e10.tar.gz" + sha256: "d8cd967ca4daa376614995fe8a439a957a303c7ca5b9858ca15ad243e6b176d3" diff --git a/recipes/tree-sitter-sql/all/conanfile.py b/recipes/tree-sitter-sql/all/conanfile.py new file mode 100644 index 0000000000000..59cc675b453aa --- /dev/null +++ b/recipes/tree-sitter-sql/all/conanfile.py @@ -0,0 +1,71 @@ +from conan import ConanFile +from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout +from conan.tools.files import copy, get +from conan.tools.microsoft import is_msvc +import os + + +required_conan_version = ">=1.53.0" + + +class TreeSitterSqlConan(ConanFile): + name = "tree-sitter-sql" + description = "SQL grammar for tree-sitter" + license = "MIT" + url = "https://github.com/conan-io/conan-center-index" + homepage = "https://github.com/DerekStride/tree-sitter-sql" + topics = ("tree-sitter", "sql", "parser") + package_type = "library" + settings = "os", "arch", "compiler", "build_type" + options = { + "shared": [True, False], + "fPIC": [True, False], + } + default_options = { + "shared": False, + "fPIC": True, + } + exports_sources = ["CMakeLists.txt"] + + def config_options(self): + if self.settings.os == "Windows": + del self.options.fPIC + + def configure(self): + if is_msvc(self): + del self.options.shared + self.package_type = "static-library" + if self.options.get_safe("shared"): + self.options.rm_safe("fPIC") + self.settings.rm_safe("compiler.cppstd") + self.settings.rm_safe("compiler.libcxx") + + def layout(self): + cmake_layout(self, src_folder="src") + + def requirements(self): + self.requires("tree-sitter/0.24.3", transitive_headers=True) + + def source(self): + get(self, **self.conan_data["sources"][self.version], strip_root=True) + + def generate(self): + tc = CMakeToolchain(self) + tc.variables["TREE_SITTER_SQL_SRC_DIR"] = self.source_folder.replace("\\", "/") + tc.generate() + tc = CMakeDeps(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir)) + cmake.build() + + def package(self): + copy(self, "LICENSE", self.source_folder, os.path.join(self.package_folder, "licenses")) + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = ["tree-sitter-sql"] + self.cpp_info.set_property("pkg_config_name", "tree-sitter-sql") diff --git a/recipes/tree-sitter-sql/all/test_package/CMakeLists.txt b/recipes/tree-sitter-sql/all/test_package/CMakeLists.txt new file mode 100644 index 0000000000000..349ad00bbfc6d --- /dev/null +++ b/recipes/tree-sitter-sql/all/test_package/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.15) +project(test_package LANGUAGES C) + +find_package(PkgConfig) +pkg_check_modules(TREE_SITTER_SQL IMPORTED_TARGET "tree-sitter-sql") + +add_executable(${PROJECT_NAME} test_package.c) +target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::TREE_SITTER_SQL) diff --git a/recipes/tree-sitter-sql/all/test_package/conanfile.py b/recipes/tree-sitter-sql/all/test_package/conanfile.py new file mode 100644 index 0000000000000..e8f597c20ddd4 --- /dev/null +++ b/recipes/tree-sitter-sql/all/test_package/conanfile.py @@ -0,0 +1,30 @@ +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import cmake_layout, CMake +import os + + +class TestPackageConan(ConanFile): + settings = "os", "arch", "compiler", "build_type" + generators = "PkgConfigDeps", "CMakeToolchain", "VirtualRunEnv" + test_type = "explicit" + + def requirements(self): + self.requires(self.tested_reference_str) + + def layout(self): + cmake_layout(self) + + def build_requirements(self): + if not self.conf.get("tools.gnu:pkg_config", check_type=str): + self.tool_requires("pkgconf/[>=2.2 <3]") + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if can_run(self): + bin_path = os.path.join(self.cpp.build.bindir, "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/tree-sitter-sql/all/test_package/test_package.c b/recipes/tree-sitter-sql/all/test_package/test_package.c new file mode 100644 index 0000000000000..7b528742c2577 --- /dev/null +++ b/recipes/tree-sitter-sql/all/test_package/test_package.c @@ -0,0 +1,26 @@ +#include +#include + +#include "tree_sitter/api.h" +#include "tree-sitter-sql.h" + +int main() { + TSParser *parser = ts_parser_new(); + ts_parser_set_language(parser, tree_sitter_sql()); + const char *source_code = "select * from dummy_table;\n"; + TSTree *tree = ts_parser_parse_string( + parser, + NULL, + source_code, + strlen(source_code) + ); + TSNode root_node = ts_tree_root_node(tree); + + char *string = ts_node_string(root_node); + printf("Syntax tree: %s\n", string); + free(string); + + ts_tree_delete(tree); + ts_parser_delete(parser); + return 0; +} diff --git a/recipes/tree-sitter-sql/config.yml b/recipes/tree-sitter-sql/config.yml new file mode 100644 index 0000000000000..ef7972c36ec69 --- /dev/null +++ b/recipes/tree-sitter-sql/config.yml @@ -0,0 +1,3 @@ +versions: + "0.3.5": + folder: all