Skip to content

Commit

Permalink
hack in Windows support
Browse files Browse the repository at this point in the history
This is an ugly hack, and hopefully can be done in a nicer way.
  • Loading branch information
dae committed Dec 2, 2020
1 parent 1516eeb commit 119c478
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions worker/bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@ A simplified rust_binary implementation based on cargo so we can compile
the worker binary.
"""

load("@io_bazel_rules_rust//rust:private/rustc.bzl", "get_cc_toolchain", "get_linker_and_args")

def _rewrite_to_unix_path(path):
drive = path[0]
return "/" + drive + path[2:].replace("\\", "/")

def _rust_binary_impl(ctx):
toolchain = ctx.toolchains["@io_bazel_rules_rust//rust:toolchain"]
output = ctx.actions.declare_file(ctx.label.name + toolchain.binary_ext)
cache_dir = ctx.actions.declare_directory(ctx.label.name + "_cache")
folder = ctx.files.srcs[0].dirname
rust_bin = toolchain.cargo.dirname

# + [toolchain.rust_lib, toolchain.rustc_lib]
# get linker env for building on Windows
cc_toolchain, feature_configuration = get_cc_toolchain(ctx)
_, _, link_env = get_linker_and_args(ctx, cc_toolchain, feature_configuration, depset())

# this is an awful hack to extract the path to MSVC on Windows, so we can inject it
# into the path after use_default_shell_env has set it up. Without default_shell_env,
# compilation fails on other platforms because `cc` is not in the path. There is no doubt
# a better way to handle this.
path = link_env.get("PATH")
if path:
# cc toolchain has placed it at head of path
msvc_path = path.split(";")[0]
msvc_path = _rewrite_to_unix_path(msvc_path) + ":"
else:
msvc_path = ""

lib = link_env.get("LIB", "")
if lib:
lib = _rewrite_to_unix_path(lib)

ctx.actions.run_shell(
inputs = ctx.files.srcs,
outputs = [output, cache_dir],
# executable = toolchain.cargo,
command = """\
export RUSTC="$(pwd)/{rustc}"; \
export CARGO="$(pwd)/{cargo}"; \
export OUTPUT="$(pwd)/{output}"; \
export CARGO_HOME="$(pwd)/{cache_dir}"; \
export PATH="{msvc_path}$PATH"; \
export LIB="{lib}"; \
cd {folder} && \
"$CARGO" build -q --release && \
mv target/release/rustc-worker "$OUTPUT" """.format(
Expand All @@ -29,6 +55,8 @@ mv target/release/rustc-worker "$OUTPUT" """.format(
rustc = toolchain.rustc.path,
output = output.path,
cache_dir = cache_dir.path,
msvc_path = msvc_path,
lib = lib,
),
tools = [toolchain.cargo, toolchain.rustc] + toolchain.rust_lib.files.to_list() + toolchain.rustc_lib.files.to_list(),
use_default_shell_env = True,
Expand All @@ -45,6 +73,8 @@ rust_cargo_binary = rule(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
},
fragments = ["cpp"],
host_fragments = ["cpp"],
executable = True,
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
Expand Down

0 comments on commit 119c478

Please sign in to comment.