bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Syntactic sugar for `target_compatible_with` a `bool_flag`, `config_setting`

Open tpudlik opened this issue 1 year ago • 2 comments

Description of the feature request:

I would like to mark a target as target_compatible_with a config_setting. I hoped I could do,

target_compatible_with = [":my_config_setting"]

It turns out this is not supported. Instead, I must express this as,

target_compatible_with = select({
  ":my_config_setting": [],
  "//conditions:default": ["@platforms//:incompatible"],
})

That's a little verbose, and a little obscure!

Actually, things are even worse than this, because the config_setting is used only to detect that a Skylib bool_flag is true. What I'd really love to say is,

bool_flag(
  name = "my_flag",
  build_setting_default = False,
)

cc_library(
  target_compatible_with = [":my_flag"],
)

But instead I write,

bool_flag(
  name = "my_flag",
  build_setting_default = False,
)

config_setting(
  name = "my_flag_true",
  flag_values =  {
    ":my_flag": "True",
  },
)

cc_library(
  target_compatible_with = select({
    ":my_flag_true": [],
    "//conditions:default": ["@platforms//:incompatible"],
  }),
)

@katre

Which category does this issue belong to?

Configurability

What underlying problem are you trying to solve with this feature?

No response

Which operating system are you running Bazel on?

No response

What is the output of bazel info release?

No response

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

tpudlik avatar Mar 29 '24 21:03 tpudlik

To clarify, the underlying problem is that:

  1. target_compatible_with expects to receive constraint_value targets
  2. You are instead passing it a config_setting, and
  3. The special value @platforms//:incompatible isn't actually special to bazel at all, it's just defined in a way that no platform will ever match it.

We can certainly take a look at supporting this, but I don't actually think it's going to be a clean change to the current support.

katre avatar Apr 01 '24 10:04 katre

Yeah, that's right. I just wanted to report that this user journey is a bit awkward for me. But I understand it may not be easy to smooth out!

tpudlik avatar Apr 01 '24 23:04 tpudlik