Update build rule application

Prior to this CL, the build rules for zephyr-bazel required a reference
to @zephyr//. This approach worked, but was limiting (we weren't able to
use globs). Instead, this new approach generates a patch on the fly and
applies it to Zephyr such that the BUILD.bazel and .bzl files in this
repo are copied to Zephyr's repo directly. The benefits:

1. The used can choose the name. @zephyr is not required.
2. We can reference files as though they were local.
3. We can check for merge conflicts with Zephyr in CI since we're
   generating a patch and applying it directly to Zephyr.

BUG=b/346596832

Change-Id: Ifc024c1dec485f4b7d5140b4c1fb20cffeee68b5
Reviewed-on: https://pigweed-review.googlesource.com/c/zephyr/zephyr-bazel/+/223215
Reviewed-by: Sergio Soares <sergiosoares@google.com>
Commit-Queue: Yuval Peress <peress@google.com>
14 files changed
tree: 34af9ad49570c8e3a29bc095f3764c804049bf58
  1. boards/
  2. dts/
  3. examples/
  4. include/
  5. scripts/
  6. AUTHORS
  7. BUILD.bazel
  8. CONTRIBUTING.md
  9. defs.bzl
  10. generate_diff.py
  11. LICENSE
  12. MODULE.bazel
  13. MODULE.bazel.lock
  14. OWNERS
  15. README.md
  16. setup.bzl
  17. WORKSPACE
README.md

Zephyr-Bazel

The way this repository works is by overlaying itself on top of Zephyr. To get started, first make sure you have Bazel‘s skylib version 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",
    ],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

To grab the zephyr-bazel repo, use a git_repository rule in your WORKSPACE, such as:

git_repository(
    name = "zephyr-bazel",
    remote = "https://pigweed.googlesource.com/zephyr/zephyr-bazel"
    branch = "main",
)

Once you have @zephyr-bazel, we can load the patch rule to generate the Zephyr diff that will augment Zephyr to include the BUILD.zephyr rules.

load("@zephyr-bazel//:setup.bzl", "create_zephyr_patch_file")

create_zephyr_patch_file(
    name = "zephyr-patch",
    filename = "patch.diff",
    # This is optional, use it to see what's going on under the hood
    debug = True,
)

We now have a diff file at @zephyr-patch//:patch.diff. We're ready to load Zephyr.

git_repository(
    name = "zephyr",
    remote = "https://github.com/zephyrproject-rtos/zephyr.git",
    branch = "main",
    patches = [
        "@zephyr-patch//:patch.diff",
    ],
)

The final step will be to load Zephyr's python dependencies:

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

pip_parse(
    name = "py_deps",
    python_interpreter_target = interpreter,
    requirements_lock = "@@zephyr//:scripts/requirements-base.txt",
)

load("@py_deps//:requirements.bzl", zephyr_install_deps = "install_deps")

zephyr_install_deps()

Application

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

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

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

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