K2 scripting: separate scripts compilation into another session

when scripts are compiled along with other sources.
#KT-65865 fixed
This commit is contained in:
Ilya Chernikov
2024-02-05 17:41:15 +01:00
committed by Space Team
parent 3ce2172c79
commit e5a6900458
9 changed files with 120 additions and 21 deletions
@@ -8,10 +8,7 @@ package org.jetbrains.kotlin.cli.common
import org.jetbrains.kotlin.KtSourceFile
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.fir.DependencyListForCliModule
import org.jetbrains.kotlin.fir.FirModuleData
import org.jetbrains.kotlin.fir.FirModuleDataImpl
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.analysis.checkers.OptInLanguageVersionSettingsCheckers
import org.jetbrains.kotlin.fir.checkers.registerExtendedCommonCheckers
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar
@@ -68,6 +65,7 @@ fun <F> prepareJvmSessions(
librariesScope: AbstractProjectFileSearchScope,
libraryList: DependencyListForCliModule,
isCommonSource: (F) -> Boolean,
isScript: (F) -> Boolean,
fileBelongsToModule: (F, String) -> Boolean,
createProviderAndScopeForIncrementalCompilation: (List<F>) -> IncrementalCompilationContext?,
): List<SessionWithSources<F>> {
@@ -79,7 +77,7 @@ fun <F> prepareJvmSessions(
return prepareSessions(
files, configuration, rootModuleName, JvmPlatforms.unspecifiedJvmPlatform,
JvmPlatformAnalyzerServices, metadataCompilationMode = false, libraryList, isCommonSource, fileBelongsToModule,
JvmPlatformAnalyzerServices, metadataCompilationMode = false, libraryList, isCommonSource, isScript, fileBelongsToModule,
createLibrarySession = { sessionProvider ->
FirJvmSessionFactory.createLibrarySession(
rootModuleName,
@@ -149,7 +147,8 @@ fun <F> prepareJsSessions(
): List<SessionWithSources<F>> {
return prepareSessions(
files, configuration, rootModuleName, JsPlatforms.defaultJsPlatform, JsPlatformAnalyzerServices,
metadataCompilationMode = false, libraryList, isCommonSource, fileBelongsToModule,
metadataCompilationMode = false, libraryList, isCommonSource, isScript = { false },
fileBelongsToModule,
createLibrarySession = { sessionProvider ->
FirJsSessionFactory.createLibrarySession(
rootModuleName,
@@ -196,7 +195,8 @@ fun <F> prepareNativeSessions(
): List<SessionWithSources<F>> {
return prepareSessions(
files, configuration, rootModuleName, NativePlatforms.unspecifiedNativePlatform, NativePlatformAnalyzerServices,
metadataCompilationMode, libraryList, isCommonSource, fileBelongsToModule, createLibrarySession = { sessionProvider ->
metadataCompilationMode, libraryList, isCommonSource, isScript = { false },
fileBelongsToModule, createLibrarySession = { sessionProvider ->
FirNativeSessionFactory.createLibrarySession(
rootModuleName,
resolvedLibraries,
@@ -244,7 +244,8 @@ fun <F> prepareWasmSessions(
}
return prepareSessions(
files, configuration, rootModuleName, WasmPlatforms.Default, analyzerServices,
metadataCompilationMode = false, libraryList, isCommonSource, fileBelongsToModule,
metadataCompilationMode = false, libraryList, isCommonSource, isScript = { false },
fileBelongsToModule,
createLibrarySession = { sessionProvider ->
FirWasmSessionFactory.createLibrarySession(
rootModuleName,
@@ -291,7 +292,8 @@ fun <F> prepareCommonSessions(
): List<SessionWithSources<F>> {
return prepareSessions(
files, configuration, rootModuleName, CommonPlatforms.defaultCommonPlatform, CommonPlatformAnalyzerServices,
metadataCompilationMode = true, libraryList, isCommonSource, fileBelongsToModule, createLibrarySession = { sessionProvider ->
metadataCompilationMode = true, libraryList, isCommonSource, isScript = { false }, fileBelongsToModule,
createLibrarySession = { sessionProvider ->
FirCommonSessionFactory.createLibrarySession(
rootModuleName,
sessionProvider,
@@ -335,11 +337,13 @@ private inline fun <F> prepareSessions(
metadataCompilationMode: Boolean,
libraryList: DependencyListForCliModule,
isCommonSource: (F) -> Boolean,
isScript: (F) -> Boolean,
fileBelongsToModule: (F, String) -> Boolean,
createLibrarySession: (FirProjectSessionProvider) -> FirSession,
createSourceSession: FirSessionProducer<F>,
): List<SessionWithSources<F>> {
val languageVersionSettings = configuration.languageVersionSettings
val (scripts, nonScriptFiles) = files.partition(isScript)
val isMppEnabled = languageVersionSettings.supportsFeature(LanguageFeature.MultiPlatformProjects)
val hmppModuleStructure = configuration.get(CommonConfigurationKeys.HMPP_MODULE_STRUCTURE)
@@ -353,26 +357,58 @@ private inline fun <F> prepareSessions(
}
}
return when {
metadataCompilationMode || !isMppEnabled -> listOf(
createSingleSession(
files, rootModuleName, libraryList, targetPlatform, analyzerServices,
sessionProvider, sessionConfigurator, createSourceSession
val nonScriptSessions =when {
metadataCompilationMode || !isMppEnabled -> {
listOf(
createSingleSession(
nonScriptFiles, rootModuleName, libraryList, targetPlatform, analyzerServices,
sessionProvider, sessionConfigurator, createSourceSession
)
)
)
}
hmppModuleStructure == null -> createSessionsForLegacyMppProject(
files, rootModuleName, libraryList, targetPlatform, analyzerServices,
nonScriptFiles, rootModuleName, libraryList, targetPlatform, analyzerServices,
sessionProvider, sessionConfigurator, isCommonSource, createSourceSession
)
else -> createSessionsForHmppProject(
files, rootModuleName, hmppModuleStructure, libraryList, targetPlatform, analyzerServices,
nonScriptFiles, rootModuleName, hmppModuleStructure, libraryList, targetPlatform, analyzerServices,
sessionProvider, sessionConfigurator, fileBelongsToModule, createSourceSession
)
}
return if (scripts.isEmpty()) nonScriptSessions
else nonScriptSessions +
createScriptsSession(
scripts, rootModuleName, libraryList, nonScriptSessions.last().session.moduleData,
targetPlatform, analyzerServices, sessionProvider, sessionConfigurator, createSourceSession
)
}
private inline fun <F> createScriptsSession(
scripts: List<F>,
rootModuleName: Name,
libraryList: DependencyListForCliModule,
lastModuleData: FirModuleData,
targetPlatform: TargetPlatform,
analyzerServices: PlatformDependentAnalyzerServices,
sessionProvider: FirProjectSessionProvider,
noinline sessionConfigurator: FirSessionConfigurator.() -> Unit,
createSourceSession: FirSessionProducer<F>,
): SessionWithSources<F> =
createSingleSession(
scripts, Name.identifier("${rootModuleName.asString()}-scripts"),
DependencyListForCliModule(
libraryList.regularDependencies,
listOf(lastModuleData),
libraryList.friendsDependencies,
libraryList.moduleDataProvider
),
targetPlatform, analyzerServices,
sessionProvider, sessionConfigurator, createSourceSession
)
private inline fun <F> createSingleSession(
files: List<F>,
rootModuleName: Name,
@@ -214,6 +214,7 @@ object FirKotlinToJvmBytecodeCompiler {
ktFiles, configuration, projectEnvironment, Name.special("<$rootModuleName>"),
extensionRegistrars, librariesScope, libraryList,
isCommonSource = { it.isCommonSource == true },
isScript = { it.isScript() },
fileBelongsToModule = { file, moduleName -> file.hmppModuleName == moduleName },
createProviderAndScopeForIncrementalCompilation = { providerAndScopeForIncrementalCompilation }
)
@@ -296,6 +296,7 @@ fun compileModuleToAnalyzedFir(
allSources, moduleConfiguration, projectEnvironment, Name.special("<$rootModuleName>"),
extensionRegistrars, librariesScope, libraryList,
isCommonSource = input.groupedSources.isCommonSourceForLt,
isScript = { false },
fileBelongsToModule = input.groupedSources.fileBelongsToModuleForLt,
createProviderAndScopeForIncrementalCompilation = { files ->
val scope = projectEnvironment.getSearchScopeBySourceFiles(files)