Add DTS support to Bazel builds of Zephyr

Add 2 functions exposed by defs.bzl:
- dts_library: create a library by squashing DTS files into a merged
  DTS file. This can be thought of as a single layer of an overlay.
- dts_cc_library: Takes a dts_library and converts it to an interface
  library with a single header.

In order to test this, native_sim overlay was added. See README for more.

Change-Id: If6f36fdb15ea33f525049158271e859e0369fad5
Reviewed-on: https://pigweed-review.googlesource.com/c/zephyr/zephyr-bazel/+/217851
Commit-Queue: Yuval Peress <peress@google.com>
Reviewed-by: Sergio Soares <sergiosoares@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
13 files changed
tree: f72c7aa257d485546a1e4cc3ef445d5cb44b2bea
  1. boards/
  2. dts/
  3. include/
  4. scripts/
  5. AUTHORS
  6. BUILD.bazel
  7. CONTRIBUTING.md
  8. defs.bzl
  9. LICENSE
  10. OWNERS
  11. README.md
  12. WORKSPACE
README.md

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",
        ],
    }),
)