Bazel build: Add extra dependencies not in `WORKSPACE`

421 views Asked by At

I'd like to write some profiling scripts on a project, and use action_listener or aspect to take action. However, it's better not modifying the project files. Is there any way to add some external dependencies without change WORKSPACE file?

1

There are 1 answers

0
Vertexwahn On

WORKSPACE

workspace(name = "my_workspace")

load("//:bazel/third_party/my_profile_deps.bzl", "my_profile_deps")

my_profile_deps()

bazel/third_party/my_profile_deps.bzl

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

def my_profile_deps():
    """Fetch my profile deps."""
    

    if True: # Switch to turn on/off
        # ... 
        maybe(
            http_archive,
            name = "some_lib",
            build_file = "...",
            sha256 = "...",
            strip_prefix = "..",
            urls = [
                "...",
                "...",
            ],
        )

Dependencies are only fetched if needed. Therefore, you can think of introducing configurable build attributes.