Skip to content

Commit

Permalink
Add build setting to enable specifying other tool locations
Browse files Browse the repository at this point in the history
On some systems, the CC toolchain discovered by bazel uses a different
path for the linker than where the linker (that is specified by the
`-fuse-ld=` flag) actually lives on the system.

The issue stems from rules_rust deciding to pass `-fuse-ld=lld` a link arg,
rather than just using what was detected by the CC toolchain. This results
in the linker not being found, as reported by
lowRISC/opentitan#12448

This is a workaround that enables telling rules_rust where other linker
directories might be on your system. It is used by passing
`--@rules_rust//:extra_rustc_toolchain_dirs=/path/to/somewhere/else` to
`bazel ...` invocations.

Signed-off-by: Tim Trippel <[email protected]>
  • Loading branch information
timothytrippel authored and nbdd0121 committed Nov 14, 2023
1 parent 5c759b1 commit a40e4ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
"extra_exec_rustc_flags",
"extra_rustc_flag",
"extra_rustc_flags",
"extra_rustc_toolchain_dirs",
"is_proc_macro_dep",
"is_proc_macro_dep_enabled",
"no_std",
Expand Down Expand Up @@ -96,6 +97,15 @@ per_crate_rustc_flag(
visibility = ["//visibility:public"],
)

# This setting is to enable passing additional `-B` options to the CC toolchain driver binary to
# aid in locating toolchain directories on systems where not all toolchain tools are installed in
# the same system location.
extra_rustc_toolchain_dirs(
name = "extra_rustc_toolchain_dirs",
build_setting_default = "",
visibility = ["//visibility:public"],
)

# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
label_flag(
name = "clippy.toml",
Expand Down
4 changes: 4 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ load(
_extra_exec_rustc_flags = "extra_exec_rustc_flags",
_extra_rustc_flag = "extra_rustc_flag",
_extra_rustc_flags = "extra_rustc_flags",
_extra_rustc_toolchain_dirs = "extra_rustc_toolchain_dirs",
_is_proc_macro_dep = "is_proc_macro_dep",
_is_proc_macro_dep_enabled = "is_proc_macro_dep_enabled",
_no_std = "no_std",
Expand Down Expand Up @@ -118,6 +119,9 @@ extra_rustc_flag = _extra_rustc_flag
extra_rustc_flags = _extra_rustc_flags
# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_rustc_toolchain_dirs = _extra_rustc_toolchain_dirs
# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_exec_rustc_flag = _extra_exec_rustc_flag
# See @rules_rust//rust/private:rustc.bzl for a complete description.

Expand Down
3 changes: 3 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,9 @@ _common_attrs = {
"_extra_rustc_flags": attr.label(
default = Label("//:extra_rustc_flags"),
),
"_extra_rustc_toolchain_dirs": attr.label(
default = Label("//:extra_rustc_toolchain_dirs"),
),
"_import_macro_dep": attr.label(
default = Label("//util/import"),
cfg = "exec",
Expand Down
22 changes: 22 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ PerCrateRustcFlagsInfo = provider(
fields = {"per_crate_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"},
)

ExtraRustcToolchainDirsInfo = provider(
doc = "Pass each value as an additional `-B` flag to rustc invocations. Enables use of linkers placed in different directories on the system.",
fields = {"extra_rustc_toolchain_dirs": "List[string] Extra `-B` flags to pass to rustc."},
)

IsProcMacroDepInfo = provider(
doc = "Records if this is a transitive dependency of a proc-macro.",
fields = {"is_proc_macro_dep": "Boolean"},
Expand Down Expand Up @@ -447,6 +452,10 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
action_name = action_name,
variables = link_variables,
)

# Make sure linker is locateable.
if hasattr(ctx.attr, "_extra_rustc_toolchain_dirs"):
link_args = link_args + ctx.attr._extra_rustc_toolchain_dirs[ExtraRustcToolchainDirsInfo].extra_rustc_toolchain_dirs
link_env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = action_name,
Expand Down Expand Up @@ -2112,6 +2121,19 @@ per_crate_rustc_flag = rule(
build_setting = config.string(flag = True, allow_multiple = True),
)

def _extra_rustc_toolchain_dirs_impl(ctx):
return ExtraRustcToolchainDirsInfo(extra_rustc_toolchain_dirs = ["-B" + f for f in ctx.build_setting_value if f != ""])

extra_rustc_toolchain_dirs = rule(
doc = (
"Add additional `-B` rustc toolchain flags to specificy where CC toolchain executables are located on the system by" +
"using the command line switch `--@rules_rust//:extra_rustc_toolchain_dirs`. " +
"Multiple uses are accumulated."
),
implementation = _extra_rustc_toolchain_dirs_impl,
build_setting = config.string(flag = True, allow_multiple = True),
)

def _no_std_impl(ctx):
value = str(ctx.attr._no_std[BuildSettingInfo].value)
if is_exec_configuration(ctx):
Expand Down

0 comments on commit a40e4ec

Please sign in to comment.