@@ -9,14 +9,13 @@ import org.gradle.api.NamedDomainObjectContainer
9
9
import org.gradle.api.Project
10
10
import org.gradle.api.tasks.Exec
11
11
import org.gradle.internal.extensions.stdlib.capitalized
12
- import org.gradle.kotlin.dsl.extra
13
- import org.gradle.kotlin.dsl.named
14
- import org.gradle.kotlin.dsl.provideDelegate
15
- import org.gradle.kotlin.dsl.register
12
+ import org.gradle.kotlin.dsl.*
16
13
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
17
14
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultCInteropSettings
18
15
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
19
16
import org.jetbrains.kotlin.gradle.tasks.CInteropProcess
17
+ import org.jetbrains.kotlin.konan.target.Family
18
+ import org.jetbrains.kotlin.konan.target.KonanTarget
20
19
import java.io.File
21
20
22
21
// works with the cinterop-c Bazel project
@@ -42,30 +41,87 @@ fun KotlinMultiplatformExtension.configureCLibCInterop(
42
41
}
43
42
}
44
43
45
- val buildCinteropCLib = project.tasks.register<Exec >(" buildCinteropCLib" ) {
46
- group = " build"
47
- workingDir = cinteropCLib
48
- commandLine(" bash" , " -c" , " ${cinteropCLib} /build_all_targets.sh $bazelTask out" )
49
- inputs.files(project.fileTree(cinteropCLib) { exclude(" bazel-*/**" , " out/**" ) })
50
- outputs.dir(cinteropCLib.resolve(" out" ))
44
+ targets.withType<KotlinNativeTarget >().configureEach {
45
+ val buildTargetName = bazelTask.split(" :" ).last()
46
+ // bazel library build task
47
+ val taskName = " buildCLib${buildTargetName.capitalized()} _$targetName "
48
+ val buildCinteropCLib = project.tasks.register<Exec >(taskName) {
49
+ val platform = bazelPlatformName
50
+ val os = bazelOsName
51
51
52
- dependsOn(checkBazel)
53
- }
52
+ // the name used for the static library files (e.g. iosSimulatorArm64 -> ios_simulator_arm64)
53
+ val platformShortName = konanTarget.visibleName
54
+ val fileName = " lib$buildTargetName .$platformShortName .a"
55
+ val outFile = cinteropCLib.resolve(" out" ).resolve(fileName)
56
+
57
+ group = " build"
58
+ workingDir = cinteropCLib
59
+ commandLine(" bash" , " -c" , " ./build_target.sh $bazelTask $outFile $platform $os " )
60
+ inputs.files(project.fileTree(cinteropCLib) { exclude(" bazel-*/**" , " out/**" ) })
61
+ outputs.files(outFile)
54
62
55
- targets.filterIsInstance<KotlinNativeTarget >().forEach {
56
- it.compilations.getByName(" main" ) {
63
+ dependsOn(checkBazel)
64
+ }
65
+
66
+ // cinterop klib build config
67
+ compilations.getByName(" main" ) {
57
68
cinterops {
58
69
configureCinterop(cinteropCLib)
59
70
}
60
71
61
72
cinterops.all {
62
73
val interop = this
63
74
64
- val interopTask = " cinterop${interop.name.capitalized()}${it .targetName.capitalized()} "
75
+ val interopTask = " cinterop${interop.name.capitalized()}${this @configureEach .targetName.capitalized()} "
65
76
project.tasks.named(interopTask, CInteropProcess ::class ) {
66
77
dependsOn(buildCinteropCLib)
67
78
}
68
79
}
69
80
}
70
81
}
71
82
}
83
+
84
+ /* *
85
+ * Returns the Bazel platform name for the given [KotlinNativeTarget].
86
+ *
87
+ * For Apple targets, compare the following two lists:
88
+ * - https://kotlinlang.org/docs/native-target-support.html
89
+ * - https://github.com/bazelbuild/apple_support/blob/master/configs/platforms.bzl
90
+ */
91
+ private val KotlinNativeTarget .bazelPlatformName: String
92
+ get() {
93
+ val appleSupport = " @build_bazel_apple_support//platforms"
94
+ return when (konanTarget) {
95
+ KonanTarget .MACOS_ARM64 -> " $appleSupport :macos_arm64"
96
+ KonanTarget .MACOS_X64 -> " $appleSupport :macos_x86_64"
97
+ KonanTarget .IOS_ARM64 -> " $appleSupport :ios_arm64"
98
+ KonanTarget .IOS_SIMULATOR_ARM64 -> " $appleSupport :ios_sim_arm64"
99
+ KonanTarget .IOS_X64 -> " $appleSupport :ios_x86_64"
100
+ KonanTarget .WATCHOS_ARM32 -> " $appleSupport :watchos_armv7k"
101
+ // WATCHOS_ARM64 is the "older" arm64_32 target, not arm64 (which is WATCH_DEVICE_ARM64)
102
+ KonanTarget .WATCHOS_ARM64 -> " $appleSupport :watchos_arm64_32"
103
+ KonanTarget .WATCHOS_DEVICE_ARM64 -> " $appleSupport :watchos_device_arm64"
104
+ KonanTarget .WATCHOS_SIMULATOR_ARM64 -> " $appleSupport :watchos_arm64"
105
+ KonanTarget .WATCHOS_X64 -> " $appleSupport :watchos_x86_64"
106
+ KonanTarget .TVOS_ARM64 -> " $appleSupport :tvos_arm64"
107
+ KonanTarget .TVOS_SIMULATOR_ARM64 -> " $appleSupport :tvos_sim_arm64"
108
+ KonanTarget .TVOS_X64 -> " $appleSupport :tvos_x86_64"
109
+ KonanTarget .LINUX_ARM32_HFP -> TODO ()
110
+ KonanTarget .LINUX_ARM64 -> TODO ()
111
+ KonanTarget .LINUX_X64 -> TODO ()
112
+ KonanTarget .ANDROID_ARM32 -> TODO ()
113
+ KonanTarget .ANDROID_ARM64 -> TODO ()
114
+ KonanTarget .ANDROID_X64 -> TODO ()
115
+ KonanTarget .ANDROID_X86 -> TODO ()
116
+ KonanTarget .MINGW_X64 -> TODO ()
117
+ }
118
+ }
119
+
120
+ private val KotlinNativeTarget .bazelOsName
121
+ get() = when (konanTarget.family) {
122
+ Family .OSX -> " macos"
123
+ Family .IOS -> " ios"
124
+ Family .TVOS -> " tvos"
125
+ Family .WATCHOS -> " watchos"
126
+ else -> TODO ()
127
+ }
0 commit comments