Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:OptIn(ExperimentalWasmDsl::class)

import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsages
import org.jetbrains.kotlin.gradle.targets.wasm.d8.D8EnvSpec
import org.jetbrains.kotlin.gradle.targets.wasm.d8.D8Plugin

Expand Down Expand Up @@ -32,9 +34,22 @@ idea {
module.generatedSourceDirs.add(projectDir.resolve("test-gen"))
}

val annotationsRuntimeClasspath: Configuration by configurations.creating { isTransitive = false }
val testArtifacts: Configuration by configurations.creating

val annotationsRuntimeClasspath by configurations.dependencyScope("annotationsRuntimeClasspath") {
isTransitive = false
}
val annotationsJvmRuntimeClasspath by configurations.resolvable("annotationsJvmRuntimeClasspath") {
extendsFrom(annotationsRuntimeClasspath)
}
val annotationsJsRuntimeClasspath by configurations.resolvable("annotationsJsRuntimeClasspath") {
extendsFrom(annotationsRuntimeClasspath)
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(KotlinUsages.KOTLIN_RUNTIME))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.js)
}
}

dependencies {
compileOnly(libs.kotlin.compiler)

Expand Down Expand Up @@ -67,12 +82,15 @@ buildConfig {
}

tasks.test {
dependsOn(annotationsRuntimeClasspath)
dependsOn(testArtifacts)
dependsOn(annotationsJvmRuntimeClasspath)
dependsOn(annotationsJsRuntimeClasspath)

useJUnitPlatform()
workingDir = rootDir

systemProperty("annotationsRuntime.classpath", annotationsRuntimeClasspath.asPath)
systemProperty("annotationsRuntime.jvm.classpath", annotationsJvmRuntimeClasspath.asPath)
systemProperty("annotationsRuntime.js.classpath", annotationsJsRuntimeClasspath.asPath)

// Properties required to run the internal test framework.
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-stdlib", "kotlin-stdlib")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,57 @@ package org.jetbrains.kotlin.compiler.plugin.template.services

import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.platform.isJs
import org.jetbrains.kotlin.platform.jvm.isJvm
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
import org.jetbrains.kotlin.test.services.RuntimeClasspathProvider
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.targetPlatform
import java.io.File

private val annotationsRuntimeClasspath =
System.getProperty("annotationsRuntime.classpath")?.split(File.pathSeparator)?.map(::File)
?: error("Unable to get a valid classpath from 'annotationsRuntime.classpath' property")

fun TestConfigurationBuilder.configureAnnotations() {
useConfigurators(::PluginAnnotationsProvider)
useCustomRuntimeClasspathProviders(::PluginAnnotationsClasspathProvider)
}

private class PluginAnnotationsProvider(testServices: TestServices) : EnvironmentConfigurator(testServices) {
override fun configureCompilerConfiguration(configuration: CompilerConfiguration, module: TestModule) {
configuration.addJvmClasspathRoots(annotationsRuntimeClasspath)
val platform = module.targetPlatform(testServices)
when {
platform.isJvm() -> {
configuration.addJvmClasspathRoots(annotationsJvmRuntimeClasspath)
}

platform.isJs() -> {
val libraries = configuration.getList(JSConfigurationKeys.LIBRARIES)
configuration.put(
JSConfigurationKeys.LIBRARIES,
libraries + annotationsJsRuntimeClasspath.map { it.absolutePath },
)
}
}
}
}

private class PluginAnnotationsClasspathProvider(testServices: TestServices) : RuntimeClasspathProvider(testServices) {
override fun runtimeClassPaths(module: TestModule) = annotationsRuntimeClasspath
}
override fun runtimeClassPaths(module: TestModule): List<File> {
val targetPlatform = module.targetPlatform(testServices)
return when {
targetPlatform.isJvm() -> annotationsJvmRuntimeClasspath
targetPlatform.isJs() -> annotationsJsRuntimeClasspath
else -> emptyList()
}
}
}

private val annotationsJvmRuntimeClasspath = classpathFiles("annotationsRuntime.jvm.classpath")
private val annotationsJsRuntimeClasspath = classpathFiles("annotationsRuntime.js.classpath")

private fun classpathFiles(property: String): List<File> {
val property = System.getProperty(property)
?: error("Unable to get a valid classpath from '$property' property")
return property.split(File.pathSeparator).map(::File)
}
2 changes: 2 additions & 0 deletions compiler-plugin/testData/box/simple.fir.ir.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FILE fqName:foo.bar fileName:/simple.kt
FUN name:box visibility:public modality:FINAL returnType:kotlin.String
annotations:
SomeAnnotation
BLOCK_BODY
VAR name:result type:kotlin.String [val]
CALL 'public final fun foo (): kotlin.String declared in foo.bar.MyClass' type=kotlin.String origin=null
Expand Down
2 changes: 1 addition & 1 deletion compiler-plugin/testData/box/simple.fir.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FILE: simple.kt
package foo.bar

public final fun box(): R|kotlin/String| {
@R|org/jetbrains/kotlin/compiler/plugin/template/SomeAnnotation|() public final fun box(): R|kotlin/String| {
lval result: R|kotlin/String| = R|foo/bar/MyClass|().R|foo/bar/MyClass.foo|()
^box when () {
==(R|<local>/result|, String(Hello world)) -> {
Expand Down
3 changes: 3 additions & 0 deletions compiler-plugin/testData/box/simple.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package foo.bar

import org.jetbrains.kotlin.compiler.plugin.template.SomeAnnotation

@SomeAnnotation
fun box(): String {
val result = MyClass().foo()
return if (result == "Hello world") { "OK" } else { "Fail: $result" }
Expand Down
Loading