Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.uniqueName
import org.jetbrains.kotlin.utils.topologicalSort

/**
* This functions allows sorting the libraries in the reverse topological order using the legacy manifest-based mechanics.
* I.e. [KLIB_PROPERTY_UNIQUE_NAME] and [KLIB_PROPERTY_DEPENDS] properties.
*
* TODO(KT-70118): This is a temporary solution for the Kotlin/Native compiler's second stage. It is going to be
* replaced by the IR linker-based dependency computation or another sustainable solution.
*/
fun Collection<KotlinLibrary>.legacyKlibReverseTopoSort(): List<KotlinLibrary> {
if (size <= 1) return toList()

val dependencies = LegacyKlibDependencies(this)

return topologicalSort(
nodes = this,
reportCycle = { library -> error("Cyclic dependency in library graph for: ${library.location}") },
dependencies = { dependencies.getDependenciesFor(this@topologicalSort) }
).reversed()
}

/**
* This class allows computing the dependencies of a library using the legacy manifest-based mechanics.
* I.e. [KLIB_PROPERTY_UNIQUE_NAME] and [KLIB_PROPERTY_DEPENDS] properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ interface KotlinLibraryResolveResult {

fun filterRoots(predicate: (KotlinResolvedLibrary) -> Boolean): KotlinLibraryResolveResult

/**
* Returns the list of libraries in reverse topological order.
*/
fun getFullList(): List<KotlinLibrary>

fun forEach(action: (KotlinLibrary) -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,27 @@ class KotlinLibraryResolverResultImpl(

private val all: List<KotlinResolvedLibrary>
by lazy {
val result = mutableSetOf<KotlinResolvedLibrary>()
val queue = ArrayDeque(roots) // Use a queue for traversal
result.addAll(roots)

while (queue.isNotEmpty()) {
val current = queue.removeFirst()
for (dependency in current.resolvedDependencies) {
if (result.add(dependency)) {
queue.add(dependency)
}
buildList {
val visiting = mutableSetOf<KotlinResolvedLibrary>()
val visited = mutableSetOf<KotlinResolvedLibrary>()
fun dfs(current: KotlinResolvedLibrary) {
if (current in visited) return
if (current in visiting) error("Cyclic dependency in library graph for: ${current.library.location}")
visiting.add(current)
current.resolvedDependencies.forEach(::dfs)
add(current)
visited.add(current)
}
roots.forEach(::dfs)
}
result.toList()
}

override fun filterRoots(predicate: (KotlinResolvedLibrary) -> Boolean) =
KotlinLibraryResolverResultImpl(roots.filter(predicate))

/**
* Returns the list of libraries in reverse topological order.
*/
override fun getFullList(): List<KotlinLibrary> = all.map { it.library }

override fun forEach(action: (KotlinLibrary) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import kotlinx.cli.default
import kotlinx.cli.required
import kotlinx.metadata.klib.ChunkedKlibModuleFragmentWriteStrategy
import kotlinx.metadata.klib.KlibMetadataVersion
import org.jetbrains.kotlin.backend.common.legacyKlibReverseTopoSort
import org.jetbrains.kotlin.config.KlibAbiCompatibilityLevel
import org.jetbrains.kotlin.konan.ForeignExceptionMode
import org.jetbrains.kotlin.konan.TempFiles
Expand Down Expand Up @@ -582,7 +581,7 @@ private fun resolveDependencies(
noStdLib = false,
noDefaultLibs = noDefaultLibs,
noEndorsedLibs = noEndorsedLibs
).getFullList().legacyKlibReverseTopoSort()
).getFullList()
validateNoLibrariesWerePassedViaCliByUniqueName(cinteropArguments.library, resolvedLibraries, resolver.logger)
return resolvedLibraries
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import java.nio.channels.FileLock
import java.nio.channels.OverlappingFileLockException
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import org.jetbrains.kotlin.backend.common.legacyKlibReverseTopoSort
import org.jetbrains.kotlin.konan.config.konanHome

internal fun KotlinLibrary.getAllTransitiveDependencies(allLibraries: Map<String, KotlinLibrary>): List<KotlinLibrary> {
Expand Down Expand Up @@ -75,7 +74,7 @@ class CacheBuilder(
&& (config.isFinalBinary || config.produce.isFullCache)
&& (autoCacheableFrom.isNotEmpty() || icEnabled)

private val allLibraries by lazy { config.resolvedLibraries.getFullList().legacyKlibReverseTopoSort() }
private val allLibraries by lazy { config.resolvedLibraries.getFullList() }
private val uniqueNameToLibrary by lazy { allLibraries.associateBy { it.uniqueName } }
private val uniqueNameToHash = mutableMapOf<String, FingerprintHash>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.jetbrains.kotlin.backend.konan

import org.jetbrains.kotlin.backend.common.legacyKlibReverseTopoSort
import org.jetbrains.kotlin.utils.atMostOne
import org.jetbrains.kotlin.backend.konan.llvm.FunctionOrigin
import org.jetbrains.kotlin.backend.konan.llvm.llvmSymbolOrigin
Expand Down Expand Up @@ -172,7 +171,7 @@ internal class DependenciesTrackerImpl(
usedWeakBitcodeOfFile.map { UsedLibraryFile(it, weak = true) }

private val topSortedLibraries by lazy {
context.config.resolvedLibraries.getFullList().legacyKlibReverseTopoSort()
context.config.resolvedLibraries.getFullList()
}

private inner class CachedBitcodeDependenciesComputer {
Expand Down Expand Up @@ -447,7 +446,7 @@ data class DependenciesTrackingResult(
val allNativeLibs = DependenciesSerializer.deserialize(path, dependencies.subList(allNativeDepsIndex + 1, allCachedBitcodeDepsIndex)).map { it.libName }
val allCachedBitcodeDeps = DependenciesSerializer.deserialize(path, dependencies.subList(allCachedBitcodeDepsIndex + 1, dependencies.size))

val topSortedLibraries = config.resolvedLibraries.getFullList().legacyKlibReverseTopoSort()
val topSortedLibraries = config.resolvedLibraries.getFullList()
val nativeDependenciesToLink = topSortedLibraries.mapNotNull { if (it.uniqueName in nativeLibsToLink) it else null }
val allNativeDependencies = topSortedLibraries.mapNotNull { if (it.uniqueName in allNativeLibs) it else null }
val allCachedBitcodeDependencies = allCachedBitcodeDeps.map { unresolvedDep ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.backend.konan

import com.google.common.base.StandardSystemProperty
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.backend.common.legacyKlibReverseTopoSort
import org.jetbrains.kotlin.backend.common.linkage.partial.partialLinkageConfig
import org.jetbrains.kotlin.backend.konan.ir.BridgesPolicy
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCEntryPoints
Expand Down Expand Up @@ -423,8 +422,11 @@ class NativeSecondStageCompilationConfig(
val exportedLibraries: List<KotlinLibrary>
get() = getExportedLibraries(configuration, resolve.resolvedLibraries, resolve.resolver.searchPathResolver, report = true)

/**
* Returns the list of libraries in reverse topological order.
*/
fun librariesWithDependencies(): List<KotlinLibrary> {
return resolvedLibraries.filterRoots { (!it.library.isFromKotlinNativeDistribution && !purgeUserLibs) || it.library.hasDeclarationsAccessedDuringFrontendResolve }.getFullList().legacyKlibReverseTopoSort()
return resolvedLibraries.filterRoots { (!it.library.isFromKotlinNativeDistribution && !purgeUserLibs) || it.library.hasDeclarationsAccessedDuringFrontendResolve }.getFullList()
}

internal val externalDependenciesFile = configuration.externalDependencies?.let(::File)
Expand Down

This file was deleted.