Skip to content

Commit 580940d

Browse files
committed
Added cross_compile examples
1 parent 8a11b4a commit 580940d

File tree

19 files changed

+594
-10
lines changed

19 files changed

+594
-10
lines changed

.bazelci/presubmit.yml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,36 @@ 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: macos_arm64
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+
build_targets:
733+
- "//..."
734+
test_targets:
735+
- "//..."
736+
cross_compile_macos:
737+
name: Cross compile example on MacOS
738+
platform: ubuntu2204
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+
build_targets:
743+
- "//..."
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+
build_targets:
753+
- "//..."
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
@@ -775,6 +798,13 @@ tasks:
775798
- "//:all"
776799
test_targets:
777800
- "//..."
801+
# TODO: https://github.com/bazelbuild/rules_rust/issues/2075
802+
# cross_compile_zig:
803+
# name: Cross compile example with Zig
804+
# platform: ubuntu2204
805+
# working_directory: examples/cross_compile_zig
806+
# build_targets:
807+
# - "//..."
778808
example_ffi_linux:
779809
platform: ubuntu2204
780810
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: 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"

0 commit comments

Comments
 (0)