Skip to content

Commit 2e2d2a6

Browse files
committed
Added cross_compile examples
1 parent 57d3485 commit 2e2d2a6

File tree

15 files changed

+584
-9
lines changed

15 files changed

+584
-9
lines changed

.bazelci/presubmit.yml

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,40 @@ tasks:
723723
working_directory: examples/compile_opt
724724
build_targets:
725725
- "//..."
726-
# TODO: https://github.com/bazelbuild/rules_rust/issues/2075
727-
# cross_compile_zig:
728-
# name: Cross compile example with Zig
729-
# platform: ubuntu2204
730-
# working_directory: examples/cross_compile_zig
731-
# build_targets:
732-
# - "//..."
726+
cross_compile_linux:
727+
name: Cross compile example on Linux
728+
platform: ubuntu2204
729+
working_directory: examples/cross_compile
730+
shell_commands:
731+
- sed -i 's/_FORCE_DISABLE_CC_TOOLCHAIN = False/_FORCE_DISABLE_CC_TOOLCHAIN = True/' ../../rust/private/utils.bzl
732+
test_flags:
733+
- "--keep_going"
734+
test_targets:
735+
- "//..."
736+
cross_compile_macos:
737+
name: Cross compile example on MacOS
738+
platform: macos_arm64
739+
working_directory: examples/cross_compile
740+
shell_commands:
741+
- sed -i '' 's/_FORCE_DISABLE_CC_TOOLCHAIN = False/_FORCE_DISABLE_CC_TOOLCHAIN = True/' ../../rust/private/utils.bzl
742+
test_flags:
743+
- "--keep_going"
744+
test_targets:
745+
- "//..."
746+
cross_compile_windows:
747+
name: Cross compile example on Windows
748+
platform: windows
749+
working_directory: examples/cross_compile
750+
batch_commands:
751+
- 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"
752+
test_flags:
753+
- "--keep_going"
754+
test_targets:
755+
- "//..."
733756
cross_compile_musl_macos_to_linux:
734757
name: Cross compile example Musl from macOS to Linux
735758
platform: macos_arm64
736759
working_directory: examples/cross_compile_musl
737-
build_targets:
738-
- "//..."
739760
test_targets:
740761
- "//..."
741762
cross_compile_musl_linux_to_linux:
@@ -775,6 +796,13 @@ tasks:
775796
- "//:all"
776797
test_targets:
777798
- "//..."
799+
# TODO: https://github.com/bazelbuild/rules_rust/issues/2075
800+
# cross_compile_zig:
801+
# name: Cross compile example with Zig
802+
# platform: ubuntu2204
803+
# working_directory: examples/cross_compile_zig
804+
# build_targets:
805+
# - "//..."
778806
example_ffi_linux:
779807
platform: ubuntu2204
780808
working_directory: examples/ffi

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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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-unknown-linux-musl",
8+
"wasm32-wasip1",
9+
"x86_64-unknown-linux-musl",
10+
11+
# TODO: Windows requires additional libraries at link time for
12+
# cross-compilation. A workaround is needed.
13+
"aarch64-pc-windows-msvc",
14+
"x86_64-pc-windows-msvc",
15+
]
16+
17+
rust_library(
18+
name = "secrets",
19+
srcs = ["src/lib.rs"],
20+
edition = "2024",
21+
)
22+
23+
[
24+
rust_binary(
25+
name = "cross_compile_{}".format(platform),
26+
srcs = ["src/main.rs"],
27+
edition = "2024",
28+
platform = "//platforms:{}".format(platform),
29+
deps = [":secrets"],
30+
)
31+
for platform in PLATFORMS
32+
]
33+
34+
[
35+
infer_test(
36+
name = "cross_compile_{}_infer_test".format(platform),
37+
file = ":cross_compile_{}".format(platform),
38+
type = {
39+
"aarch64-apple-darwin": "application/x-mach-binary",
40+
"aarch64-pc-windows-msvc": "application/vnd.microsoft.portable-executable",
41+
"aarch64-unknown-linux-musl": "application/x-executable",
42+
"wasm32-wasip1": "application/wasm",
43+
"x86_64-pc-windows-msvc": "application/vnd.microsoft.portable-executable",
44+
"x86_64-unknown-linux-musl": "application/x-executable",
45+
}.get(platform, platform),
46+
)
47+
for platform in PLATFORMS
48+
]

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"

0 commit comments

Comments
 (0)