├── LICENSE.md ├── README.md ├── build.gradle.kts ├── compiler-plugin ├── build.gradle.kts └── src │ ├── main │ ├── kotlin │ │ └── org │ │ │ └── example │ │ │ └── compiler │ │ │ └── plugin │ │ │ ├── SimplePluginComponentRegistrar.kt │ │ │ ├── SimplePluginRegistrar.kt │ │ │ ├── fir │ │ │ └── SimpleClassGenerator.kt │ │ │ └── ir │ │ │ ├── AbstractTransformerForGenerator.kt │ │ │ ├── SimpleIrBodyGenerator.kt │ │ │ └── SimpleIrGenerationExtension.kt │ └── resources │ │ └── META-INF │ │ └── services │ │ └── org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar │ └── test │ ├── java │ └── org │ │ └── example │ │ └── compiler │ │ └── plugin │ │ └── runners │ │ └── BoxTestGenerated.java │ ├── kotlin │ └── org │ │ └── example │ │ └── compiler │ │ └── plugin │ │ ├── GenerateTests.kt │ │ ├── runners │ │ ├── AbstractBoxTest.kt │ │ └── BaseTestRunner.kt │ │ └── services │ │ └── ExtensionRegistrarConfigurator.kt │ └── resources │ └── org │ └── example │ └── compiler │ └── plugin │ └── code-gen │ └── box │ ├── simple.fir.ir.txt │ ├── simple.fir.txt │ └── simple.kt ├── example └── Main.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle.kts /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Anastasiia Birillo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## A simple Kotlin compiler plugin example 2 | 3 | This Kotlin compiler plugin generates a top level class: 4 | 5 | ```kotlin 6 | public final class foo.bar.MyClass { 7 | fun foo(): String = "Hello world" 8 | } 9 | ``` 10 | 11 | This plugin uses `1.8.20-dev-1278` bootstrap Kotlin version 12 | and should be used also with the same version of the compiler. 13 | 14 | The plugin uses the [K2](https://kotlinlang.org/docs/whatsnew17.html#new-kotlin-k2-compiler-for-the-jvm-in-alpha) compiler. 15 | 16 | Also, the plugin use the Kotlin compiler [test infrastructure](https://github.com/JetBrains/kotlin/tree/master/compiler/test-infrastructure). 17 | The `compiler-plugin` module has a special task `generateTests` that generates simple box tests. 18 | Please, don't change the [generated tests](./compiler-plugin/src/test/java/org/example/compiler/plugin/runners) manually. 19 | 20 | The [test resources' folder](./compiler-plugin/src/test/resources) contains also a dumped FIR and IR representations. 21 | 22 | ### How to run 23 | 24 | The repository has the [example](./example) folder, you can compile the [Main.kt](./example/Main.kt) manually with the compiler plugin: 25 | 1) Build a `jar` file with the plugin (you can find it in `compiler-plugin/build/libs` folder) 26 | 2) Compile the [Main.kt](./example/Main.kt) file with the Kotlin compiler at least `1.8.20-dev-1278` version: 27 | ```shell 28 | kotlinc -verbose -Xuse-k2 -Xplugin=path_to_plugin_jar path_to_Main_kt 29 | ``` 30 | 2* - you can download the [Kotlin repository](https://github.com/JetBrains/kotlin), 31 | run the `dist` task and get the necessary version of the Kotlin compiler. 32 | 3) run `java foo.bar.MainKt`, it will print `Hello world` 33 | 34 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | group = "org.example" 2 | version = "1.0-SNAPSHOT" 3 | 4 | fun properties(key: String) = project.findProperty(key).toString() 5 | 6 | plugins { 7 | val kotlinVersion by System.getProperties() 8 | `maven-publish` 9 | id("tanvd.kosogor") version "1.0.13" 10 | id("org.jetbrains.dokka") version "1.6.21" 11 | kotlin("jvm").version(kotlinVersion.toString()) 12 | } 13 | 14 | allprojects { 15 | apply { 16 | plugin("kotlin") 17 | 18 | tasks.withType> { 19 | kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn" 20 | } 21 | } 22 | 23 | tasks.withType().all { 24 | kotlinOptions { 25 | jvmTarget = "11" 26 | } 27 | } 28 | 29 | repositories { 30 | mavenCentral() 31 | maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") 32 | } 33 | } -------------------------------------------------------------------------------- /compiler-plugin/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import tanvd.kosogor.proxy.publishJar 2 | 3 | group = rootProject.group 4 | version = rootProject.version 5 | 6 | dependencies { 7 | val kotlinVersion by System.getProperties() 8 | val junitPlatformVersion = "1.9.0" 9 | 10 | kotlin("compiler") 11 | .let { 12 | compileOnly(it) 13 | testImplementation(it) 14 | } 15 | 16 | testRuntimeOnly("org.jetbrains.kotlin:kotlin-test:$kotlinVersion") 17 | testRuntimeOnly("org.jetbrains.kotlin:kotlin-script-runtime:$kotlinVersion") 18 | testRuntimeOnly("org.jetbrains.kotlin:kotlin-annotations-jvm:$kotlinVersion") 19 | 20 | testImplementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") 21 | testImplementation("org.jetbrains.kotlin:kotlin-compiler-internal-test-framework:$kotlinVersion") 22 | testImplementation("junit:junit:4.12") 23 | 24 | testImplementation(platform("org.junit:junit-bom:5.8.0")) 25 | testImplementation("org.junit.jupiter:junit-jupiter:5.9.0") 26 | testImplementation("org.junit.platform:junit-platform-commons:$junitPlatformVersion") 27 | testImplementation("org.junit.platform:junit-platform-launcher:$junitPlatformVersion") 28 | testImplementation("org.junit.platform:junit-platform-runner:$junitPlatformVersion") 29 | testImplementation("org.junit.platform:junit-platform-suite-api:$junitPlatformVersion") 30 | } 31 | 32 | tasks.withType { 33 | useJUnitPlatform() 34 | workingDir = project.rootDir 35 | 36 | doFirst { 37 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-stdlib", "kotlin-stdlib") 38 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-stdlib-jdk8", "kotlin-stdlib-jdk8") 39 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-reflect", "kotlin-reflect") 40 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-test", "kotlin-test") 41 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-script-runtime", "kotlin-script-runtime") 42 | setLibraryProperty("org.jetbrains.kotlin.test.kotlin-annotations-jvm", "kotlin-annotations-jvm") 43 | setLibraryProperty("org.jetbrains.kotlin.compiler", "kotlin-compiler") 44 | } 45 | } 46 | 47 | publishJar {} 48 | 49 | fun Test.setLibraryProperty(propName: String, jarName: String) { 50 | val path = 51 | project.configurations.testRuntimeClasspath 52 | .get() 53 | .files 54 | .find { """$jarName-\d.*jar""".toRegex().matches(it.name) } 55 | ?.absolutePath 56 | ?: return 57 | println("$propName: $path") 58 | systemProperty(propName, path) 59 | } 60 | 61 | tasks.create("generateTests") { 62 | classpath = sourceSets.test.get().runtimeClasspath 63 | mainClass.set("org.example.compiler.plugin.GenerateTestsKt") 64 | } 65 | 66 | sourceSets { 67 | main { 68 | java.setSrcDirs(listOf("src/main")) 69 | resources.setSrcDirs(listOf("src/main/resources")) 70 | } 71 | test { 72 | java.setSrcDirs(listOf("src/test/java", "src/test/kotlin")) 73 | resources.setSrcDirs(listOf("src/test/resources")) 74 | } 75 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/SimplePluginComponentRegistrar.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin 2 | 3 | import org.example.compiler.plugin.ir.SimpleIrGenerationExtension 4 | import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension 5 | import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar 6 | import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi 7 | import org.jetbrains.kotlin.config.CompilerConfiguration 8 | import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrarAdapter 9 | 10 | // The main plugin registrar 11 | @OptIn(ExperimentalCompilerApi::class) 12 | class SimplePluginComponentRegistrar: CompilerPluginRegistrar() { 13 | override val supportsK2: Boolean 14 | get() = true 15 | 16 | // Register all plugin's extensions 17 | override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) { 18 | FirExtensionRegistrarAdapter.registerExtension(SimplePluginRegistrar()) 19 | IrGenerationExtension.registerExtension(SimpleIrGenerationExtension()) 20 | } 21 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/SimplePluginRegistrar.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin 2 | 3 | import org.example.compiler.plugin.fir.SimpleClassGenerator 4 | import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar 5 | 6 | // Register all FIR extensions 7 | class SimplePluginRegistrar : FirExtensionRegistrar() { 8 | override fun ExtensionRegistrarContext.configurePlugin() { 9 | +::SimpleClassGenerator 10 | } 11 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/fir/SimpleClassGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.fir 2 | 3 | import org.jetbrains.kotlin.GeneratedDeclarationKey 4 | import org.jetbrains.kotlin.descriptors.ClassKind 5 | import org.jetbrains.kotlin.descriptors.EffectiveVisibility 6 | import org.jetbrains.kotlin.descriptors.Modality 7 | import org.jetbrains.kotlin.descriptors.Visibilities 8 | import org.jetbrains.kotlin.fir.FirSession 9 | import org.jetbrains.kotlin.fir.containingClassForStaticMemberAttr 10 | import org.jetbrains.kotlin.fir.declarations.FirResolvePhase 11 | import org.jetbrains.kotlin.fir.declarations.builder.buildPrimaryConstructor 12 | import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass 13 | import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction 14 | import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl 15 | import org.jetbrains.kotlin.fir.declarations.origin 16 | import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension 17 | import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext 18 | import org.jetbrains.kotlin.fir.moduleData 19 | import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider 20 | import org.jetbrains.kotlin.fir.symbols.impl.* 21 | import org.jetbrains.kotlin.fir.types.ConeTypeProjection 22 | import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef 23 | import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl 24 | import org.jetbrains.kotlin.name.* 25 | 26 | /* 27 | * Generates top level class 28 | * 29 | * public final class foo.bar.MyClass { 30 | * fun foo(): String = "Hello world" 31 | * } 32 | */ 33 | class SimpleClassGenerator(session: FirSession) : FirDeclarationGenerationExtension(session) { 34 | companion object { 35 | val MY_CLASS_ID = ClassId(FqName.fromSegments(listOf("foo", "bar")), Name.identifier("MyClass")) 36 | 37 | val FOO_ID = CallableId(MY_CLASS_ID, Name.identifier("foo")) 38 | } 39 | 40 | override fun generateClassLikeDeclaration(classId: ClassId): FirClassLikeSymbol<*>? { 41 | // Generate declaration only for the necessary ClassId (foo.bar.MyClass) 42 | if (classId != MY_CLASS_ID) return null 43 | val klass = buildRegularClass { 44 | moduleData = session.moduleData 45 | origin = Key.origin 46 | status = FirResolvedDeclarationStatusImpl( 47 | Visibilities.Public, 48 | Modality.FINAL, 49 | EffectiveVisibility.Public 50 | ) 51 | classKind = ClassKind.CLASS 52 | scopeProvider = session.kotlinScopeProvider 53 | name = classId.shortClassName 54 | symbol = FirRegularClassSymbol(classId) 55 | superTypeRefs.add(session.builtinTypes.anyType) 56 | } 57 | return klass.symbol 58 | } 59 | 60 | // We need to convert the generated class into ConeTypeRef to assign the return 61 | // type for constructors and for dispatch receiver types 62 | private fun ClassId.toConeType(typeArguments: Array = emptyArray()) = 63 | ConeClassLikeTypeImpl(ConeClassLikeLookupTagImpl(this), typeArguments, isNullable = false) 64 | 65 | override fun generateConstructors(context: MemberGenerationContext): List { 66 | val classId = context.owner.classId 67 | // We need to check if we are generating constructors only for the necessary ClassId (foo.bar.MyClass) 68 | require(classId == MY_CLASS_ID) 69 | val constructor = buildPrimaryConstructor { 70 | resolvePhase = FirResolvePhase.BODY_RESOLVE 71 | moduleData = session.moduleData 72 | origin = Key.origin 73 | returnTypeRef = buildResolvedTypeRef { 74 | type = classId.toConeType() 75 | } 76 | status = FirResolvedDeclarationStatusImpl( 77 | Visibilities.Public, 78 | Modality.FINAL, 79 | EffectiveVisibility.Public 80 | ) 81 | symbol = FirConstructorSymbol(classId) 82 | }.also { 83 | it.containingClassForStaticMemberAttr = ConeClassLikeLookupTagImpl(classId) 84 | } 85 | return listOf(constructor.symbol) 86 | } 87 | 88 | // In this function we will generate only one function: fun foo(): String = "Hello world", 89 | // but other functions can be generated too 90 | override fun generateFunctions( 91 | callableId: CallableId, 92 | context: MemberGenerationContext? 93 | ): List { 94 | val function = buildSimpleFunction { 95 | resolvePhase = FirResolvePhase.BODY_RESOLVE 96 | moduleData = session.moduleData 97 | origin = Key.origin 98 | status = FirResolvedDeclarationStatusImpl( 99 | Visibilities.Public, 100 | Modality.FINAL, 101 | EffectiveVisibility.Public 102 | ) 103 | returnTypeRef = session.builtinTypes.stringType 104 | name = callableId.callableName 105 | symbol = FirNamedFunctionSymbol(callableId) 106 | // it's better to use default type on corresponding firClass to handle type parameters 107 | // but in this case we know that MyClass don't have any generics 108 | dispatchReceiverType = callableId.classId?.toConeType() 109 | } 110 | return listOf(function.symbol) 111 | } 112 | 113 | override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>) = if (classSymbol.classId == MY_CLASS_ID) { 114 | setOf(FOO_ID.callableName, SpecialNames.INIT) 115 | } else { 116 | emptySet() 117 | } 118 | 119 | override fun getTopLevelClassIds() = setOf(MY_CLASS_ID) 120 | 121 | override fun hasPackage(packageFqName: FqName) = packageFqName == MY_CLASS_ID.packageFqName 122 | 123 | // Don't forget to create this key 124 | object Key : GeneratedDeclarationKey() 125 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/ir/AbstractTransformerForGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.ir 2 | 3 | import org.jetbrains.kotlin.GeneratedDeclarationKey 4 | import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext 5 | import org.jetbrains.kotlin.ir.IrElement 6 | import org.jetbrains.kotlin.ir.declarations.* 7 | import org.jetbrains.kotlin.ir.expressions.IrBody 8 | import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl 9 | import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl 10 | import org.jetbrains.kotlin.ir.types.IrSimpleType 11 | import org.jetbrains.kotlin.ir.util.primaryConstructor 12 | import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid 13 | import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid 14 | 15 | abstract class AbstractTransformerForGenerator(context: IrPluginContext) : IrElementVisitorVoid { 16 | protected val irFactory = context.irFactory 17 | protected val irBuiltIns = context.irBuiltIns 18 | 19 | abstract fun interestedIn(key: GeneratedDeclarationKey): Boolean 20 | abstract fun generateBodyForFunction(function: IrSimpleFunction, key: GeneratedDeclarationKey): IrBody? 21 | abstract fun generateBodyForConstructor(constructor: IrConstructor, key: GeneratedDeclarationKey): IrBody? 22 | 23 | final override fun visitElement(element: IrElement) { 24 | when (element) { 25 | is IrDeclaration, 26 | is IrFile, 27 | is IrModuleFragment -> element.acceptChildrenVoid(this) 28 | else -> {} 29 | } 30 | } 31 | 32 | final override fun visitSimpleFunction(declaration: IrSimpleFunction) { 33 | visitSimpleFunctionOrConstructor(declaration) { d, k -> generateBodyForFunction(d as IrSimpleFunction, k) } 34 | } 35 | 36 | final override fun visitConstructor(declaration: IrConstructor) { 37 | visitSimpleFunctionOrConstructor(declaration) { d, k -> generateBodyForConstructor(d as IrConstructor, k) } 38 | } 39 | 40 | // ------------------------ utilities ------------------------ 41 | 42 | protected fun generateBodyForDefaultConstructor(declaration: IrConstructor): IrBody? { 43 | val type = declaration.returnType as? IrSimpleType ?: return null 44 | 45 | val delegatingAnyCall = IrDelegatingConstructorCallImpl( 46 | -1, 47 | -1, 48 | irBuiltIns.anyType, 49 | irBuiltIns.anyClass.owner.primaryConstructor?.symbol ?: return null, 50 | typeArgumentsCount = 0, 51 | valueArgumentsCount = 0 52 | ) 53 | 54 | val initializerCall = IrInstanceInitializerCallImpl( 55 | -1, 56 | -1, 57 | (declaration.parent as? IrClass)?.symbol ?: return null, 58 | type 59 | ) 60 | 61 | return irFactory.createBlockBody(-1, -1, listOf(delegatingAnyCall, initializerCall)) 62 | } 63 | 64 | private fun visitSimpleFunctionOrConstructor( 65 | declaration: IrFunction, 66 | generator: (IrFunction, GeneratedDeclarationKey) -> IrBody? 67 | ) { 68 | val origin = declaration.origin 69 | if (origin !is IrDeclarationOrigin.GeneratedByPlugin || !interestedIn(origin.pluginKey)) return 70 | require(declaration.body == null) 71 | declaration.body = generator(declaration, origin.pluginKey) 72 | } 73 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/ir/SimpleIrBodyGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.ir 2 | 3 | import org.example.compiler.plugin.fir.SimpleClassGenerator 4 | import org.jetbrains.kotlin.GeneratedDeclarationKey 5 | import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext 6 | import org.jetbrains.kotlin.ir.declarations.IrConstructor 7 | import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction 8 | import org.jetbrains.kotlin.ir.expressions.IrBody 9 | import org.jetbrains.kotlin.ir.expressions.IrConstKind 10 | import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl 11 | import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl 12 | 13 | class SimpleIrBodyGenerator(pluginContext: IrPluginContext) : AbstractTransformerForGenerator(pluginContext) { 14 | // We should use the key from SimpleClassGenerator to identify the classes and functions for generation 15 | override fun interestedIn(key: GeneratedDeclarationKey) = key == SimpleClassGenerator.Key 16 | 17 | override fun generateBodyForFunction(function: IrSimpleFunction, key: GeneratedDeclarationKey): IrBody { 18 | // We need to generate body only for this function: fun foo(): String = "Hello world" 19 | require(function.name == SimpleClassGenerator.FOO_ID.callableName) 20 | // Generate string constant 21 | val const = IrConstImpl(-1, -1, irBuiltIns.stringType, IrConstKind.String, value = "Hello world") 22 | // We need to return the string const from the function 23 | val returnStatement = IrReturnImpl(-1, -1, irBuiltIns.nothingType, function.symbol, const) 24 | return irFactory.createBlockBody(-1, -1, listOf(returnStatement)) 25 | } 26 | 27 | override fun generateBodyForConstructor(constructor: IrConstructor, key: GeneratedDeclarationKey): IrBody? { 28 | // We have only a default constructor in the generated class 29 | return generateBodyForDefaultConstructor(constructor) 30 | } 31 | } -------------------------------------------------------------------------------- /compiler-plugin/src/main/kotlin/org/example/compiler/plugin/ir/SimpleIrGenerationExtension.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.ir 2 | 3 | import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension 4 | import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext 5 | import org.jetbrains.kotlin.ir.declarations.IrModuleFragment 6 | import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid 7 | 8 | // The entry point for IR extensions 9 | class SimpleIrGenerationExtension: IrGenerationExtension { 10 | override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { 11 | listOf(SimpleIrBodyGenerator(pluginContext)).forEach { moduleFragment.acceptChildrenVoid(it) } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /compiler-plugin/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar: -------------------------------------------------------------------------------- 1 | org.example.compiler.plugin.SimplePluginComponentRegistrar -------------------------------------------------------------------------------- /compiler-plugin/src/test/java/org/example/compiler/plugin/runners/BoxTestGenerated.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package org.example.compiler.plugin.runners; 4 | 5 | import com.intellij.testFramework.TestDataPath; 6 | import org.jetbrains.kotlin.test.util.KtTestUtil; 7 | import org.jetbrains.kotlin.test.TargetBackend; 8 | import org.jetbrains.kotlin.test.TestMetadata; 9 | import org.junit.jupiter.api.Nested; 10 | import org.junit.jupiter.api.Test; 11 | 12 | import java.io.File; 13 | import java.util.regex.Pattern; 14 | 15 | /** This class is generated by {@link org.example.compiler.plugin.GenerateTestsKt}. DO NOT MODIFY MANUALLY */ 16 | @SuppressWarnings("all") 17 | @TestMetadata("compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box") 18 | @TestDataPath("$PROJECT_ROOT") 19 | public class BoxTestGenerated extends AbstractBoxTest { 20 | @Test 21 | public void testAllFilesPresentInBox() throws Exception { 22 | KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); 23 | } 24 | 25 | @Test 26 | @TestMetadata("simple.kt") 27 | public void testSimple() throws Exception { 28 | runTest("compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box/simple.kt"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /compiler-plugin/src/test/kotlin/org/example/compiler/plugin/GenerateTests.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin 2 | 3 | import org.example.compiler.plugin.runners.AbstractBoxTest 4 | import org.jetbrains.kotlin.generators.generateTestGroupSuiteWithJUnit5 5 | 6 | private const val TEST_DATA_ROOT = "compiler-plugin/src/test/resources/org/example/compiler/plugin" 7 | 8 | // The main entry point for tests generation 9 | // Run this command to generate all compiler tests 10 | fun main() { 11 | generateTestGroupSuiteWithJUnit5 { 12 | testGroup( 13 | testDataRoot = "$TEST_DATA_ROOT/code-gen", 14 | testsRoot = "compiler-plugin/src/test/java" 15 | ) { 16 | testClass { 17 | model("box") 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /compiler-plugin/src/test/kotlin/org/example/compiler/plugin/runners/AbstractBoxTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.runners 2 | 3 | import org.jetbrains.kotlin.config.JvmTarget 4 | import org.jetbrains.kotlin.platform.jvm.JvmPlatforms 5 | import org.jetbrains.kotlin.test.TargetBackend 6 | import org.jetbrains.kotlin.test.backend.BlackBoxCodegenSuppressor 7 | import org.jetbrains.kotlin.test.backend.handlers.IrTextDumpHandler 8 | import org.jetbrains.kotlin.test.backend.handlers.IrTreeVerifierHandler 9 | import org.jetbrains.kotlin.test.backend.ir.JvmIrBackendFacade 10 | import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder 11 | import org.jetbrains.kotlin.test.builders.fir2IrStep 12 | import org.jetbrains.kotlin.test.builders.irHandlersStep 13 | import org.jetbrains.kotlin.test.directives.CodegenTestDirectives 14 | import org.jetbrains.kotlin.test.directives.ConfigurationDirectives 15 | import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives 16 | import org.jetbrains.kotlin.test.model.DependencyKind 17 | import org.jetbrains.kotlin.test.runners.RunnerWithTargetBackendForTestGeneratorMarker 18 | 19 | open class AbstractBoxTest : BaseTestRunner(), RunnerWithTargetBackendForTestGeneratorMarker { 20 | override val targetBackend: TargetBackend 21 | get() = TargetBackend.JVM_IR 22 | 23 | override fun TestConfigurationBuilder.configuration() { 24 | globalDefaults { 25 | targetBackend = TargetBackend.JVM_IR 26 | targetPlatform = JvmPlatforms.defaultJvmPlatform 27 | dependencyKind = DependencyKind.Binary 28 | } 29 | 30 | defaultDirectives { 31 | +CodegenTestDirectives.DUMP_IR 32 | +ConfigurationDirectives.WITH_STDLIB 33 | JvmEnvironmentConfigurationDirectives.JVM_TARGET with JvmTarget.JVM_11 34 | +CodegenTestDirectives.IGNORE_DEXING 35 | +JvmEnvironmentConfigurationDirectives.FULL_JDK 36 | } 37 | 38 | commonFirWithPluginFrontendConfiguration() 39 | fir2IrStep() 40 | irHandlersStep { 41 | useHandlers( 42 | ::IrTextDumpHandler, 43 | ::IrTreeVerifierHandler, 44 | ) 45 | } 46 | facadeStep(::JvmIrBackendFacade) 47 | 48 | useAfterAnalysisCheckers(::BlackBoxCodegenSuppressor) 49 | enableMetaInfoHandler() 50 | } 51 | } -------------------------------------------------------------------------------- /compiler-plugin/src/test/kotlin/org/example/compiler/plugin/runners/BaseTestRunner.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.runners 2 | 3 | import org.example.compiler.plugin.services.ExtensionRegistrarConfigurator 4 | import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder 5 | import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives 6 | import org.jetbrains.kotlin.test.initIdeaConfiguration 7 | import org.jetbrains.kotlin.test.runners.AbstractKotlinCompilerTest 8 | import org.jetbrains.kotlin.test.runners.baseFirDiagnosticTestConfiguration 9 | import org.jetbrains.kotlin.test.services.EnvironmentBasedStandardLibrariesPathProvider 10 | import org.jetbrains.kotlin.test.services.KotlinStandardLibrariesPathProvider 11 | import org.junit.jupiter.api.BeforeAll 12 | 13 | abstract class BaseTestRunner : AbstractKotlinCompilerTest() { 14 | companion object { 15 | @BeforeAll 16 | @JvmStatic 17 | fun setUp() { 18 | initIdeaConfiguration() 19 | } 20 | } 21 | 22 | override fun createKotlinStandardLibrariesPathProvider(): KotlinStandardLibrariesPathProvider { 23 | return EnvironmentBasedStandardLibrariesPathProvider 24 | } 25 | } 26 | 27 | fun TestConfigurationBuilder.commonFirWithPluginFrontendConfiguration() { 28 | baseFirDiagnosticTestConfiguration() 29 | 30 | defaultDirectives { 31 | +FirDiagnosticsDirectives.ENABLE_PLUGIN_PHASES 32 | +FirDiagnosticsDirectives.FIR_DUMP 33 | } 34 | 35 | useConfigurators( 36 | ::ExtensionRegistrarConfigurator 37 | ) 38 | } -------------------------------------------------------------------------------- /compiler-plugin/src/test/kotlin/org/example/compiler/plugin/services/ExtensionRegistrarConfigurator.kt: -------------------------------------------------------------------------------- 1 | package org.example.compiler.plugin.services 2 | 3 | import org.example.compiler.plugin.SimplePluginRegistrar 4 | import org.example.compiler.plugin.ir.SimpleIrGenerationExtension 5 | import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension 6 | import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar 7 | import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi 8 | import org.jetbrains.kotlin.config.CompilerConfiguration 9 | import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrarAdapter 10 | import org.jetbrains.kotlin.test.model.TestModule 11 | import org.jetbrains.kotlin.test.services.EnvironmentConfigurator 12 | import org.jetbrains.kotlin.test.services.TestServices 13 | 14 | class ExtensionRegistrarConfigurator(testServices: TestServices) : EnvironmentConfigurator(testServices) { 15 | // Imitate the behaviour of the plugin 16 | @OptIn(ExperimentalCompilerApi::class) 17 | override fun CompilerPluginRegistrar.ExtensionStorage.registerCompilerExtensions( 18 | module: TestModule, 19 | configuration: CompilerConfiguration 20 | ) { 21 | FirExtensionRegistrarAdapter.registerExtension(SimplePluginRegistrar()) 22 | IrGenerationExtension.registerExtension(SimpleIrGenerationExtension()) 23 | } 24 | } -------------------------------------------------------------------------------- /compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box/simple.fir.ir.txt: -------------------------------------------------------------------------------- 1 | FILE fqName:foo.bar fileName:/simple.kt 2 | FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String 3 | BLOCK_BODY 4 | VAR name:result type:kotlin.String [val] 5 | CALL 'public final fun foo (): kotlin.String declared in foo.bar.MyClass' type=kotlin.String origin=null 6 | $this: CONSTRUCTOR_CALL 'public constructor () [primary] declared in foo.bar.MyClass' type=foo.bar.MyClass origin=null 7 | RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in foo.bar' 8 | WHEN type=kotlin.String origin=IF 9 | BRANCH 10 | if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ 11 | arg0: GET_VAR 'val result: kotlin.String [val] declared in foo.bar.box' type=kotlin.String origin=null 12 | arg1: CONST String type=kotlin.String value="Hello world" 13 | then: CONST String type=kotlin.String value="OK" 14 | BRANCH 15 | if: CONST Boolean type=kotlin.Boolean value=true 16 | then: STRING_CONCATENATION type=kotlin.String 17 | CONST String type=kotlin.String value="Fail: " 18 | GET_VAR 'val result: kotlin.String [val] declared in foo.bar.box' type=kotlin.String origin=null 19 | -------------------------------------------------------------------------------- /compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box/simple.fir.txt: -------------------------------------------------------------------------------- 1 | FILE: simple.kt 2 | package foo.bar 3 | 4 | public final fun box(): R|kotlin/String| { 5 | lval result: R|kotlin/String| = R|foo/bar/MyClass|().R|foo/bar/MyClass.foo|() 6 | ^box when () { 7 | ==(R|/result|, String(Hello world)) -> { 8 | String(OK) 9 | } 10 | else -> { 11 | (String(Fail: ), R|/result|) 12 | } 13 | } 14 | 15 | } 16 | FILE: __GENERATED DECLARATIONS__.kt 17 | package foo.bar 18 | 19 | public final class MyClass : R|kotlin/Any| { 20 | public final fun foo(): R|kotlin/String| 21 | 22 | public constructor(): R|foo/bar/MyClass| 23 | 24 | } 25 | -------------------------------------------------------------------------------- /compiler-plugin/src/test/resources/org/example/compiler/plugin/code-gen/box/simple.kt: -------------------------------------------------------------------------------- 1 | package foo.bar 2 | 3 | fun box(): String { 4 | val result = MyClass().foo() 5 | return if (result == "Hello world") { "OK" } else { "Fail: $result" } 6 | } 7 | -------------------------------------------------------------------------------- /example/Main.kt: -------------------------------------------------------------------------------- 1 | package foo.bar 2 | 3 | fun main() { 4 | val result = MyClass().foo() 5 | println(result) 6 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | systemProp.kotlinVersion=1.8.20-dev-1278 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nbirillo/simple-kotlin-compiler-plugin/2a3550457a13f3bc9881906c10251259f35efa67/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | 2 | rootProject.name = "kotlin-compiler-plugin-example" 3 | 4 | pluginManagement { 5 | repositories { 6 | mavenCentral() 7 | gradlePluginPortal() 8 | maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") 9 | } 10 | } 11 | 12 | include( 13 | "compiler-plugin" 14 | ) 15 | 16 | --------------------------------------------------------------------------------