[JS IR] Manage cache root dir from IC cache updater

- pass cache root directory instead of dir list
 - manage klib cache dirs from cache updater
This commit is contained in:
Alexander Korepanov
2022-11-15 21:08:29 +01:00
committed by Space Team
parent c7589245c4
commit ec6a7094e6
5 changed files with 13 additions and 20 deletions
@@ -650,7 +650,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
val cacheUpdater = CacheUpdater(
mainModule = arguments.includes!!,
allModules = libraries,
icCachePaths = cacheDirectories,
icCacheRootDir = cacheDirectories.last(),
compilerConfiguration = configurationJs,
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
mainArguments = mainCallArguments,
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.library.KLIB_PROPERTY_DEPENDS
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.name.FqName
import java.io.File
import java.security.MessageDigest
import java.util.EnumSet
fun interface JsIrCompilerICInterface {
@@ -48,7 +49,7 @@ enum class DirtyFileState(val str: String) {
class CacheUpdater(
mainModule: String,
allModules: Collection<String>,
icCachePaths: Collection<String>,
private val icCacheRootDir: String,
private val compilerConfiguration: CompilerConfiguration,
private val irFactory: () -> IrFactory,
private val mainArguments: List<String>?,
@@ -60,8 +61,6 @@ class CacheUpdater(
private val dependencyGraph = buildDependenciesGraph(libraries)
private val configHash = compilerConfiguration.configHashForIC()
private val cacheMap = libraries.values.zip(icCachePaths).toMap()
private val mainLibraryFile = KotlinLibraryFile(File(mainModule).canonicalPath)
private val mainLibrary = libraries[mainLibraryFile] ?: notFoundIcError("main library", mainLibraryFile)
@@ -114,9 +113,13 @@ class CacheUpdater(
private inner class CacheUpdaterInternal {
val signatureHashCalculator = IdSignatureHashCalculator()
private val incrementalCaches = libraries.entries.associate { (libFile, lib) ->
val cachePath = cacheMap[lib] ?: notFoundIcError("cache path", KotlinLibraryFile(lib))
libFile to IncrementalCache(lib, cachePath)
private val incrementalCaches = run {
val md5 = MessageDigest.getInstance("MD5")
libraries.entries.associate { (libFile, lib) ->
val file = File(libFile.path)
val hash = md5.digest(file.absolutePath.encodeToByteArray()).joinToString("", transform = { "%02x".format(it) })
libFile to IncrementalCache(lib, File(icCacheRootDir, "${file.name}.$hash"))
}
}
private fun getLibIncrementalCache(libFile: KotlinLibraryFile) =
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.protobuf.CodedOutputStream
import java.io.File
internal class IncrementalCache(private val library: KotlinLibrary, cachePath: String) {
internal class IncrementalCache(private val library: KotlinLibrary, private val cacheDir: File) {
companion object {
private const val CACHE_HEADER = "ic.header.bin"
@@ -24,7 +24,6 @@ internal class IncrementalCache(private val library: KotlinLibrary, cachePath: S
}
private var forceRebuildJs = false
private val cacheDir = File(cachePath)
private val signatureToIndexMappingFromMetadata = hashMapOf<KotlinSourceFile, MutableMap<IdSignature, Int>>()
private val libraryFile = KotlinLibraryFile(library)
@@ -92,10 +92,6 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
return File(File(buildDir, moduleName), "$moduleName.klib")
}
private fun resolveModuleCache(moduleName: String, buildDir: File): File {
return File(File(buildDir, moduleName), "cache")
}
private fun createConfiguration(moduleName: String, language: List<String>): CompilerConfiguration {
val copy = environment.configuration.copy()
copy.put(CommonConfigurationKeys.MODULE_NAME, moduleName)
@@ -127,7 +123,6 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
private inner class TestStepInfo(
val moduleName: String,
val modulePath: String,
val icCacheDir: String,
val expectedFileStats: Map<String, Set<String>>
)
@@ -157,7 +152,6 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
return TestStepInfo(
module.safeModuleName,
outputKlibFile.canonicalPath,
resolveModuleCache(module, buildDir).canonicalPath,
expectedFileStats
)
}
@@ -229,7 +223,6 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
}
fun execute() {
val stdlibCacheDir = resolveModuleCache(STDLIB_ALIAS, buildDir).canonicalPath
for (projStep in projectInfo.steps) {
val testInfo = projStep.order.map { setupTestStep(projStep, it) }
@@ -237,7 +230,7 @@ abstract class AbstractInvalidationTest : KotlinTestWithEnvironment() {
val cacheUpdater = CacheUpdater(
mainModule = testInfo.last().modulePath,
allModules = testInfo.mapTo(mutableListOf(STDLIB_KLIB)) { it.modulePath },
icCachePaths = testInfo.mapTo(mutableListOf(stdlibCacheDir)) { it.icCacheDir },
icCacheRootDir = buildDir.resolve("incremental-cache").absolutePath,
compilerConfiguration = configuration,
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
mainArguments = null,
@@ -189,13 +189,11 @@ abstract class AbstractJsKLibABITestCase : KtUsefulTestCase() {
mainModuleKlibFile: File,
allDependencies: KlibABITestUtils.Dependencies
): CompilationOutputs {
fun cacheDir(library: File): File = buildDir.resolve("libs-cache").resolve(library.name).apply { mkdirs() }
// TODO: what about friend dependencies?
val cacheUpdater = CacheUpdater(
mainModule = mainModuleKlibFile.absolutePath,
allModules = allDependencies.regularDependencies.map { it.path },
icCachePaths = allDependencies.regularDependencies.map { cacheDir(it).path },
icCacheRootDir = buildDir.resolve("libs-cache").absolutePath,
compilerConfiguration = configuration,
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
mainArguments = null,