Skip to content

Commit 3e50df1

Browse files
Generate KSP classes jar as output
1 parent fe0d460 commit 3e50df1

File tree

16 files changed

+319
-0
lines changed

16 files changed

+319
-0
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ common --enable_bzlmod=true
33
common:rbe --java_runtime_version=11
44
common:rbe --tool_java_runtime_version=11
55

6+
common --java_runtime_version=11
7+
common --tool_java_runtime_version=11
8+
69
build --strategy=KotlinCompile=worker
710
build --test_output=all
811
build --verbose_failures

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@ bazel_dep(name = "stardoc", version = "0.5.6", repo_name = "io_bazel_stardoc")
9393
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
9494

9595
bazel_dep(name = "rules_testing", version = "0.5.0", dev_dependency = True)
96+
bazel_dep(name = "zlib", version = "1.3.1", dev_dependency = True)

src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class KotlinBuilder
9191
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
9292
INSTRUMENT_COVERAGE("--instrument_coverage"),
9393
KSP_GENERATED_JAVA_SRCJAR("--ksp_generated_java_srcjar"),
94+
KSP_GENERATED_CLASSES_JAR("--ksp_generated_classes_jar"),
9495
}
9596
}
9697

@@ -250,6 +251,9 @@ class KotlinBuilder
250251
argMap.optionalSingle(KotlinBuilderFlags.KSP_GENERATED_JAVA_SRCJAR)?.let {
251252
generatedKspSrcJar = it
252253
}
254+
argMap.optionalSingle(KotlinBuilderFlags.KSP_GENERATED_CLASSES_JAR)?.let {
255+
generatedKspClassesJar = it
256+
}
253257
}
254258

255259
with(root.directoriesBuilder) {

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,21 @@ internal fun JvmCompilationTask.expandWithSourceJarSources(): JvmCompilationTask
477477
)
478478
}
479479

480+
/**
481+
* Produce a jar of classes generated by KSP.
482+
*/
483+
internal fun JvmCompilationTask.createdGeneratedKspClassesJar() {
484+
JarCreator(
485+
path = Paths.get(outputs.generatedKspClassesJar),
486+
normalize = true,
487+
verbose = false,
488+
).also {
489+
it.addDirectory(Paths.get(directories.generatedClasses))
490+
it.setJarOwner(info.label, info.bazelRuleKind)
491+
it.execute()
492+
}
493+
}
494+
480495
private val Directories.stubs
481496
get() =
482497
Files

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class KotlinJvmTaskExecutor
139139
if (outputs.generatedKspSrcJar.isNotEmpty()) {
140140
context.execute("creating KSP generated src jar", ::createGeneratedKspKotlinSrcJar)
141141
}
142+
if (outputs.generatedKspClassesJar.isNotEmpty()) {
143+
context.execute("creating KSP generated classes jar", ::createdGeneratedKspClassesJar)
144+
}
142145
}
143146
}
144147
}

src/main/protobuf/kotlin_model.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ message JvmCompilationTask {
123123
string generated_class_jar = 7;
124124
// Source jar containing the generated KSP sources.
125125
string generated_ksp_src_jar = 8;
126+
// The path to the jar containing the generated KSP classes
127+
string generated_ksp_classes_jar = 9;
126128
}
127129

128130
message Inputs {

src/test/data/jvm/ksp/BUILD

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ kt_ksp_plugin(
4848
],
4949
)
5050

51+
kt_ksp_plugin(
52+
name = "generate_bytecode_plugin",
53+
processor_class = "com.example.BytecodeGeneratorProcessor",
54+
deps = [
55+
"//src/test/data/jvm/ksp/bytecodegenerator/processor/src/main/com/example:processor",
56+
],
57+
)
58+
5159
kt_jvm_library(
5260
name = "ksp_kotlin_resources",
5361
srcs = ["CoffeeAppModel.kt"],
@@ -140,9 +148,40 @@ kt_jvm_library(
140148
],
141149
)
142150

151+
kt_jvm_library(
152+
name = "ksp_generate_bytecode",
153+
srcs = ["BytecodeExample.kt"],
154+
plugins = [":generate_bytecode_plugin"],
155+
deps = [
156+
"//src/test/data/jvm/ksp/bytecodegenerator/annotation",
157+
],
158+
)
159+
160+
kt_jvm_library(
161+
name = "ksp_bytecode_plugin_generates_no_classes_with_other_plugins",
162+
# these files don't have relevant annotations, and don't generate the bytecode/generated classes
163+
# from generate_bytecode_plugin
164+
srcs = [
165+
"CoffeeApp.kt",
166+
"CoffeeBean.java",
167+
"CoffeeMaker.kt",
168+
"DripCoffeeModule.kt",
169+
],
170+
plugins = [
171+
":generate_bytecode_plugin",
172+
":dagger",
173+
],
174+
deps = [
175+
"@kotlin_rules_maven//:com_google_dagger_dagger",
176+
"@kotlin_rules_maven//:com_google_dagger_dagger_compiler",
177+
],
178+
)
179+
143180
filegroup(
144181
name = "ksp",
145182
srcs = [
183+
":ksp_bytecode_plugin_generates_no_classes_with_other_plugins.jar",
184+
":ksp_generate_bytecode.jar",
146185
":ksp_kotlin_resources.jar",
147186
":ksp_kotlin_resources_missing_plugin.jar",
148187
":ksp_kotlin_resources_multiple_plugins.jar",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package src.test.data.jvm.ksp
2+
3+
import src.test.data.jvm.ksp.bytecodegenerator.annotation.GenerateBytecode
4+
5+
@GenerateBytecode
6+
class BytecodeExample
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//kotlin:jvm.bzl", "kt_jvm_library")
2+
3+
# Copyright 2018 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
package(default_visibility = ["//visibility:private"])
17+
18+
kt_jvm_library(
19+
name = "annotation",
20+
srcs = ["GenerateBytecode.kt"],
21+
visibility = ["//src/test:__subpackages__"],
22+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package src.test.data.jvm.ksp.bytecodegenerator.annotation
2+
3+
@Target(AnnotationTarget.CLASS)
4+
@Retention(AnnotationRetention.SOURCE)
5+
annotation class GenerateBytecode

0 commit comments

Comments
 (0)