[Build] Add the kotlin- prefix to the build tools API modules names
KT-57396
This commit is contained in:
committed by
Space Team
parent
b6fdc2dbfc
commit
ec4fab57a7
@@ -0,0 +1,9 @@
|
||||
## Build Tools API
|
||||
|
||||
This module contains public interfaces for Kotlin build tools.
|
||||
Using APIs from this module should be the preferred way to work with Kotlin compiler when integrating Kotlin builds into different build systems.
|
||||
The Kotlin stdlib of at least Kotlin 1.4 is expected to be a dependency of a consumer of the API.
|
||||
|
||||
The default implementation of the API is located in the [kotlin-build-tools-impl](../kotlin-build-tools-impl) directory.
|
||||
Interfaces implementation are expected to be loaded using the [ServiceLoader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html).
|
||||
The purpose of such a segregation is to allow using this API with different Kotlin compiler versions.
|
||||
@@ -0,0 +1,14 @@
|
||||
public abstract interface class org/jetbrains/kotlin/buildtools/api/CompilationService {
|
||||
public static final field Companion Lorg/jetbrains/kotlin/buildtools/api/CompilationService$Companion;
|
||||
public abstract fun compile ()V
|
||||
public static fun loadImplementation (Ljava/lang/ClassLoader;)Lorg/jetbrains/kotlin/buildtools/api/CompilationService;
|
||||
}
|
||||
|
||||
public final class org/jetbrains/kotlin/buildtools/api/CompilationService$Companion {
|
||||
public final fun loadImplementation (Ljava/lang/ClassLoader;)Lorg/jetbrains/kotlin/buildtools/api/CompilationService;
|
||||
}
|
||||
|
||||
public final class org/jetbrains/kotlin/buildtools/api/SharedApiClassesClassLoader {
|
||||
public static final fun newInstance ()Ljava/lang/ClassLoader;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("jps-compatible")
|
||||
id("org.jetbrains.kotlinx.binary-compatibility-validator")
|
||||
}
|
||||
|
||||
configureKotlinCompileTasksGradleCompatibility()
|
||||
|
||||
dependencies {
|
||||
compileOnly(kotlinStdlib())
|
||||
}
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
publish()
|
||||
|
||||
standardPublicJars()
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.buildtools.api
|
||||
|
||||
/**
|
||||
* A facade for invoking compilation in Kotlin compiler. It allows to use compiler in different modes.
|
||||
* TODO: add a mention where to see the available modes after implementing them
|
||||
*/
|
||||
interface CompilationService {
|
||||
fun compile()
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun loadImplementation(classLoader: ClassLoader) = loadImplementation(CompilationService::class, classLoader)
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
@file:JvmName("SharedApiClassesClassLoader")
|
||||
|
||||
package org.jetbrains.kotlin.buildtools.api
|
||||
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Creates a [ClassLoader] which reuses the API classes from the ClassLoader which loaded the API.
|
||||
* This way an API implementation can be loaded with almost fully isolated classpath, sharing only the classes from `org.jetbrains.kotlin.buildtools.api`,
|
||||
* so a caller still able to pass API parameters in a compatible way.
|
||||
*/
|
||||
@Suppress("FunctionName")
|
||||
@JvmName("newInstance")
|
||||
fun SharedApiClassesClassLoader(): ClassLoader = SharedApiClassesClassLoaderImpl(
|
||||
SharedApiClassesClassLoaderImpl::class.java.classLoader,
|
||||
ClassLoader.getSystemClassLoader(),
|
||||
SharedApiClassesClassLoaderImpl::class.java.`package`.name,
|
||||
)
|
||||
|
||||
internal fun <T : Any> loadImplementation(cls: KClass<T>, classLoader: ClassLoader): T {
|
||||
val implementations = ServiceLoader.load(cls.java, classLoader)
|
||||
implementations.firstOrNull() ?: error("The classpath contains no implementation for ${cls.qualifiedName}")
|
||||
return implementations.singleOrNull()
|
||||
?: error("The classpath contains more than one implementation for ${cls.qualifiedName}")
|
||||
}
|
||||
|
||||
private class SharedApiClassesClassLoaderImpl(
|
||||
private val parent: ClassLoader,
|
||||
fallback: ClassLoader,
|
||||
private val allowedPackage: String,
|
||||
) : ClassLoader(fallback) {
|
||||
override fun loadClass(name: String, resolve: Boolean): Class<*> {
|
||||
return if (name.startsWith(allowedPackage)) {
|
||||
parent.loadClass(name)
|
||||
} else {
|
||||
super.loadClass(name, resolve)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user