Add compiler argument -Xfriend-paths for kotlinc-jvm

#KT-21910 Fixed
This commit is contained in:
Alexander Udalov
2018-07-25 12:58:38 +02:00
parent 8edbb10420
commit 4bd66d5aa8
10 changed files with 99 additions and 36 deletions
@@ -16,22 +16,11 @@
package org.jetbrains.kotlin.cli
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.test.CompilerTestUtil
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
import org.junit.Assert
import java.io.File
/**
* This test checks that [org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments.friendPaths] works by invoking the compiler's
* "exec" method directly. Once there's a CLI argument for friend paths, it can be simplified to use that CLI argument instead.
*/
class FriendPathsTest : TestCaseWithTmpdir() {
private fun getTestDataDirectory(): File = File("compiler/testData/friendPaths/")
@@ -40,7 +29,13 @@ class FriendPathsTest : TestCaseWithTmpdir() {
val libDest = File(tmpdir, "lib.jar")
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
Assert.assertEquals(ExitCode.OK, invokeCompiler(libDest.path))
CompilerTestUtil.executeCompilerAssertSuccessful(
K2JVMCompiler(),
listOf(
"-d", tmpdir.path, "-cp", libDest.path, File(getTestDataDirectory(), "usage.kt").path,
"-Xfriend-paths=${libDest.path}"
)
)
}
fun testDirectory() {
@@ -48,28 +43,12 @@ class FriendPathsTest : TestCaseWithTmpdir() {
val libDest = File(tmpdir, "lib")
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
Assert.assertEquals(ExitCode.OK, invokeCompiler(libDest.path))
}
private fun invokeCompiler(libraryPath: String): ExitCode {
return K2JVMCompiler().exec(ThrowingMessageCollector(), Services.EMPTY, K2JVMCompilerArguments().apply {
destination = tmpdir.path
classpath = libraryPath
freeArgs = arrayListOf(File(getTestDataDirectory(), "usage.kt").path)
friendPaths = arrayOf(libraryPath)
})
}
private class ThrowingMessageCollector : MessageCollector {
override fun clear() {}
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
if (severity.isError) {
Assert.fail("${severity.presentableName}: $message at $location")
}
}
override fun hasErrors(): Boolean = false
CompilerTestUtil.executeCompilerAssertSuccessful(
K2JVMCompiler(),
listOf(
"-d", tmpdir.path, "-cp", libDest.path, File(getTestDataDirectory(), "usage.kt").path,
"-Xfriend-paths=${libDest.path}"
)
)
}
}
@@ -471,6 +471,15 @@ class CompileKotlinAgainstCustomBinariesTest : AbstractKotlinCompilerIntegration
)
}
fun testInternalFromForeignModule() {
compileKotlin("source.kt", tmpdir, listOf(compileLibrary("library")))
}
fun testInternalFromFriendModule() {
val library = compileLibrary("library")
compileKotlin("source.kt", tmpdir, listOf(library), additionalOptions = listOf("-Xfriend-paths=${library.path}"))
}
companion object {
// compiler before 1.1.4 version did not include suspension marks into bytecode.
private fun stripSuspensionMarksToImitateLegacyCompiler(bytes: ByteArray): Pair<ByteArray, Int> {