/
KotlinMetadataTargetConfigurator.kt
373 lines (307 loc) · 18.2 KB
/
KotlinMetadataTargetConfigurator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2010-2019 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.gradle.targets.metadata
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Usage
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.component.SoftwareComponentInternal
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.sources.*
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
import org.jetbrains.kotlin.gradle.tasks.registerTask
import org.jetbrains.kotlin.gradle.tasks.locateTask
import org.jetbrains.kotlin.gradle.utils.addExtendsFromRelation
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.gradle.utils.setArchiveAppendixCompatible
import org.jetbrains.kotlin.gradle.utils.setArchiveClassifierCompatible
import java.io.File
import java.util.concurrent.Callable
internal const val ALL_COMPILE_METADATA_CONFIGURATION_NAME = "allSourceSetsCompileDependenciesMetadata"
internal const val ALL_RUNTIME_METADATA_CONFIGURATION_NAME = "allSourceSetsRuntimeDependenciesMetadata"
internal val Project.isKotlinGranularMetadataEnabled: Boolean
get() = PropertiesProvider(rootProject).enableGranularSourceSetsMetadata == true
class KotlinMetadataTargetConfigurator(kotlinPluginVersion: String) :
KotlinOnlyTargetConfigurator<KotlinCommonCompilation, KotlinMetadataTarget>(
createDefaultSourceSets = false,
createTestCompilation = false,
kotlinPluginVersion = kotlinPluginVersion
) {
companion object {
internal const val ALL_METADATA_JAR_NAME = "allMetadataJar"
internal fun transformGranularMetadataTaskName(sourceSet: KotlinSourceSet) =
lowerCamelCaseName("transform", sourceSet.name, "DependenciesMetadata")
// TODO generalize once a general production-test and other kinds of inter-compilation visibility are supported
// Currently, this is a temporary ad-hoc mechanism for exposing the commonMain dependencies to the test source sets
internal fun dependsOnWithInterCompilationDependencies(project: Project, sourceSet: KotlinSourceSet): Set<KotlinSourceSet> =
sourceSet.dependsOn.toMutableSet().apply {
if (sourceSet.name == KotlinSourceSet.COMMON_TEST_SOURCE_SET_NAME)
add(project.kotlinExtension.sourceSets.getByName(KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME))
}
}
private val KotlinOnlyTarget<KotlinCommonCompilation>.apiElementsConfiguration: Configuration
get() = project.configurations.getByName(apiElementsConfigurationName)
override fun configureTarget(target: KotlinMetadataTarget) {
super.configureTarget(target)
if (target.project.isKotlinGranularMetadataEnabled) {
target as KotlinMetadataTarget
createMergedAllSourceSetsConfigurations(target)
val allMetadataJar = target.project.tasks.getByName(ALL_METADATA_JAR_NAME) as Jar
createMetadataCompilationsForCommonSourceSets(target, allMetadataJar)
setupDependencyTransformationForCommonSourceSets(target)
target.project.configurations.getByName(target.apiElementsConfigurationName).attributes
.attribute(Usage.USAGE_ATTRIBUTE, target.project.usageByName(KotlinUsages.KOTLIN_METADATA))
}
}
override fun setupCompilationDependencyFiles(project: Project, compilation: KotlinCompilation<KotlinCommonOptions>) {
/** See [createTransformedMetadataClasspath] and its usage. */
if (project.isKotlinGranularMetadataEnabled)
compilation.compileDependencyFiles = project.files()
else
super.setupCompilationDependencyFiles(project, compilation)
}
override fun buildCompilationProcessor(compilation: KotlinCommonCompilation): KotlinSourceSetProcessor<*> {
val tasksProvider = KotlinTasksProvider(compilation.target.targetName)
return KotlinCommonSourceSetProcessor(compilation.target.project, compilation, tasksProvider, kotlinPluginVersion)
}
override fun createJarTasks(target: KotlinOnlyTarget<KotlinCommonCompilation>) {
super.createJarTasks(target)
if (target.project.isKotlinGranularMetadataEnabled) {
/** This JAR is created in addition to the main one, published with a classifier, but is by default used
* for project dependencies (as the Kotlin Granular metadata is enabled across all projects in a build, this is OK).
* See also [KotlinMetadataTarget.kotlinComponents]
*/
target.project.tasks.create(ALL_METADATA_JAR_NAME, Jar::class.java).apply {
description = "Assembles a jar archive containing the metadata for all Kotlin source sets."
group = BasePlugin.BUILD_GROUP
setArchiveAppendixCompatible { target.name.toLowerCase() }
setArchiveClassifierCompatible { "all" }
}
}
}
private fun setupDependencyTransformationForCommonSourceSets(target: KotlinMetadataTarget) {
target.project.whenEvaluated {
val publishedCommonSourceSets: Set<KotlinSourceSet> = getPublishedCommonSourceSets(project)
kotlinExtension.sourceSets.all {
setupDependencyTransformationForSourceSet(target.project, it, it in publishedCommonSourceSets)
}
}
}
private fun createMetadataCompilationsForCommonSourceSets(
target: KotlinMetadataTarget,
allMetadataJar: Jar
) = target.project.whenEvaluated {
// Do this after all targets are configured by the user build script
val publishedCommonSourceSets: Set<KotlinSourceSet> = getPublishedCommonSourceSets(project)
val sourceSetsWithMetadataCompilations: Map<KotlinSourceSet, KotlinCommonCompilation> =
publishedCommonSourceSets.associate { sourceSet ->
sourceSet to configureMetadataCompilation(target, sourceSet, allMetadataJar)
}
sourceSetsWithMetadataCompilations.forEach { (sourceSet, metadataCompilation) ->
val compileMetadataTransformationTasksForHierarchy = mutableSetOf<TaskProvider<TransformKotlinGranularMetadata>>()
// Adjust metadata compilation to support source set hierarchies, i.e. use both the outputs of dependsOn source set compilation
// and their dependencies metadata transformed for compilation:
sourceSet.getSourceSetHierarchy().forEach { hierarchySourceSet ->
if (hierarchySourceSet != sourceSet) {
val dependencyCompilation = sourceSetsWithMetadataCompilations.getValue(hierarchySourceSet as DefaultKotlinSourceSet)
metadataCompilation.compileDependencyFiles += dependencyCompilation.output.classesDirs.filter { it.exists() }
}
project.locateTask<TransformKotlinGranularMetadata>(transformGranularMetadataTaskName(hierarchySourceSet))
?.let(compileMetadataTransformationTasksForHierarchy::add)
}
metadataCompilation.compileDependencyFiles += createTransformedMetadataClasspath(
project.configurations.getByName(ALL_COMPILE_METADATA_CONFIGURATION_NAME),
project,
compileMetadataTransformationTasksForHierarchy
)
}
val generateMetadata = createGenerateProjectStructureMetadataTask()
allMetadataJar.from(project.files(Callable {
generateMetadata.get().resultXmlFile
}).builtBy(generateMetadata)) { spec ->
spec.into("META-INF").rename { MULTIPLATFORM_PROJECT_METADATA_FILE_NAME }
}
}
private fun createMergedAllSourceSetsConfigurations(target: KotlinMetadataTarget): Unit = with(target.project) {
listOf(ALL_COMPILE_METADATA_CONFIGURATION_NAME, ALL_RUNTIME_METADATA_CONFIGURATION_NAME).forEach { configurationName ->
project.configurations.create(configurationName).apply {
isCanBeConsumed = false
isCanBeResolved = true
usesPlatformOf(target)
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.usageByName(KotlinUsages.KOTLIN_METADATA))
}
}
}
private fun configureMetadataCompilation(
target: KotlinMetadataTarget,
sourceSet: KotlinSourceSet,
allMetadataJar: Jar
): KotlinCommonCompilation {
val project = target.project
// With the metadata target, we publish all API dependencies of all the published source sets together:
target.apiElementsConfiguration.extendsFrom(project.configurations.getByName(sourceSet.apiConfigurationName))
val metadataCompilation = when (sourceSet.name) {
// Historically, we already had a 'main' compilation in metadata targets; TODO consider removing it instead
KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME -> target.compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
else -> target.compilations.create(sourceSet.name) { compilation ->
compilation.addExactSourceSetsEagerly(setOf(sourceSet))
}
}
project.addExtendsFromRelation(metadataCompilation.compileDependencyConfigurationName, ALL_COMPILE_METADATA_CONFIGURATION_NAME)
allMetadataJar.from(metadataCompilation.output.allOutputs) { spec ->
spec.into(metadataCompilation.defaultSourceSet.name)
}
project.registerTask<TransformKotlinGranularMetadata>(transformGranularMetadataTaskName(sourceSet), listOf(sourceSet)) { }
return metadataCompilation
}
private fun setupDependencyTransformationForSourceSet(
project: Project,
sourceSet: KotlinSourceSet,
isSourceSetPublished: Boolean
) {
KotlinDependencyScope.values().forEach { scope ->
val allMetadataConfiguration = project.configurations.getByName(
when (scope) {
KotlinDependencyScope.RUNTIME_ONLY_SCOPE -> ALL_RUNTIME_METADATA_CONFIGURATION_NAME
else -> ALL_COMPILE_METADATA_CONFIGURATION_NAME
}
)
val granularMetadataTransformation = GranularMetadataTransformation(
project,
sourceSet,
listOf(scope),
allMetadataConfiguration,
lazy {
dependsOnWithInterCompilationDependencies(project, sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { checkNotNull(it.dependencyTransformations[scope]) }
}
)
(sourceSet as DefaultKotlinSourceSet).dependencyTransformations[scope] = granularMetadataTransformation
val sourceSetMetadataConfigurationByScope = project.sourceSetMetadataConfigurationByScope(sourceSet, scope)
granularMetadataTransformation.applyToConfiguration(sourceSetMetadataConfigurationByScope)
val sourceSetDependencyConfigurationByScope = project.sourceSetDependencyConfigurationByScope(sourceSet, scope)
// All source set dependencies except for compileOnly agree in versions with all other published runtime dependencies:
if (scope != KotlinDependencyScope.COMPILE_ONLY_SCOPE) {
if (isSourceSetPublished) {
project.addExtendsFromRelation(
ALL_RUNTIME_METADATA_CONFIGURATION_NAME,
sourceSetDependencyConfigurationByScope.name
)
}
project.addExtendsFromRelation(
sourceSetMetadataConfigurationByScope.name,
ALL_RUNTIME_METADATA_CONFIGURATION_NAME
)
}
// All source set dependencies except for runtimeOnly agree in versions with all other published compile dependencies:
if (scope != KotlinDependencyScope.RUNTIME_ONLY_SCOPE) {
if (isSourceSetPublished) {
project.addExtendsFromRelation(
ALL_COMPILE_METADATA_CONFIGURATION_NAME,
sourceSetDependencyConfigurationByScope.name
)
}
project.addExtendsFromRelation(
sourceSetMetadataConfigurationByScope.name,
ALL_COMPILE_METADATA_CONFIGURATION_NAME
)
}
}
}
/** Ensure that the [configuration] excludes the dependencies that are classified by this [GranularMetadataTransformation] as
* [MetadataDependencyResolution.ExcludeAsUnrequested], and uses exactly the same versions as were resolved for the requested
* dependencies during the transformation. */
private fun GranularMetadataTransformation.applyToConfiguration(configuration: Configuration) {
// Run this action immediately before the configuration first takes part in dependency resolution:
configuration.withDependencies {
val (unrequested, requested) = metadataDependencyResolutions
.partition { it is MetadataDependencyResolution.ExcludeAsUnrequested }
unrequested.forEach { configuration.exclude(mapOf("group" to it.dependency.moduleGroup, "module" to it.dependency.moduleName)) }
requested.forEach {
val notation = listOf(it.dependency.moduleGroup, it.dependency.moduleName, it.dependency.moduleVersion).joinToString(":")
configuration.resolutionStrategy.force(notation)
}
}
}
private fun createTransformedMetadataClasspath(
fromFiles: Iterable<File>,
project: Project,
transformationTaskHolders: Set<TaskProvider<TransformKotlinGranularMetadata>>
): FileCollection {
return project.files(Callable {
val allResolutionsByArtifactFile: Map<File, Iterable<MetadataDependencyResolution>> =
mutableMapOf<File, MutableList<MetadataDependencyResolution>>().apply {
transformationTaskHolders.forEach {
val resolutions = it.get().metadataDependencyResolutions
resolutions.forEach { resolution ->
val artifacts = resolution.dependency.moduleArtifacts.map { it.file }
artifacts.forEach { artifactFile ->
getOrPut(artifactFile) { mutableListOf() }.add(resolution)
}
}
}
}
val transformedFilesByResolution: Map<MetadataDependencyResolution, FileCollection> =
transformationTaskHolders.flatMap { it.get().filesByResolution.toList() }.toMap()
mutableSetOf<Any /* File | FileCollection */>().apply {
fromFiles.forEach { file ->
val resolutions = allResolutionsByArtifactFile[file]
if (resolutions == null) {
add(file)
} else {
val chooseVisibleSourceSets =
resolutions.filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>()
if (chooseVisibleSourceSets.isNotEmpty()) {
add(chooseVisibleSourceSets.map { transformedFilesByResolution.getValue(it) })
} else if (resolutions.any { it is MetadataDependencyResolution.KeepOriginalDependency }) {
add(file)
} // else: all dependency transformations exclude this dependency as unrequested; don't add any files
}
}
}
}).builtBy(transformationTaskHolders)
}
private fun getPublishedCommonSourceSets(project: Project): Set<KotlinSourceSet> {
val compilationsBySourceSet: Map<KotlinSourceSet, Set<KotlinCompilation<*>>> =
CompilationSourceSetUtil.compilationsBySourceSets(project)
// For now, we will only compile metadata from source sets used by multiple platforms
// TODO once the compiler is able to analyze common code with platform-specific features and dependencies, lift this restriction
val sourceSetsUsedInMultipleTargets = compilationsBySourceSet.filterValues { compilations ->
compilations.map { it.target.platformType }.distinct().size > 1
}
// We don't want to publish source set metadata from source sets that don't participate in any compilation that is published,
// such as test or benchmark sources; find all published compilations:
val publishedCompilations = getPublishedPlatformCompilations(project).values
return sourceSetsUsedInMultipleTargets
.filterValues { compilations -> compilations.any { it in publishedCompilations } }
.keys
}
private fun Project.createGenerateProjectStructureMetadataTask(): TaskProvider<GenerateProjectStructureMetadata> =
project.registerTask("generateProjectStructureMetadata") { task ->
task.lazyKotlinProjectStructureMetadata = lazy { checkNotNull(buildKotlinProjectStructureMetadata(project)) }
}
}
internal fun getPublishedPlatformCompilations(project: Project): Map<KotlinUsageContext, KotlinCompilation<*>> {
val result = mutableMapOf<KotlinUsageContext, KotlinCompilation<*>>()
project.multiplatformExtension.targets.withType(AbstractKotlinTarget::class.java).forEach { target ->
if (target.platformType == KotlinPlatformType.common)
return@forEach
target.kotlinComponents
.filterIsInstance<SoftwareComponentInternal>()
.forEach { component ->
component.usages
.filterIsInstance<KotlinUsageContext>()
.forEach { usage -> result[usage] = usage.compilation }
}
}
return result
}