Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support K2 #305

Merged
merged 2 commits into from
Aug 12, 2022
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.kotlin_version = '1.7.0'
ext.embedded_kotlin_version = '1.7.10-331'
ext.kotlin_version = '1.7.10'
ext.embedded_kotlin_version = '1.7.10'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c

var languageVersion: String? = null

/** Use the new experimental K2 compiler */
var useK2: Boolean by default { false }

/** Additional string arguments to the Kotlin compiler */
var kotlincArguments: List<String> = emptyList()

Expand Down Expand Up @@ -126,6 +129,7 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
args.allWarningsAsErrors = allWarningsAsErrors
args.reportOutputFiles = reportOutputFiles
args.reportPerf = reportPerformance
args.useK2 = useK2

if (languageVersion != null)
args.languageVersion = this.languageVersion
Expand Down Expand Up @@ -191,7 +195,16 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
} else {
emptyList()
}
args.pluginClasspaths = (args.pluginClasspaths ?: emptyArray()) + arrayOf(getResourcesPath())
args.pluginClasspaths = (args.pluginClasspaths ?: emptyArray()) +
/** The resources path contains the MainComponentRegistrar and MainCommandLineProcessor which will
be found by the Kotlin compiler's service loader. We add it only when the user has actually given
us ComponentRegistrar instances to be loaded by the MainComponentRegistrar because the experimental
K2 compiler doesn't support plugins yet. This way, users of K2 can prevent MainComponentRegistrar
from being loaded and crashing K2 by setting both [compilerPlugins] and [commandLineProcessors] to
the emptyList. */
if (compilerPlugins.isNotEmpty() || commandLineProcessors.isNotEmpty())
arrayOf(getResourcesPath())
else emptyArray()
}

val compilerMessageCollector = PrintingMessageCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class MainComponentRegistrar : ComponentRegistrar {
companion object {
/** This compiler plugin is instantiated by K2JVMCompiler using
* a service locator. So we can't just pass parameters to it easily.
* Instead we need to use a thread-local global variable to pass
* Instead, we need to use a thread-local global variable to pass
* any parameters that change between compilations
*/
val threadLocalParameters: ThreadLocal<ThreadLocalParameters> = ThreadLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,5 +904,18 @@ class KotlinCompilationTests {
}
}

@Test
fun `runs the K2 compiler without compiler plugins`() {
val result = defaultCompilerConfig().apply {
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
compilerPlugins = emptyList()
pluginClasspaths = emptyList()
useK2 = true
}.compile()

assertThat(result.exitCode).isEqualTo(ExitCode.OK)
assertClassLoadable(result, "KSource")
}

class InheritedClass {}
}