Skip to content

Commit

Permalink
Try fixing windows
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Apr 11, 2022
1 parent 0b89c43 commit 6fc0ca9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
2 changes: 2 additions & 0 deletions go/private/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bzl_library(
"//go/private:mode",
"//go/private:providers",
"//go/private/rules:transition",
"//go/private/skylib/lib:copy_file",
],
)

Expand Down Expand Up @@ -117,6 +118,7 @@ bzl_library(
"//go/private:providers",
"//go/private/rules:binary",
"//go/private/rules:transition",
"//go/private/skylib/lib:copy_file",
"@bazel_skylib//lib:structs",
],
)
Expand Down
19 changes: 13 additions & 6 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ load(
"go_transition",
"transition_attrs",
)
load(
"//go/private/skylib/lib:copy_file.bzl",
"copy_bash",
"copy_cmd",
)
load(
"//go/private:mode.bzl",
"LINKMODE_C_ARCHIVE",
Expand Down Expand Up @@ -393,11 +398,10 @@ def _forward_binary_output(ctx):
di = ctx.attr.transition_dep[0][DefaultInfo]

copied_executable = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = copied_executable,
target_file = ep.executable,
is_executable = True,
)
if ctx.attr.is_windows:
copy_cmd(ctx, ep.executable, copied_executable)
else:
copy_bash(ctx, ep.executable, copied_executable)

data_runfiles = di.data_runfiles.merge(ctx.runfiles([copied_executable]))
default_runfiles = di.default_runfiles.merge(ctx.runfiles([copied_executable]))
Expand All @@ -411,7 +415,10 @@ def _forward_binary_output(ctx):

go_transition_binary = rule(
implementation = _forward_binary_output,
attrs = _merge(transition_attrs, {"transition_dep": attr.label(cfg = go_transition)}),
attrs = _merge(transition_attrs, {
"is_windows": attr.bool(),
"transition_dep": attr.label(cfg = go_transition),
}),
executable = True,
)

Expand Down
16 changes: 11 additions & 5 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ load(
"go_transition",
"transition_attrs",
)
load(
"//go/private/skylib/lib:copy_file.bzl",
"copy_bash",
"copy_cmd",
)
load(
"//go/private:mode.bzl",
"LINKMODE_NORMAL",
Expand All @@ -65,11 +70,11 @@ def _forward_test_output(ctx):
di = ctx.attr.transition_dep[0][DefaultInfo]

copied_executable = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = copied_executable,
target_file = ep.executable,
is_executable = True,
)

if ctx.attr.is_windows:
copy_cmd(ctx, ep.executable, copied_executable)
else:
copy_bash(ctx, ep.executable, copied_executable)

data_runfiles = di.data_runfiles.merge(ctx.runfiles([copied_executable]))
default_runfiles = di.default_runfiles.merge(ctx.runfiles([copied_executable]))
Expand All @@ -85,6 +90,7 @@ go_transition_test = rule(
implementation = _forward_test_output,
attrs = _merge(transition_attrs, {
"transition_dep": attr.label(cfg = go_transition),
"is_windows": attr.bool(),
# Workaround for bazelbuild/bazel#6293. See comment in lcov_merger.sh.
"_lcov_merger": attr.label(
executable = True,
Expand Down
9 changes: 8 additions & 1 deletion go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def go_transition_wrapper(kind, transition_kind, name, use_basename, keys_to_str
# but preserve things like tags and exec_compatible_with,
# which are general to all tests rather than specific to go_test specifically,
# and should apply to generated tests and binaries too.
transition_kind(name = name, transition_dep = transitioned_name, **_minus_keys(kwargs, keys_to_strip))
transition_kwargs = _minus_keys(kwargs, keys_to_strip)
transition_kwargs["name"] = name
transition_kwargs["transition_dep"] = transitioned_name
transition_kwargs["is_windows"] = select({
"@bazel_tools//src/conditions:windows": True,
"//conditions:default": False,
})
transition_kind(**transition_kwargs)
tags = kwargs.pop("tags", [])
if "manual" not in tags:
tags += ["manual"]
Expand Down
6 changes: 6 additions & 0 deletions go/private/skylib/lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ bzl_library(
srcs = ["versions.bzl"],
visibility = ["//go:__subpackages__"],
)

bzl_library(
name = "copy_file",
srcs = ["copy_file.bzl"],
visibility = ["//go:__subpackages__"],
)
38 changes: 38 additions & 0 deletions go/private/skylib/lib/copy_file.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def copy_cmd(ctx, src, dst):
# Most Windows binaries built with MSVC use a certain argument quoting
# scheme. Bazel uses that scheme too to quote arguments. However,
# cmd.exe uses different semantics, so Bazel's quoting is wrong here.
# To fix that we write the command to a .bat file so no command line
# quoting or escaping is required.
bat = ctx.actions.declare_file(ctx.label.name + "-cmd.bat")
ctx.actions.write(
output = bat,
# Do not use lib/shell.bzl's shell.quote() method, because that uses
# Bash quoting syntax, which is different from cmd.exe's syntax.
content = "@copy /Y \"%s\" \"%s\" >NUL" % (
src.path.replace("/", "\\"),
dst.path.replace("/", "\\"),
),
is_executable = True,
)
ctx.actions.run(
inputs = [src],
tools = [bat],
outputs = [dst],
executable = "cmd.exe",
arguments = ["/C", bat.path.replace("/", "\\")],
mnemonic = "CopyFile",
progress_message = "Copying files",
use_default_shell_env = True,
)

def copy_bash(ctx, src, dst):
ctx.actions.run_shell(
tools = [src],
outputs = [dst],
command = "cp -f \"$1\" \"$2\"",
arguments = [src.path, dst.path],
mnemonic = "CopyFile",
progress_message = "Copying files",
use_default_shell_env = True,
)

0 comments on commit 6fc0ca9

Please sign in to comment.