Florian Ludewig
Florian Ludewig

Reputation: 6002

Bazel Build Multiple Targets at Once

I want a Bazel rule that is able to build multiple targets at once. So basically something like this:

build_all(
  name = "build_all",
  targets = [
    "//services/service1:build",
    "//services/service2:build",
    "//services/service3:build",
  ]
)

So I would just run

bazel build //:build_all

to build all my services with one simple command (and the same for testing). But I couldn't find any current solutions.

Is there a way to achieve this?

Upvotes: 11

Views: 15615

Answers (5)

Matthew Johnson
Matthew Johnson

Reputation: 1

The suggestion to use a file group rule as a build-in rule to collect a bunch of dependencies works well if the dependencies are single files (like a cc_binary), but will not trigger creation and collection of auxiliary runfiles (like the labels under "data" in a py_binary) needed for some types of targets.

If you do want to build multiple targets at once, including their runfiles, the sh_binary built in rule with a simple no-opt script is also quite flexible, and you can specify an arbitrary list of targets in its data attribute. These targets will be considered runnable inputs to the sh_binary, and thus will trigger creation of the indirect targets themselves as well as all of their supplementary runtime data.

Upvotes: 0

dieend
dieend

Reputation: 2299

You can also use --build_tag_filters to selectively build multiple targets with a given tags.

EDIT: --build_tag_filters is using OR logic. If you want to build target that satisfy multiple tags, I would suggest you to query it first, and then build the resulting target.

e.g.

QUERY="attr(tags, 'tag1', attr(tags, 'tag2', ...))"
BUILD_TARGETS=$(bazel query "$QUERY")
bazel build "$BUILD_TARGETS"

Upvotes: 1

gph
gph

Reputation: 1360

If you only want a subset of the build targets the other answers are probably better, but you could also try bazel build :all and bazel test :all

Upvotes: 3

Florian Ludewig
Florian Ludewig

Reputation: 6002

Because I was trying to deploy multiple Kubernetes configurations I ended up using Multi-Object Actions for rules_k8s which then looks like:

load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")

k8s_objects(
   name = "deployments",
   objects = [
      "//services/service1:build",
      "//services/service2:build",
      "//services/service3:build",
   ]
)

Upvotes: 2

Ondrej K.
Ondrej K.

Reputation: 9664

It would appear that filegroup would be a ready made rule that could be abused for the purpose:

filegroup(
  name = "build_all",
  srcs = [
    "//services/service1:build",
    "//services/service2:build",
    "//services/service3:build",
  ]
)

It otherwise allows you to give a collective name to bunch of files (labels) to be passed conveniently along, but seems to work just as well as a summary target to use on the command line.

Upvotes: 16

Related Questions