Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to parse target query : UnknownOperatingSystem while cross-compiling under Bzlmod #2529

Open
ar3s3ru opened this issue Feb 28, 2024 · 2 comments

Comments

@ar3s3ru
Copy link

ar3s3ru commented Feb 28, 2024

Hi folks, I am experiencing yet a different issue with cross-compilation on my M1 Macbook targeting linux-arm64 and linux-amd64. I have already reported #2518 while I tried to reproduce this error.

This is the main error line I get from the bazel build output:

cargo:warning=error: unable to parse target query 'aarch64-unknown-linux-gnu': UnknownOperatingSystem

Full output is here:

bazel build //backend/app:image_linux-arm64
INFO: Analyzed target //backend/app:image_linux-arm64 (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/external/rules_rust~override~crate~crates__ring-0.17.8/BUILD.bazel:192:19: Running Cargo build script ring failed: (Exit 1): cargo_build_script_runner failed: error executing command (from target @rules_rust~override~crate~crates__ring-0.17.8//:ring_bs) 
  (cd /private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/sandbox/darwin-sandbox/1125/execroot/_main && \
  exec env - \
    AR=external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/ar \
    CARGO_CRATE_NAME=ring \
    CARGO_ENCODED_RUSTFLAGS='--sysroot=${pwd}/bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~rust~rust_darwin_aarch64__aarch64-unknown-linux-gnu__stable_tools/rust_toolchain\x1f--cap-lints=allow' \
    CARGO_FEATURE_ALLOC=1 \
    CARGO_FEATURE_DEFAULT=1 \
    CARGO_FEATURE_DEV_URANDOM_FALLBACK=1 \
    CARGO_MANIFEST_DIR=bazel-out/darwin_arm64-opt-exec-2B5CBBC6-ST-6c1468b2613d/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs_.runfiles/rules_rust~override~crate~crates__ring-0.17.8/ \
    CARGO_MANIFEST_LINKS=ring_core_0_17_8 \
    CARGO_PKG_NAME=ring \
    CARGO_PKG_VERSION=0.17.8 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=17 \
    CARGO_PKG_VERSION_PATCH=8 \
    CARGO_PKG_VERSION_PRE='' \
    CC=external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/aarch64-linux-gnu.2.31/c++ \
    CFLAGS='-no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fno-lto -fsanitize-undefined-strip-path-components=-1' \
    CXX=external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/aarch64-linux-gnu.2.31/c++ \
    CXXFLAGS='-no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fno-lto -fsanitize-undefined-strip-path-components=-1' \
    DEBUG=true \
    HOST=aarch64-apple-darwin \
    LD=external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/aarch64-linux-gnu.2.31/c++ \
    LDFLAGS='-lSystem -lresolv -fno-lto -fsanitize-undefined-strip-path-components=-1' \
    NUM_JOBS=1 \
    OPT_LEVEL=0 \
    PATH=/bin:/usr/bin:/usr/local/bin \
    PROFILE=debug \
    RUSTC=bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~rust~rust_darwin_aarch64__aarch64-unknown-linux-gnu__stable_tools/rust_toolchain/bin/rustc \
    TARGET=aarch64-unknown-linux-gnu \
  bazel-out/darwin_arm64-opt-exec-2B5CBBC6-ST-6c1468b2613d/bin/external/rules_rust~override/cargo/cargo_build_script_runner/cargo_build_script_runner bazel-out/darwin_arm64-opt-exec-2B5CBBC6-ST-6c1468b2613d/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs_ ring_core_0_17_8 bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.out_dir bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.env bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.flags bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.linkflags bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.linksearchpaths bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.depenv bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.stdout.log bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.stderr.log '')
# Configuration: dde114dbf783ac14d66c30f52d5e5471b3bc06d2f2d9f3f870bbf87eef263463
# Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
Build script process failed with exit code 1
--stdout:
cargo:rerun-if-env-changed=RING_PREGENERATE_ASM
cargo:rustc-env=RING_CORE_PREFIX=ring_core_0_17_8_
OPT_LEVEL = Some("0")
TARGET = Some("aarch64-unknown-linux-gnu")
HOST = Some("aarch64-apple-darwin")
cargo:rerun-if-env-changed=CC_aarch64-unknown-linux-gnu
CC_aarch64-unknown-linux-gnu = None
cargo:rerun-if-env-changed=CC_aarch64_unknown_linux_gnu
CC_aarch64_unknown_linux_gnu = None
cargo:rerun-if-env-changed=TARGET_CC
TARGET_CC = None
cargo:rerun-if-env-changed=CC
CC = Some("/private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/sandbox/darwin-sandbox/1125/execroot/_main/external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/aarch64-linux-gnu.2.31/c++")
cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
cargo:rerun-if-env-changed=CFLAGS_aarch64-unknown-linux-gnu
CFLAGS_aarch64-unknown-linux-gnu = None
cargo:rerun-if-env-changed=CFLAGS_aarch64_unknown_linux_gnu
CFLAGS_aarch64_unknown_linux_gnu = None
cargo:rerun-if-env-changed=TARGET_CFLAGS
TARGET_CFLAGS = None
cargo:rerun-if-env-changed=CFLAGS
CFLAGS = Some("-no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -fno-lto -fsanitize-undefined-strip-path-components=-1")
cargo:warning=error: unable to parse target query 'aarch64-unknown-linux-gnu': UnknownOperatingSystem

--stderr:

error occurred: Command "/private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/sandbox/darwin-sandbox/1125/execroot/_main/external/hermetic_cc_toolchain~3.0.1~toolchains~zig_sdk/tools/aarch64-linux-gnu.2.31/c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "--target=aarch64-unknown-linux-gnu" "-no-canonical-prefixes" "-Wno-builtin-macro-redefined" "-D__DATE__=\"redacted\"" "-D__TIMESTAMP__=\"redacted\"" "-D__TIME__=\"redacted\"" "-fno-lto" "-fsanitize-undefined-strip-path-components=-1" "-I" "include" "-I" "/private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/sandbox/darwin-sandbox/1125/execroot/_main/bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.out_dir" "-fvisibility=hidden" "-std=c1x" "-Wall" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-g3" "-DNDEBUG" "-o" "/private/var/tmp/_bazel_ar3s3ru/fd932b244535be68186275b71b1b71d2/sandbox/darwin-sandbox/1125/execroot/_main/bazel-out/darwin_arm64-fastbuild-ST-9f5b482a3bc2/bin/external/rules_rust~override~crate~crates__ring-0.17.8/ring_bs.out_dir/fad98b632b8ce3cc-curve25519.o" "-c" "crypto/curve25519/curve25519.c" with args "c++" did not execute successfully (status code exit status: 1).

Target //backend/app:image_linux-arm64 failed to build

My MODULE.bazel setup:

# Hiding the unrelevant bits and pieces
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_pkg", version = "0.9.1")
bazel_dep(name = "rules_oci", version = "1.5.1")
bazel_dep(name = "rules_rust", version = "0.39.0")
bazel_dep(name = "hermetic_cc_toolchain", version = "3.0.1")

git_override(
    module_name = "rules_rust",
    commit = "777f3e5c5d280b14d37bb7574a88f1cbfb3f8d46",
    remote = "https://github.com/bazelbuild/rules_rust",
)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
    name = "distroless_base",
    digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
    image = "gcr.io/distroless/base",
    platforms = [
        "linux/amd64",
        "linux/arm64",
    ],
)
use_repo(
    oci,
    "distroless_base",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
    edition = "2021",
    # NOTE: update also the Nix devshell version if changing this!
    versions = ["1.76.0"],
    # NOTE: For cross-compilation to deployment target environments.
    extra_target_triples = [
        "aarch64-unknown-linux-gnu",
        "x86_64-unknown-linux-gnu",
    ],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

# Hermetic toolchains used for Rust cross-compilation.
toolchains = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
use_repo(toolchains, "zig_sdk")

register_toolchains(
    "@zig_sdk//toolchain:linux_amd64_gnu.2.31",
    "@zig_sdk//toolchain:linux_arm64_gnu.2.31",
)

crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.from_cargo(
    name = "crates",
    cargo_lockfile = "//:Cargo.lock",
    manifests = [
        "//:Cargo.toml",
        "//gen/rust:Cargo.toml",
        "//backend/app:Cargo.toml",
    ],
)
use_repo(crate, "crates")

The targets I'm trying to build, in backend/app/BUILD.bazel:

load("@crates//:defs.bzl", "all_crate_deps")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")

rust_library(
    name = "app_lib",
    srcs = glob(
        ["src/**/*.rs"],
        exclude = ["src/bin/**/*.rs"],
    ),
    crate_name = "backend_app",
    edition = "2021",
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
    ),
    visibility = ["//visibility:public"],
    deps = [
        "//gen/rust",
    ] + all_crate_deps(
        normal = True,
    ),
)

rust_test(
    name = "app_test",
    srcs = glob(
        ["src/**/*.rs"],
        exclude = ["src/bin/**/*.rs"],
    ),
    crate_name = "backend_app",
    edition = "2021",
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
        proc_macro_dev = True,
    ),
    deps = [
        ":app",
        "//gen/rust",
    ] + all_crate_deps(
        normal = True,
        normal_dev = True,
    ),
)

rust_binary(
    name = "app",
    srcs = glob(["src/bin/**/*.rs"]),
    crate_name = "backend_app",
    edition = "2021",
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
    ),
    visibility = ["//visibility:public"],
    deps = [
        ":app_lib",
        "//gen/rust",
    ] + all_crate_deps(
        normal = True,
    ),
)

rust_binary(
    name = "app_linux-amd64",
    srcs = glob(["src/bin/**/*.rs"]),
    crate_name = "backend_app",
    edition = "2021",
    platform = "//tools/bazel/platforms:linux_amd64",
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
    ),
    visibility = ["//visibility:public"],
    deps = [
        ":app_lib",
        "//gen/rust",
    ] + all_crate_deps(
        normal = True,
    ),
)

pkg_tar(
    name = "app_linux-amd64_layer",
    srcs = [":app_linux-amd64"],
)

oci_image(
    name = "image_linux-amd64",
    base = "@distroless_base",
    entrypoint = ["/{}".format("app_linux-amd64")],
    tars = [":app_linux-amd64_layer"],
)

oci_tarball(
    name = "{}.tar".format("image_linux-amd64"),
    image = ":image_linux-amd64",
    repo_tags = ["{}:latest".format("app")],
)

rust_binary(
    name = "app_linux-arm64",
    srcs = glob(["src/bin/**/*.rs"]),
    crate_name = "backend_app",
    edition = "2021",
    platform = "//tools/bazel/platforms:linux_arm64",
    proc_macro_deps = all_crate_deps(
        proc_macro = True,
    ),
    visibility = ["//visibility:public"],
    deps = [
        ":app_lib",
        "//gen/rust",
    ] + all_crate_deps(
        normal = True,
    ),
)

pkg_tar(
    name = "app_linux-arm64_layer",
    srcs = [":app_linux-arm64"],
)

oci_image(
    name = "image_linux-arm64",
    base = "@distroless_base",
    entrypoint = ["/{}".format("app_linux-arm64")],
    tars = [":app_linux-arm64_layer"],
)

oci_tarball(
    name = "{}.tar".format("image_linux-arm64"),
    image = ":image_linux-arm64",
    repo_tags = ["{}:latest".format("app")],
)
@mattbryant-asana
Copy link

mattbryant-asana commented Mar 15, 2024

I'm encountering the same issue (cargo:warning=error: unable to parse target query 'x86_64-unknown-linux-gnu': UnknownOperatingSystem) with ring 0.16.20 -> 0.17.8 + rules_rust 0.38 -> 0.40. Seems like it's probably related to ziglang/zig#7360 and hermetic_cc_toolchain.

An extremely hacky fix is to patch https://github.com/rust-lang/cc-rs to generate the targets your bazel configuration requires.

@ian-mcdowell
Copy link

This seems related:
rust-lang/cc-rs#948

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants