-
Notifications
You must be signed in to change notification settings - Fork 525
Add support for optional cc_toolchain #3665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
7cc8279 to
ff94a0b
Compare
c7dccbf to
95a3d34
Compare
92d42c4 to
43b8276
Compare
|
@illicitonion @krasimirgg curious about your thoughts here 🙏 |
| if include: | ||
| env["INCLUDE"] = include | ||
| # Defaults for cxx flags. | ||
| env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we define these inside the build_script_runner executable to avoid passing extra env vars we don't need?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#3665 (comment) for this reason I think this is harder than it's worth or spreads out configuration definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can punt for now. In general I want us to stop using .path as that makes path-mapping not work, so many times we are building crates in multiple configurations even when not really necessary. But lets address in followups
cargo/private/BUILD.bazel
Outdated
| name = "no_ar", | ||
| srcs = ["no_binary.rs"], | ||
| edition = "2021", | ||
| rustc_env = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we take the busybox approach? build a single copy of this, symlink it a few times, and then look at argv[0] to get the name it was invoked with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit trickier to do so since I'd either need to write tempdir logic or come up with some heuristic for where to put these files where I feel this approach is more stable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need a TMPDIR? You can define the targets exactly as now, in this package. Just change the rule to be a copy_file(symlink=True) instead of a brand new binary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated! I'm nervous about symlink=True as I don't trust symlinks to work on windows. E.g. bazelbuild/bazel#21747
rust/private/repository_utils.bzl
Outdated
| linker_label = "None" | ||
| linker_type = "None" | ||
| if include_linker: | ||
| linker_label = "\"//:rust-lld\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be a bit cleaner to do
linker_label = None
if include_linker:
linker_label = "//:rust-lld"
...
template.format(linker_label = repr(linker_label))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
| skip_next = False | ||
| continue | ||
|
|
||
| # Strip -Wl, prefix if using direct driver (it's only for compiler drivers) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in my mind this is another +1 to switching to cc_common.link :)
| cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration), | ||
| map_each = get_lib_name, | ||
| format_each = "-lstatic=%s", | ||
| map_each = _get_dirname, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we actually need to use these -L or can we just pass static_runtime_lib directly so it expands to the actual libs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably pass static_runtime_lib but my changes here are mostly cleanup so I wouldn't want to include that in this PR.
| ) | ||
|
|
||
| def toolchain_linker_preference(): | ||
| """A flag to control which linker is preferred for linking Rust binaries. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you expand this a bit to explain the difference between this setting and the cc_common.link one? i.e. what's the difference between enabling that and setting this one to cc? I'm worried that we're getting so many different linker options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe the options should be "rust means rust linker, cc means use cc_common.link" so there's fewer possibilities?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this parameter is a bit awkward. The idea is that even if you have both provided rust_toolchain.linker and a cc_toolchain is configured, that you can be explicit about what linker you'd like to use. The primary use case is just to force the use of rust_toolchain.linker even when a cc_toolchain is defined.
rust/toolchain.bzl
Outdated
|
|
||
| sysroot_linker = _symlink_sysroot_bin(ctx, name, dest, linker_bin) | ||
| sysroot_linker_files = _symlink_sysroot_tree(ctx, name, linker, linker[DefaultInfo].default_runfiles.files) | ||
| direct_files.extend([sysroot_linker]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: direct_files.append, same on the line below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
rust/toolchain.bzl
Outdated
| # `target` cfg is used so a linker can be chose based on the target | ||
| # platform. Linker binaries are still required to be runnable in the | ||
| # `exec` configuration. | ||
| cfg = "target", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't seem right, I think what you want is set cfg = "exec" but instantiate it as
linker = select({
"@platforms//...": ...
})
and, interestingly enough, the select will be evaluated against the target platform, because it's resolved before the exec transition is applied. See cerisier/toolchains_llvm_bootstrapped@aaeeacd#diff-5e02a5ab7afca2441282a65cca097c8fade25a1d6d9ffbb71b920c19a1e58babR32-R39 and the corresponding explanation on the Bazel issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
7451d08 to
69cfdd1
Compare
cdabdcc to
814d054
Compare
be68df1 to
210c378
Compare
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| copy_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use the version from https://registry.bazel.build/docs/bazel_lib, allow_symlink = True is safe.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would require adding a new dependency which I'd really like to avoid. There's some TODOs referring to bazelbuild/bazel#21747 that I'd like to clean up at some point and this could be one of the things visited in that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok let's leave a TODO. Realistically that's a pretty lightweight dep and probably most projects already pull it in :)
| if include: | ||
| env["INCLUDE"] = include | ||
| # Defaults for cxx flags. | ||
| env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can punt for now. In general I want us to stop using .path as that makes path-mapping not work, so many times we are building crates in multiple configurations even when not really necessary. But lets address in followups
Changes:
rust_allocator_librariesrule implementation into it's own filemandatory = Falsefor@bazel_tools//tools/cpp:toolchain_typetoolchains.find_cc_toolchainreturnsNonelinkeras an optional attribute torust_toolchain. This is expected to berust-lldfor now.@rules_rust//rust/settings:toolchain_linker_preferencewhich in the eventrust_toolchain.linkerand acc_toolchainare present, chooses the linker to use. (Defaults tocc).--@rules_rust//settings:default_allocator_libraryas a flag to globally control this value.