From bf9c66e275a096da5c5080ff6e34a5a9c4d5552f Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Mon, 6 Apr 2020 11:53:28 +0700 Subject: [PATCH] KLIB: Merge manifest for stdlib produced for different targets (cherry picked from commit b7f12c9ae64f8a4a0785105a53f118083e660386) --- .../main/kotlin/org/jetbrains/kotlin/Utils.kt | 63 ++++++++++++++++++- build.gradle | 10 +++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt b/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt index b4d4075be8e..4aabe7105b3 100644 --- a/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt +++ b/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt @@ -7,7 +7,11 @@ package org.jetbrains.kotlin import org.gradle.api.Project import org.gradle.api.Task +import org.jetbrains.kotlin.konan.properties.loadProperties +import org.jetbrains.kotlin.konan.properties.propertyList +import org.jetbrains.kotlin.konan.properties.saveProperties import org.jetbrains.kotlin.konan.target.* +import org.jetbrains.kotlin.library.KLIB_PROPERTY_NATIVE_TARGETS import java.io.FileInputStream import java.io.IOException import java.io.File @@ -17,6 +21,7 @@ import java.net.URL import java.util.Base64 import org.jetbrains.report.json.* import java.nio.file.Path +import org.jetbrains.kotlin.konan.file.File as KFile //region Project properties. @@ -254,4 +259,60 @@ fun compileSwift(project: Project, target: KonanTarget, sources: List, o } fun targetSupportsMimallocAllocator(targetName: String) = - HostManager().targetByName(targetName).supportsMimallocAllocator() \ No newline at end of file + HostManager().targetByName(targetName).supportsMimallocAllocator() + +fun Project.mergeManifestsByTargets(source: File, destination: File) { + logger.info("Merging manifests: $source -> $destination") + + val sourceFile = KFile(source.absolutePath) + val sourceProperties = sourceFile.loadProperties() + + val destinationFile = KFile(destination.absolutePath) + val destinationProperties = destinationFile.loadProperties() + + // check that all properties except for KLIB_PROPERTY_NATIVE_TARGETS are equivalent + val mismatchedProperties = (sourceProperties.keys + destinationProperties.keys) + .asSequence() + .map { it.toString() } + .filter { it != KLIB_PROPERTY_NATIVE_TARGETS } + .sorted() + .mapNotNull { propertyKey: String -> + val sourceProperty: String? = sourceProperties.getProperty(propertyKey) + val destinationProperty: String? = destinationProperties.getProperty(propertyKey) + when { + sourceProperty == null -> "\"$propertyKey\" is absent in $sourceFile" + destinationProperty == null -> "\"$propertyKey\" is absent in $destinationFile" + sourceProperty == destinationProperty -> { + // properties match, OK + null + } + sourceProperties.propertyList(propertyKey, escapeInQuotes = true).toSet() == + destinationProperties.propertyList(propertyKey, escapeInQuotes = true).toSet() -> { + // properties match, OK + null + } + else -> "\"$propertyKey\" differ: [$sourceProperty] vs [$destinationProperty]" + } + } + .toList() + + check(mismatchedProperties.isEmpty()) { + buildString { + appendln("Found mismatched properties while merging manifest files: $source -> $destination") + mismatchedProperties.joinTo(this, "\n") + } + } + + // merge KLIB_PROPERTY_NATIVE_TARGETS property + val sourceNativeTargets = sourceProperties.propertyList(KLIB_PROPERTY_NATIVE_TARGETS) + val destinationNativeTargets = destinationProperties.propertyList(KLIB_PROPERTY_NATIVE_TARGETS) + + val mergedNativeTargets = HashSet().apply { + addAll(sourceNativeTargets) + addAll(destinationNativeTargets) + } + + destinationProperties[KLIB_PROPERTY_NATIVE_TARGETS] = mergedNativeTargets.joinToString(" ") + + destinationFile.saveProperties(destinationProperties) +} diff --git a/build.gradle b/build.gradle index 7f2728d36fd..b084d980b40 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.CollisionDetector import org.jetbrains.kotlin.CollisionTransformer import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import org.apache.tools.ant.filters.ReplaceTokens +import org.jetbrains.kotlin.UtilsKt buildscript { apply from: "gradle/kotlinGradlePlugin.gradle" @@ -465,6 +466,15 @@ targetList.each { target -> from(project(':runtime').file("build/${target}Stdlib")) { include('**') into(stdlib) + eachFile { + if (name == 'manifest') { + def existingManifest = file("$destinationDir/$path") + if (existingManifest.exists()) { + UtilsKt.mergeManifestsByTargets(project, file, existingManifest) + exclude() + } + } + } } from(project(':runtime').file("build/$target")) { include("runtime.bc")