Add support for optional cc_toolchain#3665
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.
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.
#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.
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
| name = "no_ar", | ||
| srcs = ["no_binary.rs"], | ||
| edition = "2021", | ||
| rustc_env = { |
There was a problem hiding this comment.
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.
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.
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.
Updated! I'm nervous about symlink=True as I don't trust symlinks to work on windows. E.g. bazelbuild/bazel#21747
| linker_label = "None" | ||
| linker_type = "None" | ||
| if include_linker: | ||
| linker_label = "\"//:rust-lld\"" |
There was a problem hiding this comment.
might be a bit cleaner to do
linker_label = None
if include_linker:
linker_label = "//:rust-lld"
...
template.format(linker_label = repr(linker_label))
| skip_next = False | ||
| continue | ||
|
|
||
| # Strip -Wl, prefix if using direct driver (it's only for compiler drivers) |
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
|
|
||
| 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.
nit: direct_files.append, same on the line below
| # `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.
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 hermeticbuild/hermetic-llvm@aaeeacd#diff-5e02a5ab7afca2441282a65cca097c8fade25a1d6d9ffbb71b920c19a1e58babR32-R39 and the corresponding explanation on the Bazel issue
89e2929 to
210c378
Compare
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| copy_file( |
There was a problem hiding this comment.
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.
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.
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.
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
illicitonion
left a comment
There was a problem hiding this comment.
Works for me! Thank you!
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
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.