bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
667 stars 431 forks source link

cargo_build_script uses target version of data instead of exec #884

Closed djmarcin closed 3 years ago

djmarcin commented 3 years ago

I'm trying to pass PROTOC to a cargo_build_script in order to use tonic. However the protoc path is PROTOC='${pwd}/bazel-out/darwin-fastbuild/bin/external/com_google_protobuf/protoc' when it should be PROTOC='${pwd}/bazel-out/darwin-opt-exec-123456/bin/external/com_google_protobuf/protoc'

The cargo_build_script config looks like:

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    build_script_env = {
        "PROTOC": "$(execpath @com_google_protobuf//:protoc)",
    },
    data = [
        "//proto/backend",
        "@com_google_protobuf//:protoc",
        "@com_google_protobuf//:well_known_protos",
    ],
    deps = [
        "//third_party/cargo:tonic_build",
    ],
)

Because of this, when cross compiling the script attempts to invoke a protoc binary compiled for the target, rather than the host.

UebelAndre commented 3 years ago

I believe this was satisfied by https://github.com/bazelbuild/rules_rust/pull/885

Please re-open if I'm mistaken 😄

alealv commented 2 years ago

Hi

I'm still having this problem when I build for iOS on an x86_64 machine.

bazel build //libs/protos-rs:protos --config=ios_arm64 --compilation_mode=fastbuild

This is my .bazelrc file

# .bazelrc
build:ios --apple_platform_type=ios
build:ios --copt=-Wno-c++11-narrowing
build:ios --noenable_platform_specific_config
build:ios --xcode_version_config=//bazel/xcode:host_xcodes
# Disable C++ warnings
build:ios --copt=-w
build:ios --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
# iOS CPU specific settings
build:ios_arm64 --config=ios
build:ios_arm64 --cpu=ios_arm64
build:ios_arm64 --platforms=//bazel/platforms:ios_arm64_platform

My platforms file

platform(
    name = "ios_arm64_platform",
    constraint_values = [
        "@platforms//os:ios",
        "@platforms//cpu:aarch64",
    ],
)

And this is the rule failing

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    build_script_env = {
        "RUSTFMT": "$(execpath @rules_rust//rust/toolchain:current_exec_rustfmt_files)",
        "PROTOC": "$(execpath @com_google_protobuf//:protoc)",
    },
    crate_features = [
        "transport",
    ],
    data = [
        "//api/protos",
        "@com_google_protobuf//:protoc",
        "@com_google_protobuf//:well_known_protos",
        "@rules_rust//rust/toolchain:current_exec_rustfmt_files",
    ],
    deps = [
        "@raze__tonic_build__0_6_2//:tonic_build",
    ],
)

rust_library(
    name = "protos",
    srcs = glob([
        "src/**/*.rs",
    ]),
    visibility = ["//visibility:public"],
    deps = all_crate_deps() + [
        "build_script",
    ],
)
UebelAndre commented 2 years ago

@alealv Thanks for the info! But can you also share an error you get when failing to build that target? And can you elaborate on why you suspect this to be related to that failure? 🙏

alealv commented 2 years ago

So, I have two different types of errors depending on the rules_rust version:

Error with rust_rules v0.4.0

❯ bazel build //libs/protos-rs:protos --config=ios_arm64 --compilation_mode=fastbuild
INFO: Invocation ID: abbef599-01ff-413b-b616-572caf527b93
DEBUG: /Users/xdev/.cache/bazel/fc7c18fb53b8d51e4a6f9440a6cadab3/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:68:14:
Current running Bazel is ahead of bazel-toolchains repo. Please update your pin to bazel-toolchains repo in your WORKSPACE file.
DEBUG: /Users/xdev/.cache/bazel/fc7c18fb53b8d51e4a6f9440a6cadab3/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:113:18: rbe_ubuntu2004 not using checked in configs; registry was set to 'registry.gitlab.com' and toolchain_config_suite_spec is configured for 'marketplace.gcr.io'
ERROR: /Users/xdev/main/libs/protos-rs/BUILD.bazel:511:13: While resolving toolchains for target //libs/protos-rs:protos: no matching toolchains found for types @rules_rust//rust:toolchain
ERROR: Analysis of target '//libs/protos-rs:protos' failed; build aborted:
INFO: Elapsed time: 0.098s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

I believe my WORKSPACE file is correct

http_archive(
    name = "rules_rust",
    sha256 = "edb87c0d2ba70823fe3df7862676d695599314a4634b9758bd55f0e8f19c2751",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/0.4.0/rules_rust-v0.4.0.tar.gz",
        "https://github.com/bazelbuild/rules_rust/releases/download/0.4.0/rules_rust-v0.4.0.tar.gz",
    ],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

Error with rust_rules commit 69a738a7

I get some error with protobuf I guess selecting wrong toolchain for CPU

❯ bazel build //libs/protos-rs:protos --config=ios_arm64 --compilation_mode=fastbuild
INFO: Invocation ID: 549eacef-005d-4933-b15c-9e836eb98d45
DEBUG: /Users/xdev/.cache/bazel/fc7c18fb53b8d51e4a6f9440a6cadab3/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:68:14:
Current running Bazel is ahead of bazel-toolchains repo. Please update your pin to bazel-toolchains repo in your WORKSPACE file.
DEBUG: /Users/xdev/.cache/bazel/fc7c18fb53b8d51e4a6f9440a6cadab3/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:113:18: rbe_ubuntu2004 not using checked in configs; registry was set to 'registry.gitlab.com' and toolchain_config_suite_spec is configured for 'marketplace.gcr.io'
INFO: Analyzed target //libs/protos-rs:protos (156 packages loaded, 5959 targets configured).
INFO: Found 1 target...
ERROR: /Users/xdev/main/libs/protos-rs/BUILD.bazel:490:19: Running Cargo build script build_script failed: (Exit 1): cargo_build_script_runner failed: error executing command bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_rust/cargo/cargo_build_script_runner/cargo_build_script_runner bazel-out/darwin-opt-exec-2B5CBBC6/bin/libs/protos-rs/build_script_ '' ... (remaining 7 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
Build script process failed with exit code 101
--stdout:

--stderr:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: Uncategorized, error: "failed to invoke protoc (hint: https://docs.rs/prost-build/#sourcing-protoc): Bad CPU type in executable (os error 86)" }', libs/protos-rs/build.rs:80:48
stack backtrace:
   0:        0x108fe0d02 - std::backtrace_rs::backtrace::libunwind::trace::h95efb3c30cc8d3eb
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:        0x108fe0d02 - std::backtrace_rs::backtrace::trace_unsynchronized::hf55232b46c2b2a5c
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x108fe0d02 - std::sys_common::backtrace::_print_fmt::hd000e024c6701252
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/sys_common/backtrace.rs:66:5
   3:        0x108fe0d02 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h4cae82d438451481
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/sys_common/backtrace.rs:45:22
   4:        0x108ffee1b - core::fmt::write::hb68c3045179d0cad
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/fmt/mod.rs:1190:17
   5:        0x108fdce3e - std::io::Write::write_fmt::haf84c797e63d79f0
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/io/mod.rs:1657:15
   6:        0x108fe3040 - std::sys_common::backtrace::_print::h4051b3c764203017
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/sys_common/backtrace.rs:48:5
   7:        0x108fe3040 - std::sys_common::backtrace::print::h946847bf78a6f565
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/sys_common/backtrace.rs:35:9
   8:        0x108fe3040 - std::panicking::default_hook::{{closure}}::he18137441e51da1f
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:295:22
   9:        0x108fe2d26 - std::panicking::default_hook::ha3efe84526f027fa
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:314:9
  10:        0x108fe379d - std::panicking::rust_panic_with_hook::h429a7ddefa5f0258
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:698:17
  11:        0x108fe34c3 - std::panicking::begin_panic_handler::{{closure}}::h9b033a6b15b84a74
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:588:13
  12:        0x108fe1197 - std::sys_common::backtrace::__rust_end_short_backtrace::hcdd3bec8e0e38aa6
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/sys_common/backtrace.rs:138:18
  13:        0x108fe318a - rust_begin_unwind
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:584:5
  14:        0x10900cb93 - core::panicking::panic_fmt::hf7d6e5207e013f69
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/panicking.rs:143:14
  15:        0x10900cc15 - core::result::unwrap_failed::h95d9e30ede493473
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/result.rs:1749:5
  16:        0x108da3bc1 - build_script_::main::h5c0d15d2da15d1af
  17:        0x108da2646 - std::sys_common::backtrace::__rust_begin_short_backtrace::h2a25b1908513dca1
  18:        0x108da2c0c - std::rt::lang_start::{{closure}}::h4d4b699d7651d52a
  19:        0x108fe0435 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h0b34a00ab870ad57
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/ops/function.rs:259:13
  20:        0x108fe0435 - std::panicking::try::do_call::hf96b04a014b00830
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:492:40
  21:        0x108fe0435 - std::panicking::try::h52320e36f38e53ab
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:456:19
  22:        0x108fe0435 - std::panic::catch_unwind::hfb74ccd231e67a94
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panic.rs:137:14
  23:        0x108fe0435 - std::rt::lang_start_internal::{{closure}}::hac12182f9ae31dd1
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/rt.rs:128:48
  24:        0x108fe0435 - std::panicking::try::do_call::h7607cb0e1d9db8ca
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:492:40
  25:        0x108fe0435 - std::panicking::try::h5bcafddd15b2a478
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:456:19
  26:        0x108fe0435 - std::panic::catch_unwind::h74f42b63c70aad90
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panic.rs:137:14
  27:        0x108fe0435 - std::rt::lang_start_internal::hef78d5782ed29805
                               at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/rt.rs:128:20
  28:        0x108da4289 - _main

Target //libs/protos-rs:protos failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /Users/xdev/main/libs/protos-rs/BUILD.bazel:511:13 Compiling Rust rlib protos (4 files) failed: (Exit 1): cargo_build_script_runner failed: error executing command bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_rust/cargo/cargo_build_script_runner/cargo_build_script_runner bazel-out/darwin-opt-exec-2B5CBBC6/bin/libs/protos-rs/build_script_ '' ... (remaining 7 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
INFO: Elapsed time: 3.056s, Critical Path: 0.32s
INFO: 12 processes: 12 internal.
FAILED: Build did NOT complete successfully
alealv commented 2 years ago

It was resolved by using tools argument instead of data. Here is the full BUILD file

```python
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_library")
load("//cargo:crates.bzl", "all_crate_deps")

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    build_script_env = {
        "PROTOC": "$(execpath :protoc)",
        "RUSTFMT": "$(execpath :rustfmt_bin)",
    },
    crate_features = [
        "transport",
    ],
    data = [
        "//api/protos",
        "@com_google_protobuf//:protoc",
        "@com_google_protobuf//:well_known_protos",
    ],
    tools = [
        ":protoc",
        ":rustfmt_bin",
    ],
    deps = [
        "@raze__tonic_build__0_6_2//:tonic_build",
    ],
)

rust_library(
    name = "protos",
    srcs = glob([
        "src/**/*.rs",
    ]),
    visibility = ["//visibility:public"],
    deps = all_crate_deps() + [
        "build_script",
    ],
)

alias(
    name = "rustfmt_bin",
    actual = select({
        "@bazel_tools//src/conditions:linux_aarch64": "@rust_linux_aarch64//:rustfmt_bin",
        "@bazel_tools//src/conditions:linux_x86_64": "@rust_linux_x86_64//:rustfmt_bin",
        "@bazel_tools//src/conditions:darwin_arm64": "@rust_darwin_aarch64//:rustfmt_bin",
        "@bazel_tools//src/conditions:darwin_x86_64": "@rust_darwin_x86_64//:rustfmt_bin",
    }),
    visibility = ["//visibility:public"],
)

alias(
    name = "protoc",
    actual = select({
        "@bazel_tools//src/conditions:linux_aarch64": "@com_google_protobuf_protoc_linux_aarch64//:protoc",
        "@bazel_tools//src/conditions:linux_x86_64": "@com_google_protobuf_protoc_linux_x86_64//:protoc",
        "@bazel_tools//src/conditions:darwin_arm64": "@com_google_protobuf_protoc_macos_aarch64//:protoc",
        "@bazel_tools//src/conditions:darwin_x86_64": "@com_google_protobuf_protoc_macos_x86_64//:protoc",
    }),
    visibility = ["//visibility:public"],
)
tinkrtailor commented 1 year ago

@alealv I am trying to get the same thing working but I am getting the error

no such package '@com_google_protobuf_protoc_macos_aarch64//': The repository '@com_google_protobuf_protoc_macos_aarch64' could not be resolved: Repository '@com_google_protobuf_protoc_macos_aarch64' is not defined and referenced by

Did you have to add something to your workspace to make these targets available?