checkmark1234321
checkmark1234321

Reputation: 33

BAZEL: How to exclude certain targets if a configuration setting is true

Let's say I have 3 libraries in different folders:

a_library(
  name = "a",
)
b_library(
  name = "b",
)
c_library(
  name = "c",
)

I want to bazel build ... to build all libraries, while bazel build ... -c dbg only builds a_library and b_library. I'm wondering what I should change?

What i've tried:

I tried following https://bazel.build/docs/configurable-attributes, where I made a config setting for dbg and selected tag=['default'] when you passed in -c dbg, but apparently tags aren't configurable.

I've also tried modifying my .bazelrc file to add a tag to c_library. Eg: build --build_tag_filters="disable_for_dbg", but I'm not sure how to get this to only run when I pass -c dbg

Any help is appreciated, thanks!

Upvotes: 2

Views: 5285

Answers (1)

Brian Silverman
Brian Silverman

Reputation: 3868

Incompatible target skipping is the feature for this. It'll exclude the libraries from wildcards, and anything which (transitively) depends on them. If you explicitly list one of them on the command line (bazel build -c dbg c), you'll get an error. Like this:

config_setting(
    name = "debug_build",
    values = {
        "compilation_mode": "dbg",
    },
)

c_library(
    name = "c",
    target_compatible_with = select({
        ":debug_build": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
)

@platforms//:incompatible is not part of any platform, so any target with target_compatible_with that includes it will always be skipped. target_compatible_with is configurable, so you can add that platform constraint conditionally via select.

Upvotes: 4

Related Questions