使用 bazel 为 Android 构建 rust_binary

问题描述 投票:0回答:1

我可以轻松构建

cc_binary
并在 Android 设备上执行它。我想使用
rust_rules
做同样的事情。但无法构建它。我对必须使用的标志感到困惑。

我没有在Linux上尝试过,但我想确切地知道我在做什么。

我的

BUILD.bazel

rust_binary(
    name = "hello",
    srcs = [
        "src/main.rs",
    ],
)

目前我尝试使用以下标志实现可执行的 rust 二进制文件(我使用的是 Bazel 7.0.0):

bazel build //rust:hello --android_platforms=//:android_arm64 --extra_toolchains=@androidndk//:toolchain

但是失败了

external/androidndk/toolchains/llvm/prebuilt/darwin-x86_64/BUILD:8:19: in cc_toolchain_suite rule @@androidndk//toolchains/llvm/prebuilt/darwin-x86_64:cc_toolchain_suite: cc_toolchain_suite '@@androidndk//toolchains/llvm/prebuilt/darwin-x86_64:cc_toolchain_suite' does not contain a toolchain for cpu 'darwin_arm64'

我在 macOS 上运行。

android rust android-ndk bazel
1个回答
0
投票

通过添加解决了问题

extra_target_triples = [
   "aarch64-linux-android",
],

rust_register_toolchains

完整答案在这里https://github.com/bazelbuild/rules_rust/issues/2390#issuecomment-1877114113

我能够在 Android 上构建并运行

rust_binary

bazel build //:hello --platforms=//:android_arm64
adb pull bazel-bin/hello /data/local/tmp
adb shell /data/local/tmp/hello
Hello world!
© www.soinside.com 2019 - 2024. All rights reserved.