Skip to content

Commit ea1d3a5

Browse files
committed
Added cross_compile examples
1 parent 8a11b4a commit ea1d3a5

File tree

19 files changed

+587
-3
lines changed

19 files changed

+587
-3
lines changed

.bazelci/presubmit.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,36 @@ tasks:
730730
# working_directory: examples/cross_compile_zig
731731
# build_targets:
732732
# - "//..."
733+
cross_compile_linux:
734+
name: Cross compile example on Linux
735+
platform: macos_arm64
736+
working_directory: examples/cross_compile
737+
shell_commands:
738+
- sed -i '' 's/_FORCE_DISABLE_CC_TOOLCHAIN = False/_FORCE_DISABLE_CC_TOOLCHAIN = True/' rust/private/utils.bzl
739+
build_targets:
740+
- "//..."
741+
test_targets:
742+
- "//..."
743+
cross_compile_macos:
744+
name: Cross compile example on MacOS
745+
platform: ubuntu2204
746+
working_directory: examples/cross_compile
747+
shell_commands:
748+
- sed -i 's/_FORCE_DISABLE_CC_TOOLCHAIN = False/_FORCE_DISABLE_CC_TOOLCHAIN = True/' rust/private/utils.bzl
749+
build_targets:
750+
- "//..."
751+
test_targets:
752+
- "//..."
753+
cross_compile_windows:
754+
name: Cross compile example on Windows
755+
platform: windows
756+
working_directory: examples/cross_compile
757+
batch_commands:
758+
- powershell -Command "(Get-Content rust\private\utils.bzl) -replace '_FORCE_DISABLE_CC_TOOLCHAIN = False', '_FORCE_DISABLE_CC_TOOLCHAIN = True' | Set-Content rust\private\utils.bzl"
759+
build_targets:
760+
- "//..."
761+
test_targets:
762+
- "//..."
733763
cross_compile_musl_macos_to_linux:
734764
name: Cross compile example Musl from macOS to Linux
735765
platform: macos_arm64

examples/cross_compile/.bazelrc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
###############################################################################
2+
## Bazel Configuration Flags
3+
##
4+
## `.bazelrc` is a Bazel configuration file.
5+
## https://bazel.build/docs/best-practices#bazelrc-file
6+
###############################################################################
7+
8+
# https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
9+
common --enable_platform_specific_config
10+
11+
# Enable the only currently supported report type
12+
# https://bazel.build/reference/command-line-reference#flag--combined_report
13+
coverage --combined_report=lcov
14+
15+
# Avoid fully cached builds reporting no coverage and failing CI
16+
# https://bazel.build/reference/command-line-reference#flag--experimental_fetch_all_coverage_outputs
17+
coverage --experimental_fetch_all_coverage_outputs
18+
19+
# Required for some of the tests
20+
# https://bazel.build/reference/command-line-reference#flag--experimental_cc_shared_library
21+
common --experimental_cc_shared_library
22+
23+
###############################################################################
24+
## Unique configuration groups
25+
###############################################################################
26+
27+
# Enable use of the nightly toolchains.
28+
build:nightly --@rules_rust//rust/toolchain/channel=nightly
29+
30+
# Enable rustfmt for all targets in the workspace
31+
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
32+
build:rustfmt --output_groups=+rustfmt_checks
33+
34+
# Enable clippy for all targets in the workspace
35+
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
36+
build:clippy --output_groups=+clippy_checks
37+
38+
# Enable unpretty for all targets in the workspace
39+
build:unpretty --aspects=@rules_rust//rust:defs.bzl%rust_unpretty_aspect
40+
build:unpretty --output_groups=+rust_unpretty
41+
42+
# `unpretty` requires the nightly toolchain. See tracking issue:
43+
# https://github.com/rust-lang/rust/issues/43364
44+
build:unpretty --config=nightly
45+
46+
###############################################################################
47+
## Incompatibility flags
48+
###############################################################################
49+
50+
# https://github.com/bazelbuild/bazel/issues/8195
51+
build --incompatible_disallow_empty_glob=true
52+
53+
# https://github.com/bazelbuild/bazel/issues/12821
54+
build --nolegacy_external_runfiles
55+
56+
# https://github.com/bazelbuild/bazel/issues/23043.
57+
build --incompatible_autoload_externally=
58+
59+
###############################################################################
60+
## Bzlmod
61+
###############################################################################
62+
63+
# A configuration for disabling bzlmod.
64+
common:no-bzlmod --noenable_bzlmod --enable_workspace
65+
66+
# Disable the bzlmod lockfile, so we don't accidentally commit MODULE.bazel.lock
67+
common --lockfile_mode=off
68+
69+
###############################################################################
70+
## Custom user flags
71+
##
72+
## This should always be the last thing in the `.bazelrc` file to ensure
73+
## consistent behavior when setting flags in that file as `.bazelrc` files are
74+
## evaluated top to bottom.
75+
###############################################################################
76+
77+
try-import %workspace%/user.bazelrc

examples/cross_compile/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bazel-*
2+
user.bazelrc

examples/cross_compile/BUILD.bazel

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("@rules_rust//rust:rust_binary.bzl", "rust_binary")
2+
load("@rules_rust//rust:rust_library.bzl", "rust_library")
3+
load("//tools:infer_test.bzl", "infer_test")
4+
5+
PLATFORMS = [
6+
"aarch64-apple-darwin",
7+
"aarch64-pc-windows-msvc",
8+
"aarch64-unknown-linux-gnu",
9+
"wasm32-wasip1",
10+
"x86_64-pc-windows-msvc",
11+
"x86_64-unknown-linux-gnu",
12+
]
13+
14+
rust_library(
15+
name = "secrets",
16+
srcs = ["src/lib.rs"],
17+
edition = "2024",
18+
)
19+
20+
[
21+
rust_binary(
22+
name = "cross_compile_{}".format(platform),
23+
srcs = ["src/main.rs"],
24+
edition = "2024",
25+
platform = "//platforms:{}".format(platform),
26+
deps = [":secrets"],
27+
)
28+
for platform in PLATFORMS
29+
]
30+
31+
[
32+
infer_test(
33+
name = "cross_compile_{}_infer_test".format(platform),
34+
file = ":cross_compile_{}".format(platform),
35+
type = select({
36+
"@platforms//os:macos": "application/x-mach-binary",
37+
"@platforms//os:none": "application/x-mach-binary",
38+
"@platforms//os:windows": "application/x-mach-binary",
39+
"//conditions:default": "application/elf-binary",
40+
}),
41+
)
42+
for platform in PLATFORMS
43+
]

examples/cross_compile/Cargo.lock

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cross_compile/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "cross_compile_example"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
infer = "0.19.0"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module(
2+
name = "cross_compile_example",
3+
version = "0.0.0",
4+
)
5+
6+
bazel_dep(name = "rules_rust", version = "0.0.0")
7+
local_path_override(
8+
module_name = "rules_rust",
9+
path = "../..",
10+
)
11+
12+
bazel_dep(name = "platforms", version = "1.0.0")
13+
bazel_dep(name = "bazel_skylib", version = "1.8.2")
14+
15+
RUST_EDITION = "2024"
16+
17+
RUST_VERSION = "1.91.0"
18+
19+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
20+
rust.toolchain(
21+
edition = RUST_EDITION,
22+
extra_target_triples = [
23+
"aarch64-apple-darwin",
24+
"aarch64-pc-windows-msvc",
25+
"aarch64-unknown-linux-gnu",
26+
"wasm32-unknown-unknown",
27+
"wasm32-wasip1",
28+
"wasm32-wasip2",
29+
"x86_64-apple-darwin",
30+
"x86_64-pc-windows-msvc",
31+
"x86_64-unknown-linux-gnu",
32+
],
33+
versions = [RUST_VERSION],
34+
)
35+
use_repo(rust, "rust_toolchains")
36+
37+
register_toolchains("@rust_toolchains//:all")
38+
39+
crate = use_extension(
40+
"@rules_rust//crate_universe:extensions.bzl",
41+
"crate",
42+
)
43+
crate.from_cargo(
44+
name = "crates_io",
45+
cargo_lockfile = "//:Cargo.lock",
46+
manifests = [
47+
"//:Cargo.toml",
48+
],
49+
)
50+
use_repo(crate, "crates_io")

examples/cross_compile/WORKSPACE.bazel

Whitespace-only changes.

0 commit comments

Comments
 (0)