Zephyr-Bazel

This repository contains the logic required to build Zephyr with Bazel. To get started, add the repo to your git submodules or West manifest via https://pigweed.googlesource.com/zephyr/zephyr-bazel/. For the purpose of this doc, we'll assume you have everything checked out under third_party/:

  • third_party/zephyr/ - contains your checkout of Zephyr upstream.
  • third_party/zephyr-bazel/ - contains this repo.

In your root WORKSPACE file, add the following:

# Zephyr
new_local_repository(
    name = "zephyr",
    path = "third_party/zephyr",
    build_file_content = """
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
package(default_visibility = ["//visibility:public"])

exports_files(glob(["**"]))

directory(
    name = "root",
    srcs = glob(["**"], exclude = ["BUILD"])
)
"""
)

# Zephyr-bazel
local_repository(
    name = "zephyr-bazel",
    path = "third_party/zephyr-bazel",
)

Note that the above requires skylib 1.7.1 or greater! If you don't have it, you can use:

http_archive(
    name = "bazel_skylib",
    sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
    ],
)

Next, you‘ll need to make sure all the Python requirements from Zephyr are also loaded. If you haven’t already added pip_parse to your WORKSPACE, that load command is included in the following:

load("@rules_python//python:pip.bzl", "pip_parse")

# Load the Zephyr pip requirements
pip_parse(
    name = "py_deps",
    python_interpreter_target = interpreter,
    requirements_lock = "@@zephyr-bazel//scripts:requirements-base-lock.txt",
)

# Load the generated install command
load("@py_deps//:requirements.bzl", zephyr_install_deps = "install_deps")

# Run the installer
zephyr_install_deps()

Application

In your main application, you can now use the Zephyr utilities for building your app.

load("@zephyr-bazel//:defs.bzl", "dts_cc_library")

dts_cc_library(
    name = "app_native_sim_dts",
    dts_lib = "@zephyr-bazel//boards/native/native_sim:native_sim",
)

cc_binary(
    ...
    deps = [
        ...
        "@zephyr-bazel//:zephyr",
    ] + select({
        "@platform//host": [
            ":app_native_sim_dts",
            "@zephyr-bazel//:posix",
        ],
    }),
    copts = select({
        "@platform//host": [
            "-DCONFIG_ARCH_POSIX=1",
        ],
    }),
)