From 22ee170f444f3312f7168ebe633f854a0fc7ff5e Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 23 Feb 2023 11:36:38 +0000 Subject: [PATCH] [K2/N] Prohibit single-stage compilation with language version 2.0 Kotlin/Native compiler can compile source files directly to a native binary (e.g. an executable or a framework). This is the so-called "single-stage" compilation mode. It is used only in the command-line compiler, while Kotlin Gradle plugin always produces an intermediate klib first, and then compiles it to a native binary ("two-stage" compilation mode). When running Kotlin/Native compiler with '-language-version 2.0' in the single-stage compilation mode, it in fact doesn't use the new K2 frontend. This is misleading and error-prone. To address this problem, the commit prohibits the single-stage compilation with '-language-version 2.0'. This affects only the command-line compiler, and shouldn't affect compilation with Gradle. ^KT-56855 --- .../backend/konan/driver/phases/Frontend.kt | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Frontend.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Frontend.kt index e29acdfd96c..2cf8e667608 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Frontend.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/Frontend.kt @@ -6,9 +6,7 @@ package org.jetbrains.kotlin.backend.konan.driver.phases import org.jetbrains.kotlin.analyzer.AnalysisResult -import org.jetbrains.kotlin.backend.konan.FrontendServices -import org.jetbrains.kotlin.backend.konan.KonanCompilationException -import org.jetbrains.kotlin.backend.konan.KonanConfig +import org.jetbrains.kotlin.backend.konan.* import org.jetbrains.kotlin.backend.konan.TopDownAnalyzerFacadeForKonan import org.jetbrains.kotlin.backend.konan.driver.BasicPhaseContext import org.jetbrains.kotlin.backend.konan.driver.PhaseContext @@ -16,9 +14,11 @@ import org.jetbrains.kotlin.backend.konan.driver.PhaseEngine import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.languageVersionSettings import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly sealed class FrontendPhaseOutput { object ShouldNotGenerateCode : FrontendPhaseOutput() @@ -54,9 +54,30 @@ internal val FrontendPhase = createSimpleNamedCompilerPhase( input.configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME) ) + val sourceFiles = input.getSourceFiles() + + if (sourceFiles.isNotEmpty()) { + if (input.configuration.getBoolean(CommonConfigurationKeys.USE_FIR)) { + context.reportCompilationError( + "language version 2.0 doesn't support compiling sources " + + "(${sourceFiles.first().virtualFilePath} in particular) " + + "directly to native binaries " + + "(e.g. a ${context.config.produce.name.toLowerCaseAsciiOnly()}).\n" + + "If you are using the command-line compiler (e.g. konanc or kotlinc-native), then " + + "compile the sources to a klib first with '-p library' compiler flag, " + + "and then use '-Xinclude=' flag to compile this to a binary.\n" + + "See more details at https://youtrack.jetbrains.com/issue/KT-56855\n" + + "If you are seeing this error message when compiling with Gradle, " + + "please report this here: https://kotl.in/issue" + ) + } + } else { + // TODO: we shouldn't be here in this case. + } + // Build AST and binding info. - analyzerWithCompilerReport.analyzeAndReport(input.getSourceFiles()) { - TopDownAnalyzerFacadeForKonan.analyzeFiles(input.getSourceFiles(), context) + analyzerWithCompilerReport.analyzeAndReport(sourceFiles) { + TopDownAnalyzerFacadeForKonan.analyzeFiles(sourceFiles, context) } if (analyzerWithCompilerReport.hasErrors()) { throw KonanCompilationException()