Skip to content

Set target_cpu in toolchain#327

Closed
keith wants to merge 7 commits intobazelbuild:mainfrom
keith:ks/set-target_cpu-in-toolchain
Closed

Set target_cpu in toolchain#327
keith wants to merge 7 commits intobazelbuild:mainfrom
keith:ks/set-target_cpu-in-toolchain

Conversation

@keith
Copy link
Copy Markdown
Member

@keith keith commented Jan 28, 2025

This is currently usable in select statements (which is deprecated but
won't be flipped for 1 year) and also is used internally by bazel for
some paths like _solib_k8. We can derive this from platform configs

@armandomontanez
Copy link
Copy Markdown
Collaborator

I'd really prefer we don't carry this one forward, and instead fix any instances that rely on target_cpu to ensure they're controlled through a more future-looking mechanism.

@keith
Copy link
Copy Markdown
Member Author

keith commented Jan 28, 2025

I agree with that in theory. But given bazel's long deprecation timelines it seems to me that the cost of us carrying it along for a while with a change like this, is very much worth it compared to the migration cost to make sure your transitive closure avoids needing this.

In our moderately sized C++ project at least grpc: grpc/grpc#38319

protobuf: https://github.com/protocolbuffers/protobuf/blob/71c259485c2a40105144c4f0f0d1910a6ae0d169/build_defs/BUILD.bazel#L35-L38

and abseil: https://github.com/abseil/abseil-cpp/pull/1797/files

all use this mechanism. So my thinking is that even if we snapped our fingers and fixed all of today's deprecated uses, it's non-trivial to upgrade these dependencies in any projects that potentially want to use this API.

I'm also quite worried about moving bazel off of this API, here are a few places I found that have been using it forever:

This is currently usable in select statements (which is deprecated but
won't be flipped for 1 year) and also is used internally by bazel for
some paths like `_solib_k8`. We can derive this from platform configs
@keith
Copy link
Copy Markdown
Member Author

keith commented Feb 18, 2025

@armandomontanez ptal

Copy link
Copy Markdown
Collaborator

@armandomontanez armandomontanez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this doesn't modify the public API, I'm inclined to just move forward with this.

Comment thread cc/toolchains/BUILD Outdated
],
)

config_setting(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should definitely be private. we don't want people to start getting these from rules_cc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing now that it's possible making these private will inherently not work since they're referenced under a macro. If that's the case, we should probably put them in impl/ to at least signal that they shouldn't be referenced externally.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to solve this is to just split the attrs into OS and CPU so you don't need the config_setting, and then do the conversion at the starlark level. Not the prettiest but it prevents misuse.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea the macro was the issue, i moved them to impl. should be pretty clear that they're private, im not sure if buildifier lints against impl or just internal though

known_features = known_features,
enabled_features = enabled_features,
compiler = compiler,
cpu = select({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this have a fallback case for other platforms? Also, what about Windows? It does have "well-known" legacy CPU values.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea I wasn't sure what other ones we thought this supported today. happy to add whichever here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"//conditions:default" : "" is important so that this doesn't throw an error if one of these branches isn't followed. IMO it's fine to incrementally iterate on this list as we uncover more magic.

For windows, protobuf seems to check "win32" and "win64". That's probably the closest we'll find to a "source of truth" for that one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the windows ones.

I used local as the default because that appears to be what the legacy toolchain used. but we can do an empty string too if we want? maintaining compat on that one doesn't feel as important to me

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing anywhere that relies on "local", so in theory the value is arbitrary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switched to empty instead

known_features = known_features,
enabled_features = enabled_features,
compiler = compiler,
cpu = select({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing anywhere that relies on "local", so in theory the value is arbitrary.

armandomontanez and others added 2 commits February 26, 2025 09:06
Refers to @rules_cc labels via Label rather than @rules_cc.
@keith keith deleted the ks/set-target_cpu-in-toolchain branch April 24, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants