From 91243fa54122be62b253eb0cd483a31f38b00d06 Mon Sep 17 00:00:00 2001 From: Kuat Yessenov Date: Wed, 1 Jun 2022 20:59:07 -0700 Subject: [PATCH] antlr: drop rules_antlr for custom rules Signed-off-by: Kuat Yessenov --- bazel/BUILD | 8 +++++ bazel/antlr.bzl | 73 +++++++++++++++++++++++++++++++++++------ bazel/deps.bzl | 23 +++++++------ bazel/deps_extra.bzl | 2 -- parser/parser.cc | 6 ++-- parser/source_factory.h | 2 +- 6 files changed, 86 insertions(+), 28 deletions(-) diff --git a/bazel/BUILD b/bazel/BUILD index ffd0fb0cd..f95444438 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -1 +1,9 @@ package(default_visibility = ["//visibility:public"]) + +load("@rules_java//java:defs.bzl", "java_binary") + +java_binary( + name = "antlr4_tool", + runtime_deps = ["@antlr4_jar//jar"], + main_class = "org.antlr.v4.Tool", +) diff --git a/bazel/antlr.bzl b/bazel/antlr.bzl index ea5520582..ec110c599 100644 --- a/bazel/antlr.bzl +++ b/bazel/antlr.bzl @@ -16,25 +16,18 @@ Generate C++ parser and lexer from a grammar file. """ -load("@rules_antlr//antlr:antlr4.bzl", "antlr") - -def antlr_cc_library(name, src, package = None, listener = False, visitor = True): +def antlr_cc_library(name, src, package): """Creates a C++ lexer and parser from a source grammar. Args: name: Base name for the lexer and the parser rules. src: source ANTLR grammar file package: The namespace for the generated code - listener: generate ANTLR listener (default: False) - visitor: generate ANTLR visitor (default: True) """ generated = name + "_grammar" - antlr( + antlr_library( name = generated, - srcs = [src], - language = "Cpp", - listener = listener, - visitor = visitor, + src = src, package = package, ) native.cc_library( @@ -46,3 +39,63 @@ def antlr_cc_library(name, src, package = None, listener = False, visitor = True ], linkstatic = 1, ) + +def _antlr_library(ctx): + output = ctx.actions.declare_directory(ctx.attr.name) + + antlr_args = ctx.actions.args() + antlr_args.add("-Dlanguage=Cpp") + antlr_args.add("-no-listener") + antlr_args.add("-visitor") + antlr_args.add("-o", output.path) + antlr_args.add("-package", ctx.attr.package) + antlr_args.add(ctx.file.src) + + basename = ctx.file.src.basename[:-3] + suffixes = ["Lexer", "Parser", "BaseVisitor", "Visitor"] + + ctx.actions.run( + arguments = [antlr_args], + inputs = [ctx.file.src], + outputs = [output], + executable = ctx.executable._tool, + progress_message = "Processing ANTLR grammar", + ) + + files = [] + for suffix in suffixes: + header = ctx.actions.declare_file(basename + suffix + ".h") + source = ctx.actions.declare_file(basename + suffix + ".cpp") + generated = output.path + "/" + ctx.file.src.short_path[:-3] + suffix + + ctx.actions.run_shell( + mnemonic = "CopyHeader" + suffix, + inputs = [output], + outputs = [header], + command = 'cp "{generated}" "{out}"'.format(generated = generated + ".h", out = header.path), + ) + ctx.actions.run_shell( + mnemonic = "CopySource" + suffix, + inputs = [output], + outputs = [source], + command = 'cp "{generated}" "{out}"'.format(generated = generated + ".cpp", out = source.path), + ) + + files.append(header) + files.append(source) + + compilation_context = cc_common.create_compilation_context(headers = depset(files)) + return [DefaultInfo(files = depset(files)), CcInfo(compilation_context = compilation_context)] + +antlr_library = rule( + implementation = _antlr_library, + attrs = { + "src": attr.label(allow_single_file = [".g4"], mandatory = True), + "package": attr.string(), + "_tool": attr.label( + executable = True, + cfg = "host", + default = Label("//bazel:antlr4_tool"), + ), + }, +) diff --git a/bazel/deps.bzl b/bazel/deps.bzl index 0edf314df..abe35fdfc 100644 --- a/bazel/deps.bzl +++ b/bazel/deps.bzl @@ -2,7 +2,7 @@ Main dependencies of cel-cpp. """ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar") def base_deps(): """Base evaluator and test dependencies.""" @@ -69,15 +69,9 @@ def base_deps(): def parser_deps(): """ANTLR dependency for the parser.""" - http_archive( - name = "rules_antlr", - sha256 = "26e6a83c665cf6c1093b628b3a749071322f0f70305d12ede30909695ed85591", - strip_prefix = "rules_antlr-0.5.0", - urls = ["https://github.com/marcohu/rules_antlr/archive/0.5.0.tar.gz"], - ) + # Apr 15, 2022 + ANTLR4_VERSION = "4.10.1" - ANTLR4_RUNTIME_GIT_SHA = "70b2edcf98eb612a92d3dbaedb2ce0b69533b0cb" # Dec 7, 2021 - ANTLR4_RUNTIME_SHA = "fae73909f95e1320701e29ac03bab9233293fb5b90d3ce857279f1b46b614c83" http_archive( name = "antlr4_runtimes", build_file_content = """ @@ -89,9 +83,14 @@ cc_library( includes = ["runtime/Cpp/runtime/src"], ) """, - sha256 = ANTLR4_RUNTIME_SHA, - strip_prefix = "antlr4-" + ANTLR4_RUNTIME_GIT_SHA, - urls = ["https://github.com/antlr/antlr4/archive/" + ANTLR4_RUNTIME_GIT_SHA + ".tar.gz"], + sha256 = "a320568b738e42735946bebc5d9d333170e14a251c5734e8b852ad1502efa8a2", + strip_prefix = "antlr4-" + ANTLR4_VERSION, + urls = ["https://github.com/antlr/antlr4/archive/v" + ANTLR4_VERSION + ".tar.gz"], + ) + http_jar( + name = "antlr4_jar", + urls = ["https://www.antlr.org/download/antlr-" + ANTLR4_VERSION + "-complete.jar"], + sha256 = "41949d41f20d31d5b8277187735dd755108df52b38db6c865108d3382040f918", ) def flatbuffers_deps(): diff --git a/bazel/deps_extra.bzl b/bazel/deps_extra.bzl index 76cb8c5d6..40a47f01b 100644 --- a/bazel/deps_extra.bzl +++ b/bazel/deps_extra.bzl @@ -4,7 +4,6 @@ Transitive dependencies. load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language") -load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_dependencies") load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") @@ -50,5 +49,4 @@ def cel_cpp_deps_extra(): cc = True, go = True, # cel-spec requirement ) - rules_antlr_dependencies("4.8") cel_spec_deps_extra() diff --git a/parser/parser.cc b/parser/parser.cc index 588f36d48..0fc1db41a 100644 --- a/parser/parser.cc +++ b/parser/parser.cc @@ -43,9 +43,9 @@ #include "internal/strings.h" #include "internal/unicode.h" #include "internal/utf8.h" -#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelBaseVisitor.h" -#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelLexer.h" -#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelParser.h" +#include "parser/internal/CelBaseVisitor.h" +#include "parser/internal/CelLexer.h" +#include "parser/internal/CelParser.h" #include "parser/macro.h" #include "parser/options.h" #include "parser/source_factory.h" diff --git a/parser/source_factory.h b/parser/source_factory.h index 8d21f59d3..a9fe01a6e 100644 --- a/parser/source_factory.h +++ b/parser/source_factory.h @@ -24,7 +24,7 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "antlr4-runtime.h" -#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelParser.h" +#include "parser/internal/CelParser.h" namespace google::api::expr::parser {