Skip to content

Commit

Permalink
refactor: rework toolchain registration for WORKSPACE and bzlmod users (
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide authored and alexeagle committed Dec 23, 2023
1 parent 8398c76 commit 850014b
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 102 deletions.
12 changes: 6 additions & 6 deletions .bcr/patches/go_dev_dep.patch
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
diff --git a/MODULE.bazel b/MODULE.bazel
index 07a6eff..26702ad 100644
index e63fa5b..9d78a88 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -34,19 +34,19 @@ register_toolchains(
@@ -50,19 +50,19 @@ use_repo(host, "aspect_bazel_lib_host")
bazel_dep(
name = "gazelle",
version = "0.33.0",
- # In released versions: dev_dependency = True
+ dev_dependency = True
+ dev_dependency = True,
)
bazel_dep(
name = "rules_go",
version = "0.41.0",
repo_name = "io_bazel_rules_go",
- # In released versions: dev_dependency = True
+ dev_dependency = True
+ dev_dependency = True,
)

go_deps = use_extension(
"@gazelle//:extensions.bzl",
"go_deps",
- # In released versions: dev_dependency = True
+ dev_dependency = True
+ dev_dependency = True,
)
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
21 changes: 6 additions & 15 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,16 @@ http_archive(
url = "https://github.com/aspect-build/bazel-lib/releases/download/${TAG}/${ARCHIVE}",
)
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")
aspect_bazel_lib_dependencies()
\`\`\`
# Required bazel-lib dependencies
Optional toolchains:
\`\`\`starlark
# Register the following toolchain to use jq
load("@aspect_bazel_lib//lib:repositories.bzl", "register_jq_toolchains")
register_jq_toolchains()
aspect_bazel_lib_dependencies()
# Register the following toolchain to use yq
# Register bazel-lib toolchains
load("@aspect_bazel_lib//lib:repositories.bzl", "register_yq_toolchains")
aspect_bazel_lib_register_toolchains()
register_yq_toolchains()
\`\`\`
EOF
15 changes: 13 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ bazel_dep(name = "platforms", version = "0.0.7")
# 0.5.4 is the first version with bzlmod support
bazel_dep(name = "stardoc", version = "0.5.4", repo_name = "io_bazel_stardoc")

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext")
use_repo(ext, "bsd_tar_toolchains", "copy_directory_toolchains", "copy_to_directory_toolchains", "coreutils_toolchains", "expand_template_toolchains", "jq_toolchains", "yq_toolchains")
bazel_lib_toolchains = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")
bazel_lib_toolchains.copy_directory()
bazel_lib_toolchains.copy_to_directory()
bazel_lib_toolchains.jq()
bazel_lib_toolchains.yq()
bazel_lib_toolchains.coreutils()
bazel_lib_toolchains.tar()
bazel_lib_toolchains.expand_template()
use_repo(bazel_lib_toolchains, "bsd_tar_toolchains", "copy_directory_toolchains", "copy_to_directory_toolchains", "coreutils_toolchains", "expand_template_toolchains", "jq_toolchains", "yq_toolchains")

register_toolchains(
"@copy_directory_toolchains//:all",
Expand All @@ -32,6 +39,10 @@ register_toolchains(
"@bsd_tar_toolchains//:host_toolchain",
)

host = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "host", dev_dependency = True)
host.host()
use_repo(host, "aspect_bazel_lib_host")

# To allow /tools to be built from source
# NOTE: when publishing to BCR, we patch this to be dev_dependency, as we publish pre-built binaries
# along with our releases.
Expand Down
4 changes: 3 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ load(":internal_deps.bzl", "bazel_lib_internal_deps")
# Fetch deps needed only locally for development
bazel_lib_internal_deps()

load("//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
load("//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

aspect_bazel_lib_dependencies()

aspect_bazel_lib_register_toolchains()

# For running our own unit tests
load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")

Expand Down
8 changes: 0 additions & 8 deletions docs/jq.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docs/repositories.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions docs/yq.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions e2e/copy_to_directory/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ local_path_override(
module_name = "aspect_bazel_lib",
path = "../..",
)

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext")
ext.host()
use_repo(ext, "aspect_bazel_lib_host")
4 changes: 3 additions & 1 deletion e2e/copy_to_directory/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ local_repository(
path = "../..",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "register_copy_to_directory_toolchains")

aspect_bazel_lib_dependencies()

register_copy_to_directory_toolchains()

############################################
# rules_go is needed to consume tools from sources

Expand Down
4 changes: 2 additions & 2 deletions e2e/coreutils/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ local_path_override(
path = "../..",
)

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext")
use_repo(ext, "coreutils_toolchains")
bazel_lib_toolchains = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")
use_repo(bazel_lib_toolchains, "coreutils_toolchains")
4 changes: 3 additions & 1 deletion e2e/coreutils/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local_repository(
path = "../..",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "register_coreutils_toolchains")

aspect_bazel_lib_dependencies()

register_coreutils_toolchains()
6 changes: 2 additions & 4 deletions e2e/smoke/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ local_repository(
path = "../..",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "register_jq_toolchains", "register_yq_toolchains")
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

aspect_bazel_lib_dependencies()

register_jq_toolchains()

register_yq_toolchains()
aspect_bazel_lib_register_toolchains()

############################################
# rules_go is needed to consume tools from sources
Expand Down
98 changes: 85 additions & 13 deletions lib/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

load(
"@aspect_bazel_lib//lib:repositories.bzl",
"DEFAULT_COPY_DIRECTORY_REPOSITORY",
"DEFAULT_COPY_TO_DIRECTORY_REPOSITORY",
"DEFAULT_COREUTILS_REPOSITORY",
"DEFAULT_COREUTILS_VERSION",
"DEFAULT_EXPAND_TEMPLATE_REPOSITORY",
"DEFAULT_JQ_REPOSITORY",
"DEFAULT_JQ_VERSION",
"DEFAULT_TAR_REPOSITORY",
"DEFAULT_YQ_REPOSITORY",
"DEFAULT_YQ_VERSION",
"register_copy_directory_toolchains",
"register_copy_to_directory_toolchains",
"register_coreutils_toolchains",
Expand All @@ -10,17 +20,10 @@ load(
"register_tar_toolchains",
"register_yq_toolchains",
)
load("//lib/private:extension_utils.bzl", "extension_utils")
load("//lib/private:host_repo.bzl", "host_repo")

def _toolchain_extension(mctx):
register_copy_directory_toolchains(register = False)
register_copy_to_directory_toolchains(register = False)
register_jq_toolchains(register = False)
register_yq_toolchains(register = False)
register_coreutils_toolchains(register = False)
register_tar_toolchains(register = False)
register_expand_template_toolchains(register = False)

def _host_extension_impl(mctx):
create_host_repo = False
for module in mctx.modules:
if len(module.tags.host) > 0:
Expand All @@ -29,8 +32,77 @@ def _toolchain_extension(mctx):
if create_host_repo:
host_repo(name = "aspect_bazel_lib_host")

# TODO: some way for users to control repo name/version of the tools installed
ext = module_extension(
implementation = _toolchain_extension,
tag_classes = {"host": tag_class(attrs = {})},
host = module_extension(
implementation = _host_extension_impl,
tag_classes = {
"host": tag_class(attrs = {}),
},
)

def _toolchains_extension_impl(mctx):
extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.copy_directory,
toolchain_name = "copy_directory",
toolchain_repos_fn = lambda name, version: register_copy_directory_toolchains(name = name, register = False),
get_version_fn = lambda attr: None,
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.copy_to_directory,
toolchain_name = "copy_to_directory",
toolchain_repos_fn = lambda name, version: register_copy_to_directory_toolchains(name = name, register = False),
get_version_fn = lambda attr: None,
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.jq,
toolchain_name = "jq",
toolchain_repos_fn = lambda name, version: register_jq_toolchains(name = name, version = version, register = False),
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.yq,
toolchain_name = "yq",
toolchain_repos_fn = lambda name, version: register_yq_toolchains(name = name, version = version, register = False),
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.coreutils,
toolchain_name = "coreutils",
toolchain_repos_fn = lambda name, version: register_coreutils_toolchains(name = name, version = version, register = False),
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.tar,
toolchain_name = "tar",
default_repository = DEFAULT_TAR_REPOSITORY,
toolchain_repos_fn = lambda name, version: register_tar_toolchains(name = name, register = False),
get_version_fn = lambda attr: None,
)

extension_utils.toolchain_repos_bfs(
mctx = mctx,
get_tag_fn = lambda tags: tags.expand_template,
toolchain_name = "expand_template",
toolchain_repos_fn = lambda name, version: register_expand_template_toolchains(name = name, register = False),
get_version_fn = lambda attr: None,
)

toolchains = module_extension(
implementation = _toolchains_extension_impl,
tag_classes = {
"copy_directory": tag_class(attrs = {"name": attr.string(default = DEFAULT_COPY_DIRECTORY_REPOSITORY)}),
"copy_to_directory": tag_class(attrs = {"name": attr.string(default = DEFAULT_COPY_TO_DIRECTORY_REPOSITORY)}),
"jq": tag_class(attrs = {"name": attr.string(default = DEFAULT_JQ_REPOSITORY), "version": attr.string(default = DEFAULT_JQ_VERSION)}),
"yq": tag_class(attrs = {"name": attr.string(default = DEFAULT_YQ_REPOSITORY), "version": attr.string(default = DEFAULT_YQ_VERSION)}),
"coreutils": tag_class(attrs = {"name": attr.string(default = DEFAULT_COREUTILS_REPOSITORY), "version": attr.string(default = DEFAULT_COREUTILS_VERSION)}),
"tar": tag_class(attrs = {"name": attr.string(default = DEFAULT_TAR_REPOSITORY)}),
"expand_template": tag_class(attrs = {"name": attr.string(default = DEFAULT_EXPAND_TEMPLATE_REPOSITORY)}),
},
)
8 changes: 0 additions & 8 deletions lib/jq.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ def jq(name, srcs, filter = None, filter_file = None, args = [], out = None, **k
For jq documentation, see https://stedolan.github.io/jq/.
To use this rule you must register the jq toolchain in your WORKSPACE:
```starlark
load("@aspect_bazel_lib//lib:repositories.bzl", "register_jq_toolchains")
register_jq_toolchains()
```
Usage examples:
```starlark
Expand Down
Loading

0 comments on commit 850014b

Please sign in to comment.