chore: replace is_windows select pattern with target_platform_has_constraint pattern (#95)

This commit is contained in:
Greg Magolan 2022-04-29 00:36:51 -07:00 committed by GitHub
parent 67e1072e25
commit f1e10df09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 33 additions and 49 deletions

View File

@ -100,9 +100,5 @@ def copy_to_directory(
include_external_repositories = include_external_repositories,
exclude_prefixes = exclude_prefixes,
replace_prefixes = replace_prefixes,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -60,9 +60,5 @@ def params_file(
args = args,
data = data,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -83,8 +83,10 @@ def copy_directory_action(ctx, src, dst, is_windows = False):
_copy_bash(ctx, src, dst)
def _copy_directory_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
dst = ctx.actions.declare_directory(ctx.attr.out)
copy_directory_action(ctx, ctx.file.src, dst, ctx.attr.is_windows)
copy_directory_action(ctx, ctx.file.src, dst, is_windows)
files = depset(direct = [dst])
runfiles = ctx.runfiles(files = [dst])
@ -96,10 +98,10 @@ _copy_directory = rule(
provides = [DefaultInfo],
attrs = {
"src": attr.label(mandatory = True, allow_single_file = True),
"is_windows": attr.bool(mandatory = True),
# Cannot declare out as an output here, because there's no API for declaring
# TreeArtifact outputs.
"out": attr.string(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
)
@ -123,10 +125,6 @@ def copy_directory(name, src, out, **kwargs):
_copy_directory(
name = name,
src = src,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
out = out,
**kwargs
)

View File

@ -111,6 +111,8 @@ def copy_file_action(ctx, src, dst, dir_path = None, is_windows = False):
_copy_bash(ctx, src, src_path, dst)
def _copy_file_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if ctx.attr.allow_symlink:
if len(ctx.files.src) != 1:
fail("src must be a single file when allow_symlink is True")
@ -127,14 +129,14 @@ def _copy_file_impl(ctx):
ctx.attr.src[DirectoryPathInfo].directory,
ctx.outputs.out,
dir_path = ctx.attr.src[DirectoryPathInfo].path,
is_windows = ctx.attr.is_windows,
is_windows = is_windows,
)
else:
if len(ctx.files.src) != 1:
fail("src must be a single file or a target that provides a DirectoryPathInfo")
if ctx.files.src[0].is_directory:
fail("cannot use copy_file on a directory; try copy_directory instead")
copy_file_action(ctx, ctx.files.src[0], ctx.outputs.out, is_windows = ctx.attr.is_windows)
copy_file_action(ctx, ctx.files.src[0], ctx.outputs.out, is_windows = is_windows)
files = depset(direct = [ctx.outputs.out])
runfiles = ctx.runfiles(files = [ctx.outputs.out])
@ -145,10 +147,10 @@ def _copy_file_impl(ctx):
_ATTRS = {
"src": attr.label(mandatory = True, allow_files = True),
"is_windows": attr.bool(mandatory = True),
"is_executable": attr.bool(mandatory = True),
"allow_symlink": attr.bool(mandatory = True),
"out": attr.output(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
_copy_file = rule(
@ -203,10 +205,6 @@ def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kw
name = name,
src = src,
out = out,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
is_executable = is_executable,
allow_symlink = allow_symlink,
**kwargs

View File

@ -59,7 +59,9 @@ def copy_files_to_bin_actions(ctx, files, is_windows = False):
return [copy_file_to_bin_action(ctx, file, is_windows = is_windows) for file in files]
def _impl(ctx):
files = copy_files_to_bin_actions(ctx, ctx.files.srcs, is_windows = ctx.attr.is_windows)
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
files = copy_files_to_bin_actions(ctx, ctx.files.srcs, is_windows = is_windows)
return DefaultInfo(
files = depset(files),
runfiles = ctx.runfiles(files = files),
@ -69,8 +71,8 @@ _copy_to_bin = rule(
implementation = _impl,
provides = [DefaultInfo],
attrs = {
"is_windows": attr.bool(mandatory = True),
"srcs": attr.label_list(mandatory = True, allow_files = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
)
@ -97,9 +99,5 @@ def copy_to_bin(name, srcs, **kwargs):
_copy_to_bin(
name = name,
srcs = srcs,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -11,7 +11,7 @@ _copy_to_directory_attr = {
"include_external_repositories": attr.string_list(default = []),
"exclude_prefixes": attr.string_list(default = []),
"replace_prefixes": attr.string_dict(default = {}),
"is_windows": attr.bool(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
def _longest_match(subject, tests, allow_partial = False):
@ -163,6 +163,8 @@ if exist "{src}\\*" (
)
def _copy_to_directory_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if not ctx.attr.srcs:
msg = "srcs must not be empty in copy_to_directory %s" % ctx.label
fail(msg)
@ -183,7 +185,7 @@ def _copy_to_directory_impl(ctx):
dst_path = skylib_paths.normalize("/".join([output.path, output_path]))
copy_paths.append((src_path, dst_path, src_file))
if ctx.attr.is_windows:
if is_windows:
_copy_to_dir_cmd(ctx, copy_paths, output)
else:
_copy_to_dir_bash(ctx, copy_paths, output)

View File

@ -27,6 +27,8 @@ def _runfiles_path(f):
return f.path # source file
def _diff_test_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if DirectoryPathInfo in ctx.attr.file1:
file1 = ctx.attr.file1[DirectoryPathInfo].directory
file1_path = "/".join([_runfiles_path(file1), ctx.attr.file1[DirectoryPathInfo].path])
@ -49,7 +51,7 @@ def _diff_test_impl(ctx):
msg = "diff_test comparing the same file %s" % file1
fail(msg)
if ctx.attr.is_windows:
if is_windows:
test_bin = ctx.actions.declare_file(ctx.label.name + "-test.bat")
ctx.actions.write(
output = test_bin,
@ -237,7 +239,7 @@ _diff_test = rule(
allow_files = True,
mandatory = True,
),
"is_windows": attr.bool(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
test = True,
implementation = _diff_test_impl,
@ -258,9 +260,5 @@ def diff_test(name, file1, file2, **kwargs):
name = name,
file1 = file1,
file2 = file2,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -5,12 +5,12 @@ load("//lib/private:expand_make_vars.bzl", "expand_locations")
_ATTRS = {
"args": attr.string_list(),
"data": attr.label_list(allow_files = True),
"is_windows": attr.bool(mandatory = True),
"newline": attr.string(
values = ["unix", "windows", "auto"],
default = "auto",
),
"out": attr.output(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
def _expand_locations(ctx, s):
@ -20,8 +20,10 @@ def _expand_locations(ctx, s):
return expand_locations(ctx, s, targets = ctx.attr.data).split(" ")
def _impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if ctx.attr.newline == "auto":
newline = "\r\n" if ctx.attr.is_windows else "\n"
newline = "\r\n" if is_windows else "\n"
elif ctx.attr.newline == "windows":
newline = "\r\n"
else:

View File

@ -46,10 +46,6 @@ def write_source_file(
in_file = in_file,
out_file = out_file.name if out_file else None,
additional_update_targets = additional_update_targets,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)
@ -132,7 +128,7 @@ _write_source_file_attrs = {
"out_file": attr.string(mandatory = False),
# buildifier: disable=attr-cfg
"additional_update_targets": attr.label_list(cfg = "host", mandatory = False),
"is_windows": attr.bool(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
def _write_source_file_sh(ctx, paths):
@ -246,6 +242,8 @@ if exist "%in%\\*" (
return updater
def _write_source_file_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if ctx.attr.out_file and not ctx.attr.in_file:
fail("in_file must be specified if out_file is set")
if ctx.attr.in_file and not ctx.attr.out_file:
@ -271,7 +269,7 @@ def _write_source_file_impl(ctx):
out_path = "/".join([ctx.label.package, ctx.attr.out_file]) if ctx.label.package else ctx.attr.out_file
paths.append((in_path, out_path))
if ctx.attr.is_windows:
if is_windows:
updater = _write_source_file_bat(ctx, paths)
else:
updater = _write_source_file_sh(ctx, paths)

View File

@ -123,6 +123,8 @@ exit /b 1
return test
def _impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
if DirectoryPathInfo in ctx.attr.in_file:
in_file = ctx.attr.in_file[DirectoryPathInfo].directory
in_file_path = "/".join([in_file.short_path, ctx.attr.in_file[DirectoryPathInfo].path])
@ -132,7 +134,7 @@ def _impl(ctx):
in_file = ctx.files.in_file[0]
in_file_path = in_file.short_path
if ctx.attr.is_windows:
if is_windows:
test = _impl_bat(ctx, in_file_path, ctx.file.out_file.short_path)
else:
test = _impl_sh(ctx, in_file_path, ctx.file.out_file.short_path)
@ -162,7 +164,7 @@ _write_source_file_test = rule(
allow_files = True,
mandatory = True,
),
"is_windows": attr.bool(mandatory = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
test = True,
)
@ -184,8 +186,4 @@ def write_source_file_test(name, in_file, out_file):
write_source_file_target = name + "_updater",
in_file = in_file,
out_file = out_file,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
)