{"id":13450683,"url":"https://github.com/bazelembedded/bazel-embedded","last_synced_at":"2025-03-23T16:31:57.225Z","repository":{"id":42620793,"uuid":"222221762","full_name":"bazelembedded/bazel-embedded","owner":"bazelembedded","description":"Tools for embedded/bare-metal development using bazel","archived":false,"fork":false,"pushed_at":"2024-04-25T04:19:00.000Z","size":238,"stargazers_count":109,"open_issues_count":17,"forks_count":31,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-19T16:51:53.357Z","etag":null,"topics":["arm","bare-metal","bazel","bazel-toolchain","cortex-m","embedded"],"latest_commit_sha":null,"homepage":"https://www.nb.rough.run/tags/bazel/","language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bazelembedded.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["silvergasp"]}},"created_at":"2019-11-17T08:59:27.000Z","updated_at":"2025-02-01T06:24:27.000Z","dependencies_parsed_at":"2024-04-06T03:32:16.402Z","dependency_job_id":"879d881f-3f30-4418-8ac5-4d5fcf0e38c5","html_url":"https://github.com/bazelembedded/bazel-embedded","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Fbazel-embedded","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Fbazel-embedded/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Fbazel-embedded/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Fbazel-embedded/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bazelembedded","download_url":"https://codeload.github.com/bazelembedded/bazel-embedded/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245130905,"owners_count":20565742,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm","bare-metal","bazel","bazel-toolchain","cortex-m","embedded"],"created_at":"2024-07-31T07:00:37.286Z","updated_at":"2025-03-23T16:31:56.891Z","avatar_url":"https://github.com/bazelembedded.png","language":"Starlark","funding_links":["https://github.com/sponsors/silvergasp"],"categories":["Tooling"],"sub_categories":["Toolchains"],"readme":"# bazel-embedded\n\n[![CI](https://github.com/bazelembedded/bazel-embedded/actions/workflows/main.yml/badge.svg)](https://github.com/bazelembedded/bazel-embedded/actions/workflows/main.yml)\n\n\n\u003cimg src=\"https://upload.wikimedia.org/wikipedia/en/7/7d/Bazel_logo.svg\" alt=\"drawing\" width=\"200\"/\u003e\n\nAt this point it is relatively easy to add support for new architectures, that have gcc based compilers. In future we will be adding clang support, so that we can make use of clangs static-analyzers. If you would like an architecture added to this repository let us know.\n\nCurrently supported hosts:\n- Windows\n- Mac/Unix\n- Linux\n\nCurrent support is limited to Arm Cortex-M Devices:\n- Cortex M0\n- Cortex M1\n- Cortex M3\n- Cortex M4 (with/out fpu)\n- Cortex M7 (with/out fpu)\n\n## What is included\nList of support;\n- [x] Toolchains\n- [ ] Static analysers \n- [X] A collection of BUILD file templates for common embedded libraries\n- [x] Utilities for programming targets\n- [x] Utilities for debugging targets\n- [ ] Parallel execution for a test \"farm\" of embedded test devices\n\n## Our company needs feature X and would pay for its development\nReach out to [@silvergasp](https://github.com/silvergasp).\n\n## Getting started\nAdd the following to your WORKSPACE file\n\n\n```py\nload(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")\n\ngit_repository(\n    name = \"bazel_embedded\",\n    commit = \"d3cbe4eff9a63d3dee63067d61096d681daca33b\",\n    remote = \"https://github.com/bazelembedded/bazel-embedded.git\",\n    shallow_since = \"1585022166 +0800\",\n)\n\nload(\"@bazel_embedded//:bazel_embedded_deps.bzl\", \"bazel_embedded_deps\")\n\nbazel_embedded_deps()\n\nload(\"@bazel_embedded//platforms:execution_platforms.bzl\", \"register_platforms\")\n\nregister_platforms()\n\nload(\n    \"@bazel_embedded//toolchains/compilers/gcc_arm_none_eabi:gcc_arm_none_repository.bzl\",\n    \"gcc_arm_none_compiler\",\n)\n\ngcc_arm_none_compiler()\n\nload(\"@bazel_embedded//toolchains/gcc_arm_none_eabi:gcc_arm_none_toolchain.bzl\", \"register_gcc_arm_none_toolchain\")\n\nregister_gcc_arm_none_toolchain()\n\nload(\"@bazel_embedded//tools/openocd:openocd_repository.bzl\", \"openocd_deps\")\n\nopenocd_deps()\n```\n\nAdd the following to your .bazelrc file\n```\n# Enable toolchain resolution with cc\nbuild --incompatible_enable_cc_toolchain_resolution\n```\n\nCross Compile your target\n\n```sh\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m0\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m1\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m3\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m4\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m7\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m4_fpu\nbazel build //:your_target --platforms=@bazel_embedded//platforms:cortex_m7_fpu\n```\n\nExplore the examples for more in depth details...\n\nYou may choose to upload your code to a microcontroller using the [openocd](tools/openocd/README.md) package which allows you to program using SWD or JTAG. An example of this is shown below;\n\nBUILD\n\n```python\nload(\"@rules_cc//cc:defs.bzl\", \"cc_binary\")\nload(\"@bazel_embedded//tools/openocd:defs.bzl\", \"openocd_debug_server\", \"openocd_flash\")\n\n# This target can be run to launch a gdb server on port 3333\nopenocd_debug_server(\n    name = \"main_debug_server\",\n    device_configs = [\n        \"target/stm32h7x_dual_bank.cfg\",\n    ],\n    interface_configs = [\n        \"interface/stlink.cfg\",\n    ],\n    transport = \"hla_swd\",\n)\n# The target to flash\ncc_binary(\n    name = \"main\",\n    srcs = [\"main.cc\"],\n    linkopts = [\n        # app_code.ld is a linker script in the same directory\n        \"-T $(location :app_code.ld)\",\n        \"-lc\",\n        \"-lm\",\n        \"-lnosys\",\n        \"-u _printf_float\",\n    ],\n    visibility = [\"//visibility:public\"],\n    deps = [\n        \":app_code.ld\",\n        \"//libs/cpp/board_support:board_support_package\",\n    ],\n)\n# Run this target to upload to the microcontroller\nopenocd_flash(\n    name = \"main_flash\",\n    device_configs = [\n        \"target/stm32h7x_dual_bank.cfg\",\n    ],\n    image = \":main.stripped\",\n    interface_configs = [\n        \"interface/stlink.cfg\",\n    ],\n    transport = \"hla_swd\",\n)\n```\n\nAlternatively, an execution wrapper can be used to execute a binary on an embedded target using openocd, and bazel's `--run_under` command line option.\n\nBUILD\n```python\nload(\"@bazel_embedded//tools/openocd:defs.bzl\", \"openocd_execution_wrapper\")\n\nopenocd_execution_wrapper(\n    name = \"test_wrapper\",\n    baud_rate = \"115200\",\n    device_configs = [\n        \"target/stm32h7x_dual_bank.cfg\",\n    ],\n    fail_string = \"FAILED\",\n    interface_configs = [\n        \"interface/stlink.cfg\",\n    ],\n    port = \"/dev/ttyACM0\",\n    success_string = \"PASSED\",\n    transport = \"hla_swd\",\n)\n```\n```sh\nbazel run //:your_target --platforms=@bazel_embedded//platforms:cortex_m7_fpu --run_under=//:test_wrapper\nbazel test //:your_target --platforms=@bazel_embedded//platforms:cortex_m7_fpu --run_under=//:test_wrapper\n```\nThis will pipe the serial output from the microcontroller over /dev/ttyACM0 to stdout. If a line contains 'PASSED', the wrapper will return 0, if a line contains 'FAILED' the wrapper will return 1. This is useful if you are wrapping a cc_test. If success_string or fail_string is not specified, the wrapper will not exit unless sent a sigterm (e.g. by CTRL-C). Leaving these empty can be useful when wrapping a standard cc_binary.\n\n## How to have your embedded code coexist with host code\nThere are a number of cases where you may want to build and test everything but running.\n\n```bash\nbazel build //...\n```\n\nHowever by default this will capture and build all libraries for the host. This includes libraries that are only compatible with the host system. This can lead to scenarios where code will not compile on your host breaking a wildcards for building. As of bazel 4.0, incompatible target skipping is now supported which allows you to specify the constraint_values that your target is compatible. Bazel will then skip targets included in the wildcard that are not supported. If a target is built with the wrong target explicitly bazel will issue a warning saying that your target is not supported with the given platform.\n\ne.g. \n\n```python\n\n# Linux only\ncc_library(\n    name = \"linux_only_lib\",\n    srcs = [\"some_lib.cc\"],\n    target_compatible_with = [\n        \"@platforms//os:linux\",\n    ],\n)\n\n# Compatible with everything\ncc_library(\n    name = \"generic\",\n    srcs = [\"generic.cc\"],\n)\n\n# Cortex m4 only\ncc_library(\n    name = \"cortex_m4_only_lib\",\n    srcs = [\"some_lib_m4.cc\"],\n    target_compatible_with = [\n        \"@platforms//cpu:armv7e-m\",\n    ],\n)\n\n# All cortex-m architectures\ncc_library(\n    name = \"cortex_m_all_lib\",\n    srcs = [\"some_lib_m_all.cc\"],\n    # Allow all cortex m architectures using a select statement. Reject anything else\n    target_compatible_with = select({\n        \"@platforms//cpu:armv6-m\": [],\n        \"@platforms//cpu:armv7-m\": [],\n        \"@platforms//cpu:armv7e-m\": [],\n        \"//conditions:default\": [\"@platforms//:incompatible\"],\n    ],\n)\n\n# Depends on platform specific target therefore can only be built with cortex m4\ncc_library(\n    name = \"depends_on_m4\",\n    deps = [\":cortex_m4_only_lib\"],\n)\n```\n\nFor reference the following architectures constraint values map to cpu layouts.\n\n\"@platforms//cpu:armv6-m\" -\u003e Cortex-M0, Cortex-M0+, Cortex-M1\n\n\"@platforms//cpu:armv7-m\" -\u003e Cortex-M3\n\n\"@platforms//cpu:armv7e-m\" -\u003e Cortex-M4, Cortex-M7\n\n\n## Feature configuration\nBazel can be configured using [features](https://docs.bazel.build/versions/master/cc-toolchain-config-reference.html#features). Each toolchain in this repository aims to implement a consistent behaviour for a given set of features. The list of available configuration features can be listed using the following command.\n```bash\nbazel run @bazel_embedded//toolchains/features:print_all_features\n```\nFrom here you may use each feature from the command line, the following example enables all warnings as errors and optimises for release;\n```bash\nbazel build //toolchains/compilation_tests/... --platforms=@bazel_embedded//platforms:cortex_m0 --features=all_warnings_as_errors,opt\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelembedded%2Fbazel-embedded","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbazelembedded%2Fbazel-embedded","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelembedded%2Fbazel-embedded/lists"}