Skip to content

Commit

Permalink
Use C++ environment variables when constructing link command line (#252)
Browse files Browse the repository at this point in the history
* Use C++ environment variables when constructing link command line

This PR makes sure that rustc action also takes declared environment
variables from the C++ toolchain when constructing the action. This way
the C++ toolchain can start using more environment variables without
breaking rust.

* Fix iterating depset
  • Loading branch information
Marcel Hlopko authored and fweikert committed Sep 27, 2019
1 parent f727669 commit 55f7701
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 1 addition & 7 deletions rust/private/legacy_cc_starlark_api_shim.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ def get_libs_for_static_executable(dep):
A depset[File]
"""
libraries_to_link = dep[CcInfo].linking_context.libraries_to_link

# Remove `if` line once Bazel 0.27 is used
# (https://github.com/bazelbuild/bazel/issues/8118)
if type(libraries_to_link) == type(depset()):
libraries_to_link = libraries_to_link.to_list()

return depset([_get_preferred_artifact(lib) for lib in libraries_to_link])
return depset([_get_preferred_artifact(lib) for lib in libraries_to_link.to_list()])

def _get_preferred_artifact(library_to_link):
return (
Expand Down
14 changes: 11 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,17 @@ def _get_linker_and_args(ctx, rpaths):
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME,
variables = link_variables,
)
link_env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME,
variables = link_variables,
)
ld = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME,
)

return ld, link_args
return ld, link_args, link_env

def rustc_compile_action(
ctx,
Expand Down Expand Up @@ -246,7 +251,7 @@ def rustc_compile_action(

# Link!
rpaths = _compute_rpaths(toolchain, output_dir, dep_info)
ld, link_args = _get_linker_and_args(ctx, rpaths)
ld, link_args, link_env = _get_linker_and_args(ctx, rpaths)
args.add("--codegen=linker=" + ld)
args.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")

Expand Down Expand Up @@ -289,11 +294,14 @@ def rustc_compile_action(
toolchain.rustc.path,
)

env = _get_rustc_env(ctx, toolchain)
env.update(link_env)

ctx.actions.run_shell(
command = command,
inputs = compile_inputs,
outputs = [crate_info.output],
env = _get_rustc_env(ctx, toolchain),
env = env,
arguments = [args],
mnemonic = "Rustc",
progress_message = "Compiling Rust {} {} ({} files)".format(crate_info.type, ctx.label.name, len(crate_info.srcs)),
Expand Down

0 comments on commit 55f7701

Please sign in to comment.