Skip to content

Commit

Permalink
Build bootloader with bazel (#52)
Browse files Browse the repository at this point in the history
Signed-off-by: Greg Balke <gbalke@berkeley.edu>
  • Loading branch information
gbalke authored Jul 12, 2022
1 parent ae93be8 commit 8c1e87d
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enable toolchain resolution with cc
build --incompatible_enable_cc_toolchain_resolution
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ firmware/.settings/
bd_tools.egg-info/
build/
dist/

bazel-*
5 changes: 5 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

buildifier(
name = "buildifier",
)
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ lint_python:
lint_cpp:
cpplint --recursive common/ bootloader/ firmware/

format: format_python
format: format_python format_bazel

format_python:
isort tests bd_tools
black tests bd_tools

format_bazel:
bazel run //:buildifier

setup: init_submodules install_python_packages install_bd_tools

init_submodules:
Expand Down
96 changes: 96 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

git_repository(
name = "bazel_embedded",
commit = "d3cbe4eff9a63d3dee63067d61096d681daca33b",
remote = "https://github.com/bazelembedded/bazel-embedded.git",
shallow_since = "1585022166 +0800",
)

load("@bazel_embedded//:bazel_embedded_deps.bzl", "bazel_embedded_deps")

bazel_embedded_deps()

load("@bazel_embedded//platforms:execution_platforms.bzl", "register_platforms")

register_platforms()

load(
"@bazel_embedded//toolchains/compilers/gcc_arm_none_eabi:gcc_arm_none_repository.bzl",
"gcc_arm_none_compiler",
)

gcc_arm_none_compiler()

load("@bazel_embedded//toolchains/gcc_arm_none_eabi:gcc_arm_none_toolchain.bzl", "register_gcc_arm_none_toolchain")

register_gcc_arm_none_toolchain()

load("@bazel_embedded//tools/openocd:openocd_repository.bzl", "openocd_deps")

openocd_deps()

git_repository(
name = "rules_meta",
commit = "062936fe3bab149bc2fc664301d8151868a3c874",
remote = "https://github.com/fmeum/rules_meta.git",
shallow_since = "1647421183 +0100",
)

### BUILDIFIER DEPENDENCIES ###
# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "d6b2513456fe2229811da7eb67a444be7785f5323c6708b38d851d2b51e54d83",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.30.0/rules_go-v0.30.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.30.0/rules_go-v0.30.0.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")

go_rules_dependencies()

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains")

go_register_toolchains(version = "1.17.2")

http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

# If you use WORKSPACE.bazel, use the following line instead of the bare gazelle_dependencies():
# gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")
gazelle_dependencies()

http_archive(
name = "com_google_protobuf",
sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568",
strip_prefix = "protobuf-3.19.4",
urls = [
"https://github.com/protocolbuffers/protobuf/archive/v3.19.4.tar.gz",
],
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3",
strip_prefix = "buildtools-4.2.2",
urls = [
"https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz",
],
)
68 changes: 68 additions & 0 deletions bootloader/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("//third_party:variables.bzl", "chibios_inc_paths")
load("//:platforms.bzl", "cc_f4_binary")

cc_f4_binary(
name = "bootloader",
srcs = glob(["src/*.c"]) + glob(["src/*.cpp"]),
copts = chibios_inc_paths + [
"-O2",
"-ggdb",
"-ffunction-sections",
"-fdata-sections",
"-fno-common",
],
linkopts = [
"-T $(location :link.ld)",
"-lc",
"-lm",
"-lnosys",
"-u _printf_float",
],
target_compatible_with = [
"@platforms//cpu:armv7e-m",
],
visibility = ["//visibility:public"],
deps = [
":link.ld",
":main_libs",
"//third_party:chibios_hal",
],
)

cc_library(
name = "main_libs",
hdrs = glob(
["include/*.h"],
exclude = [
"include/stm32f4xx_conf.h",
"include/chconf.h",
"include/halconf.h",
"include/mcuconf.h",
],
) + glob(
["include/*.hpp"],
exclude = [
"include/peripherals.hpp",
],
),
copts = chibios_inc_paths,
strip_include_prefix = "include",
deps = [
"//common",
],
)

cc_library(
name = "os_config",
hdrs = [
"include/chconf.h",
"include/halconf.h",
"include/mcuconf.h",
"include/peripherals.hpp",
"include/stm32f4xx_conf.h",
],
includes = ["include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
Empty file added bootloader/os_config.bzl
Empty file.
26 changes: 26 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//third_party:variables.bzl", "chibios_inc_paths")

filegroup(
name = "board_config_hdrs",
srcs = ["board.h"],
visibility = ["//visibility:public"],
)

filegroup(
name = "board_config_srcs",
srcs = ["board.c"],
visibility = ["//visibility:public"],
)

cc_library(
name = "common",
srcs = glob(["src/*.c"]) + glob(["src/*.cpp"]),
hdrs = glob(["include/*.h"]) + glob(["include/*.hpp"]),
copts = chibios_inc_paths,
strip_include_prefix = "include",
visibility = ["//visibility:public"],
deps = [
"//third_party:chibios_hal",
],
)
9 changes: 9 additions & 0 deletions platforms.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_meta//meta:defs.bzl", "meta")

cc_f4_binary = meta.wrap_with_transition(
native.cc_binary,
{
"platforms": meta.replace_with(["@bazel_embedded//platforms:cortex_m4_fpu"]),
},
executable = True,
)
Loading

0 comments on commit 8c1e87d

Please sign in to comment.