Skip to content

Commit

Permalink
wip: rust code won't compile
Browse files Browse the repository at this point in the history
* 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. bazelbuild/rules_rust#276
  • Loading branch information
festa78 committed Sep 1, 2022
1 parent cff5b0d commit 61b0607
Show file tree
Hide file tree
Showing 21 changed files with 502 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go_rust/bazel_docker_test/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:macos --@io_bazel_rules_docker//transitions:enable=false
build:macos --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
1 change: 1 addition & 0 deletions go_rust/bazel_docker_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
Empty file added go_rust/bazel_docker_test/BUILD
Empty file.
67 changes: 67 additions & 0 deletions go_rust/bazel_docker_test/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -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": {}
}
5 changes: 5 additions & 0 deletions go_rust/bazel_docker_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]

members = [
"hello_rust",
]
100 changes: 100 additions & 0 deletions go_rust/bazel_docker_test/WORKSPACE
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions go_rust/bazel_docker_test/hello_go/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
name = "hello_go",
srcs = ["src/main.go"],
)
9 changes: 9 additions & 0 deletions go_rust/bazel_docker_test/hello_go/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello, World in go")
}
15 changes: 15 additions & 0 deletions go_rust/bazel_docker_test/hello_rust/BUILD
Original file line number Diff line number Diff line change
@@ -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,
),
)
9 changes: 9 additions & 0 deletions go_rust/bazel_docker_test/hello_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions go_rust/bazel_docker_test/hello_rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
2 changes: 2 additions & 0 deletions rust/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:macos --@io_bazel_rules_docker//transitions:enable=false
build:macos --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
2 changes: 2 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bazel-*
target
Empty file added rust/BUILD
Empty file.
67 changes: 67 additions & 0 deletions rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -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": {}
}
11 changes: 11 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]

members = [
"hello",
]
Loading

0 comments on commit 61b0607

Please sign in to comment.