@@ -7,15 +7,19 @@ import org.gradle.api.plugins.ExtensionAware
77import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
88import org.jetbrains.kotlin.gradle.plugin.cocoapods.CocoapodsExtension
99import org.jetbrains.kotlin.gradle.plugin.cocoapods.KotlinCocoapodsPlugin
10+ import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
11+ import org.jetbrains.kotlin.gradle.utils.named
1012import org.jetbrains.kotlin.konan.target.HostManager
1113import org.slf4j.LoggerFactory
14+ import java.util.Locale
1215
1316internal const val SENTRY_EXTENSION_NAME = " sentryKmp"
1417internal const val LINKER_EXTENSION_NAME = " linker"
1518internal const val AUTO_INSTALL_EXTENSION_NAME = " autoInstall"
1619internal const val COCOAPODS_AUTO_INSTALL_EXTENSION_NAME = " cocoapods"
1720internal const val COMMON_MAIN_AUTO_INSTALL_EXTENSION_NAME = " commonMain"
1821internal const val KOTLIN_EXTENSION_NAME = " kotlin"
22+ internal const val LINK_SENTRY_COCOA_FRAMEWORKS_TASK_NAME = " linkSentryCocoaFrameworks"
1923
2024@Suppress(" unused" )
2125class SentryPlugin : Plugin <Project > {
@@ -43,9 +47,13 @@ class SentryPlugin : Plugin<Project> {
4347 }
4448 }
4549
46- internal fun executeConfiguration (project : Project , hostIsMac : Boolean = HostManager .hostIsMac) {
50+ internal fun executeConfiguration (
51+ project : Project ,
52+ hostIsMac : Boolean = HostManager .hostIsMac
53+ ) {
4754 val sentryExtension = project.extensions.getByType(SentryExtension ::class .java)
48- val hasCocoapodsPlugin = project.plugins.findPlugin(KotlinCocoapodsPlugin ::class .java) != null
55+ val hasCocoapodsPlugin =
56+ project.plugins.findPlugin(KotlinCocoapodsPlugin ::class .java) != null
4957
5058 if (sentryExtension.autoInstall.enabled.get()) {
5159 val autoInstall = sentryExtension.autoInstall
@@ -65,8 +73,9 @@ class SentryPlugin : Plugin<Project> {
6573 // executing the (potentially expensive) path-resolution logic when the build is only
6674 // concerned with non-Apple targets such as Android.
6775
68- val kmpExtension = project.extensions.findByName(KOTLIN_EXTENSION_NAME ) as ? KotlinMultiplatformExtension
69- ? : throw GradleException (" Error fetching Kotlin Multiplatform extension." )
76+ val kmpExtension =
77+ project.extensions.findByName(KOTLIN_EXTENSION_NAME ) as ? KotlinMultiplatformExtension
78+ ? : throw GradleException (" Error fetching Kotlin Multiplatform extension." )
7079
7180 val appleTargets = kmpExtension.appleTargets().toList()
7281
@@ -75,30 +84,37 @@ class SentryPlugin : Plugin<Project> {
7584 return
7685 }
7786
78- project.gradle.taskGraph.whenReady { taskGraph ->
79- val targetsInGraph = appleTargets.filter { target ->
80- taskGraph.allTasks.any { task -> task.path.contains(target.name, ignoreCase = true ) }
87+ project.gradle.taskGraph.whenReady { graph ->
88+ // Check which of the Kotlin/Native targets are actually in the graph
89+ val activeTargets = appleTargets.filter { target ->
90+ val targetName = target.name.replaceFirstChar {
91+ it.uppercase()
92+ }
93+ val path = if (project.path == " :" )
94+ " :compileKotlin$targetName "
95+ else
96+ " ${project.path} :compileKotlin$targetName "
97+ // Will throw if it doesn't exist
98+ graph.hasTask(path)
8199 }
82100
83- if (targetsInGraph .isEmpty()) {
84- project.logger.info (" No Apple target tasks requested – skipping Sentry Cocoa framework linking. " )
101+ if (activeTargets .isEmpty()) {
102+ project.logger.lifecycle (" No Apple compile task scheduled for this build - skipping Sentry Cocoa framework linking" )
85103 return @whenReady
86104 }
87105
88- project.logger.info(
89- " Configuring Sentry Cocoa framework linking for Apple targets present in the task graph: " +
90- targetsInGraph.joinToString { it.name }
91- )
106+ project.logger.lifecycle(" Set up Sentry Cocoa linking for target: ${activeTargets.first().name} " )
92107
93108 CocoaFrameworkLinker (
94- logger = project.logger,
95- pathResolver = FrameworkPathResolver (project),
96- binaryLinker = FrameworkLinker (project.logger)
97- ).configure(targetsInGraph )
109+ logger = project.logger,
110+ pathResolver = FrameworkPathResolver (project),
111+ binaryLinker = FrameworkLinker (project.logger)
112+ ).configure(appleTargets = activeTargets )
98113 }
99114 }
100115 }
101116
117+
102118 companion object {
103119 internal val logger by lazy {
104120 LoggerFactory .getLogger(SentryPlugin ::class .java)
@@ -120,9 +136,9 @@ internal fun Project.installSentryForKmp(
120136 if (unsupportedTargets.any { unsupported -> target.name.contains(unsupported) }) {
121137 throw GradleException (
122138 " Unsupported target: ${target.name} . " +
123- " Cannot auto install in commonMain. " +
124- " Please create an intermediate sourceSet with targets that the Sentry SDK " +
125- " supports (apple, jvm, android) and add the dependency manually."
139+ " Cannot auto install in commonMain. " +
140+ " Please create an intermediate sourceSet with targets that the Sentry SDK " +
141+ " supports (apple, jvm, android) and add the dependency manually."
126142 )
127143 }
128144 }
0 commit comments