From 61b06071fe49078632480c04d2bf37e278aafe40 Mon Sep 17 00:00:00 2001 From: festa78 Date: Sun, 7 Aug 2022 18:35:57 +0900 Subject: [PATCH] wip: rust code won't compile * use local repo to debug - rust_rules needs generator_urls provided. use Github release page - needs to add edition attr. to rust_binary/_image method by some reasons. * toolchain not found for cpp (or rust). * can build and run rust_binary solely, but with rust_image it fails with toolchain not found error. * image_transition target seems missing os/cpu target. * disable image_transition avoid this issue. But instead causing linux exec format error. Need to choose amd64 platform target? - rust_binary can run without selecting amd64 platform, but rust_image. * it seems rust rules do not provide platform definition for our case. manually adding a custom platform for linux/x86_64 target. setting this to --target_platform resolves execution platform for rust. However, we still have resolution error with cpp toolchain, because it seems there are no linux/x86_64 toolchain? * execution (also host?) platform is aarch64/osx by local_config. * rust's toolchain doesn't have exec_compatible_with aarch64/osx and target_compatible_with amd64/linux. * Only reasonable rust toolchain which can run on x86 docker is to have x86 target for both exe_/target_compatible_with. But now we still miss cpp_toolchain for that combination. * cpp toolchains are auto-generated by cc_configure methods which use local_config information coupled with m1 mac. So it does not contain x86/linux pattens. * another solution is to replace docker base image to arm64 image. can override image base by 'base' option in rust_image. * even using aarch64 docker image and arm64 binary, it gives Exec format error. Why? - Turned out the binary has Mach-O format, which is only for M1?, but not ELF format. * cc_toolchain does not have x86 or aarch64, linux target compatible toolchain, but only x86 or aarch64 and osx. - cc_toolchain does not have non-osx exec_compatible toolchain, because bazel auto-detect osx local env to make osx only toolchains. - check cc_configure.bzl. * rust only have x86,linux -> x86,linux, or aarch64,linux -> aarch64,linux exec_/target_compatible combination. * normal build outputs Mach-O format, not able to run on docker images which are mostly based on linux and supports ELF format. * image_transition generates new settings with proper cpu/os parameters but actually set nothing. Not sure why * Only way to solve this issue is to write a custom toolchain for cpp or generate it from the official implementation for osx/arm64 -> linux/amd64 or arm64 cross compilation. * needs to write osx/aarch64 -> linux/amd64 cross compile toolchain for both rust and cpp. * ref. https://github.com/bazelbuild/rules_rust/issues/276 --- go_rust/bazel_docker_test/.bazelrc | 2 + go_rust/bazel_docker_test/.gitignore | 1 + go_rust/bazel_docker_test/BUILD | 0 go_rust/bazel_docker_test/Cargo.Bazel.lock | 67 ++++++++++ go_rust/bazel_docker_test/Cargo.toml | 5 + go_rust/bazel_docker_test/WORKSPACE | 100 ++++++++++++++ go_rust/bazel_docker_test/hello_go/BUILD | 6 + .../bazel_docker_test/hello_go/src/main.go | 9 ++ go_rust/bazel_docker_test/hello_rust/BUILD | 15 +++ .../bazel_docker_test/hello_rust/Cargo.toml | 9 ++ .../bazel_docker_test/hello_rust/src/main.rs | 3 + rust/.bazelrc | 2 + rust/.gitignore | 2 + rust/BUILD | 0 rust/Cargo.Bazel.lock | 67 ++++++++++ rust/Cargo.lock | 11 ++ rust/Cargo.toml | 5 + rust/WORKSPACE | 123 ++++++++++++++++++ rust/hello/BUILD | 63 +++++++++ rust/hello/Cargo.toml | 9 ++ rust/hello/src/main.rs | 3 + 21 files changed, 502 insertions(+) create mode 100644 go_rust/bazel_docker_test/.bazelrc create mode 100644 go_rust/bazel_docker_test/.gitignore create mode 100644 go_rust/bazel_docker_test/BUILD create mode 100644 go_rust/bazel_docker_test/Cargo.Bazel.lock create mode 100644 go_rust/bazel_docker_test/Cargo.toml create mode 100644 go_rust/bazel_docker_test/WORKSPACE create mode 100644 go_rust/bazel_docker_test/hello_go/BUILD create mode 100644 go_rust/bazel_docker_test/hello_go/src/main.go create mode 100644 go_rust/bazel_docker_test/hello_rust/BUILD create mode 100644 go_rust/bazel_docker_test/hello_rust/Cargo.toml create mode 100644 go_rust/bazel_docker_test/hello_rust/src/main.rs create mode 100644 rust/.bazelrc create mode 100644 rust/.gitignore create mode 100644 rust/BUILD create mode 100644 rust/Cargo.Bazel.lock create mode 100644 rust/Cargo.lock create mode 100644 rust/Cargo.toml create mode 100644 rust/WORKSPACE create mode 100644 rust/hello/BUILD create mode 100644 rust/hello/Cargo.toml create mode 100644 rust/hello/src/main.rs diff --git a/go_rust/bazel_docker_test/.bazelrc b/go_rust/bazel_docker_test/.bazelrc new file mode 100644 index 0000000..dbb924f --- /dev/null +++ b/go_rust/bazel_docker_test/.bazelrc @@ -0,0 +1,2 @@ +build:macos --@io_bazel_rules_docker//transitions:enable=false +build:macos --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 diff --git a/go_rust/bazel_docker_test/.gitignore b/go_rust/bazel_docker_test/.gitignore new file mode 100644 index 0000000..ac51a05 --- /dev/null +++ b/go_rust/bazel_docker_test/.gitignore @@ -0,0 +1 @@ +bazel-* diff --git a/go_rust/bazel_docker_test/BUILD b/go_rust/bazel_docker_test/BUILD new file mode 100644 index 0000000..e69de29 diff --git a/go_rust/bazel_docker_test/Cargo.Bazel.lock b/go_rust/bazel_docker_test/Cargo.Bazel.lock new file mode 100644 index 0000000..b8f6123 --- /dev/null +++ b/go_rust/bazel_docker_test/Cargo.Bazel.lock @@ -0,0 +1,67 @@ +{ + "checksum": "1880b9ea81e4dbd47ad152a782f5435714718268b6b878574eb80910a38d5bdc", + "crates": { + "direct-cargo-bazel-deps 0.0.1": { + "name": "direct-cargo-bazel-deps", + "version": "0.0.1", + "repository": null, + "targets": [ + { + "Library": { + "crate_name": "direct_cargo_bazel_deps", + "crate_root": ".direct_cargo_bazel_deps.rs", + "srcs": { + "include": [ + "**/*.rs" + ], + "exclude": [] + } + } + } + ], + "library_target_name": "direct_cargo_bazel_deps", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "edition": "2018", + "version": "0.0.1" + }, + "license": null + }, + "hello_rust 0.1.0": { + "name": "hello_rust", + "version": "0.1.0", + "repository": null, + "targets": [ + { + "Binary": { + "crate_name": "hello_rust", + "crate_root": "src/main.rs", + "srcs": { + "include": [ + "**/*.rs" + ], + "exclude": [] + } + } + } + ], + "library_target_name": null, + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "edition": "2021", + "version": "0.1.0" + }, + "license": null + } + }, + "binary_crates": [], + "workspace_members": { + "direct-cargo-bazel-deps 0.0.1": "", + "hello_rust 0.1.0": "hello_rust" + }, + "conditions": {} +} diff --git a/go_rust/bazel_docker_test/Cargo.toml b/go_rust/bazel_docker_test/Cargo.toml new file mode 100644 index 0000000..94f8d91 --- /dev/null +++ b/go_rust/bazel_docker_test/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "hello_rust", +] \ No newline at end of file diff --git a/go_rust/bazel_docker_test/WORKSPACE b/go_rust/bazel_docker_test/WORKSPACE new file mode 100644 index 0000000..410048a --- /dev/null +++ b/go_rust/bazel_docker_test/WORKSPACE @@ -0,0 +1,100 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# go +http_archive( + name = "io_bazel_rules_go", + sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + ], +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") + +go_rules_dependencies() + +go_register_toolchains(version = "1.18.3") + +http_archive( + name = "bazel_gazelle", + sha256 = "5982e5463f171da99e3bdaeff8c0f48283a7a5f396ec5282910b9e8a49c0dd7e", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz", + ], +) + +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") + +gazelle_dependencies() + +# rust +http_archive( + name = "rules_rust", + sha256 = "39655ab175e3c6b979f362f55f58085528f1647957b0e9b3a07f81d8a9c3ea0a", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/0.2.0/rules_rust-v0.2.0.tar.gz", + "https://github.com/bazelbuild/rules_rust/releases/download/0.2.0/rules_rust-v0.2.0.tar.gz", + ], +) + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") + +rules_rust_dependencies() + +rust_register_toolchains() + +load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") + +crate_universe_dependencies() + +load("@rules_rust//crate_universe:defs.bzl", "crates_repository") + +crates_repository( + name = "crate_index", + lockfile = "//:Cargo.Bazel.lock", + manifests = [ + "//:Cargo.toml", + "//hello_rust:Cargo.toml", + ], +) + +load("@crate_index//:defs.bzl", "crate_repositories") + +crate_repositories() + +# rules_docker +http_archive( + name = "io_bazel_rules_docker", + sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", + urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], +) + +load( + "@io_bazel_rules_docker//repositories:repositories.bzl", + container_repositories = "repositories", +) + +container_repositories() + +# NOTE: it installs go-v0.24.2.tar.gz which contains go v1.15.2. +load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps") + +container_deps() + +# rules_docker go +load( + "@io_bazel_rules_docker//go:image.bzl", + _go_image_repos = "repositories", +) + +_go_image_repos() + +# rules_docker rust +load( + "@io_bazel_rules_docker//rust:image.bzl", + _rust_image_repos = "repositories", +) + +_rust_image_repos() diff --git a/go_rust/bazel_docker_test/hello_go/BUILD b/go_rust/bazel_docker_test/hello_go/BUILD new file mode 100644 index 0000000..a010dac --- /dev/null +++ b/go_rust/bazel_docker_test/hello_go/BUILD @@ -0,0 +1,6 @@ +load("@io_bazel_rules_docker//go:image.bzl", "go_image") + +go_image( + name = "hello_go", + srcs = ["src/main.go"], +) diff --git a/go_rust/bazel_docker_test/hello_go/src/main.go b/go_rust/bazel_docker_test/hello_go/src/main.go new file mode 100644 index 0000000..bb7a8a5 --- /dev/null +++ b/go_rust/bazel_docker_test/hello_go/src/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, World in go") +} diff --git a/go_rust/bazel_docker_test/hello_rust/BUILD b/go_rust/bazel_docker_test/hello_rust/BUILD new file mode 100644 index 0000000..c0d33ef --- /dev/null +++ b/go_rust/bazel_docker_test/hello_rust/BUILD @@ -0,0 +1,15 @@ +load("@crate_index//:defs.bzl", "aliases", "all_crate_deps") +load("@io_bazel_rules_docker//rust:image.bzl", "rust_image") + +rust_image( + name = "hello_rust", + srcs = glob(["src/*.rs"]), + aliases = aliases(), + proc_macro_deps = all_crate_deps( + proc_macro = True, + ), + visibility = ["//visibility:public"], + deps = all_crate_deps( + normal = True, + ), +) diff --git a/go_rust/bazel_docker_test/hello_rust/Cargo.toml b/go_rust/bazel_docker_test/hello_rust/Cargo.toml new file mode 100644 index 0000000..1f3214a --- /dev/null +++ b/go_rust/bazel_docker_test/hello_rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hello_rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +#[dependencies] +#thiserror = "1.0" diff --git a/go_rust/bazel_docker_test/hello_rust/src/main.rs b/go_rust/bazel_docker_test/hello_rust/src/main.rs new file mode 100644 index 0000000..fbedd92 --- /dev/null +++ b/go_rust/bazel_docker_test/hello_rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} \ No newline at end of file diff --git a/rust/.bazelrc b/rust/.bazelrc new file mode 100644 index 0000000..dbb924f --- /dev/null +++ b/rust/.bazelrc @@ -0,0 +1,2 @@ +build:macos --@io_bazel_rules_docker//transitions:enable=false +build:macos --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..9a7792a --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,2 @@ +bazel-* +target diff --git a/rust/BUILD b/rust/BUILD new file mode 100644 index 0000000..e69de29 diff --git a/rust/Cargo.Bazel.lock b/rust/Cargo.Bazel.lock new file mode 100644 index 0000000..bcaca85 --- /dev/null +++ b/rust/Cargo.Bazel.lock @@ -0,0 +1,67 @@ +{ + "checksum": "e6768397ef18bbbd463dab1c6e13c5a9d8783642c08dcc6f875f59eb0c31a3d6", + "crates": { + "direct-cargo-bazel-deps 0.0.1": { + "name": "direct-cargo-bazel-deps", + "version": "0.0.1", + "repository": null, + "targets": [ + { + "Library": { + "crate_name": "direct_cargo_bazel_deps", + "crate_root": ".direct_cargo_bazel_deps.rs", + "srcs": { + "include": [ + "**/*.rs" + ], + "exclude": [] + } + } + } + ], + "library_target_name": "direct_cargo_bazel_deps", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "edition": "2018", + "version": "0.0.1" + }, + "license": null + }, + "hello 0.1.0": { + "name": "hello", + "version": "0.1.0", + "repository": null, + "targets": [ + { + "Binary": { + "crate_name": "hello", + "crate_root": "src/main.rs", + "srcs": { + "include": [ + "**/*.rs" + ], + "exclude": [] + } + } + } + ], + "library_target_name": null, + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "edition": "2021", + "version": "0.1.0" + }, + "license": null + } + }, + "binary_crates": [], + "workspace_members": { + "direct-cargo-bazel-deps 0.0.1": "", + "hello 0.1.0": "hello" + }, + "conditions": {} +} diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..0a94872 --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,11 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "direct-cargo-bazel-deps" +version = "0.0.1" + +[[package]] +name = "hello" +version = "0.1.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..5551110 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "hello", +] \ No newline at end of file diff --git a/rust/WORKSPACE b/rust/WORKSPACE new file mode 100644 index 0000000..e3a9ef8 --- /dev/null +++ b/rust/WORKSPACE @@ -0,0 +1,123 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# rust +local_repository( + name = "rules_rust", + path = "/Users/takaaki.tagawa/ghq/github.com/bazelbuild/rules_rust", +) +# http_archive( +# name = "rules_rust", +# sha256 = "6bfe75125e74155955d8a9854a8811365e6c0f3d33ed700bc17f39e32522c822", +# urls = [ +# "https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/0.9.0/rules_rust-v0.9.0.tar.gz", +# "https://github.com/bazelbuild/rules_rust/releases/download/0.9.0/rules_rust-v0.9.0.tar.gz", +# ], +# ) + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") + +rules_rust_dependencies() + +rust_register_toolchains() + +load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") + +crate_universe_dependencies(bootstrap = True) + +load("@rules_rust//crate_universe:defs.bzl", "crates_repository") + +crates_repository( + name = "crate_index", + cargo_lockfile = "//:Cargo.lock", + generator_urls = { + "aarch64-apple-darwin": "https://github.com/bazelbuild/rules_rust/releases/download/0.9.0/cargo-bazel-aarch64-apple-darwin", + }, + manifests = [ + "//:Cargo.toml", + "//hello:Cargo.toml", + ], +) + +load("@crate_index//:defs.bzl", "crate_repositories") + +crate_repositories() + +# go +http_archive( + name = "io_bazel_rules_go", + sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + ], +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") + +go_rules_dependencies() + +go_register_toolchains(version = "1.18.3") + +http_archive( + name = "bazel_gazelle", + sha256 = "5982e5463f171da99e3bdaeff8c0f48283a7a5f396ec5282910b9e8a49c0dd7e", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.25.0/bazel-gazelle-v0.25.0.tar.gz", + ], +) + +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") + +gazelle_dependencies() + +# rules_docker +local_repository( + name = "io_bazel_rules_docker", + path = "/Users/takaaki.tagawa/ghq/github.com/bazelbuild/rules_docker", +) +# http_archive( +# name = "io_bazel_rules_docker", +# sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", +# urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], +# ) + +load( + "@io_bazel_rules_docker//repositories:repositories.bzl", + container_repositories = "repositories", +) + +container_repositories() + +# NOTE: it installs go-v0.24.2.tar.gz which contains go v1.15.2. +load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps") + +container_deps() + +# rules_docker rust +load( + "@io_bazel_rules_docker//rust:image.bzl", + _rust_image_repos = "repositories", +) + +_rust_image_repos() + +load( + "@io_bazel_rules_docker//container:container.bzl", + "container_pull", +) + +# container_pull( +# name = "cc_image_base_arm64", +# architecture = "arm64", +# digest = "sha256:85e93527f62963332bf6ca0157d8ebd09ef72e34eff087b6f5ea05020da1dbdc", +# registry = "gcr.io", +# repository = "distroless/cc", +# ) + +# container_pull( +# name = "cc_image_base_arm64", +# registry = "index.docker.io", +# repository = "arm64v8/rust", +# tag = "latest", +# ) diff --git a/rust/hello/BUILD b/rust/hello/BUILD new file mode 100644 index 0000000..6ba5c33 --- /dev/null +++ b/rust/hello/BUILD @@ -0,0 +1,63 @@ +load("@crate_index//:defs.bzl", "aliases", "all_crate_deps") +load("@io_bazel_rules_docker//rust:image.bzl", "rust_image") +load("@rules_rust//rust:defs.bzl", "rust_binary") +load("@rules_rust//rust/platform:platform.bzl", "declare_config_settings") + +declare_config_settings() + +platform( + name = "my_host", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + # "@platforms//cpu:aarch64", + ], +) + +platform( + name = "my_target", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + # "@platforms//cpu:aarch64", + ], +) + +# rust_binary( +# name = "hello", +# srcs = ["src/main.rs"], +# aliases = aliases(), +# edition = "2021", +# proc_macro_deps = all_crate_deps( +# proc_macro = True, +# ), +# visibility = ["//visibility:public"], +# deps = all_crate_deps( +# normal = True, +# ), +# ) + +# rust_image( +# name = "hello_image", +# base = "@cc_image_base_arm64//image", +# binary = ":hello", +# ) + +rust_image( + name = "hello_image", + srcs = glob(["src/*.rs"]), + aliases = aliases(), + # base = "@cc_image_base_arm64//image", + edition = "2021", + proc_macro_deps = all_crate_deps( + proc_macro = True, + ), + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + visibility = ["//visibility:public"], + deps = all_crate_deps( + normal = True, + ), +) diff --git a/rust/hello/Cargo.toml b/rust/hello/Cargo.toml new file mode 100644 index 0000000..5028886 --- /dev/null +++ b/rust/hello/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +#[dependencies] +#thiserror = "1.0" diff --git a/rust/hello/src/main.rs b/rust/hello/src/main.rs new file mode 100644 index 0000000..fbedd92 --- /dev/null +++ b/rust/hello/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} \ No newline at end of file