Skip to content

Commit 7805b95

Browse files
authored
Add ksp compiler support (#160)
1 parent 2a26594 commit 7805b95

File tree

6 files changed

+139
-10
lines changed

6 files changed

+139
-10
lines changed

WORKSPACE

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ KOTLIN_VERSION = "1.9.25"
1212

1313
KOTLINC_RELEASE_SHA = "6ab72d6144e71cbbc380b770c2ad380972548c63ab6ed4c79f11c88f2967332e"
1414

15-
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version")
15+
KSP_VERSION = "1.8.10-1.0.9"
16+
17+
KSP_COMPILER_RELEASE_SHA = "2f60c27956e4033c4c94355624e3fe88f255df42d8b67af44c1f2cdcbd513a13"
18+
19+
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version", "ksp_version")
1620

1721
KOTLINC_RELEASE = kotlinc_version(
1822
release = KOTLIN_VERSION,
1923
sha256 = KOTLINC_RELEASE_SHA,
2024
)
2125

22-
kotlin_repositories(compiler_release = KOTLINC_RELEASE)
26+
KSP_COMPILER_RELEASE = ksp_version(
27+
release = KSP_VERSION,
28+
sha256 = KSP_COMPILER_RELEASE_SHA,
29+
)
30+
31+
kotlin_repositories(
32+
compiler_release = KOTLINC_RELEASE,
33+
ksp_compiler_release = KSP_COMPILER_RELEASE,
34+
)
2335

2436
register_toolchains("//:kotlin_toolchain")
2537

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ grazel {
129129
tag = "1.9.25"
130130
sha = "6ab72d6144e71cbbc380b770c2ad380972548c63ab6ed4c79f11c88f2967332e"
131131
}
132+
kspCompiler {
133+
tag = "1.8.10-1.0.9"
134+
sha = "2f60c27956e4033c4c94355624e3fe88f255df42d8b67af44c1f2cdcbd513a13"
135+
}
132136
toolchain {
133137
enabled = true
134138
apiVersion = "1.7"

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/rules/KotlinRules.kt

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,58 @@ fun StatementsBuilder.kotlinRepository(repositoryRule: BazelRepositoryRule) {
5050
private const val KOTLIN_VERSION = "KOTLIN_VERSION"
5151
private const val KOTLINC_RELEASE = "KOTLINC_RELEASE"
5252
private const val KOTLINC_RELEASE_SHA = "KOTLINC_RELEASE_SHA"
53+
private const val KSP_VERSION = "KSP_VERSION"
54+
private const val KSP_COMPILER_RELEASE = "KSP_COMPILER_RELEASE"
55+
private const val KSP_COMPILER_RELEASE_SHA = "KSP_COMPILER_RELEASE_SHA"
5356

5457
fun StatementsBuilder.kotlinCompiler(
5558
kotlinCompilerVersion: String,
56-
kotlinCompilerReleaseSha: String
59+
kotlinCompilerReleaseSha: String,
60+
kspCompilerVersion: String? = null,
61+
kspCompilerReleaseSha: String? = null
5762
) {
5863
KOTLIN_VERSION `=` kotlinCompilerVersion.quote
5964
KOTLINC_RELEASE_SHA `=` kotlinCompilerReleaseSha.quote
65+
66+
val hasKsp = kspCompilerVersion != null && kspCompilerReleaseSha != null
67+
if (hasKsp) {
68+
KSP_VERSION `=` kspCompilerVersion!!.quote
69+
KSP_COMPILER_RELEASE_SHA `=` kspCompilerReleaseSha!!.quote
70+
}
6071
newLine()
6172

62-
load(
63-
"@io_bazel_rules_kotlin//kotlin:repositories.bzl",
64-
"kotlin_repositories",
65-
"kotlinc_version"
66-
)
73+
if (hasKsp) {
74+
load(
75+
"@io_bazel_rules_kotlin//kotlin:repositories.bzl",
76+
"kotlin_repositories",
77+
"kotlinc_version",
78+
"ksp_version"
79+
)
80+
} else {
81+
load(
82+
"@io_bazel_rules_kotlin//kotlin:repositories.bzl",
83+
"kotlin_repositories",
84+
"kotlinc_version"
85+
)
86+
}
6787

6888
KOTLINC_RELEASE `=` """kotlinc_version(
6989
release = $KOTLIN_VERSION,
7090
sha256 = $KOTLINC_RELEASE_SHA
7191
)
7292
""".trimIndent()
7393

74-
add("""kotlin_repositories(compiler_release = $KOTLINC_RELEASE)""")
94+
if (hasKsp) {
95+
KSP_COMPILER_RELEASE `=` """ksp_version(
96+
release = $KSP_VERSION,
97+
sha256 = $KSP_COMPILER_RELEASE_SHA
98+
)
99+
""".trimIndent()
100+
101+
add("""kotlin_repositories(compiler_release = $KOTLINC_RELEASE, ksp_compiler_release = $KSP_COMPILER_RELEASE)""")
102+
} else {
103+
add("""kotlin_repositories(compiler_release = $KOTLINC_RELEASE)""")
104+
}
75105
}
76106

77107
/**

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/extension/KotlinExtension.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ data class KotlinCompiler(
7070
var sha: String = "46720991a716e90bfc0cf3f2c81b2bd735c14f4ea6a5064c488e04fd76e6b6c7"
7171
)
7272

73+
/**
74+
* Configuration for KSP (Kotlin Symbol Processing) compiler.
75+
* When configured, KSP compiler release will be added to kotlin_repositories in WORKSPACE.
76+
*
77+
* @param tag KSP version tag (e.g., "1.8.10-1.0.9")
78+
* @param sha SHA256 hash of the KSP compiler release
79+
*/
80+
data class KspCompiler(
81+
var tag: String? = null,
82+
var sha: String? = null
83+
)
84+
7385
/**
7486
* Configuration for Kotlin compiler and toolchains. Options configured will be used in root `BUILD.bazel`, `WORKSPACE`
7587
* respectively.
@@ -95,6 +107,7 @@ data class KotlinCompiler(
95107
*/
96108
data class KotlinExtension(
97109
val compiler: KotlinCompiler = KotlinCompiler(),
110+
val kspCompiler: KspCompiler = KspCompiler(),
98111
val kotlinCOptions: KotlinCOptions = KotlinCOptions(),
99112
val javaCOptions: JavaCOptions = JavaCOptions(),
100113
val toolchain: KotlinToolChain = KotlinToolChain(),
@@ -137,6 +150,15 @@ data class KotlinExtension(
137150
closure.call()
138151
}
139152

153+
fun kspCompiler(block: KspCompiler.() -> Unit) {
154+
block(kspCompiler)
155+
}
156+
157+
fun kspCompiler(closure: Closure<*>) {
158+
closure.delegate = kspCompiler
159+
closure.call()
160+
}
161+
140162
fun gitRepository(closure: Closure<*>) {
141163
repository = GitRepositoryRule(name = RULE_KOTLIN_NAME, remote = "")
142164
closure.delegate = repository

grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/internal/WorkspaceBuilder.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,18 @@ internal class WorkspaceBuilder(
196196
* Add Kotlin specific statements to WORKSPACE namely
197197
* * Kotlin repository
198198
* * Kotlin compiler
199+
* * KSP compiler (if configured)
199200
* * Registering toolchains
200201
*/
201202
private fun StatementsBuilder.kotlinRules() {
202203
val kotlin = grazelExtension.rules.kotlin
203204
kotlinRepository(repositoryRule = kotlin.repository)
204-
kotlinCompiler(kotlin.compiler.tag, kotlin.compiler.sha)
205+
kotlinCompiler(
206+
kotlinCompilerVersion = kotlin.compiler.tag,
207+
kotlinCompilerReleaseSha = kotlin.compiler.sha,
208+
kspCompilerVersion = kotlin.kspCompiler.tag,
209+
kspCompilerReleaseSha = kotlin.kspCompiler.sha
210+
)
205211
registerKotlinToolchain(toolchain = kotlin.toolchain)
206212
}
207213
}

grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/KotlinWorkspaceRulesTest.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,61 @@ class KotlinWorkspaceRulesTest {
137137
}
138138
}
139139

140+
@Test
141+
fun `assert KSP compiler not generated when not configured`() {
142+
val kotlinTag = "1.6.21"
143+
val kotlinSha = "somesha256"
144+
rootProject.configure<GrazelExtension> {
145+
rules.kotlin.compiler {
146+
tag = kotlinTag
147+
sha = kotlinSha
148+
}
149+
}
150+
generateWorkspace().truth {
151+
contains("""KOTLIN_VERSION = "$kotlinTag"""")
152+
contains("""KOTLINC_RELEASE_SHA = "$kotlinSha"""")
153+
contains("kotlin_repositories(compiler_release = KOTLINC_RELEASE)")
154+
doesNotContain("KSP_VERSION")
155+
doesNotContain("KSP_COMPILER_RELEASE")
156+
doesNotContain("ksp_version")
157+
doesNotContain("ksp_compiler_release")
158+
}
159+
}
160+
161+
@Test
162+
fun `assert KSP compiler generated when configured`() {
163+
val kotlinTag = "1.8.10"
164+
val kotlinSha = "kotlinsha256"
165+
val kspTag = "1.8.10-1.0.9"
166+
val kspSha = "kspsha256"
167+
rootProject.configure<GrazelExtension> {
168+
rules.kotlin {
169+
compiler {
170+
tag = kotlinTag
171+
sha = kotlinSha
172+
}
173+
kspCompiler {
174+
tag = kspTag
175+
sha = kspSha
176+
}
177+
}
178+
}
179+
generateWorkspace().truth {
180+
// Kotlin compiler vars
181+
contains("""KOTLIN_VERSION = "$kotlinTag"""")
182+
contains("""KOTLINC_RELEASE_SHA = "$kotlinSha"""")
183+
// KSP compiler vars
184+
contains("""KSP_VERSION = "$kspTag"""")
185+
contains("""KSP_COMPILER_RELEASE_SHA = "$kspSha"""")
186+
// Load statement should include ksp_version
187+
contains("ksp_version")
188+
// KSP_COMPILER_RELEASE assignment
189+
contains("KSP_COMPILER_RELEASE = ksp_version(")
190+
// kotlin_repositories should include ksp_compiler_release
191+
contains("ksp_compiler_release = KSP_COMPILER_RELEASE")
192+
}
193+
}
194+
140195
fun generateWorkspace() = workspaceFactory
141196
.create(
142197
projectsToMigrate = listOf(rootProject, subProject),

0 commit comments

Comments
 (0)