Skip to content

Commit f92d4b7

Browse files
authored
Add support for KSP annotation processing via KspCompile (bazelbuild#955)
1 parent 395a525 commit f92d4b7

36 files changed

+688
-39
lines changed

.bazelci/presubmit.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ tasks:
3333
working_directory: examples/android
3434
test_targets:
3535
- //...
36+
example-ksp:
37+
name: "Example - Android KSP"
38+
platform: ubuntu1804
39+
working_directory: examples/ksp
40+
test_targets:
41+
- //...
3642
example-associates:
3743
name: "Example - Associates"
3844
platform: ubuntu1804

examples/android/WORKSPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ maven_install(
4545
"androidx.test.espresso:espresso-core:3.1.1",
4646
"org.hamcrest:hamcrest-library:1.3",
4747
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
48+
"com.squareup.moshi:moshi:1.14.0",
49+
"com.squareup.moshi:moshi-kotlin:1.14.0",
50+
"com.squareup.moshi:moshi-kotlin-codegen:1.14.0",
4851
"com.google.dagger:dagger:2.45",
4952
"com.google.dagger:dagger-compiler:2.45",
5053
"com.google.dagger:dagger-producers:2.45",

examples/android/libKtAndroid/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin")
1+
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin", "kt_ksp_plugin")
22
load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library")
33
load("@rules_java//java:defs.bzl", "java_plugin")
44

@@ -19,12 +19,23 @@ java_plugin(
1919
deps = ["@maven//:com_google_auto_value_auto_value"],
2020
)
2121

22+
kt_ksp_plugin(
23+
name = "moshi-kotlin-codegen",
24+
processor_class = "com.squareup.moshi.kotlin.codegen.ksp.JsonClassSymbolProcessorProvider",
25+
deps = [
26+
"@maven//:com_squareup_moshi_moshi",
27+
"@maven//:com_squareup_moshi_moshi_kotlin",
28+
"@maven//:com_squareup_moshi_moshi_kotlin_codegen",
29+
],
30+
)
31+
2232
kt_android_library(
2333
name = "my_kt",
2434
srcs = glob(["src/main/java/**/*.kt"]),
2535
custom_package = "examples.android.lib",
2636
manifest = "src/main/AndroidManifest.xml",
2737
plugins = [
38+
":moshi-kotlin-codegen",
2839
":serialization_plugin",
2940
":autovalue",
3041
],
@@ -35,6 +46,7 @@ kt_android_library(
3546
":res2",
3647
"@maven//:androidx_appcompat_appcompat",
3748
"@maven//:com_google_auto_value_auto_value_annotations",
49+
"@maven//:com_squareup_moshi_moshi",
3850
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
3951
],
4052
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package examples.android.lib
2+
3+
import com.squareup.moshi.JsonClass
4+
5+
@JsonClass(generateAdapter = true)
6+
data class DataJsonModel(val data: String)

examples/android/libKtAndroid/src/main/java/examples/android/lib/MainActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.widget.Button
66
import android.widget.LinearLayout
77
import android.widget.LinearLayout.LayoutParams
88
import androidx.appcompat.app.AppCompatActivity
9+
import com.squareup.moshi.Moshi
910

1011
class MainActivity : AppCompatActivity() {
1112

@@ -22,6 +23,9 @@ class MainActivity : AppCompatActivity() {
2223
// Ensure Serialization plugin has run and generated code correctly.
2324
Data.serializer()
2425

26+
val adapter = DataJsonModelJsonAdapter(Moshi.Builder().build())
27+
println(adapter.toJson(DataJsonModel("foo")))
28+
2529
TestKtValue.create {
2630
setName("Auto Value Test") // can't use property syntax, because autovalue builder's codegen
2731
}

examples/ksp/BUILD

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_ksp_plugin")
15+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
16+
load("@rules_java//java:defs.bzl", "java_binary", "java_plugin")
17+
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
18+
19+
package(default_visibility = ["//visibility:public"])
20+
21+
define_kt_toolchain(name = "kotlin_toolchain")
22+
23+
java_plugin(
24+
name = "autovalue",
25+
generates_api = True,
26+
processor_class = "com.google.auto.value.processor.AutoValueProcessor",
27+
deps = ["@maven//:com_google_auto_value_auto_value"],
28+
)
29+
30+
kt_ksp_plugin(
31+
name = "moshi-kotlin-codegen",
32+
processor_class = "com.squareup.moshi.kotlin.codegen.ksp.JsonClassSymbolProcessorProvider",
33+
deps = [
34+
"@maven//:com_squareup_moshi_moshi",
35+
"@maven//:com_squareup_moshi_moshi_kotlin",
36+
"@maven//:com_squareup_moshi_moshi_kotlin_codegen",
37+
],
38+
)
39+
40+
kt_jvm_library(
41+
name = "coffee_lib",
42+
srcs = glob([
43+
"*.kt",
44+
"*.java",
45+
]),
46+
plugins = [
47+
"//:moshi-kotlin-codegen",
48+
"//:autovalue",
49+
],
50+
deps = [
51+
"@maven//:com_google_auto_value_auto_value_annotations",
52+
"@maven//:com_squareup_moshi_moshi",
53+
"@maven//:com_squareup_moshi_moshi_kotlin",
54+
],
55+
)
56+
57+
java_binary(
58+
name = "coffee_app",
59+
main_class = "coffee.CoffeeApp",
60+
visibility = ["//visibility:public"],
61+
runtime_deps = [":coffee_lib"],
62+
)
63+
64+
build_test(
65+
name = "force_build_app_test",
66+
targets = [
67+
"//:coffee_app",
68+
# build_test doesn't actually fail unless you force the deploy jar to be built
69+
"//:coffee_app_deploy.jar",
70+
],
71+
)

examples/ksp/CoffeeApp.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2018 The Bazel Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package coffee
17+
18+
import com.squareup.moshi.Moshi
19+
20+
class CoffeeApp {
21+
22+
companion object {
23+
24+
private val adapter = CoffeeAppModelJsonAdapter(Moshi.Builder().build())
25+
private val d = AutoValue_CoffeeAppJavaModel.Builder()
26+
.setCoffeeAppModel(CoffeeAppModel("1"))
27+
.build()
28+
29+
@JvmStatic
30+
fun main(args: Array<String>) {
31+
println(
32+
adapter.toJson(d.coffeeAppModel())
33+
)
34+
}
35+
}
36+
}

examples/ksp/CoffeeAppJavaModel.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package coffee;
2+
3+
import com.google.auto.value.AutoValue;
4+
5+
@AutoValue
6+
public abstract class CoffeeAppJavaModel {
7+
8+
abstract CoffeeAppModel coffeeAppModel();
9+
10+
Builder builder() {
11+
return new AutoValue_CoffeeAppJavaModel.Builder();
12+
}
13+
14+
@AutoValue.Builder
15+
abstract static class Builder {
16+
17+
abstract Builder setCoffeeAppModel(CoffeeAppModel coffeeAppModel);
18+
19+
abstract CoffeeAppJavaModel build();
20+
}
21+
}

examples/ksp/CoffeeAppModel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package coffee
2+
3+
import com.squareup.moshi.JsonClass
4+
5+
@JsonClass(generateAdapter = true)
6+
data class CoffeeAppModel(val id: String)

examples/ksp/WORKSPACE

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
workspace(name = "ksp_example")
2+
3+
local_repository(
4+
name = "release_archive",
5+
path = "../../src/main/starlark/release_archive",
6+
)
7+
8+
load("@release_archive//:repository.bzl", "archive_repository")
9+
10+
archive_repository(
11+
name = "io_bazel_rules_kotlin",
12+
)
13+
14+
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "versions")
15+
16+
kotlin_repositories()
17+
18+
register_toolchains("//:kotlin_toolchain")
19+
20+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
21+
22+
http_archive(
23+
name = "bazel_skylib",
24+
sha256 = versions.SKYLIB_SHA,
25+
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/%s/bazel-skylib-%s.tar.gz" % (
26+
versions.SKYLIB_VERSION,
27+
versions.SKYLIB_VERSION,
28+
)],
29+
)
30+
31+
http_archive(
32+
name = "rules_jvm_external",
33+
sha256 = versions.RULES_JVM_EXTERNAL_SHA,
34+
strip_prefix = "rules_jvm_external-%s" % versions.RULES_JVM_EXTERNAL_TAG,
35+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % versions.RULES_JVM_EXTERNAL_TAG,
36+
)
37+
38+
load("@rules_jvm_external//:defs.bzl", "maven_install")
39+
40+
maven_install(
41+
artifacts = [
42+
"com.squareup.moshi:moshi:1.14.0",
43+
"com.squareup.moshi:moshi-kotlin:1.14.0",
44+
"com.squareup.moshi:moshi-kotlin-codegen:1.14.0",
45+
"com.google.auto.value:auto-value:1.10.1",
46+
"com.google.auto.value:auto-value-annotations:1.10.1",
47+
],
48+
repositories = [
49+
"https://maven.google.com",
50+
"https://repo1.maven.org/maven2",
51+
],
52+
)

kotlin/core.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load(
66
load(
77
"//kotlin/internal/jvm:jvm.bzl",
88
_kt_compiler_plugin = "kt_compiler_plugin",
9+
_kt_ksp_plugin = "kt_ksp_plugin",
910
)
1011
load(
1112
"//kotlin/internal:toolchains.bzl",
@@ -18,3 +19,4 @@ kt_register_toolchains = _kt_register_toolchains
1819
kt_javac_options = _kt_javac_options
1920
kt_kotlinc_options = _kt_kotlinc_options
2021
kt_compiler_plugin = _kt_compiler_plugin
22+
kt_ksp_plugin = _kt_ksp_plugin

kotlin/internal/defs.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ JavaPluginInfo = getattr(java_common, "JavaPluginInfo")
2525
# The name of the Kotlin compiler workspace.
2626
KT_COMPILER_REPO = "com_github_jetbrains_kotlin"
2727

28+
# The name of the KSP compiler plugin workspace
29+
KSP_COMPILER_PLUGIN_REPO = "com_github_google_ksp"
30+
2831
KtJvmInfo = provider(
2932
fields = {
3033
"module_name": "the module name",
@@ -57,3 +60,9 @@ KtCompilerPluginInfo = provider(
5760
"options": "List of plugin options, represented as structs with an id and a value field, to be passed to the compiler",
5861
},
5962
)
63+
64+
KspPluginInfo = provider(
65+
fields = {
66+
"plugins": "List of JavaPLuginInfo providers for the plugins to run with KSP",
67+
},
68+
)

0 commit comments

Comments
 (0)