[K/N] Implemented cache orchestration machinery in the compiler

This commit is contained in:
Igor Chevdar
2022-11-25 12:49:16 +02:00
committed by Space Team
parent 816971ed47
commit 9c86532eb0
9 changed files with 326 additions and 51 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analyzer.CompilationErrorException
import org.jetbrains.kotlin.backend.konan.*
import org.jetbrains.kotlin.cli.common.*
import org.jetbrains.kotlin.cli.common.arguments.K2NativeCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot
import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
@@ -57,40 +58,23 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
}
val pluginLoadResult =
PluginCliParser.loadPluginsSafe(arguments.pluginClasspaths, arguments.pluginOptions, arguments.pluginConfigurations, configuration)
PluginCliParser.loadPluginsSafe(arguments.pluginClasspaths, arguments.pluginOptions, arguments.pluginConfigurations, configuration)
if (pluginLoadResult != ExitCode.OK) return pluginLoadResult
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable,
configuration, EnvironmentConfigFiles.NATIVE_CONFIG_FILES)
val project = environment.project
val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) ?: MessageCollector.NONE
if (configuration.getBoolean(CommonConfigurationKeys.USE_FIR)) {
messageCollector.report(WARNING, "New compiler pipeline K2 is experimental in Kotlin/Native. No compatibility guarantees are yet provided")
}
configuration.put(CLIConfigurationKeys.FLEXIBLE_PHASE_CONFIG, createFlexiblePhaseConfig(arguments))
val enoughArguments = arguments.freeArgs.isNotEmpty() || arguments.isUsefulWithoutFreeArgs
if (!enoughArguments) {
configuration.report(ERROR, "You have not specified any compilation arguments. No output has been produced.")
messageCollector.report(ERROR, "You have not specified any compilation arguments. No output has been produced.")
}
/* Set default version of metadata version */
val metadataVersionString = arguments.metadataVersion
if (metadataVersionString == null) {
configuration.put(CommonConfigurationKeys.METADATA_VERSION, KlibMetadataVersion.INSTANCE)
}
val relativePathBases = arguments.relativePathBases
if (relativePathBases != null) {
configuration.put(CommonConfigurationKeys.KLIB_RELATIVE_PATH_BASES, relativePathBases.toList())
}
configuration.put(CommonConfigurationKeys.KLIB_NORMALIZE_ABSOLUTE_PATH, arguments.normalizeAbsolutePath)
configuration.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
val environment = prepareEnvironment(arguments, configuration, rootDisposable)
try {
KonanDriver(project, environment, configuration).run()
runKonanDriver(configuration, environment, rootDisposable)
} catch (e: Throwable) {
if (e is KonanCompilationException || e is CompilationErrorException)
return ExitCode.COMPILATION_ERROR
@@ -109,6 +93,53 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
return ExitCode.OK
}
private fun prepareEnvironment(
arguments: K2NativeCompilerArguments,
configuration: CompilerConfiguration,
rootDisposable: Disposable
): KotlinCoreEnvironment {
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable,
configuration, EnvironmentConfigFiles.NATIVE_CONFIG_FILES)
configuration.put(CLIConfigurationKeys.FLEXIBLE_PHASE_CONFIG, createFlexiblePhaseConfig(arguments))
/* Set default version of metadata version */
val metadataVersionString = arguments.metadataVersion
if (metadataVersionString == null) {
configuration.put(CommonConfigurationKeys.METADATA_VERSION, KlibMetadataVersion.INSTANCE)
}
val relativePathBases = arguments.relativePathBases
if (relativePathBases != null) {
configuration.put(CommonConfigurationKeys.KLIB_RELATIVE_PATH_BASES, relativePathBases.toList())
}
configuration.put(CommonConfigurationKeys.KLIB_NORMALIZE_ABSOLUTE_PATH, arguments.normalizeAbsolutePath)
configuration.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
return environment
}
private fun runKonanDriver(
configuration: CompilerConfiguration,
environment: KotlinCoreEnvironment,
rootDisposable: Disposable
) {
val konanDriver = KonanDriver(environment.project, environment, configuration) { args, setupConfiguration ->
val spawnedArguments = K2NativeCompilerArguments()
parseCommandLineArguments(args, spawnedArguments)
val spawnedConfiguration = CompilerConfiguration()
spawnedConfiguration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY))
spawnedConfiguration.setupCommonArguments(spawnedArguments, this::createMetadataVersion)
setupPlatformSpecificArgumentsAndServices(spawnedConfiguration, spawnedArguments, Services.EMPTY)
spawnedConfiguration.setupConfiguration()
val spawnedEnvironment = prepareEnvironment(spawnedArguments, spawnedConfiguration, rootDisposable)
runKonanDriver(spawnedConfiguration, spawnedEnvironment, rootDisposable)
}
konanDriver.run()
}
val K2NativeCompilerArguments.isUsefulWithoutFreeArgs: Boolean
get() = listTargets || listPhases || checkDependencies || !includes.isNullOrEmpty() ||
libraryToAddToCache != null || !exportedLibraries.isNullOrEmpty()
@@ -276,12 +307,12 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
val libraryToAddToCache = parseLibraryToAddToCache(arguments, configuration, outputKind)
if (libraryToAddToCache != null && !arguments.outputName.isNullOrEmpty())
configuration.report(ERROR, "${K2NativeCompilerArguments.ADD_CACHE} already implicitly sets output file name")
val cacheDirectories = arguments.cacheDirectories.toNonNullList()
libraryToAddToCache?.let { put(LIBRARY_TO_ADD_TO_CACHE, it) }
put(CACHE_DIRECTORIES, cacheDirectories)
put(CACHED_LIBRARIES, parseCachedLibraries(arguments, configuration))
val filesToCache = arguments.filesToCache
filesToCache?.let { put(FILES_TO_CACHE, it.toList()) }
put(CACHE_DIRECTORIES, arguments.cacheDirectories.toNonNullList())
put(AUTO_CACHEABLE_FROM, arguments.autoCacheableFrom.toNonNullList())
arguments.autoCacheDir?.let { put(AUTO_CACHE_DIR, it) }
arguments.filesToCache?.let { put(FILES_TO_CACHE, it.toList()) }
put(MAKE_PER_FILE_CACHE, arguments.makePerFileCache)
parseShortModuleName(arguments, configuration, outputKind)?.let {