Skip to content

Commit

Permalink
Renamed rust_toolchain.rust_lib to rust_toolchain.rust_std
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jan 22, 2022
1 parent 77285c1 commit 18a386a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _build_script_impl(ctx):
toolchain_tools = [
# Needed for rustc to function.
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
toolchain.rust_std.files,
]

cc_toolchain = find_cpp_toolchain(ctx)
Expand Down
11 changes: 9 additions & 2 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ _build_file_for_stdlib_template = """\
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
rust_stdlib_filegroup(
name = "rust_lib-{target_triple}",
name = "rust_std-{target_triple}",
srcs = glob(
[
"lib/rustlib/{target_triple}/lib/*.rlib",
Expand All @@ -154,6 +154,13 @@ rust_stdlib_filegroup(
),
visibility = ["//visibility:public"],
)
# For legacy support
alias(
name = "rust_lib-{target_triple}",
actual = "rust_std-{target_triple}",
visibility = ["//visibility:public"],
)
"""

def BUILD_for_stdlib(target_triple):
Expand All @@ -177,7 +184,7 @@ _build_file_for_rust_toolchain_template = """\
rust_toolchain(
name = "{toolchain_name}_impl",
rust_doc = "@{workspace_name}//:rustdoc",
rust_lib = "@{workspace_name}//:rust_lib-{target_triple}",
rust_std = "@{workspace_name}//:rust_std-{target_triple}",
rustc = "@{workspace_name}//:rustc",
rustfmt = "@{workspace_name}//:rustfmt_bin",
cargo = "@{workspace_name}//:cargo",
Expand Down
10 changes: 5 additions & 5 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def collect_inputs(
([] if linker_script == None else [linker_script]),
transitive = [
toolchain.rustc_lib.files,
toolchain.rust_lib.files,
toolchain.rust_std.files,
linker_depset,
crate_info.srcs,
dep_info.transitive_crate_outputs,
Expand Down Expand Up @@ -617,10 +617,10 @@ def construct_arguments(
rustc_flags.add(linker_script.path, format = "--codegen=link-arg=-T%s")

# Gets the paths to the folders containing the standard library (or libcore)
rust_lib_paths = depset([file.dirname for file in toolchain.rust_lib.files.to_list()]).to_list()
rust_std_paths = depset([file.dirname for file in toolchain.rust_std.files.to_list()]).to_list()

# Tell Rustc where to find the standard library
rustc_flags.add_all(rust_lib_paths, before_each = "-L", format_each = "%s")
rustc_flags.add_all(rust_std_paths, before_each = "-L", format_each = "%s")
rustc_flags.add_all(rust_flags)

data_paths = getattr(attr, "data", []) + getattr(attr, "compile_data", [])
Expand Down Expand Up @@ -674,8 +674,8 @@ def construct_arguments(
data_paths,
))

# Set the SYSROOT to the directory of the rust_lib files passed to the toolchain
env["SYSROOT"] = paths.dirname(toolchain.rust_lib.files.to_list()[0].short_path)
# Set the SYSROOT to the directory of the rust_std files passed to the toolchain
env["SYSROOT"] = paths.dirname(toolchain.rust_std.files.to_list()[0].short_path)

# extra_rustc_flags apply to the target configuration, not the exec configuration.
if hasattr(ctx.attr, "_extra_rustc_flags") and is_exec_configuration(ctx):
Expand Down
6 changes: 3 additions & 3 deletions rust/private/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def find_sysroot(rust_toolchain):
Returns:
str: A path assignable as `SYSROOT` for an action.
"""
sysroot_anchor = rust_toolchain.rust_lib.files.to_list()[0]
sysroot_anchor = rust_toolchain.rust_std.files.to_list()[0]
directory = sysroot_anchor.path.split(sysroot_anchor.short_path, 1)[0]
return directory.rstrip("/")

Expand Down Expand Up @@ -57,8 +57,8 @@ def _toolchain_files_impl(ctx):
files = toolchain.rustc_lib.files
elif ctx.attr.tool == "rustc_srcs":
files = toolchain.rustc_srcs.files
elif ctx.attr.tool == "rust_lib" or ctx.attr.tool == "rust_stdlib":
files = toolchain.rust_lib.files
elif ctx.attr.tool == "rust_std" or ctx.attr.tool == "rust_stdlib" or ctx.attr.tool == "rust_lib":
files = toolchain.rust_std.files
else:
fail("Unsupported tool: ", ctx.attr.tool)

Expand Down
49 changes: 31 additions & 18 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("//rust/private:utils.bzl", "dedent", "find_cc_toolchain", "make_static_lib
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")

def _rust_stdlib_filegroup_impl(ctx):
rust_lib = ctx.files.srcs
rust_std = ctx.files.srcs
dot_a_files = []
between_alloc_and_core_files = []
core_files = []
Expand All @@ -15,11 +15,11 @@ def _rust_stdlib_filegroup_impl(ctx):
alloc_files = []
self_contained_files = [
file
for file in rust_lib
for file in rust_std
if file.basename.endswith(".o") and "self-contained" in file.path
]

std_rlibs = [f for f in rust_lib if f.basename.endswith(".rlib")]
std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")]
if std_rlibs:
# std depends on everything
#
Expand All @@ -46,7 +46,7 @@ def _rust_stdlib_filegroup_impl(ctx):
for f in sorted(partitioned):
# buildifier: disable=print
print("File partitioned: {}".format(f.basename))
fail("rust_toolchain couldn't properly partition rlibs in rust_lib. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files)))
fail("rust_toolchain couldn't properly partition rlibs in rust_std. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files)))

return [
DefaultInfo(
Expand Down Expand Up @@ -97,12 +97,12 @@ def _ltl(library, ctx, cc_toolchain, feature_configuration):
pic_static_library = library,
)

def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
def _make_libstd_and_allocator_ccinfo(ctx, rust_std, allocator_library):
"""Make the CcInfo (if possible) for libstd and allocator libraries.
Args:
ctx (ctx): The rule's context object.
rust_lib: The rust standard library.
rust_std: The Rust standard library.
allocator_library: The target to use for providing allocator functions.
Expand All @@ -112,14 +112,14 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
cc_infos = []

if not rust_common.stdlib_info in ctx.attr.rust_lib:
if not rust_common.stdlib_info in rust_std:
fail(dedent("""\
{} --
The `rust_lib` ({}) must be a target providing `rust_common.stdlib_info`
(typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl).
See https://github.com/bazelbuild/rules_rust/pull/802 for more information.
""").format(ctx.label, ctx.attr.rust_lib))
rust_stdlib_info = ctx.attr.rust_lib[rust_common.stdlib_info]
""").format(ctx.label, rust_std))
rust_stdlib_info = rust_std[rust_common.stdlib_info]

if rust_stdlib_info.self_contained_files:
compilation_outputs = cc_common.create_compilation_outputs(
Expand Down Expand Up @@ -187,7 +187,7 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
)

link_inputs = cc_common.create_linker_input(
owner = rust_lib.label,
owner = rust_std.label,
libraries = std_inputs,
)

Expand Down Expand Up @@ -237,12 +237,21 @@ def _rust_toolchain_impl(ctx):
rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value
third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value

if ctx.attr.rust_lib:
# buildifier: disable=print
print("`rust_toolchain.rust_lib` is deprecated. Please update {} to use `rust_toolchain.rust_std`".format(
ctx.label,
))
rust_std = ctx.attr.rust_lib
else:
rust_std = ctx.attr.rust_std

expanded_stdlib_linkflags = []
for flag in ctx.attr.stdlib_linkflags:
expanded_stdlib_linkflags.append(
ctx.expand_location(
flag,
targets = ctx.attr.rust_lib[rust_common.stdlib_info].srcs,
targets = rust_std[rust_common.stdlib_info].srcs,
),
)

Expand All @@ -269,7 +278,8 @@ def _rust_toolchain_impl(ctx):
target_flag_value = ctx.file.target_json.path if ctx.file.target_json else ctx.attr.target_triple,
rustc_lib = ctx.attr.rustc_lib,
rustc_srcs = ctx.attr.rustc_srcs,
rust_lib = ctx.attr.rust_lib,
rust_std = rust_std,
rust_lib = rust_std, # `rust_lib` is deprecated and only exists for legacy support.
binary_ext = ctx.attr.binary_ext,
staticlib_ext = ctx.attr.staticlib_ext,
dylib_ext = ctx.attr.dylib_ext,
Expand All @@ -286,7 +296,7 @@ def _rust_toolchain_impl(ctx):
default_edition = ctx.attr.default_edition,
compilation_mode_opts = compilation_mode_opts,
crosstool_files = ctx.files._crosstool,
libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library),
libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library),
_incompatible_remove_transitive_libs_from_dep_info = remove_transitive_libs_from_dep_info.enabled,
_rename_first_party_crates = rename_first_party_crates,
_third_party_dir = third_party_dir,
Expand Down Expand Up @@ -353,8 +363,11 @@ rust_toolchain = rule(
allow_single_file = True,
cfg = "exec",
),
"rust_std": attr.label(
doc = "The Rust standard library.",
),
"rust_lib": attr.label(
doc = "The rust standard library.",
doc = "**Deprecated**: Use `rust_std`",
),
"rustc": attr.label(
doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.",
Expand All @@ -379,9 +392,9 @@ rust_toolchain = rule(
),
"stdlib_linkflags": attr.string_list(
doc = (
"Additional linker flags to use when Rust std lib is linked by a C++ linker " +
"(rustc will deal with these automatically). " +
"Subject to location expansion with respect to the srcs of the rust_lib attribute."
"Additional linker flags to use when Rust standard library is linked by a C++ linker " +
"(rustc will deal with these automatically). Subject to location expansion with respect " +
"to the srcs of the `rust_std` attribute."
),
mandatory = True,
),
Expand Down Expand Up @@ -432,7 +445,7 @@ rust_toolchain(
name = "rust_cpuX_impl",
rustc = "@rust_cpuX//:rustc",
rustc_lib = "@rust_cpuX//:rustc_lib",
rust_lib = "@rust_cpuX//:rust_lib",
rust_std = "@rust_cpuX//:rust_std",
rust_doc = "@rust_cpuX//:rustdoc",
binary_ext = "",
staticlib_ext = ".a",
Expand Down
6 changes: 3 additions & 3 deletions test/unit/toolchain/toolchain_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _toolchain_test():
binary_ext = "",
dylib_ext = ".so",
os = "linux",
rust_lib = ":std_libs",
rust_std = ":std_libs",
staticlib_ext = ".a",
stdlib_linkflags = [],
target_triple = "toolchain-test-triple",
Expand All @@ -70,7 +70,7 @@ def _toolchain_test():
binary_ext = "",
dylib_ext = ".so",
os = "linux",
rust_lib = ":std_libs",
rust_std = ":std_libs",
staticlib_ext = ".a",
stdlib_linkflags = [],
target_json = ":target_json",
Expand All @@ -81,7 +81,7 @@ def _toolchain_test():
binary_ext = "",
dylib_ext = ".so",
os = "linux",
rust_lib = ":std_libs",
rust_std = ":std_libs",
staticlib_ext = ".a",
stdlib_linkflags = ["test:$(location :stdlib_srcs)"],
target_json = ":target_json",
Expand Down

0 comments on commit 18a386a

Please sign in to comment.