All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.backend.common.CommonKLibResolver.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.backend.common

import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.file.ZipFileSystemAccessor
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.KotlinLibraryProperResolverWithAttributes
import org.jetbrains.kotlin.library.UnresolvedLibrary
import org.jetbrains.kotlin.library.impl.createKotlinLibraryComponents
import org.jetbrains.kotlin.library.metadata.resolver.KotlinLibraryResolveResult
import org.jetbrains.kotlin.library.metadata.resolver.KotlinLibraryResolver
import org.jetbrains.kotlin.library.metadata.resolver.impl.libraryResolver
import org.jetbrains.kotlin.util.Logger

object CommonKLibResolver {
    fun resolve(
        libraries: Collection,
        logger: Logger,
        zipAccessor: ZipFileSystemAccessor? = null
    ): KotlinLibraryResolveResult =
        resolveWithoutDependencies(
            libraries,
            logger,
            zipAccessor
        ).resolveWithDependencies()

    fun resolveWithoutDependencies(
        libraries: Collection,
        logger: Logger,
        zipAccessor: ZipFileSystemAccessor?
    ): KLibResolution {
        val unresolvedLibraries = libraries.map { UnresolvedLibrary(it, null) }
        val libraryAbsolutePaths = libraries.map { File(it).absolutePath }
        // Configure the resolver to only work with absolute paths for now.
        val libraryResolver = KLibResolverHelper(
            repositories = emptyList(),
            directLibs = libraryAbsolutePaths,
            distributionKlib = null,
            localKotlinDir = null,
            skipCurrentDir = false,
            logger = logger,
            zipAccessor = zipAccessor
        ).libraryResolver()

        return KLibResolution(
            libraryResolver,
            libraryResolver.resolveWithoutDependencies(
                unresolvedLibraries = unresolvedLibraries,
                noStdLib = true,
                noDefaultLibs = true,
                noEndorsedLibs = true
            )
        )
    }
}

class KLibResolution(
    private val libraryResolver: KotlinLibraryResolver,
    val libraries: List
) {
    fun resolveWithDependencies(): KotlinLibraryResolveResult {
        return with(libraryResolver) {
            libraries.resolveDependencies()
        }
    }
}

private class KLibResolverHelper(
    repositories: List,
    directLibs: List,
    distributionKlib: String?,
    localKotlinDir: String?,
    skipCurrentDir: Boolean,
    logger: Logger,
    private val zipAccessor: ZipFileSystemAccessor?
) : KotlinLibraryProperResolverWithAttributes(
    repositories,
    directLibs,
    distributionKlib,
    localKotlinDir,
    skipCurrentDir,
    logger,
    emptyList()
) {
    // Stick with the default KotlinLibrary for now.
    override fun libraryComponentBuilder(file: File, isDefault: Boolean) = createKotlinLibraryComponents(file, isDefault, zipAccessor)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy