1
+ /*
2
+ * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4
+ */
5
+
6
+ package org.jetbrains.kotlin.gradle.plugin.mpp.uklibs
7
+
8
+ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
9
+ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
10
+ import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
11
+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
12
+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
13
+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
14
+ import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
15
+ import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
16
+ import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
17
+
18
+ /* *
19
+ * This sealed class is intended to help in the mapping between KotlinTarget and the Uklib attribute that will be recorded in the Umanifest
20
+ */
21
+ internal sealed class UklibFragmentPlatformAttribute {
22
+ /* *
23
+ * Jvm, native and JS targets are published
24
+ */
25
+ data class ConsumeInPlatformAndMetadataCompilationsAndPublishInUmanifest (val attribute : String ) : UklibFragmentPlatformAttribute()
26
+
27
+ /* *
28
+ * Android target consumption is supported; however in platform compile dependency configuration we must resolve aar and the platform
29
+ * attribute should only be used during GMT and in publication of an Umanifest
30
+ */
31
+ data class ConsumeInMetadataCompilationsAndPublishInUmanifest (val attribute : String ) : UklibFragmentPlatformAttribute()
32
+
33
+ /* *
34
+ * External target is not currently supported. It is not ever published in Umanifest, but we must use some unique attribute during GMT
35
+ * to produce a correct metadata classpath.
36
+ */
37
+ data class ConsumeInMetadataCompilationsAndFailOnPublication (val unsupportedTargetName : String ) : UklibFragmentPlatformAttribute()
38
+
39
+ /* *
40
+ * This case should only be used for metadata target
41
+ */
42
+ data class FailOnConsumptionAndPublication (val metadataTarget : KotlinMetadataTarget ) : UklibFragmentPlatformAttribute()
43
+
44
+ fun convertToStringForPublicationInUmanifest (): String = when (this ) {
45
+ is ConsumeInPlatformAndMetadataCompilationsAndPublishInUmanifest -> attribute
46
+ is ConsumeInMetadataCompilationsAndPublishInUmanifest -> attribute
47
+ is ConsumeInMetadataCompilationsAndFailOnPublication -> error(" Publication with ${unsupportedTargetName} is not supported" )
48
+ is FailOnConsumptionAndPublication -> error(" ${metadataTarget} doesn't have a platform attribute for publication" )
49
+ }
50
+
51
+ /* *
52
+ * Convert the attribute for consumption in transforms or GMT
53
+ */
54
+ fun convertToStringForConsumption (): String = when (this ) {
55
+ is ConsumeInPlatformAndMetadataCompilationsAndPublishInUmanifest -> attribute
56
+ is ConsumeInMetadataCompilationsAndPublishInUmanifest -> attribute
57
+ is ConsumeInMetadataCompilationsAndFailOnPublication -> unsupportedTargetName
58
+ is FailOnConsumptionAndPublication -> error(" ${metadataTarget} doesn't have a platform attribute for consumption" )
59
+ }
60
+ }
61
+
62
+ /* *
63
+ * This is the per-target attribute that will be recorded within the fragment of the umanifest
64
+ *
65
+ * When resolving the uklib we will use this attribute
66
+ * - In the transform for platform compilation
67
+ * - In GMT for metadata classpath formation
68
+ */
69
+ internal val KotlinCompilation <* >.uklibFragmentPlatformAttribute: UklibFragmentPlatformAttribute
70
+ get() = this .target.uklibFragmentPlatformAttribute
71
+ internal val KotlinTarget .uklibFragmentPlatformAttribute: UklibFragmentPlatformAttribute
72
+ get() {
73
+ if (this is KotlinMetadataTarget ) {
74
+ return UklibFragmentPlatformAttribute .FailOnConsumptionAndPublication (this )
75
+ }
76
+
77
+ /* *
78
+ * FIXME: Android configurations currently do not resolve consistently with the targets below
79
+ * FIXME: Request jvm transform in Android?
80
+ */
81
+ if (this is KotlinAndroidTarget ) {
82
+ return UklibFragmentPlatformAttribute .ConsumeInMetadataCompilationsAndPublishInUmanifest (
83
+ UklibTargetFragmentAttribute .android.name
84
+ )
85
+ }
86
+
87
+ val supportedUklibTarget = when (this ) {
88
+ is KotlinNativeTarget -> konanTarget.name
89
+ is KotlinJsIrTarget -> when (platformType) {
90
+ KotlinPlatformType .js -> UklibTargetFragmentAttribute .js_ir.name
91
+ KotlinPlatformType .wasm -> when (wasmTargetType ? : error(" ${KotlinJsIrTarget ::class } missing wasm type in wasm platform " )) {
92
+ KotlinWasmTargetType .JS -> UklibTargetFragmentAttribute .wasm_js.name
93
+ KotlinWasmTargetType .WASI -> UklibTargetFragmentAttribute .wasm_wasi.name
94
+ }
95
+ else -> error(" ${KotlinJsIrTarget ::class } unexpected platform type $platformType " )
96
+ }
97
+ is KotlinJvmTarget -> UklibTargetFragmentAttribute .jvm.name
98
+ else -> null
99
+ }
100
+ if (supportedUklibTarget != null ) {
101
+ return UklibFragmentPlatformAttribute .ConsumeInPlatformAndMetadataCompilationsAndPublishInUmanifest (supportedUklibTarget)
102
+ }
103
+
104
+ return UklibFragmentPlatformAttribute .ConsumeInMetadataCompilationsAndFailOnPublication (targetName)
105
+ }
106
+
107
+ /* *
108
+ * These attribute names will be recorded in and resolved from the Umanifest
109
+ */
110
+ private enum class UklibTargetFragmentAttribute {
111
+ js_ir,
112
+ wasm_js,
113
+ wasm_wasi,
114
+ jvm,
115
+ android;
116
+ }
0 commit comments