Skip to content

Commit 187339d

Browse files
paldepindCopilot
andcommitted
C++: Build BMN extractor with Bazel
Add a public Bazel target for the Rust build-mode-none extractor, expose its generated crate universe through the standalone module, and resolve test resources through Bazel's TEST_SRCDIR runfiles root. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d202783 commit 187339d

4 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎MODULE.bazel‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ use_repo(
9898
"vendor_py__tree-sitter-graph-0.12.0",
9999
)
100100

101+
# deps for C/C++ build mode none extractor
102+
# keep in sync by running `misc/bazel/3rdparty/update_cpp_bmn_deps.sh`
103+
cpp_bmn_deps = use_extension("//misc/bazel/3rdparty:cpp_bmn_deps_extension.bzl", "b")
104+
use_repo(
105+
cpp_bmn_deps,
106+
"vendor_cpp_bmn__all_asserts-2.3.3",
107+
"vendor_cpp_bmn__anyhow-1.0.98",
108+
"vendor_cpp_bmn__cached-0.54.0",
109+
"vendor_cpp_bmn__chrono-0.4.41",
110+
"vendor_cpp_bmn__itertools-0.14.0",
111+
"vendor_cpp_bmn__num_cpus-1.17.0",
112+
"vendor_cpp_bmn__os_str_bytes-7.1.1",
113+
"vendor_cpp_bmn__rayon-1.11.0",
114+
"vendor_cpp_bmn__regex-1.11.1",
115+
"vendor_cpp_bmn__serde-1.0.219",
116+
"vendor_cpp_bmn__serde_json-1.0.140",
117+
"vendor_cpp_bmn__serde_with-1.14.0",
118+
"vendor_cpp_bmn__tempfile-3.20.0",
119+
"vendor_cpp_bmn__walkdir-2.5.0",
120+
"vendor_cpp_bmn__which-8.0.0",
121+
"vendor_cpp_bmn__zip-6.0.0",
122+
)
123+
101124
# deps for ruby+rust
102125
# keep in sync by running `misc/bazel/3rdparty/update_cargo_deps.sh`
103126
tree_sitter_extractors_deps = use_extension("//misc/bazel/3rdparty:tree_sitter_extractors_extension.bzl", "r")

‎cpp/extractor/bmn/BUILD.bazel‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_test")
2+
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
3+
load("//misc/bazel/3rdparty/cpp_bmn_deps:defs.bzl", "all_crate_deps")
4+
15
exports_files([
26
"Cargo.lock",
37
"Cargo.toml",
48
])
9+
10+
codeql_rust_binary(
11+
name = "bmn",
12+
srcs = glob(["src/**/*.rs"]),
13+
proc_macro_deps = all_crate_deps(proc_macro = True),
14+
visibility = ["//visibility:public"],
15+
deps = all_crate_deps(normal = True),
16+
)
17+
18+
rust_test(
19+
name = "unit_tests",
20+
crate = ":single_arch/bmn",
21+
data = glob([
22+
"tests/resources/**",
23+
"Cargo.toml",
24+
]),
25+
env = {
26+
"BMN_TEST_MANIFEST": "$(rlocationpath :Cargo.toml)",
27+
},
28+
)
29+
30+
test_suite(
31+
name = "tests",
32+
tests = [":unit_tests"],
33+
)

‎cpp/extractor/bmn/src/test_utils.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
use all_asserts::assert_true;
2-
use std::fs;
32
use std::fs::File;
43
use std::path::PathBuf;
5-
use tempfile::{TempDir, tempdir};
4+
use std::{env, fs};
5+
use tempfile::{tempdir, TempDir};
66

77
/// This function tries to get the root test resource path.
88
/// If `allow_symlinks` is `false` will avoid the bazel off-tree test directory as this contains
99
/// symlinks instead of the files, and we will return the folder containing the canonicalized
1010
/// `Cargo.toml` file.
1111
fn get_in_tree_resource_root(allow_symlinks: bool) -> PathBuf {
12-
// Use CARGO_MANIFEST_DIR runtime environment variable to get the path
13-
// This is the path where the resources are expected to be, but it can be the bazel out-of-tree path
14-
let manifest_path = std::env::var("CARGO_MANIFEST_DIR")
15-
.map(PathBuf::from)
16-
.expect("$CARGO_MANIFEST_DIR not set")
17-
.join("Cargo.toml");
12+
// The below ensures that we support both `bazel test` and `cargo test`.
13+
let manifest_path = if let Ok(runfiles_manifest) = env::var("BMN_TEST_MANIFEST") {
14+
let runfiles_dir = env::var("TEST_SRCDIR").expect("$TEST_SRCDIR not set");
15+
PathBuf::from(runfiles_dir).join(runfiles_manifest)
16+
} else {
17+
env::var("CARGO_MANIFEST_DIR")
18+
.map(PathBuf::from)
19+
.expect("$CARGO_MANIFEST_DIR not set")
20+
.join("Cargo.toml")
21+
};
1822

1923
let final_manifest_path = if !allow_symlinks {
2024
// If we do not want symlinks ()created by Bazel), we can canonicalize the manifest path to
@@ -120,7 +124,7 @@ mod tests {
120124
assert_true!(file_maybe_symlinked.exists());
121125
assert_true!(file_maybe_symlinked.ends_with("tests/resources/dir1/file1.c"));
122126

123-
let is_bazel = std::env::var("BAZEL_TEST")
127+
let is_bazel = env::var("BAZEL_TEST")
124128
.map(|bazel| bazel == "1")
125129
.unwrap_or(false);
126130
if is_bazel {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Module extension for using BMN's vendored crates with bzlmod."""
2+
3+
load("//misc/bazel/3rdparty/cpp_bmn_deps:defs.bzl", _crate_repositories = "crate_repositories")
4+
5+
def _crate_repositories_impl(module_ctx):
6+
direct_deps = _crate_repositories()
7+
return module_ctx.extension_metadata(
8+
root_module_direct_deps = [repo.repo for repo in direct_deps],
9+
root_module_direct_dev_deps = [],
10+
)
11+
12+
# Short name to address Windows path length issues.
13+
b = module_extension(
14+
implementation = _crate_repositories_impl,
15+
)

0 commit comments

Comments
 (0)