Skip to content

Commit

Permalink
[bzlmod] Stop relying on initialize.release.bzl (#1127)
Browse files Browse the repository at this point in the history
* [bzlmod] Stop relying on initialize.release.bzl

* [release] Separate bzlmod dev extensions from release extensions

In order to simplify the dependency tree for the rules kotlin bootstrap, bzlmod_setup needed to be separated from the release. The reason for this is twofold: kotlin_repositories in development call kt_configure which configures released_rules_kotlin. Since bzlmod_setup is also used to load released_rules_kotlin this become a dependency cycle. Second, we don't want to load released_rules_koltin in the release module, as it is a development dependency. (whoops.)

This also allows dropping a few extra bits of configuration that are unnecessary for the release.
  • Loading branch information
restingbull authored Mar 13, 2024
1 parent b9380cd commit a675511
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
3 changes: 2 additions & 1 deletion src/main/starlark/core/repositories/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ release_archive(
srcs = [
"BUILD.com_github_google_ksp.bazel",
"BUILD.com_github_jetbrains_kotlin.bazel",
"bzlmod_setup.bzl",
"bzlmod_impl.bzl",
"compiler.bzl",
"ksp.bzl",
"versions.bzl",
],
src_map = {
"initialize.release.bzl": "initialize.bzl",
"BUILD.release.bazel": "BUILD.bazel",
"bzlmod_setup.release.bzl": "bzlmod_setup.bzl",
},
deps = [
"//src/main/starlark/core/repositories/kotlin:pkg",
Expand Down
32 changes: 32 additions & 0 deletions src/main/starlark/core/repositories/bzlmod_impl.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def configure_modules_and_repositories(modules, kotlin_repositories, kotlinc_version, ksp_version):
kotlinc = None
ksp = None
for mod in modules:
for override in mod.tags.kotlinc_version:
if kotlinc:
fail("Only one kotlinc_version is supported right now!")
kotlinc = kotlinc_version(release = override.version, sha256 = override.sha256)
for override in mod.tags.ksp_version:
if ksp:
fail("Only one ksp_version is supported right now!")
ksp = ksp_version(release = override.version, sha256 = override.sha256)

kotlin_repositories_args = dict(is_bzlmod = True)
if kotlinc:
kotlin_repositories_args["compiler_release"] = kotlinc
if ksp:
kotlin_repositories_args["ksp_compiler_release"] = ksp

kotlin_repositories(**kotlin_repositories_args)

_version_tag = tag_class(
attrs = {
"version": attr.string(mandatory = True),
"sha256": attr.string(mandatory = True),
},
)

tag_classes = {
"kotlinc_version": _version_tag,
"ksp_version": _version_tag,
}
62 changes: 16 additions & 46 deletions src/main/starlark/core/repositories/bzlmod_setup.bzl
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
"""Definitions for bzlmod module extensions."""

load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(
"//src/main/starlark/core/repositories:initialize.release.bzl",
_kotlin_repositories = "kotlin_repositories",
_kotlinc_version = "kotlinc_version",
_ksp_version = "ksp_version",
)
load(
"//src/main/starlark/core/repositories:versions.bzl",
_versions = "versions",
)

_version_tag = tag_class(
attrs = {
"version": attr.string(mandatory = True),
"sha256": attr.string(mandatory = True),
},
)

def _extra_repositories():
# This tarball intentionally does not have a SHA256 because the upstream URL can change without notice
# For more context: https://github.com/bazelbuild/bazel-toolchains/blob/0c1f7c3c5f9e63f1e0ee91738b964937eea2d3e0/WORKSPACE#L28-L32
http_archive(
name = "buildkite_config",
urls = _versions.RBE.URLS,
)
load("//src/main/starlark/core/repositories:versions.bzl", _versions = "versions")
load(":bzlmod_impl.bzl", "configure_modules_and_repositories", "tag_classes")

def _rules_kotlin_extensions_impl(mctx):
kotlinc_version = None
ksp_version = None
for mod in mctx.modules:
for override in mod.tags.kotlinc_version:
if kotlinc_version:
fail("Only one kotlinc_version is supported right now!")
kotlinc_version = _kotlinc_version(release = override.version, sha256 = override.sha256)
for override in mod.tags.ksp_version:
if ksp_version:
fail("Only one ksp_version is supported right now!")
ksp_version = _ksp_version(release = override.version, sha256 = override.sha256)

_kotlin_repositories_args = dict(is_bzlmod = True)
if kotlinc_version:
_kotlin_repositories_args["compiler_release"] = kotlinc_version
if ksp_version:
_kotlin_repositories_args["ksp_compiler_release"] = ksp_version
_kotlin_repositories(**_kotlin_repositories_args)
configure_modules_and_repositories(
mctx.modules,
_kotlin_repositories,
_kotlinc_version,
_ksp_version,
)

_versions.use_repository(
name = "released_rules_kotlin",
Expand All @@ -63,12 +31,14 @@ def _rules_kotlin_extensions_impl(mctx):
],
)

_extra_repositories()
# This tarball intentionally does not have a SHA256 because the upstream URL can change without notice
# For more context: https://github.com/bazelbuild/bazel-toolchains/blob/0c1f7c3c5f9e63f1e0ee91738b964937eea2d3e0/WORKSPACE#L28-L32
http_archive(
name = "buildkite_config",
urls = _versions.RBE.URLS,
)

rules_kotlin_extensions = module_extension(
implementation = _rules_kotlin_extensions_impl,
tag_classes = {
"kotlinc_version": _version_tag,
"ksp_version": _version_tag,
},
tag_classes = tag_classes,
)
24 changes: 24 additions & 0 deletions src/main/starlark/core/repositories/bzlmod_setup.release.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load(
"//src/main/starlark/core/repositories:initialize.bzl",
_kotlin_repositories = "kotlin_repositories",
_kotlinc_version = "kotlinc_version",
_ksp_version = "ksp_version",
)
load(
":bzlmod_impl.bzl",
"configure_modules_and_repositories",
"tag_classes",
)

def _rules_kotlin_extensions_impl(mctx):
configure_modules_and_repositories(
mctx.modules,
_kotlin_repositories,
_kotlinc_version,
_ksp_version,
)

rules_kotlin_extensions = module_extension(
implementation = _rules_kotlin_extensions_impl,
tag_classes = tag_classes,
)

0 comments on commit a675511

Please sign in to comment.