[K/N] Refactor :kotlin-native:dependencies

Move K/N dependencies handling logic into gradle plugins:

`NativeDependenciesDownloader` is instantiated in
`:kotlin-native:dependencies` and provides a single configuration that
contains all directories with the dependencies. The configuration has
a variant for each target.

`NativeDependencies` is instantiated by projects that require native
dependencies (i.e. previously used to depend on
`:kotlin-native:dependencies:update`). This plugin creates a configuration
that by default depends on `:kotlin-native:dependencies`. Its extension
provides `llvmDependency`, `libffiDependency`, `hostPlatformDependency`,
`targetDependency()` to depend on the parts needed for the project and
provides accessors `llvmPath`, `libffiPath`, `hostPlatform` that are
safe to use during configuration time, but which do not automatically
resolve those dependencies.

`llvmDir`, `*LibffiDir` properties on `:kotlin-native` are removed
completely, as their use is replaced by `NativeDependencies`. As a
consequence, `evaluationDependsOn(":kotlin-native:dependencies")` is
also gone.

All direct dependencies on `:kotlin-native:dependencies:update` are
replaced with dependencies on specific parts via `NativeDependencies`
This commit is contained in:
Alexander Shabalin
2023-08-28 11:55:32 +02:00
committed by Space Team
parent 9de791b9ac
commit 4922223bec
23 changed files with 519 additions and 186 deletions
-116
View File
@@ -1,116 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.FromString
import org.jetbrains.kotlin.konan.target.*
import org.jetbrains.kotlin.konan.properties.KonanPropertiesLoader
import org.jetbrains.kotlin.konan.util.ArchiveType
import org.jetbrains.kotlin.konan.util.DependencyProcessor
buildscript {
apply from: "$rootDir/kotlin-native/gradle/kotlinGradlePlugin.gradle"
repositories {
mavenCentral()
}
}
/**
* Downloads dependencies from the given URL into the given directory.
* The set of dependencies is determined by konanPropertiesLoader.
*/
class NativeDep extends DefaultTask {
static final String baseUrl = "https://cache-redirector.jetbrains.com/download.jetbrains.com/kotlin/native"
@Internal
KonanPropertiesLoader konanPropertiesLoader = null
@Internal
final File getBaseOutDir() {
final File res = project.rootProject.project(":kotlin-native").ext.dependenciesDir
res.mkdirs()
return res
}
@TaskAction
void downloadAndExtract() {
def downloader = new DependencyProcessor(baseOutDir, konanPropertiesLoader, baseUrl, false, ArchiveType.systemDefault, { url, currentBytes, totalBytes ->
print("\n$name Downloading dependency: $url (${currentBytes}/${totalBytes}). ")
})
downloader.showInfo = project.logger.isEnabled(LogLevel.INFO)
downloader.run()
}
}
enum DependencyKind {
LLVM( "llvm", { it.llvmHome }, { "llvmDir" } ),
LIBFFI( "libffi", { it.libffiDir } )
DependencyKind(String name,
@ClosureParams(value = FromString.class, options = "KonanPropertiesLoader") Closure<String> dirGetter,
@ClosureParams(value = FromString.class, options = "KonanTarget") Closure<String> propertyNameGetter =
{target -> "${target.visibleName}${name.capitalize()}Dir"}) {
this.name = name
this.dirGetter = dirGetter
this.propertyNameGetter = propertyNameGetter
}
private String name
private Closure<String> dirGetter // KonanProperties -> String
private Closure<String> propertyNameGetter // KonanTarget -> String
String getDirectory(KonanPropertiesLoader properties) {
return dirGetter(properties)
}
String getPropertyName(KonanTarget target) {
return propertyNameGetter(target)
}
String toString() { return name }
}
def platformManager = rootProject.project(":kotlin-native").ext.platformManager
EnabledTargetsKt.enabledTargets(platformManager).each { target ->
def loader = platformManager.loader(target)
tasks.register("${target}Dependencies", NativeDep) {
konanPropertiesLoader = loader
}
// Also resolves all dependencies:
final DependencyProcessor dependencyProcessor = new DependencyProcessor(
rootProject.project(":kotlin-native").ext.dependenciesDir,
loader.properties,
loader.dependencies,
NativeDep.baseUrl,
false,
ArchiveType.systemDefault,
{ url, currentBytes, totalBytes ->
print("\n(no-task) Downloading dependency: $url (${currentBytes}/${totalBytes}). ")
})
DependencyKind.values().each { kind ->
def dir = kind.getDirectory(loader)
if (dir != null) {
String path = dependencyProcessor.resolve(dir).canonicalPath
rootProject.project(":kotlin-native").ext.set(kind.getPropertyName(target), path)
}
}
}
tasks.register("update") {
dependsOn tasks.withType(NativeDep)
mustRunAfter(tasks.withType(NativeDep))
}
rootProject.project(":kotlin-native").ext.nativeDependencies = tasks.withType(NativeDep)
tasks.register("rmDotKonan", Delete) {
def dir = System.getenv("KONAN_DATA_DIR") ?: "${System.getProperty("user.home")}/.konan"
delete dir
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import java.io.File
import org.jetbrains.kotlin.konan.target.allTargetsWithSanitizers
plugins {
id("native-dependencies-downloader")
id("native-dependencies")
}
nativeDependenciesDownloader {
repositoryURL.set("https://cache-redirector.jetbrains.com/download.jetbrains.com/kotlin/native")
dependenciesDirectory.set(rootProject.project(":kotlin-native").property("dependenciesDir") as File)
allTargets {}
}
/**
* Download all dependencies.
*/
val update by tasks.registering {
platformManager.allTargetsWithSanitizers.forEach {
dependsOn(nativeDependencies.targetDependency(it))
}
}
// TODO: This sort of task probably belongs to :kotlin-native
val rmDotKonan by tasks.registering(Delete::class) {
val dir = System.getenv("KONAN_DATA_DIR") ?: "${System.getProperty("user.home")}/.konan"
delete(dir)
}