Bazel - How to create Deps for a Rule from dynamic list of targest directories

63 Views Asked by At

I want to create deps in a rule in Bazel that will be dynamic and not hardcoded, from a list of directories.

Here is the file structure:


|   //plugins_folder
├── code0.py
├── BUILD
├── plugin1
|   ├── BUILD
|   ├── code1-0.py
|   ├── code1-1.py
├── plugin2
|   ├── BUILD
|   ├── code2-0.py
|   ├── code2-1.py

File0.py depends on file1-0.py and file2-0.py

I want the BUILD file in the root to use the BUILD files in the plugins without adding them explicitly to the root BUILD, but to calculate the deps dynamically

The root BUILD file should behave like this:

py_library(
    name = "code0",
    srcs = ["code0.py"],
    deps = ["//plugin1:code1-0", "//plugin1:code1-1", "//plugin1:code2-0", "//plugin1:code2-1"]
)

But instead of having the deps above to hardcoded, I want to get them dynamically.

Something like this (obviously, it does not work as glob does not retrieve targets but files):

py_library(
    name = "code0",
    srcs = ["code0.py"],
    deps = [":%s" % x[:-3] for x in glob(["**/*.py"])],
)
1

There are 1 best solutions below

1
On BEST ANSWER

Use subpackages or better yet subpackages from the future compatible bazel_skylib

py_library(
    name = "all_plugins",
    srcs = ["some_source.py"],
    deps = [
        "//plugins_folder/%s:all" % subpkg for subpkg in subpackages(include=["plugin*"])
    ],
)

Verify you have all the dependencies using: bazel query 'deps(//plugin_folder:all_plugins)'