Skip to content

Commit ee3b452

Browse files
scentiniVinh Tran
authored and
Vinh Tran
committed
Merge main
Drop import macro (bazelbuild#2411) As discussed over at bazelbuild#2383 Fix rustfmt toolchains when consuming rules_rust with bzlmod. (bazelbuild#2410) Fixes bazelbuild#2260 Provide a better error message when trying to generate rust-project.json (bazelbuild#2196) Currently when trying to generate a `rust-project.json`, if there aren't actually any Rust targets defined, the script mysteriously fails. This adds a better error message. Update android example to use Starlark version of android_ndk_repository (bazelbuild#2417) The native version of `android_ndk_repository` rule no longer work with the newer ndk versions. When I set `ANDROID_NDK_HOME` to `/usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125`, the rule expects a different structure from the Android NDK path. ``` ERROR: /usr/local/vinhdaitran/github/rules_rust/examples/android/WORKSPACE.bazel:67:23: fetching android_ndk_repository rule //external:androidndk: java.io.IOException: Expected directory at /usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125/platforms but it is not a directory or it does not exist. Unable to read the Android NDK at /usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125, the path may be invalid. Is the path in android_ndk_repository() or ANDROID_NDK_HOME set correctly? If the path is correct, the contents in the Android NDK directory may have been modified. ``` Using the Starlark version of the rule, as recommended in https://bazel.build/reference/be/android#android_ndk_repository, fixes the issue. cc: @keith Allow ~ in repository names (bazelbuild#2427) Fixes bazelbuild#2426 Prepare rust rules for Starlark CcToolchainInfo. (bazelbuild#2424) bazelbuild#2425
1 parent 99740e8 commit ee3b452

38 files changed

+79
-3317
lines changed

.bazelci/presubmit.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ tasks:
581581
- "--android_platforms=//:arm64-v8a"
582582
build_targets:
583583
- "//:android_app"
584+
environment:
585+
# This ndk version matches with rules_android_ndk repo's CI
586+
# https://github.com/bazelbuild/rules_android_ndk/blob/877c68ef34c9f3353028bf490d269230c1990483/.bazelci/presubmit.yml#L37
587+
# The ndk is installed by this script
588+
# https://github.com/bazelbuild/continuous-integration/blob/ba56013373821feadd9f2eaa6b81eb19528795f0/macos/mac-android.sh
589+
ANDROID_NDK_HOME: /opt/android-ndk-r25b
584590
android_examples_macos:
585591
name: Android Examples
586592
platform: macos
@@ -591,6 +597,12 @@ tasks:
591597
- "--android_platforms=//:arm64-v8a"
592598
build_targets:
593599
- "//:android_app"
600+
environment:
601+
# This ndk version matches with rules_android_ndk repo's CI
602+
# https://github.com/bazelbuild/rules_android_ndk/blob/877c68ef34c9f3353028bf490d269230c1990483/.bazelci/presubmit.yml#L42
603+
# The ndk is installed by this script
604+
# https://github.com/bazelbuild/continuous-integration/blob/ba56013373821feadd9f2eaa6b81eb19528795f0/macos/mac-android.sh
605+
ANDROID_NDK_HOME: /Users/buildkite/android-ndk-r25b
594606
ios_examples:
595607
name: iOS Examples
596608
platform: macos

MODULE.bazel

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ use_repo(
100100
"rules_rust_test_load_arbitrary_tool",
101101
"rules_rust_tinyjson",
102102
"rules_rust_toolchain_test_target_json",
103-
"rules_rust_util_import__aho-corasick-0.7.15",
104-
"rules_rust_util_import__lazy_static-1.4.0",
105-
"rules_rust_util_import__proc-macro2-1.0.33",
106-
"rules_rust_util_import__quickcheck-1.0.3",
107-
"rules_rust_util_import__quote-1.0.10",
108-
"rules_rust_util_import__syn-1.0.82",
109103
"rules_rust_wasm_bindgen__anyhow-1.0.71",
110104
"rules_rust_wasm_bindgen__assert_cmd-1.0.8",
111105
"rules_rust_wasm_bindgen__diff-0.1.13",

WORKSPACE.bazel

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
4545

4646
rust_analyzer_dependencies()
4747

48-
load("@rules_rust//util/import:deps.bzl", "import_deps")
49-
50-
import_deps()
51-
5248
load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositories")
5349

5450
rust_wasm_bindgen_repositories()

crate_universe/src/utils/starlark/label.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl FromStr for Label {
6363
type Err = anyhow::Error;
6464

6565
fn from_str(s: &str) -> Result<Self, Self::Err> {
66-
let re = Regex::new(r"^(@@?[\w\d\-_\.]*)?(//)?([\w\d\-_\./+]+)?(:([\+\w\d\-_\./]+))?$")?;
66+
let re = Regex::new(r"^(@@?[\w\d\-_\.~]*)?(//)?([\w\d\-_\./+]+)?(:([\+\w\d\-_\./]+))?$")?;
6767
let cap = re
6868
.captures(s)
6969
.with_context(|| format!("Failed to parse label from string: {s}"))?;
@@ -449,6 +449,19 @@ mod test {
449449
assert_eq!(label.target(), "target");
450450
}
451451

452+
#[test]
453+
fn full_label_bzlmod_with_tilde() {
454+
let label = Label::from_str("@@repo~name//package/sub_package:target").unwrap();
455+
assert_eq!(label.to_string(), "@@repo~name//package/sub_package:target");
456+
assert!(label.is_absolute());
457+
assert_eq!(
458+
label.repository(),
459+
Some(&Repository::Canonical(String::from("repo~name")))
460+
);
461+
assert_eq!(label.package(), Some("package/sub_package"));
462+
assert_eq!(label.target(), "target");
463+
}
464+
452465
#[test]
453466
fn full_label_with_slash_after_colon() {
454467
let label = Label::from_str("@repo//package/sub_package:subdir/target").unwrap();

examples/android/.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ common --enable_platform_specific_config
33
startup --windows_enable_symlinks
44
build:windows --enable_runfiles
55

6-
build --fat_apk_cpu=arm64-v8a
6+
build --fat_apk_cpu=arm64-v8a --android_crosstool_top=@androidndk//:toolchain
77

88
# TODO: migrate all dependencies from WORKSPACE to MODULE.bazel
99
# https://github.com/bazelbuild/rules_rust/issues/2181

examples/android/WORKSPACE.bazel

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ rbe_preconfig(
4545
toolchain = "ubuntu1804-bazel-java11",
4646
)
4747

48-
android_sdk_repository(name = "androidsdk")
48+
android_sdk_repository(
49+
name = "androidsdk",
50+
)
51+
52+
http_archive(
53+
name = "rules_android_ndk",
54+
sha256 = "b1a5ddd784e6ed915c2035c0db536a278b5f50c64412128c06877115991391ef",
55+
strip_prefix = "rules_android_ndk-877c68ef34c9f3353028bf490d269230c1990483",
56+
url = "https://github.com/bazelbuild/rules_android_ndk/archive/877c68ef34c9f3353028bf490d269230c1990483.zip",
57+
)
58+
59+
load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
4960

50-
android_ndk_repository(name = "androidndk")
61+
android_ndk_repository(
62+
name = "androidndk",
63+
)

examples/bzlmod/hello_world/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ build_test(
5656
"@rules_rust//tools/rust_analyzer:gen_rust_project",
5757
],
5858
)
59+
60+
build_test(
61+
name = "rust_fmt",
62+
targets = [
63+
"@rules_rust//:rustfmt",
64+
],
65+
)

rust/private/extensions.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ load("//proto/prost:repositories.bzl", "rust_prost_dependencies")
77
load("//rust/private:repository_utils.bzl", "TINYJSON_KWARGS")
88
load("//test:deps.bzl", "rules_rust_test_deps")
99
load("//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
10-
load("//util/import:deps.bzl", "import_deps")
1110
load("//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies")
1211

1312
def _internal_deps_impl(module_ctx):
@@ -23,7 +22,6 @@ def _internal_deps_impl(module_ctx):
2322
direct_deps.extend(rust_prost_dependencies(bzlmod = True))
2423
direct_deps.extend(rust_bindgen_dependencies())
2524
direct_deps.extend(rust_analyzer_dependencies())
26-
direct_deps.extend(import_deps())
2725
direct_deps.extend(rust_wasm_bindgen_dependencies())
2826
direct_deps.extend(rules_rust_test_deps())
2927

rust/private/rustc.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,15 @@ def collect_inputs(
685685
"""
686686
linker_script = getattr(file, "linker_script") if hasattr(file, "linker_script") else None
687687

688-
linker_depset = cc_toolchain.linker_files()
688+
# TODO: As of writing this comment Bazel used Java CcToolchainInfo.
689+
# However there is ongoing work to rewrite provider in Starlark.
690+
# rules_rust is not coupled with Bazel release. Remove conditional and change to
691+
# _linker_files once Starlark CcToolchainInfo is visible to Bazel.
692+
# https://github.com/bazelbuild/rules_rust/issues/2425
693+
if hasattr(cc_toolchain, "_linker_files"):
694+
linker_depset = cc_toolchain._linker_files
695+
else:
696+
linker_depset = cc_toolchain.linker_files()
689697
compilation_mode = ctx.var["COMPILATION_MODE"]
690698

691699
use_pic = _should_use_pic(cc_toolchain, feature_configuration, crate_info.type, compilation_mode)

rust/repositories.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ def rust_register_toolchains(
262262
target_compatible_with_by_toolchain[toolchain.name] = triple_to_constraint_set(toolchain.target_triple)
263263
toolchain_types[toolchain.name] = "@rules_rust//rust:toolchain"
264264

265+
toolchain_names.append(rustfmt_repo_name)
266+
toolchain_labels[rustfmt_repo_name] = "@{}_tools//:rustfmt_toolchain".format(rustfmt_repo_name)
267+
exec_compatible_with_by_toolchain[rustfmt_repo_name] = triple_to_constraint_set(exec_triple)
268+
target_compatible_with_by_toolchain[rustfmt_repo_name] = []
269+
toolchain_types[rustfmt_repo_name] = "@rules_rust//rust/rustfmt:toolchain_type"
270+
265271
toolchain_repository_hub(
266272
name = "rust_toolchains",
267273
toolchain_names = toolchain_names,

tools/rust_analyzer/aquery.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,18 @@ fn parse_aquery_output_files(
110110
execution_root: &Path,
111111
aquery_stdout: &str,
112112
) -> anyhow::Result<Vec<PathBuf>> {
113-
let out: AqueryOutput = serde_json::from_str(aquery_stdout)?;
113+
let out: AqueryOutput = serde_json::from_str(aquery_stdout).map_err(|_| {
114+
// Parsing to `AqueryOutput` failed, try parsing into a `serde_json::Value`:
115+
match serde_json::from_str::<serde_json::Value>(aquery_stdout) {
116+
Ok(serde_json::Value::Object(_)) => {
117+
// If the JSON is an object, it's likely that the aquery command failed.
118+
anyhow::anyhow!("Aquery returned an empty result, are there any Rust targets in the specified paths?.")
119+
}
120+
_ => {
121+
anyhow::anyhow!("Failed to parse aquery output as JSON")
122+
}
123+
}
124+
})?;
114125

115126
let artifacts = out
116127
.artifacts

util/import/3rdparty/BUILD.bazel

Lines changed: 0 additions & 40 deletions
This file was deleted.

util/import/3rdparty/Cargo.Bazel.lock

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)