Skip to content

Commit

Permalink
Create a new "bootstrap" toolchain and compile with 2.1.0 (#1244)
Browse files Browse the repository at this point in the history
* Create a new "bootstrap" toolchain.

Given that Kotlin 2.1.0 now has a version metadata check during compilation, the idea of bootstrapping kotlin by using the previous is pretty much dead. Probably not the worst thing, as bzlmod did not enjoy the shennanigans necessary to make it work.

This adds a new "cli" toolchain that invokes kotlinc via the established command line interface. Additionally, it sketches out a simpler, more contained toolchain api to build the more complex usecases (such as multiplex workers, jdeps, compiler plugins, etc.) on top of. the end goal is a more agile rule approach -- with the performance optimization (jdeps, class path reductions, etc) tucked behindthe simple api.Compiler plugins, linting, and the like should be able to live comfortably in the "production" rule and toolchain combinations.

It also introduces new "core" rules, while limited in scope, should be IDE friendly and generally easier to work with than the previous bootstrapping effort -- and since they use the common providers, they will interoperate with the existing rules.

Includes myriad of small fixes to get everything running on 2.1.
  • Loading branch information
restingbull authored Dec 24, 2024
1 parent c68e194 commit 7a988ee
Show file tree
Hide file tree
Showing 60 changed files with 3,941 additions and 1,067 deletions.
2 changes: 0 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ test_suite(
tests = [
"//src/test/kotlin/io/bazel/kotlin:assertion_tests",
"//src/test/kotlin/io/bazel/kotlin/builder:builder_tests",
"//src/test/kotlin/io/bazel/kotlin/integration:integration_tests",
"//src/test/kotlin/io/bazel/worker:worker_tests",
"//src/test/starlark:convert_tests",
],
Expand All @@ -50,7 +49,6 @@ test_suite(
tests = [
":all_tests",
"//src/test/kotlin/io/bazel/kotlin:local_assertion_tests",
"//src/test/kotlin/io/bazel/kotlin/integration:local_integration_tests",
"//src/test/kotlin/io/bazel/worker:local_worker_tests",
"//src/test/starlark:convert_tests",
],
Expand Down
21 changes: 4 additions & 17 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,10 @@ use_repo(
"kotlinx_serialization_core_jvm",
"kotlinx_serialization_json",
"kotlinx_serialization_json_jvm",
"released_rules_kotlin",
)

# Once the released rules_koltin is defined, configure it.
rules_kotlin_bootstrap_extensions = use_extension(
"//src/main/starlark/core/repositories:bzlmod_bootstrap.bzl",
"rules_kotlin_bootstrap_extensions",
)
use_repo(
rules_kotlin_bootstrap_extensions,
"released_com_github_google_ksp",
"released_com_github_jetbrains_kotlin",
)

register_toolchains("@released_rules_kotlin//kotlin/internal:default_toolchain")
register_toolchains("//src/main/starlark/core/compile/cli")

# Back to the regularly scheduled configuration.
register_toolchains("//kotlin/internal:default_toolchain")

# TODO(bencodes) We should be able to remove this once rules_android has rolled out official Bzlmod support
Expand Down Expand Up @@ -73,9 +60,9 @@ maven.install(
"com.google.auto.service:auto-service-annotations:1.1.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.51",
"com.google.dagger:dagger-compiler:2.51",
"com.google.dagger:dagger-producers:2.51",
"com.google.dagger:dagger:2.53.1",
"com.google.dagger:dagger-compiler:2.53.1",
"com.google.dagger:dagger-producers:2.53.1",
"javax.annotation:javax.annotation-api:1.3.2",
"javax.inject:javax.inject:1",
"org.apache.commons:commons-compress:1.26.2",
Expand Down
2 changes: 2 additions & 0 deletions WORKSPACE.dev.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ load("//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories()

register_toolchains("//src/main/starlark/core/compile/cli")

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

android_sdk_repository(name = "androidsdk")
23 changes: 10 additions & 13 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,15 @@ Define java compiler options for `kt_jvm_*` rules with java sources.
<pre>
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")

kt_kotlinc_options(<a href="#kt_kotlinc_options-name">name</a>, <a href="#kt_kotlinc_options-include_stdlibs">include_stdlibs</a>, <a href="#kt_kotlinc_options-java_parameters">java_parameters</a>, <a href="#kt_kotlinc_options-jvm_target">jvm_target</a>, <a href="#kt_kotlinc_options-warn">warn</a>,
<a href="#kt_kotlinc_options-x_allow_result_return_type">x_allow_result_return_type</a>, <a href="#kt_kotlinc_options-x_assertions">x_assertions</a>, <a href="#kt_kotlinc_options-x_backend_threads">x_backend_threads</a>,
<a href="#kt_kotlinc_options-x_consistent_data_class_copy_visibility">x_consistent_data_class_copy_visibility</a>, <a href="#kt_kotlinc_options-x_context_receivers">x_context_receivers</a>,
kt_kotlinc_options(<a href="#kt_kotlinc_options-name">name</a>, <a href="#kt_kotlinc_options-include_stdlibs">include_stdlibs</a>, <a href="#kt_kotlinc_options-java_parameters">java_parameters</a>, <a href="#kt_kotlinc_options-jvm_target">jvm_target</a>, <a href="#kt_kotlinc_options-warn">warn</a>, <a href="#kt_kotlinc_options-x_assertions">x_assertions</a>,
<a href="#kt_kotlinc_options-x_backend_threads">x_backend_threads</a>, <a href="#kt_kotlinc_options-x_consistent_data_class_copy_visibility">x_consistent_data_class_copy_visibility</a>, <a href="#kt_kotlinc_options-x_context_receivers">x_context_receivers</a>,
<a href="#kt_kotlinc_options-x_emit_jvm_type_annotations">x_emit_jvm_type_annotations</a>, <a href="#kt_kotlinc_options-x_enable_incremental_compilation">x_enable_incremental_compilation</a>, <a href="#kt_kotlinc_options-x_explicit_api_mode">x_explicit_api_mode</a>,
<a href="#kt_kotlinc_options-x_inline_classes">x_inline_classes</a>, <a href="#kt_kotlinc_options-x_jdk_release">x_jdk_release</a>, <a href="#kt_kotlinc_options-x_jspecify_annotations">x_jspecify_annotations</a>, <a href="#kt_kotlinc_options-x_jsr_305">x_jsr_305</a>, <a href="#kt_kotlinc_options-x_jvm_default">x_jvm_default</a>,
<a href="#kt_kotlinc_options-x_lambdas">x_lambdas</a>, <a href="#kt_kotlinc_options-x_multi_platform">x_multi_platform</a>, <a href="#kt_kotlinc_options-x_no_call_assertions">x_no_call_assertions</a>, <a href="#kt_kotlinc_options-x_no_optimize">x_no_optimize</a>,
<a href="#kt_kotlinc_options-x_no_optimized_callable_references">x_no_optimized_callable_references</a>, <a href="#kt_kotlinc_options-x_no_param_assertions">x_no_param_assertions</a>,
<a href="#kt_kotlinc_options-x_no_receiver_assertions">x_no_receiver_assertions</a>, <a href="#kt_kotlinc_options-x_no_source_debug_extension">x_no_source_debug_extension</a>, <a href="#kt_kotlinc_options-x_optin">x_optin</a>, <a href="#kt_kotlinc_options-x_report_perf">x_report_perf</a>,
<a href="#kt_kotlinc_options-x_sam_conversions">x_sam_conversions</a>, <a href="#kt_kotlinc_options-x_skip_prerelease_check">x_skip_prerelease_check</a>, <a href="#kt_kotlinc_options-x_suppress_version_warnings">x_suppress_version_warnings</a>,
<a href="#kt_kotlinc_options-x_type_enhancement_improvements_strict_mode">x_type_enhancement_improvements_strict_mode</a>, <a href="#kt_kotlinc_options-x_use_fir_lt">x_use_fir_lt</a>, <a href="#kt_kotlinc_options-x_use_k2">x_use_k2</a>)
<a href="#kt_kotlinc_options-x_no_param_assertions">x_no_param_assertions</a>, <a href="#kt_kotlinc_options-x_no_receiver_assertions">x_no_receiver_assertions</a>, <a href="#kt_kotlinc_options-x_no_source_debug_extension">x_no_source_debug_extension</a>,
<a href="#kt_kotlinc_options-x_optin">x_optin</a>, <a href="#kt_kotlinc_options-x_report_perf">x_report_perf</a>, <a href="#kt_kotlinc_options-x_sam_conversions">x_sam_conversions</a>, <a href="#kt_kotlinc_options-x_skip_prerelease_check">x_skip_prerelease_check</a>,
<a href="#kt_kotlinc_options-x_suppress_version_warnings">x_suppress_version_warnings</a>, <a href="#kt_kotlinc_options-x_type_enhancement_improvements_strict_mode">x_type_enhancement_improvements_strict_mode</a>,
<a href="#kt_kotlinc_options-x_use_fir_lt">x_use_fir_lt</a>, <a href="#kt_kotlinc_options-x_use_k2">x_use_k2</a>)
</pre>

Define kotlin compiler options.
Expand All @@ -398,9 +397,8 @@ Define kotlin compiler options.
| <a id="kt_kotlinc_options-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="kt_kotlinc_options-include_stdlibs"></a>include_stdlibs | Don't automatically include the Kotlin standard libraries into the classpath (stdlib and reflect). | String | optional | `"all"` |
| <a id="kt_kotlinc_options-java_parameters"></a>java_parameters | Generate metadata for Java 1.8+ reflection on method parameters. | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-jvm_target"></a>jvm_target | The -jvm_target flag. This is only tested at 1.8. | String | optional | `""` |
| <a id="kt_kotlinc_options-jvm_target"></a>jvm_target | The target version of the generated JVM bytecode | String | optional | `""` |
| <a id="kt_kotlinc_options-warn"></a>warn | Control warning behaviour. | String | optional | `"report"` |
| <a id="kt_kotlinc_options-x_allow_result_return_type"></a>x_allow_result_return_type | Enable kotlin.Result as a return type | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_assertions"></a>x_assertions | Configures how assertions are handled. The 'jvm' option enables assertions in JVM code. | String | optional | `""` |
| <a id="kt_kotlinc_options-x_backend_threads"></a>x_backend_threads | When using the IR backend, run lowerings by file in N parallel threads. 0 means use a thread per processor core. Default value is 1. | Integer | optional | `1` |
| <a id="kt_kotlinc_options-x_consistent_data_class_copy_visibility"></a>x_consistent_data_class_copy_visibility | The effect of this compiler flag is the same as applying @ConsistentCopyVisibility annotation to all data classes in the module. See https://youtrack.jetbrains.com/issue/KT-11914 | Boolean | optional | `False` |
Expand All @@ -409,15 +407,14 @@ Define kotlin compiler options.
| <a id="kt_kotlinc_options-x_enable_incremental_compilation"></a>x_enable_incremental_compilation | Enable incremental compilation | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_explicit_api_mode"></a>x_explicit_api_mode | Enable explicit API mode for Kotlin libraries. | String | optional | `"off"` |
| <a id="kt_kotlinc_options-x_inline_classes"></a>x_inline_classes | Enable experimental inline classes | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_jdk_release"></a>x_jdk_release | The -jvm_target flag. This is only tested at 1.8. | String | optional | `""` |
| <a id="kt_kotlinc_options-x_jdk_release"></a>x_jdk_release | Compile against the specified JDK API version, similarly to javac's '-release'. This requires JDK 9 or newer. The supported versions depend on the JDK used; for JDK 17+, the supported versions are 1.8 and 9–21. This also sets the value of '-jvm-target' to be equal to the selected JDK version. | String | optional | `""` |
| <a id="kt_kotlinc_options-x_jspecify_annotations"></a>x_jspecify_annotations | Controls how JSpecify annotations are treated. Options are 'default', 'ignore', 'warn', and 'strict'. | String | optional | `""` |
| <a id="kt_kotlinc_options-x_jsr_305"></a>x_jsr_305 | Specifies how to handle JSR-305 annotations in Kotlin code. Options are 'default', 'ignore', 'warn', and 'strict'. | String | optional | `""` |
| <a id="kt_kotlinc_options-x_jvm_default"></a>x_jvm_default | Specifies that a JVM default method should be generated for non-abstract Kotlin interface member. | String | optional | `"off"` |
| <a id="kt_kotlinc_options-x_lambdas"></a>x_lambdas | Change codegen behavior of lambdas | String | optional | `"class"` |
| <a id="kt_kotlinc_options-x_multi_platform"></a>x_multi_platform | Enable experimental language support for multi-platform projects | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_call_assertions"></a>x_no_call_assertions | Don't generate not-null assertions for arguments of platform types | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_optimize"></a>x_no_optimize | Disable optimizations | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_optimized_callable_references"></a>x_no_optimized_callable_references | Do not use optimized callable reference superclasses. Available from 1.4. | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_param_assertions"></a>x_no_param_assertions | Don't generate not-null assertions on parameters of methods accessible from Java | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_receiver_assertions"></a>x_no_receiver_assertions | Don't generate not-null assertion for extension receiver arguments of platform types | Boolean | optional | `False` |
| <a id="kt_kotlinc_options-x_no_source_debug_extension"></a>x_no_source_debug_extension | Do not generate @kotlin.jvm.internal.SourceDebugExtension annotation on a class with the copy of SMAP | Boolean | optional | `False` |
Expand Down Expand Up @@ -579,8 +576,8 @@ Call this in the WORKSPACE file to setup the Kotlin rules.
| <a id="kotlin_repositories-is_bzlmod"></a>is_bzlmod | <p align="center"> - </p> | `False` |
| <a id="kotlin_repositories-compiler_repository_name"></a>compiler_repository_name | for the kotlinc compiler repository. | `"com_github_jetbrains_kotlin"` |
| <a id="kotlin_repositories-ksp_repository_name"></a>ksp_repository_name | <p align="center"> - </p> | `"com_github_google_ksp"` |
| <a id="kotlin_repositories-compiler_release"></a>compiler_release | version provider from versions.bzl. | `struct(sha256 = "88d7d8bad362ae4e114a8b9668c6887b8c85f48e340883db0e317e47c8dc2f4f", url_templates = ["https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip"], version = "2.0.10")` |
| <a id="kotlin_repositories-ksp_compiler_release"></a>ksp_compiler_release | (internal) version provider from versions.bzl. | `struct(sha256 = "e6a79e649ee383b372fa982be89686c10ee42b25e60147b3271a70fd75a9eb19", url_templates = ["https://github.com/google/ksp/releases/download/{version}/artifacts.zip"], version = "2.0.10-1.0.24")` |
| <a id="kotlin_repositories-compiler_release"></a>compiler_release | version provider from versions.bzl. | `struct(sha256 = "b6698d5728ad8f9edcdd01617d638073191d8a03139cc538a391b4e3759ad297", url_templates = ["https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip"], version = "2.1.0")` |
| <a id="kotlin_repositories-ksp_compiler_release"></a>ksp_compiler_release | (internal) version provider from versions.bzl. | `struct(sha256 = "fc27b08cadc061a4a989af01cbeccb613feef1995f4aad68f2be0f886a3ee251", url_templates = ["https://github.com/google/ksp/releases/download/{version}/artifacts.zip"], version = "2.1.0-1.0.28")` |


<a id="versions.use_repository"></a>
Expand Down
2 changes: 1 addition & 1 deletion examples/android/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ maven.install(
"junit:junit:4.12",
"androidx.test.espresso:espresso-core:3.1.1",
"org.hamcrest:hamcrest-library:1.3",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
"org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3",
"com.squareup.moshi:moshi:1.14.0",
"com.squareup.moshi:moshi-kotlin:1.14.0",
"com.squareup.moshi:moshi-kotlin-codegen:1.14.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/android/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ maven_install(
"junit:junit:4.12",
"androidx.test.espresso:espresso-core:3.1.1",
"org.hamcrest:hamcrest-library:1.3",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
"org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3",
"com.squareup.moshi:moshi:1.14.0",
"com.squareup.moshi:moshi-kotlin:1.14.0",
"com.squareup.moshi:moshi-kotlin-codegen:1.14.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/android/libKtAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ kt_android_library(
"@maven//:androidx_appcompat_appcompat",
"@maven//:com_google_auto_value_auto_value_annotations",
"@maven//:com_squareup_moshi_moshi",
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_core",
],
)

Expand Down
13 changes: 0 additions & 13 deletions examples/associates/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ kt_register_toolchains()

load("@rules_kotlin//src/main/starlark/core/repositories:versions.bzl", "versions")

http_archive(
name = "rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)

http_archive(
name = "bazel_skylib",
sha256 = versions.SKYLIB_SHA,
Expand Down Expand Up @@ -52,9 +45,3 @@ maven_install(
"https://repo1.maven.org/maven2",
],
)

http_archive(
name = "rules_pkg",
sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
url = "https://github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz",
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class MyType(
}

internal fun String.camelCase() = this.split("_").joinToString("") {
"${it[0].toUpperCase()}${it.substring(1)}"
"${it[0].uppercase()}${it.substring(1)}"
}
5 changes: 4 additions & 1 deletion examples/deps/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
common --incompatible_enable_android_toolchain_resolution
common --android_platforms=//:arm64-v8a
common --enable_workspace=true
common --enable_bzlmod=false
common --android_platforms=//:arm64-v8a
common --toolchain_resolution_debug=.*
26 changes: 3 additions & 23 deletions examples/deps/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ http_archive(
)

load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
load("@rules_kotlin//src/main/starlark/core/repositories:versions.bzl", "versions")

kotlin_repositories()

register_toolchains("//bzl:experimental_toolchain")

load("@rules_kotlin//src/main/starlark/core/repositories:versions.bzl", "versions")

http_archive(
name = "bazel_skylib",
sha256 = versions.SKYLIB_SHA,
Expand All @@ -37,24 +34,17 @@ load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"androidx.appcompat:appcompat:1.0.0",
"junit:junit:4.12",
"androidx.test.espresso:espresso-core:3.1.1",
"org.hamcrest:hamcrest-library:1.3",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
"com.google.dagger:dagger:2.45",
"com.google.dagger:dagger-compiler:2.45",
"com.google.dagger:dagger-producers:2.45",
"com.google.auto.value:auto-value:1.6.5",
"com.google.auto.value:auto-value-annotations:1.6.5",
"org.robolectric:robolectric:4.7.3",
],
repositories = [
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

register_toolchains("//bzl:experimental_toolchain")

http_archive(
name = "rules_android",
sha256 = versions.ANDROID.SHA,
Expand All @@ -70,13 +60,3 @@ load(
android_sdk_repository(
name = "androidsdk",
)

http_archive(
name = "robolectric",
strip_prefix = "robolectric-bazel-4.7.3",
urls = ["https://github.com/robolectric/robolectric-bazel/archive/4.7.3.tar.gz"],
)

load("@robolectric//bazel:robolectric.bzl", "robolectric_repositories")

robolectric_repositories()
2 changes: 1 addition & 1 deletion examples/deps/bzl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kt_javac_options(
name = "default_javac_options",
)

KOTLIN_LANGUAGE_LEVEL = "1.6"
KOTLIN_LANGUAGE_LEVEL = "2.1"

define_kt_toolchain(
name = "experimental_toolchain",
Expand Down
9 changes: 9 additions & 0 deletions examples/jetpack_compose/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ build --define=android_dexmerger_tool=d8_dexmerger
# Flags for the D8 dexer
build --define=android_incremental_dexing_tool=d8_dexbuilder
build --define=android_standalone_dexing_tool=d8_compat_dx

common --enable_workspace=true --enable_bzlmod=false
common --noincompatible_enable_android_toolchain_resolution
build --platform_mappings=platform_mappings
build --fat_apk_cpu arm64-v8a
# Uncomment the below lines to build for x86
# build --fat_apk_cpu x86_64
build --android_crosstool_top=@androidndk//:toolchain
build --@rules_kotlin//kotlin/settings:jvm_emit_jdeps=False
8 changes: 8 additions & 0 deletions examples/jetpack_compose/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ platform(
"@platforms//os:android",
],
)

platform(
name = "x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:android",
],
)
Loading

0 comments on commit 7a988ee

Please sign in to comment.