@@ -27,9 +27,9 @@ fun Project.configureNativeMultiplatform() {
27
27
}
28
28
29
29
val extension: Any = if (ideaActive)
30
- NativeIdeaInfraExtension (subproject, kotlin)
30
+ NativeIdeaInfraExtension (subproject, kotlin, " native " )
31
31
else
32
- NativeBuildInfraExtension (subproject, kotlin)
32
+ NativeBuildInfraExtension (subproject, kotlin, " native " )
33
33
34
34
(kotlin as ExtensionAware ).extensions.add(" infra" , extension)
35
35
}
@@ -38,8 +38,12 @@ fun Project.configureNativeMultiplatform() {
38
38
39
39
abstract class NativeInfraExtension (
40
40
protected val project : Project ,
41
- protected val kotlin : KotlinMultiplatformExtension
41
+ protected val kotlin : KotlinMultiplatformExtension ,
42
+ protected val sourceSetName : String
42
43
) {
44
+ protected val mainSourceSet = kotlin.sourceSets.maybeCreate(" ${sourceSetName} Main" )
45
+ protected val testSourceSet = kotlin.sourceSets.maybeCreate(" ${sourceSetName} Test" )
46
+
43
47
protected val sharedConfigs = mutableListOf<KotlinNativeTarget .() - > Unit > ()
44
48
fun shared (configure : Closure <* >) = shared { ConfigureUtil .configure(configure, this ) }
45
49
fun shared (configure : KotlinNativeTarget .() -> Unit ) {
@@ -49,10 +53,13 @@ abstract class NativeInfraExtension(
49
53
fun target (name : String ) = target(name) { }
50
54
fun target (name : String , configure : Closure <* >) = target(name) { ConfigureUtil .configure(configure, this ) }
51
55
abstract fun target (name : String , configure : KotlinNativeTarget .() -> Unit )
56
+
57
+ fun common (name : String , configure : Closure <* >) = common(name) { ConfigureUtil .configure(configure, this ) }
58
+ abstract fun common (name : String , configure : NativeInfraExtension .() -> Unit )
52
59
}
53
60
54
- class NativeIdeaInfraExtension (project : Project , kotlin : KotlinMultiplatformExtension ) :
55
- NativeInfraExtension (project, kotlin) {
61
+ class NativeIdeaInfraExtension (project : Project , kotlin : KotlinMultiplatformExtension , sourceSetName : String ) :
62
+ NativeInfraExtension (project, kotlin, sourceSetName ) {
56
63
57
64
private val hostManager = createHostManager()
58
65
@@ -74,48 +81,59 @@ class NativeIdeaInfraExtension(project: Project, kotlin: KotlinMultiplatformExte
74
81
if (name != hostPreset.name)
75
82
return
76
83
77
- kotlin.targetFromPreset(hostPreset, " native " ) {
84
+ kotlin.targetFromPreset(hostPreset, sourceSetName ) {
78
85
configure()
79
86
sharedConfigs.forEach { it() }
80
87
}
81
88
82
89
project.afterEvaluate {
83
- kotlin.sourceSets.getByName(" nativeMain " ) { sourceSet ->
90
+ kotlin.sourceSets.getByName(" ${sourceSetName} Main " ) { sourceSet ->
84
91
sourceSet.kotlin.srcDir(" ${hostPreset.name} Main/src" )
85
92
}
86
93
}
87
94
}
95
+
96
+ override fun common (name : String , configure : NativeInfraExtension .() -> Unit ) {
97
+ kotlin.sourceSets.create(" ${name} Main" ).dependsOn(mainSourceSet)
98
+ kotlin.sourceSets.create(" ${name} Test" ).dependsOn(testSourceSet)
99
+ val extension = NativeIdeaInfraExtension (project, kotlin, name)
100
+ extension.configure()
101
+ }
88
102
}
89
103
90
- class NativeBuildInfraExtension (project : Project , kotlin : KotlinMultiplatformExtension ) :
91
- NativeInfraExtension (project, kotlin) {
104
+ class NativeBuildInfraExtension (project : Project , kotlin : KotlinMultiplatformExtension , sourceSetName : String ) :
105
+ NativeInfraExtension (project, kotlin, sourceSetName ) {
92
106
93
107
private val nativePresets = kotlin.presets.filterIsInstance<AbstractKotlinNativeTargetPreset <* >>()
94
108
95
- private val nativeMain = kotlin.sourceSets.create(" nativeMain" )
96
- private val nativeTest = kotlin.sourceSets.create(" nativeTest" )
97
-
98
109
init {
99
110
project.logger.infra(" Configuring native targets for $project for build" )
100
111
project.logger.infra(" Enabled native targets: ${nativePresets.joinToString { it.name }} " )
101
112
}
102
113
103
114
override fun target (name : String , configure : KotlinNativeTarget .() -> Unit ) {
104
115
val preset = nativePresets.singleOrNull { it.name == name } ? : return
105
- project.logger.infra(" Creating target '${preset.name} ' with dependency on 'native '" )
116
+ project.logger.infra(" Creating target '${preset.name} ' with dependency on '$sourceSetName '" )
106
117
107
118
val target = kotlin.targetFromPreset(preset) {
108
119
configure()
109
120
sharedConfigs.forEach { config -> config() }
110
121
}
111
122
112
123
kotlin.sourceSets.getByName(" ${preset.name} Main" ) { sourceSet ->
113
- sourceSet.dependsOn(nativeMain )
124
+ sourceSet.dependsOn(mainSourceSet )
114
125
}
115
126
kotlin.sourceSets.getByName(" ${preset.name} Test" ) { sourceSet ->
116
- sourceSet.dependsOn(nativeTest )
127
+ sourceSet.dependsOn(testSourceSet )
117
128
}
118
129
}
130
+
131
+ override fun common (name : String , configure : NativeInfraExtension .() -> Unit ) {
132
+ kotlin.sourceSets.create(" ${name} Main" ).dependsOn(mainSourceSet)
133
+ kotlin.sourceSets.create(" ${name} Test" ).dependsOn(testSourceSet)
134
+ val extension = NativeBuildInfraExtension (project, kotlin, name)
135
+ extension.configure()
136
+ }
119
137
}
120
138
121
139
private fun createHostManager (): HostManager {
0 commit comments