Skip to content

Commit 471f368

Browse files
authored
Migrate copy_directory away from deprecated host constraint (#588)
1 parent 56a2abb commit 471f368

4 files changed

Lines changed: 25 additions & 24 deletions

File tree

rules/private/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("//:bzl_library.bzl", "bzl_library")
2-
load(":copy_file_private.bzl", "is_windows")
2+
load(":copy_common.bzl", "is_windows")
33

44
package(default_applicable_licenses = ["//:license"])
55

rules/private/copy_common.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ COPY_EXECUTION_REQUIREMENTS = {
4343
"no-remote": "1",
4444
"no-cache": "1",
4545
}
46+
47+
OsInfo = provider(
48+
doc = "Information about the target platform's OS.",
49+
fields = ["is_windows"],
50+
)
51+
52+
is_windows = rule(
53+
implementation = lambda ctx: OsInfo(
54+
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]),
55+
),
56+
attrs = {
57+
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
58+
},
59+
)

rules/private/copy_directory_private.bzl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This rule copies a directory to another location using Bash (on Linux/macOS) or
1818
cmd.exe (on Windows).
1919
"""
2020

21-
load(":copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS")
21+
load(":copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS", "OsInfo")
2222

2323
def _copy_cmd(ctx, src, dst):
2424
# Most Windows binaries built with MSVC use a certain argument quoting
@@ -108,7 +108,7 @@ def copy_directory_action(ctx, src, dst, is_windows = False):
108108

109109
def _copy_directory_impl(ctx):
110110
dst = ctx.actions.declare_directory(ctx.attr.out)
111-
copy_directory_action(ctx, ctx.file.src, dst, ctx.attr.is_windows)
111+
copy_directory_action(ctx, ctx.file.src, dst, ctx.attr._exec_is_windows[OsInfo].is_windows)
112112

113113
files = depset(direct = [dst])
114114
runfiles = ctx.runfiles(files = [dst])
@@ -120,10 +120,15 @@ _copy_directory = rule(
120120
provides = [DefaultInfo],
121121
attrs = {
122122
"src": attr.label(mandatory = True, allow_single_file = True),
123-
"is_windows": attr.bool(mandatory = True),
124123
# Cannot declare out as an output here, because there's no API for declaring
125124
# TreeArtifact outputs.
126125
"out": attr.string(mandatory = True),
126+
"_exec_is_windows": attr.label(
127+
default = ":is_windows",
128+
# The exec transition must match the exec group of the actions, which in
129+
# this case is the default exec group.
130+
cfg = "exec",
131+
),
127132
},
128133
)
129134

@@ -147,10 +152,6 @@ def copy_directory(name, src, out, **kwargs):
147152
_copy_directory(
148153
name = name,
149154
src = src,
150-
is_windows = select({
151-
"@bazel_tools//src/conditions:host_windows": True,
152-
"//conditions:default": False,
153-
}),
154155
out = out,
155156
**kwargs
156157
)

rules/private/copy_file_private.bzl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ cmd.exe (on Windows). '_copy_xfile' marks the resulting file executable,
1919
'_copy_file' does not.
2020
"""
2121

22-
load(":copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS")
22+
load(":copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS", "OsInfo")
2323

2424
def copy_cmd(ctx, src, dst):
2525
# Most Windows binaries built with MSVC use a certain argument quoting
@@ -68,7 +68,7 @@ def _copy_file_impl(ctx):
6868
target_file = ctx.file.src,
6969
is_executable = ctx.attr.is_executable,
7070
)
71-
elif ctx.attr._exec_is_windows[_OsInfo].is_windows:
71+
elif ctx.attr._exec_is_windows[OsInfo].is_windows:
7272
copy_cmd(ctx, ctx.file.src, ctx.outputs.out)
7373
else:
7474
copy_bash(ctx, ctx.file.src, ctx.outputs.out)
@@ -109,20 +109,6 @@ _copy_xfile = rule(
109109
attrs = _ATTRS,
110110
)
111111

112-
_OsInfo = provider(
113-
doc = "Information about the target platform's OS.",
114-
fields = ["is_windows"],
115-
)
116-
117-
is_windows = rule(
118-
implementation = lambda ctx: _OsInfo(
119-
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]),
120-
),
121-
attrs = {
122-
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
123-
},
124-
)
125-
126112
def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs):
127113
"""Copies a file to another location.
128114

0 commit comments

Comments
 (0)