Files
codeql/rust/ast-generator/BUILD.bazel
Paolo Tranquilli 290a1043b1 Rust: fetch ungram and rust-analyzer code instead of checking it in
* The ungram file is now taken from the rust-analyzer dependencies
  pulled in by bazel
* the grammar parsing code is not published, so it must be taken
  directly from rust-analyzer code. That part should be less prone to be
  updated than the ungram file, so it does not necessarily need to be
  in sync with the rust-analyzer version is used elsewhere.
* both need some patches. The former is patched during build, the latter
  during loading in `MODULE.bazel`.
2024-12-18 16:37:24 +01:00

93 lines
2.3 KiB
Python

load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
load("//misc/bazel/3rdparty/tree_sitter_extractors_deps:defs.bzl", "aliases", "all_crate_deps", "crate_deps")
(ra_ap_syntax_label,) = crate_deps(
["ra_ap_syntax"],
"rust/extractor",
)
ra_ap_syntax_workspace, _, _ = str(ra_ap_syntax_label).partition("//")
ungram_source = "%s//:rust.ungram" % ra_ap_syntax_workspace
genrule(
name = "ungram",
srcs = [
ungram_source,
"patches/rust.ungram.patch",
],
outs = ["rust.ungram"],
cmd = "\n".join([
"cp $(location %s) $@" % ungram_source,
"patch $@ $(location patches/rust.ungram.patch)",
]),
visibility = ["//rust/codegen:__pkg__"],
)
_codegen = [
"grammar.rs",
"grammar/ast_src.rs",
]
_codegen_srcs = ["@rust-analyzer-src//:xtask/src/codegen/%s" % f for f in _codegen]
_codegen_outs = ["src/codegen/%s" % f for f in _codegen]
genrule(
name = "codegen",
srcs = _codegen_srcs,
outs = _codegen_outs,
cmd = "\n".join(
["mkdir -p $(RULEDIR)/src/codegen/grammar"] +
[
"cp $(location %s) $(RULEDIR)/%s" % item
for item in zip(_codegen_srcs, _codegen_outs)
],
),
)
codeql_rust_binary(
name = "ast-generator",
srcs = glob(
["src/**/*.rs"],
exclude = ["src/codegen/**"],
) + [":codegen"],
aliases = aliases(),
args = ["$(rlocationpath :ungram)"],
data = [":ungram"],
proc_macro_deps = all_crate_deps(
proc_macro = True,
),
visibility = ["//rust:__subpackages__"],
deps = all_crate_deps(
normal = True,
),
)
write_file(
name = "update",
out = "update.sh",
content = [
"#!/bin/bash",
". misc/bazel/runfiles.sh",
'DST_DIR="$(dirname "$(rlocation "$1")")"',
'mkdir -p "$DST_DIR/src/codegen/grammar"',
] + [
'cp "$(rlocation "$%s")" "$DST_DIR/%s"' % item
for item in enumerate(_codegen_outs, 2)
],
is_executable = True,
)
sh_binary(
name = "inject-sources",
srcs = [":update"],
args = ["$(rlocationpath Cargo.toml)"] + ["$(rlocationpath %s)" % f for f in _codegen_outs],
data = ["Cargo.toml"] + _codegen_outs,
deps = ["//misc/bazel:sh_runfiles"],
)
exports_files(["Cargo.toml"])