https://github.com/oliverlee/cortex_m
Bazel rules and utilities for Cortex-M
https://github.com/oliverlee/cortex_m
bazel c cortex-m cpp embedded gdb nix qemu
Last synced: 6 months ago
JSON representation
Bazel rules and utilities for Cortex-M
- Host: GitHub
- URL: https://github.com/oliverlee/cortex_m
- Owner: oliverlee
- Created: 2022-05-16T20:05:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-09-02T21:09:44.000Z (10 months ago)
- Last Synced: 2025-09-02T22:10:15.682Z (10 months ago)
- Topics: bazel, c, cortex-m, cpp, embedded, gdb, nix, qemu
- Language: Starlark
- Homepage:
- Size: 1.35 MB
- Stars: 0
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Tools for building and testing for Cortex-M with Bazel
gif

# building
An elf can be built for a target platform by specifying the platform
```sh
bazel build \
--platforms=@cortex_m//platform:lm3s6965evb \
```
where `` is a `cc_binary` target.
Supported platforms provide generic startup code associated with the default
registered toolchain.
# running with `qemu`
An elf can be run with `qemu-system-arm`. This requires an installation of `nix`
to download the `qemu` package.
```sh
bazel run \
--run_under=@cortex_m//:qemu_runner \
--platforms=@cortex_m//platform:lm3s6965evb \
--
```
By default, the QEMU runner sets the machine to match the target platform.
Additional arguments can be passed to `qemu-system-arm` after `--` (e.g., the
GDB connection or to freeze CPU at startup).
# enabling semihosting
By default, semihosting is not enabled when building targets. It can be enabled
with bool flag `--@cortex_m//config:semihosting`.
```sh
bazel run \
--run_under=@cortex_m//:qemu_runner \
--platforms=@cortex_m//platform:lm3s6965evb \
--@cortex_m//config:semihosting \
```
# running tests with `qemu`
`cc_test` targets can be built for the target platform and run under emulation
with a CcTestRunner that uses `qemu-system-arm`. This runner is registered by
default. Note that `--@cortex_m//config:semihosting` must be passed so that elf
is built with semihosting enabled.
```sh
bazel test \
--platforms=@cortex_m//platform:lm3s6965evb \
--@cortex_m//config:semihosting \
//...
```
# flagless builds
`transition_config_binary` and `transition_config_test` can be used to
transition a binary target to always enable or disable semihosting, set the
target platform, and specify extra toolchains.
```starlark
load(
"@cortex_m//rules:transitions.bzl",
"transition_config_binary",
"transition_config_test",
)
transition_config_binary(
name = "binary",
src = ":_binary",
semihosting = "enabled",
platform = "@cortex_m//platform:lm3s6965evb",
)
transition_config_test(
name = "test",
src = ":_test",
platform = "@cortex_m//platform:lm3s6965evb",
)
```
# developing
Developing requires `bash`, `nix`, and coreutils on the `$PATH` to bootstrap
the Nix shell application used in `tools/bazel`. On NixOS, enable [nix-ld] or
use an [FHS environment] (e.g., see the [flake](./flake.nix) used by garnix ci).
[nix-ld]: https://github.com/nix-community/nix-ld
[FHS environment]: https://nixos.org/manual/nixpkgs/stable/#sec-fhs-environments