Build: Add proper kotlin-compiler-runner-unshaded for ide artifacts

Fixes tests in kt-212-master branch of kotlin plugin in
intellij-community repository
This commit is contained in:
Vyacheslav Gerasimov
2021-09-27 20:17:44 +03:00
parent 825ecc3a6f
commit 29787218e1
12 changed files with 36 additions and 13 deletions
+6 -10
View File
@@ -6,22 +6,18 @@ plugins {
}
dependencies {
embedded(project(":kotlin-compiler-runner-unshaded")) { isTransitive = false }
api(project(":kotlin-build-common"))
compileOnly(project(":compiler:cli-common"))
compileOnly(project(":kotlin-preloader"))
compileOnly(project(":compiler:frontend.java"))
compileOnly(project(":daemon-common"))
compileOnly(project(":daemon-common-new"))
api(project(":kotlin-daemon-client"))
compileOnly(project(":compiler:util"))
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
runtimeOnly(project(":kotlin-compiler-embeddable"))
api(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
runtimeOnly(project(":kotlin-compiler-embeddable"))
}
sourceSets {
"main" { projectDefault() }
"test" {}
"main" { }
"test" { }
}
publish()
@@ -1,28 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.compilerRunner
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.utils.KotlinPaths
open class CompilerEnvironment(
val services: Services,
val messageCollector: MessageCollector,
open val outputItemsCollector: OutputItemsCollector
)
@@ -1,176 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.compilerRunner
import com.intellij.openapi.util.io.FileUtil
import com.intellij.util.containers.Stack
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil.reportException
import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil
import org.xml.sax.Attributes
import org.xml.sax.InputSource
import org.xml.sax.SAXException
import org.xml.sax.helpers.DefaultHandler
import java.io.IOException
import java.io.Reader
import java.util.*
import javax.xml.parsers.SAXParserFactory
object CompilerOutputParser {
fun parseCompilerMessagesFromReader(messageCollector: MessageCollector, reader: Reader, collector: OutputItemsCollector) {
// Sometimes the compiler doesn't output valid XML.
// Example: error in command line arguments passed to the compiler.
// The compiler will print the usage and the SAX parser will break.
// In this case, we want to read everything from this stream and report it as an IDE error.
val stringBuilder = StringBuilder()
val wrappingReader = object : Reader() {
@Throws(IOException::class)
override fun read(cbuf: CharArray, off: Int, len: Int): Int {
val read = reader.read(cbuf, off, len)
stringBuilder.append(cbuf, off, len)
return read
}
@Throws(IOException::class)
override fun close() {
// Do nothing:
// If the SAX parser sees a syntax error, it throws an exception
// and calls close() on the reader.
// We prevent the reader from being closed here, and close it later,
// when all the text is read from it
}
}
try {
val factory = SAXParserFactory.newInstance()
val parser = factory.newSAXParser()
parser.parse(InputSource(wrappingReader), CompilerOutputSAXHandler(messageCollector, collector))
}
catch (e: Throwable) {
// Load all the text into the stringBuilder
try {
// This will not close the reader (see the wrapper above)
FileUtil.loadTextAndClose(wrappingReader)
}
catch (ioException: IOException) {
reportException(messageCollector, ioException)
}
val message = stringBuilder.toString()
reportException(messageCollector, IllegalStateException(message, e))
messageCollector.report(ERROR, message)
}
finally {
try {
reader.close()
}
catch (e: IOException) {
reportException(messageCollector, e)
}
}
}
private class CompilerOutputSAXHandler(private val messageCollector: MessageCollector, private val collector: OutputItemsCollector) : DefaultHandler() {
private val message = StringBuilder()
private val tags = Stack<String>()
private var path: String? = null
private var line: Int = 0
private var column: Int = 0
@Throws(SAXException::class)
override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) {
tags.push(qName)
message.setLength(0)
path = attributes.getValue("path")
line = safeParseInt(attributes.getValue("line"), -1)
column = safeParseInt(attributes.getValue("column"), -1)
}
@Throws(SAXException::class)
override fun characters(ch: CharArray?, start: Int, length: Int) {
if (tags.size == 1) {
// We're directly inside the root tag: <MESSAGES>
val message = String(ch!!, start, length)
if (!message.trim { it <= ' ' }.isEmpty()) {
messageCollector.report(ERROR, "Unhandled compiler output: $message")
}
}
else {
message.append(ch, start, length)
}
}
@Throws(SAXException::class)
override fun endElement(uri: String?, localName: String, qName: String) {
if (tags.size == 1) {
// We're directly inside the root tag: <MESSAGES>
return
}
val qNameLowerCase = qName.lowercase()
var category: CompilerMessageSeverity? = CATEGORIES[qNameLowerCase]
if (category == null) {
messageCollector.report(ERROR, "Unknown compiler message tag: $qName")
category = INFO
}
val text = message.toString()
if (category == OUTPUT) {
reportToCollector(text)
}
else {
messageCollector.report(category, text, CompilerMessageLocation.create(path, line, column, null))
}
tags.pop()
}
private fun reportToCollector(text: String) {
val output = OutputMessageUtil.parseOutputMessage(text)
if (output != null) {
collector.add(output.sourceFiles, output.outputFile)
}
}
companion object {
private val CATEGORIES = mapOf(
"error" to ERROR,
"warning" to WARNING,
"logging" to LOGGING,
"output" to OUTPUT,
"exception" to EXCEPTION,
"info" to INFO,
"messages" to INFO)
private fun safeParseInt(value: String?, defaultValue: Int): Int {
if (value == null) {
return defaultValue
}
try {
return Integer.parseInt(value.trim { it <= ' ' })
}
catch (e: NumberFormatException) {
return defaultValue
}
}
}
}
}
@@ -1,12 +0,0 @@
/*
* Copyright 2010-2018 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.compilerRunner
object KotlinCompilerClass {
const val JVM = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
const val JS = "org.jetbrains.kotlin.cli.js.K2JSCompiler"
const val METADATA = "org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler"
}
@@ -1,121 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.compilerRunner
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.client.DaemonReportMessage
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit
object KotlinCompilerRunnerUtils {
fun exitCodeFromProcessExitCode(log: KotlinLogger, code: Int): ExitCode {
val exitCode = ExitCode.values().find { it.code == code }
if (exitCode != null) return exitCode
log.debug("Could not find exit code by value: $code")
return if (code == 0) ExitCode.OK else ExitCode.COMPILATION_ERROR
}
@Synchronized
@JvmStatic
fun newDaemonConnection(
compilerId: CompilerId,
clientAliveFlagFile: File,
sessionAliveFlagFile: File,
messageCollector: MessageCollector,
isDebugEnabled: Boolean,
daemonOptions: DaemonOptions = configureDaemonOptions(),
additionalJvmParams: Array<String> = arrayOf()
): CompileServiceSession? = newDaemonConnection(
compilerId,
clientAliveFlagFile,
sessionAliveFlagFile,
messageCollector,
isDebugEnabled,
daemonOptions,
configureDaemonJVMOptions(
*additionalJvmParams,
inheritMemoryLimits = true,
inheritOtherJvmOptions = false,
inheritAdditionalProperties = true
)
)
@Synchronized
@JvmStatic
fun newDaemonConnection(
compilerId: CompilerId,
clientAliveFlagFile: File,
sessionAliveFlagFile: File,
messageCollector: MessageCollector,
isDebugEnabled: Boolean,
daemonOptions: DaemonOptions = configureDaemonOptions(),
daemonJVMOptions: DaemonJVMOptions
): CompileServiceSession? {
val daemonReportMessages = ArrayList<DaemonReportMessage>()
val daemonReportingTargets = DaemonReportingTargets(messages = daemonReportMessages)
val profiler = if (daemonOptions.reportPerf) WallAndThreadAndMemoryTotalProfiler(withGC = false) else DummyProfiler()
val connection = profiler.withMeasure(null) {
KotlinCompilerClient.connectAndLease(
compilerId,
clientAliveFlagFile,
daemonJVMOptions,
daemonOptions,
daemonReportingTargets,
autostart = true,
leaseSession = true,
sessionAliveFlagFile = sessionAliveFlagFile
)
}
if (connection == null || isDebugEnabled) {
for (message in daemonReportMessages) {
val severity = when (message.category) {
DaemonReportCategory.DEBUG -> CompilerMessageSeverity.INFO
DaemonReportCategory.INFO -> CompilerMessageSeverity.INFO
DaemonReportCategory.EXCEPTION -> CompilerMessageSeverity.EXCEPTION
}
messageCollector.report(severity, message.message)
}
}
fun reportTotalAndThreadPerf(
message: String, daemonOptions: DaemonOptions, messageCollector: MessageCollector, profiler: Profiler
) {
if (daemonOptions.reportPerf) {
fun Long.ms() = TimeUnit.NANOSECONDS.toMillis(this)
val counters = profiler.getTotalCounters()
messageCollector.report(
CompilerMessageSeverity.INFO, "PERF: $message ${counters.time.ms()} ms, thread ${counters.threadTime.ms()}"
)
}
}
reportTotalAndThreadPerf("Daemon connect", daemonOptions, MessageCollector.NONE, profiler)
return connection
}
}
@@ -1,14 +0,0 @@
/*
* Copyright 2010-2018 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.compilerRunner
interface KotlinLogger {
fun error(msg: String)
fun warn(msg: String)
fun info(msg: String)
fun debug(msg: String)
val isDebugEnabled: Boolean
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2018 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.compilerRunner
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.StringReader
fun reportInternalCompilerError(messageCollector: MessageCollector) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Compiler terminated with internal error")
}
fun processCompilerOutput(
environment: CompilerEnvironment,
stream: ByteArrayOutputStream,
exitCode: ExitCode?
) {
processCompilerOutput(environment.messageCollector, environment.outputItemsCollector, stream, exitCode)
}
fun processCompilerOutput(
messageCollector: MessageCollector,
outputItemsCollector: OutputItemsCollector,
stream: ByteArrayOutputStream,
exitCode: ExitCode?
) {
val reader = BufferedReader(StringReader(stream.toString()))
CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, outputItemsCollector)
if (ExitCode.INTERNAL_ERROR == exitCode) {
reportInternalCompilerError(messageCollector)
}
}