-
Notifications
You must be signed in to change notification settings - Fork 2k
bazel: build and test bare-metal examples #3220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgeisler
wants to merge
1
commit into
main
Choose a base branch
from
bazel-bare-metal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """Root BUILD file for Comprehensive Rust workspace. | ||
|
|
||
| This file defines a central test_suite for running manual QEMU tests. | ||
| """ | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| test_suite( | ||
| name = "bare_metal_tests", | ||
| tags = ["manual"], | ||
| tests = [ | ||
| "//src/bare-metal/aps/examples:improved_test", | ||
| "//src/bare-metal/aps/examples:logger_test", | ||
| "//src/bare-metal/aps/examples:minimal_test", | ||
| "//src/bare-metal/aps/examples:psci_test", | ||
| "//src/bare-metal/aps/examples:rt_test", | ||
| "//src/bare-metal/aps/examples:safemmio_test", | ||
| "//src/exercises/bare-metal/rtc:rtc_test", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # platforms/BUILD.bazel | ||
| """Target Platform Definitions for Embedded Cross-Compilation. | ||
|
|
||
| In Bazel, platforms represent execution and target build environments. | ||
| We define target platforms by declaring their constraint values (CPU | ||
| architecture and Operating System). The registered Rust toolchain | ||
| matches these constraint combinations to determine which | ||
| compiler/linker options to use: | ||
|
|
||
| - `aarch64-unknown-none` represents AArch64 bare-metal (no OS) | ||
| environments. | ||
|
|
||
| - `thumbv7em-none-eabihf` represents ARM Cortex-M4F microcontroller | ||
| environments. | ||
| """ | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| platform( | ||
| name = "aarch64-unknown-none", | ||
| constraint_values = [ | ||
| "@platforms//os:none", | ||
| "@platforms//cpu:aarch64", | ||
| ], | ||
| ) | ||
|
|
||
| platform( | ||
| name = "thumbv7em-none-eabihf", | ||
| constraint_values = [ | ||
| "@platforms//os:none", | ||
| "@platforms//cpu:armv7e-mf", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Starlark Configuration Transitions for Cross-Compilation. | ||
|
|
||
| In Bazel, test targets (like sh_test) are host-executed. If the global | ||
| invocation sets --platforms=//platforms:aarch64-unknown-none, Bazel | ||
| attempts to run the test runners themselves on the target bare-metal | ||
| platform. Because there are no test execution toolchains for | ||
| bare-metal targets, this results in analysis failures. | ||
|
|
||
| To resolve this, we keep the global invocation targeting the host | ||
| platform, and use these custom rules (aarch64_binary, | ||
| thumbv7em_binary) to apply a "transition". A transition modifies the | ||
| configuration (the --platforms flag) for the dependency edge to the | ||
| target binary. This ensures: | ||
|
|
||
| 1. The binaries are always compiled for their specific bare-metal | ||
| target platform. | ||
|
|
||
| 2. The parent test targets (sh_test) are built and run on the host | ||
| platform. | ||
| """ | ||
|
|
||
| def _aarch64_transition_impl(settings, attr): | ||
| return {"//command_line_option:platforms": ["//platforms:aarch64-unknown-none"]} | ||
|
|
||
| _aarch64_transition = transition( | ||
| implementation = _aarch64_transition_impl, | ||
| inputs = [], | ||
| outputs = ["//command_line_option:platforms"], | ||
| ) | ||
|
|
||
| def _transition_rule_impl(ctx): | ||
| # Retrieve the files from the transitioned target | ||
| return [DefaultInfo(files = ctx.attr.dep[0][DefaultInfo].files)] | ||
|
|
||
| aarch64_binary = rule( | ||
| implementation = _transition_rule_impl, | ||
| attrs = { | ||
| "dep": attr.label(cfg = _aarch64_transition), | ||
| "_allowlist_function_transition": attr.label( | ||
| default = "@bazel_tools//tools/allowlists/function_transition_allowlist", | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| def _thumbv7em_transition_impl(settings, attr): | ||
| return {"//command_line_option:platforms": ["//platforms:thumbv7em-none-eabihf"]} | ||
|
|
||
| _thumbv7em_transition = transition( | ||
| implementation = _thumbv7em_transition_impl, | ||
| inputs = [], | ||
| outputs = ["//command_line_option:platforms"], | ||
| ) | ||
|
|
||
| thumbv7em_binary = rule( | ||
| implementation = _transition_rule_impl, | ||
| attrs = { | ||
| "dep": attr.label(cfg = _thumbv7em_transition), | ||
| "_allowlist_function_transition": attr.label( | ||
| default = "@bazel_tools//tools/allowlists/function_transition_allowlist", | ||
| ), | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Bazel BUILD file for AArch64 Bare-Metal Allocator Example. | ||
|
|
||
| This package defines the allocator example target. Because this code | ||
| targets AArch64 bare-metal platforms and not the host OS: | ||
|
|
||
| 1. The raw `rust_binary` is constrained using `target_compatible_with` | ||
| to run only on `os:none` to prevent compilation errors during | ||
| wildcard host builds. | ||
| """ | ||
|
|
||
| load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| rust_binary( | ||
| name = "alloc-example", | ||
| srcs = ["src/main.rs"], | ||
| edition = "2024", | ||
| rustc_flags = [ | ||
| "-C", | ||
| "linker=rust-lld", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have this flag in the |
||
| ], | ||
| target_compatible_with = ["@platforms//os:none"], | ||
| deps = [ | ||
| "@bare_metal_alloc_example//:buddy_system_allocator", | ||
| "@bare_metal_alloc_example//:panic-halt", | ||
| ], | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need both these and the build files in the various directories? How do they interact?