Fix filling Main-Class attribute in manifest file of output jar
Before 5b7cee6221 JVM CLI compiler
was calling `KotlinToJVMBytecodeCompiler.compileBunchOfSources`.
`compileBunchOfSources` detected possible main classes,
and filled the Main-Class attribute in output jar
if if there was only one candidate.
After the change JVM CLI began calling
`KotlinToJVMBytecodeCompiler.compileModules`, which was not searching for a main class.
This change adds searching for main classes to `compileModules`.
We search for a main class only when one module is compiled,
and an output is written a jar file (so the change only affects JVM CLI compilation).
#KT-32272 Fixed
This commit is contained in:
@@ -5,10 +5,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.cli
|
||||
|
||||
import junit.framework.Assert
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||
import java.io.File
|
||||
import java.util.jar.JarFile
|
||||
|
||||
private const val EMPTY_MAIN_FUN = "fun main() {}"
|
||||
|
||||
class CustomCliTest : TestCaseWithTmpdir() {
|
||||
fun testArgfileWithNonTrivialWhitespaces() {
|
||||
@@ -16,4 +20,33 @@ class CustomCliTest : TestCaseWithTmpdir() {
|
||||
val argfile = File(tmpdir, "argfile").apply { writeText(text, Charsets.UTF_8) }
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("@" + argfile.absolutePath))
|
||||
}
|
||||
|
||||
fun testMainClass() {
|
||||
val mainKt = tmpdir.resolve("main.kt").apply {
|
||||
writeText(EMPTY_MAIN_FUN)
|
||||
}
|
||||
compileAndCheckMainClass(listOf(mainKt), expectedMainClass = "MainKt")
|
||||
}
|
||||
|
||||
fun testMultipleMainClasses() {
|
||||
val main1Kt = tmpdir.resolve("main1.kt").apply {
|
||||
writeText(EMPTY_MAIN_FUN)
|
||||
}
|
||||
val main2Kt = tmpdir.resolve("main2.kt").apply {
|
||||
writeText(EMPTY_MAIN_FUN)
|
||||
}
|
||||
|
||||
compileAndCheckMainClass(listOf(main1Kt, main2Kt), expectedMainClass = null)
|
||||
}
|
||||
|
||||
private fun compileAndCheckMainClass(sourceFiles: List<File>, expectedMainClass: String?) {
|
||||
val jarFile = tmpdir.resolve("output.jar")
|
||||
val args = listOf("-include-runtime", "-d", jarFile.absolutePath) + sourceFiles.map { it.absolutePath }
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), args)
|
||||
|
||||
JarFile(jarFile).use {
|
||||
val mainClassAttr = it.manifest.mainAttributes.getValue("Main-Class")
|
||||
Assert.assertEquals(expectedMainClass, mainClassAttr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user