fix: don't generate @aspect_bazel_lib_local_config_platform repository as it is a leaky abstraction for rule consumers and not just rule authors (#243)

This commit is contained in:
Greg Magolan 2022-09-13 21:58:08 -07:00 committed by GitHub
parent be5c9d06bc
commit 65e852f774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 142 additions and 27 deletions

View File

@ -12,7 +12,7 @@ bazel_lib_internal_deps()
load("//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
aspect_bazel_lib_dependencies()
aspect_bazel_lib_dependencies(override_local_config_platform = True)
# For running our own unit tests
load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")

View File

@ -98,6 +98,11 @@ stardoc_with_diff_test(
bzl_library_target = "//lib:host_repo",
)
stardoc_with_diff_test(
name = "platform_utils",
bzl_library_target = "//lib:platform_utils",
)
stardoc_with_diff_test(
name = "stamping",
bzl_library_target = "//lib:stamping",

40
docs/platform_utils.md generated Executable file
View File

@ -0,0 +1,40 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
Public API
<a id="platform_utils.host_platform_is_darwin"></a>
## platform_utils.host_platform_is_darwin
<pre>
platform_utils.host_platform_is_darwin()
</pre>
<a id="platform_utils.host_platform_is_linux"></a>
## platform_utils.host_platform_is_linux
<pre>
platform_utils.host_platform_is_linux()
</pre>
<a id="platform_utils.host_platform_is_windows"></a>
## platform_utils.host_platform_is_windows
<pre>
platform_utils.host_platform_is_windows()
</pre>

View File

@ -192,3 +192,9 @@ bzl_library(
name = "windows_utils",
srcs = ["windows_utils.bzl"],
)
bzl_library(
name = "platform_utils",
srcs = ["platform_utils.bzl"],
deps = ["//lib/private:platform_utils"],
)

5
lib/platform_utils.bzl Normal file
View File

@ -0,0 +1,5 @@
"Public API"
load("//lib/private:platform_utils.bzl", _platform_utils = "platform_utils")
platform_utils = _platform_utils

View File

@ -19,7 +19,12 @@ exports_files(
bzl_library(
name = "copy_common",
srcs = ["copy_common.bzl"],
deps = ["@aspect_bazel_lib_local_config_platform//:constraints"],
)
bzl_library(
name = "platform_utils",
srcs = ["platform_utils.bzl"],
deps = ["@local_config_platform//:constraints"],
)
bzl_library(
@ -28,13 +33,17 @@ bzl_library(
deps = [
":copy_common",
":directory_path",
":platform_utils",
],
)
bzl_library(
name = "copy_directory",
srcs = ["copy_directory.bzl"],
deps = [":copy_common"],
deps = [
":copy_common",
":platform_utils",
],
)
bzl_library(
@ -46,6 +55,7 @@ bzl_library(
":glob_match",
":output_files",
":paths",
":platform_utils",
"@bazel_skylib//lib:paths",
],
)

View File

@ -1,7 +1,5 @@
"Helpers for copy rules"
load("@aspect_bazel_lib_local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
# Hints for Bazel spawn strategy
COPY_EXECUTION_REQUIREMENTS = {
# ----------------+-----------------------------------------------------------------------------
@ -59,6 +57,3 @@ COPY_EXECUTION_REQUIREMENTS = {
"no-sandbox": "1",
"local": "1",
}
def is_windows_host():
return "@platforms//os:windows" in HOST_CONSTRAINTS

View File

@ -4,7 +4,8 @@ This rule copies a directory to another location using Bash (on Linux/macOS) or
cmd.exe (on Windows).
"""
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
load(":platform_utils.bzl", _platform_utils = "platform_utils")
def _copy_cmd(ctx, src, dst):
# Most Windows binaries built with MSVC use a certain argument quoting
@ -83,7 +84,7 @@ def copy_directory_action(ctx, src, dst, is_windows = None):
# Because copy actions have "local" execution requirements, we can safely assume
# the execution is the same as the host platform and generate different actions for Windows
# and non-Windows host platforms
is_windows = _is_windows_host()
is_windows = _platform_utils.host_platform_is_windows()
if is_windows:
_copy_cmd(ctx, src, dst)
else:

View File

@ -24,8 +24,9 @@ cmd.exe (on Windows). `_copy_xfile` marks the resulting file executable,
`_copy_file` does not.
"""
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
load(":directory_path.bzl", "DirectoryPathInfo")
load(":platform_utils.bzl", _platform_utils = "platform_utils")
def _copy_cmd(ctx, src, src_path, dst):
# Most Windows binaries built with MSVC use a certain argument quoting
@ -113,7 +114,7 @@ def copy_file_action(ctx, src, dst, dir_path = None, is_windows = None):
# Because copy actions have "local" execution requirements, we can safely assume
# the execution is the same as the host platform and generate different actions for Windows
# and non-Windows host platforms
is_windows = _is_windows_host()
is_windows = _platform_utils.host_platform_is_windows()
if is_windows:
_copy_cmd(ctx, src, src_path, dst)
else:

View File

@ -1,10 +1,11 @@
"copy_to_directory implementation"
load("@bazel_skylib//lib:paths.bzl", skylib_paths = "paths")
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
load(":paths.bzl", "paths")
load(":directory_path.bzl", "DirectoryPathInfo")
load(":glob_match.bzl", "glob_match")
load(":platform_utils.bzl", _platform_utils = "platform_utils")
_filter_transforms_order_docstring = """Filters and transformations are applied in the following order:
@ -764,7 +765,7 @@ def copy_to_directory_action(
# Because copy actions have "local" execution requirements, we can safely assume
# the execution is the same as the host platform and generate different actions for Windows
# and non-Windows host platforms
is_windows = _is_windows_host()
is_windows = _platform_utils.host_platform_is_windows()
if is_windows:
_copy_to_dir_cmd(ctx, copy_paths, dst)
else:

View File

@ -1,27 +1,52 @@
"""local_config_platform repository rule
"""
def _impl(rctx):
rctx.file("constraints.bzl", content = rctx.read(rctx.attr._constraints_bzl))
load(":repo_utils.bzl", "repo_utils")
rctx.file("BUILD.bazel", content = rctx.read(rctx.attr._build_bazel) + """
def _impl(rctx):
rctx.file("BUILD.bazel", """load(':constraints.bzl', 'HOST_CONSTRAINTS')
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
package(default_visibility = ['//visibility:public'])
platform(name = 'host',
# Auto-detected host platform constraints.
constraint_values = HOST_CONSTRAINTS,
)
bzl_library(
name = "constraints",
srcs = ["constraints.bzl"],
visibility = ["//visibility:public"],
)
""")
# TODO: we can detect the host CPU in the future as well if needed;
# see the repo_utils.platform(rctx) function for an example of this
if repo_utils.is_darwin(rctx):
rctx.file("constraints.bzl", content = """HOST_CONSTRAINTS = [
'@platforms//cpu:x86_64',
'@platforms//os:osx',
]
""")
elif repo_utils.is_windows(rctx):
rctx.file("constraints.bzl", content = """HOST_CONSTRAINTS = [
'@platforms//cpu:x86_64',
'@platforms//os:windows',
]
""")
else:
rctx.file("constraints.bzl", content = """HOST_CONSTRAINTS = [
'@platforms//cpu:x86_64',
'@platforms//os:linux',
]
""")
local_config_platform = repository_rule(
implementation = _impl,
doc = """Generates a copy of the auto-generated @local_config_platform repository with an added bzl_library.
doc = """Generates a repository in the same shape as the auto-generated @local_config_platform repository with an added bzl_library.
This is useful for rules that want to load `HOST_CONSTRAINTS` from `@local_config_platform//:constraints.bzl` and
also want to use stardoc for generating documentation.
""",
attrs = {
"_constraints_bzl": attr.label(default = "@local_config_platform//:constraints.bzl"),
"_build_bazel": attr.label(default = "@local_config_platform//:BUILD.bazel"),
},
)

View File

@ -0,0 +1,18 @@
"""Utility functions for platforms"""
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
def _host_platform_is_darwin():
return "@platforms//os:osx" in HOST_CONSTRAINTS
def _host_platform_is_linux():
return "@platforms//os:linux" in HOST_CONSTRAINTS
def _host_platform_is_windows():
return "@platforms//os:windows" in HOST_CONSTRAINTS
platform_utils = struct(
host_platform_is_darwin = _host_platform_is_darwin,
host_platform_is_linux = _host_platform_is_linux,
host_platform_is_windows = _host_platform_is_windows,
)

View File

@ -10,8 +10,15 @@ load("//lib/private:local_config_platform.bzl", "local_config_platform")
def http_archive(name, **kwargs):
maybe(_http_archive, name = name, **kwargs)
def aspect_bazel_lib_dependencies():
"Load dependencies required by aspect rules"
def aspect_bazel_lib_dependencies(override_local_config_platform = False):
"""Load dependencies required by aspect rules
Args:
override_local_config_platform: override the @local_config_platform repository with one that adds stardoc
support for loading constraints.bzl.
Should be set in repositories that load @aspect_bazel_lib copy actions and also generate stardoc.
"""
http_archive(
name = "bazel_skylib",
sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
@ -21,9 +28,10 @@ def aspect_bazel_lib_dependencies():
],
)
local_config_platform(
name = "aspect_bazel_lib_local_config_platform",
)
if override_local_config_platform:
local_config_platform(
name = "local_config_platform",
)
# Re-export the default versions
DEFAULT_JQ_VERSION = _DEFAULT_JQ_VERSION