Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Bzlmod #1006

Merged
merged 27 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ buildifier:
warnings: "-confusing-name,-constant-glob,-duplicated-name,-function-docstring,-function-docstring-args,-function-docstring-header,-module-docstring,-name-conventions,-no-effect,-constant-glob,-provider-params,-print,-rule-impl-return,-bzl-visibility,-unnamed-macro,-uninitialized,-unreachable"
tasks:
ubuntu1804:
name: "Tests without Bzlmod (Ubuntu 18.04)"
build_flags:
- "--enable_bzlmod=false"
test_flags:
- "--enable_bzlmod=false"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
ubuntu2004_bzlmod:
name: "Tests (Ubuntu 20.04)"
platform: ubuntu2004
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
macos:
name: "Tests (Latest Macos)"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
rbe_ubuntu1604:
test_targets:
- "--"
Expand Down Expand Up @@ -68,6 +83,21 @@ tasks:
- //...
test_targets:
- //...
examples-trivial-bzlmod-ubuntu1804:
name: "Example - Trivial Bzlmod (Ubuntu 18.04)"
platform: ubuntu1804
working_directory: examples/trivial
build_flags:
- "--enable_bzlmod=true"
test_flags:
- "--enable_bzlmod=true"
include_json_profile:
- build
- test
build_targets:
- //...
test_targets:
- //...
examples-trivial-windows:
name: "Example - Trivial (Windows)"
platform: windows
Expand Down
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
common --enable_bzlmod=true
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling Bzlmod by default for rules_kotlin


build --strategy=KotlinCompile=worker
build --test_output=all
build --verbose_failures
Expand Down
67 changes: 67 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module(
name = "io_bazel_rules_kotlin",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call this just "rules_kotlin"? This is what gets exported to BCR: https://github.com/bazelbuild/bazel-central-registry/tree/main/modules

So "io_bazel_" prefix would be a precedent.

"repo_name = " attribute controls how this can be used, that is what you need in the load statements and as dependencies.

version = "1.8.1",
compatibility_level = 1,
repo_name = "io_bazel_rules_kotlin",
)

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_python", version = "0.24.0")
bazel_dep(name = "rules_cc", version = "0.0.8")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules_cc is needed for the remote execution configs.


rules_kotlin_extensions = use_extension("//src/main/starlark/core/repositories:bzlmod_setup.bzl", "rules_kotlin_extensions")
use_repo(
rules_kotlin_extensions,
"build_bazel_rules_android",
"buildkite_config",
"com_github_google_ksp",
"com_github_jetbrains_kotlin",
"com_github_pinterest_ktlint",
"kt_java_stub_template",
)

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

# Development dependencies

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

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "kotlin_rules_maven",
artifacts = [
"com.google.code.findbugs:jsr305:3.0.2",
"junit:junit:4.13-beta-3",
"com.google.protobuf:protobuf-java:3.6.0",
"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.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",
"javax.annotation:javax.annotation-api:1.3.2",
"javax.inject:javax.inject:1",
"org.pantsbuild:jarjar:1.7.2",
"org.jetbrains.kotlinx:atomicfu-js:0.15.2",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
],
fetch_sources = True,
repositories = [
"https://maven-central.storage.googleapis.com/repos/central/data/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)
use_repo(maven, "kotlin_rules_maven")

bazel_dep(name = "rules_pkg", version = "0.7.0")
bazel_dep(name = "stardoc", version = "0.5.6", 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")
Empty file added WORKSPACE.bzlmod
Empty file.
3 changes: 2 additions & 1 deletion docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ This macro registers the kotlin toolchain.
## kotlin_repositories

<pre>
kotlin_repositories(<a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-ksp_repository_name">ksp_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>,
kotlin_repositories(<a href="#kotlin_repositories-bzlmod">bzlmod</a>, <a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-ksp_repository_name">ksp_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>,
<a href="#kotlin_repositories-ksp_compiler_release">ksp_compiler_release</a>)
</pre>

Expand All @@ -540,6 +540,7 @@ Call this in the WORKSPACE file to setup the Kotlin rules.

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="kotlin_repositories-bzlmod"></a>bzlmod | <p align="center"> - </p> | <code>False</code> |
| <a id="kotlin_repositories-compiler_repository_name"></a>compiler_repository_name | for the kotlinc compiler repository. | <code>"com_github_jetbrains_kotlin"</code> |
| <a id="kotlin_repositories-ksp_repository_name"></a>ksp_repository_name | <p align="center"> - </p> | <code>"com_github_google_ksp"</code> |
| <a id="kotlin_repositories-compiler_release"></a>compiler_release | version provider from versions.bzl. | <code>struct()</code> |
Expand Down
31 changes: 31 additions & 0 deletions examples/trivial/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module(name = "trival-example")

bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "io_bazel_rules_kotlin")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be rules_kotlin with optional repo_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")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
artifacts = [
"com.expedia:graphql-kotlin:1.0.0-RC5",
"com.expedia:graphql-kotlin-schema-generator:1.0.0-RC5",
"com.expedia:graphql-kotlin-federation:1.0.0-RC5",
"com.graphql-java:graphql-java:13.0",
"org.opentest4j:opentest4j:1.1.1",
"org.apiguardian:apiguardian-api:1.0.0",
"org.junit.platform:junit-platform-commons:1.4.2",
"org.junit.jupiter:junit-jupiter-api:5.4.2",
"org.junit.jupiter:junit-jupiter-params:5.4.2",
"org.apache.logging.log4j:log4j-core:2.16.0",
],
repositories = [
"https://maven-central.storage.googleapis.com/repos/central/data/",
"https://repo1.maven.org/maven2",
],
)
use_repo(maven, "maven")
Empty file.
1 change: 0 additions & 1 deletion kotlin/internal/jvm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ bzl_library(
deps = [
"//third_party:bzl",
"@bazel_skylib//rules:common_settings",
"@build_bazel_rules_android//android",
],
)
3 changes: 1 addition & 2 deletions src/main/starlark/core/repositories/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ bzl_library(
srcs = glob(["*.bzl"]),
visibility = ["//:__subpackages__"],
deps = [
"@rules_proto//proto",
"@rules_proto//proto/private",
"@rules_proto//proto:defs",
],
)
81 changes: 81 additions & 0 deletions src/main/starlark/core/repositories/bzlmod_setup.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""Definitions for bzlmod module extensions."""

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

_kotlinc_version_tag = tag_class(
attrs = {
"version": attr.string(
mandatory = True,
default = _versions.KOTLIN_CURRENT_COMPILER_RELEASE.version,
),
"sha256": attr.string(
mandatory = True,
default = _versions.KOTLIN_CURRENT_COMPILER_RELEASE.sha256,
),
},
)

_ksp_version_tag = tag_class(
attrs = {
"version": attr.string(
mandatory = True,
default = _versions.KSP_CURRENT_COMPILER_PLUGIN_RELEASE.version,
),
"sha256": attr.string(
mandatory = True,
default = _versions.KSP_CURRENT_COMPILER_PLUGIN_RELEASE.sha256,
),
},
)

def _extra_repositories():
# TODO(bencodes) This really needs to be a development dependency
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a TODO here to move this out into a development dependency. I'll do another pass once this is merged to get these dependencies into better shape so that we can do a production-ready release of rules_kotlin.

# 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,
)

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(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)

_extra_repositories()

rules_kotlin_extensions = module_extension(
implementation = _rules_kotlin_extensions_impl,
tag_classes = {
"kotlinc_version": _kotlinc_version_tag,
"ksp_version": _ksp_version_tag,
},
)
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()
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 @@ -87,6 +88,9 @@ def kotlin_repositories(
urls = versions.ANDROID.URLS,
)

if bzlmod:
return

versions.use_repository(
name = "rules_python",
rule = http_archive,
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