Skip to content

Commit

Permalink
Expose Toolchains and add grep/sed toolchains.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim committed Jan 12, 2024
1 parent 5b3b7d7 commit 3bc8b1e
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ toolchain_type(
name = "expand_template_toolchain_type",
)

toolchain_type(
name = "grep_toolchain_type",
)

toolchain_type(
name = "sed_toolchain_type",
)

toolchain_type(
name = "tar_toolchain_type",
)
Expand Down
204 changes: 204 additions & 0 deletions lib/private/grep_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
"Setup grep toolchain repositories and rules"

# Platform names follow the platform naming convention in @aspect_bazel_lib//:lib/private/repo_utils.bzl
GREP_PLATFORMS = {
"darwin_amd64": struct(
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
),
"linux_amd64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"linux_arm64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
"windows_amd64": struct(
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
}

DEFAULT_GREP_VERSION = "3.11"

# https://www.gnu.org/software/grep/
#
# The integrity hashes can be automatically fetched for the coreutils releases by running
# `tools/coreutils_mirror_release.sh`.
GREP_VERSIONS = {
"3.11": {
"darwin_arm64": {
"filename": "coreutils-0.0.23-aarch64-apple-darwin.tar.gz",
"sha256": "sha256-KP90sjKxtXDbLC+o5f4+gQnvP3Tr7O0RopME4g9QF5E=",
},
"linux_arm64": {
"filename": "coreutils-0.0.23-aarch64-unknown-linux-gnu.tar.gz",
"sha256": "sha256-8wMVMgAgf8JQ2+2LdoewkyDo416VEsf9RlMJl4jiBjk=",
},
"darwin_amd64": {
"filename": "coreutils-0.0.23-x86_64-apple-darwin.tar.gz",
"sha256": "sha256-SswetVAuK/hMK1r9uBvNnKj5JpSgD0bzkbsHTxOabCo=",
},
"windows_amd64": {
"filename": "coreutils-0.0.23-x86_64-pc-windows-msvc.zip",
"sha256": "sha256-aglIj5JvFGLm2ABwRzWAsZRTTD3X444V3GxHM9pGJS4=",
},
"linux_amd64": {
"filename": "coreutils-0.0.23-x86_64-unknown-linux-gnu.tar.gz",
"sha256": "sha256-u7OMW43Y46aXRRIKULfKdfUW51WJn6G70s5Xxwb6/1g=",
},
},
}

GrepInfo = provider(
doc = "Provide info for executing grep",
fields = {
"bin": "Executable grep binary",
},
)

def _grep_toolchain_impl(ctx):
binary = ctx.file.binary

# Make the $(GREP_BIN) variable available in places like genrules.
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
template_variables = platform_common.TemplateVariableInfo({
"GREP_BIN": binary.path,
})
default_info = DefaultInfo(
files = depset([binary]),
runfiles = ctx.runfiles(files = [binary]),
)
grep_info = GrepInfo(
bin = binary,
)

# Export all the providers inside our ToolchainInfo
# so the resolved_toolchain rule can grab and re-export them.
toolchain_info = platform_common.ToolchainInfo(
grep_info = grep_info,
template_variables = template_variables,
default = default_info,
)

return [default_info, toolchain_info, template_variables]

grep_toolchain = rule(
implementation = _grep_toolchain_impl,
attrs = {
"binary": attr.label(
mandatory = True,
allow_single_file = True,
),
},
)

def _coreutils_toolchains_repo_impl(rctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or target platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """# @generated by @aspect_bazel_lib//lib/private:coreutils_toolchain.bzl
# Forward all the providers
def _resolved_toolchain_impl(ctx):
toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:coreutils_toolchain_type"]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.coreutils_info,
toolchain_info.template_variables,
]
# Copied from java_toolchain_alias
# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
resolved_toolchain = rule(
implementation = _resolved_toolchain_impl,
toolchains = ["@aspect_bazel_lib//lib:coreutils_toolchain_type"],
incompatible_use_toolchain_transition = True,
)
"""
rctx.file("defs.bzl", starlark_content)

build_content = """# @generated by @aspect_bazel_lib//lib/private:coreutils_toolchain.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains flag.
# By default all these toolchains are registered by the coreutils_register_toolchains macro
# so you don't normally need to interact with these targets.
load(":defs.bzl", "resolved_toolchain")
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
"""

for [platform, meta] in GREP_PLATFORMS.items():
build_content += """
toolchain(
name = "{platform}_toolchain",
exec_compatible_with = {compatible_with},
toolchain = "@{user_repository_name}_{platform}//:coreutils_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:coreutils_toolchain_type",
)
""".format(
platform = platform,
user_repository_name = rctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)

# Base BUILD file for this repository
rctx.file("BUILD.bazel", build_content)

coreutils_toolchains_repo = repository_rule(
_coreutils_toolchains_repo_impl,
doc = """Creates a repository with toolchain definitions for all known platforms
which can be registered or selected.""",
attrs = {
"user_repository_name": attr.string(doc = "Base name for toolchains repository"),
},
)

def _coreutils_platform_repo_impl(rctx):
is_windows = rctx.attr.platform.startswith("windows_")
platform = rctx.attr.platform
filename = GREP_VERSIONS[rctx.attr.version][platform]["filename"]
url = "https://github.com/uutils/coreutils/releases/download/{}/{}".format(
rctx.attr.version,
filename,
)
rctx.download_and_extract(
url = url,
stripPrefix = filename.replace(".zip", "").replace(".tar.gz", ""),
integrity = GREP_VERSIONS[rctx.attr.version][platform]["sha256"],
)
build_content = """# @generated by @aspect_bazel_lib//lib/private:coreutils_toolchain.bzl
load("@aspect_bazel_lib//lib/private:coreutils_toolchain.bzl", "coreutils_toolchain")
exports_files(["{0}"])
coreutils_toolchain(name = "coreutils_toolchain", binary = "{0}", visibility = ["//visibility:public"])
""".format("coreutils.exe" if is_windows else "coreutils")

# Base BUILD file for this repository
rctx.file("BUILD.bazel", build_content)

coreutils_platform_repo = repository_rule(
implementation = _coreutils_platform_repo_impl,
doc = "Fetch external tools needed for coreutils toolchain",
attrs = {
"version": attr.string(mandatory = True, values = GREP_VERSIONS.keys()),
"platform": attr.string(mandatory = True, values = GREP_PLATFORMS.keys()),
},
)
Loading

0 comments on commit 3bc8b1e

Please sign in to comment.