Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Enhancements

- Gradle Plugin: implement conditional Cocoa linking for targets ([#421](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/421))

## 0.14.0

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
testImplementation(libs.junit)
testImplementation(libs.junit.params)
testImplementation(libs.mockk)
testImplementation(libs.truth)
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover"}
junit = "org.junit.jupiter:junit-jupiter-api:5.10.3"
junit-params = "org.junit.jupiter:junit-jupiter-params:5.10.3"
mockk = "io.mockk:mockk:1.13.12"
truth = { module = "com.google.truth:truth", version = "1.4.4" }
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
}
}

internal fun executeConfiguration(project: Project, hostIsMac: Boolean = HostManager.hostIsMac) {
internal fun executeConfiguration(
project: Project,
hostIsMac: Boolean = HostManager.hostIsMac
) {
val sentryExtension = project.extensions.getByType(SentryExtension::class.java)
val hasCocoapodsPlugin = project.plugins.findPlugin(KotlinCocoapodsPlugin::class.java) != null
val hasCocoapodsPlugin =
project.plugins.findPlugin(KotlinCocoapodsPlugin::class.java) != null

if (sentryExtension.autoInstall.enabled.get()) {
val autoInstall = sentryExtension.autoInstall
Expand All @@ -60,17 +64,61 @@
}

if (hostIsMac && !hasCocoapodsPlugin) {
project.logger.info("Cocoapods plugin not found. Attempting to link Sentry Cocoa framework.")
// Register a task graph listener so that we only configure Cocoa framework linking
// if at least one Apple target task is part of the requested task graph. This avoids
// executing the (potentially expensive) path-resolution logic when the build is only
// concerned with non-Apple targets such as Android.

val kmpExtension = project.extensions.findByName(KOTLIN_EXTENSION_NAME) as? KotlinMultiplatformExtension
val appleTargets = kmpExtension?.appleTargets()?.toList()
?: throw GradleException("Error fetching Apple targets from Kotlin Multiplatform plugin.")
val kmpExtension =

Check warning on line 72 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L72

Added line #L72 was not covered by tests
project.extensions.findByName(KOTLIN_EXTENSION_NAME) as? KotlinMultiplatformExtension
?: throw GradleException("Error fetching Kotlin Multiplatform extension.")

Check warning on line 74 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L74

Added line #L74 was not covered by tests

CocoaFrameworkLinker(
logger = project.logger,
pathResolver = FrameworkPathResolver(project),
binaryLinker = FrameworkLinker(project.logger)
).configure(appleTargets)
val appleTargets = kmpExtension.appleTargets().toList()

Check warning on line 76 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L76

Added line #L76 was not covered by tests

if (appleTargets.isEmpty()) {
project.logger.info("No Apple targets detected – skipping Sentry Cocoa framework linking setup.")

Check warning on line 79 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L79

Added line #L79 was not covered by tests
return
}

project.gradle.taskGraph.whenReady { graph ->

Check warning on line 83 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L83

Added line #L83 was not covered by tests
// Check which of the Kotlin/Native targets are actually in the graph
val activeTargets = appleTargets.filter { target ->

Check warning on line 85 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L85

Added line #L85 was not covered by tests
val targetName = target.name.replaceFirstChar {
it.uppercase()

Check warning on line 87 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L87

Added line #L87 was not covered by tests
}
val path = if (project.path == ":") {
":compileKotlin$targetName"

Check warning on line 90 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L90

Added line #L90 was not covered by tests
} else {
"${project.path}:compileKotlin$targetName"

Check warning on line 92 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L92

Added line #L92 was not covered by tests
}
// Will throw if it doesn't exist
graph.hasTask(path)

Check warning on line 95 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L95

Added line #L95 was not covered by tests
}

if (activeTargets.isEmpty()) {
project.logger.lifecycle(
"No Apple compile task scheduled for this build " +

Check warning on line 100 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L99-L100

Added lines #L99 - L100 were not covered by tests
"- skipping Sentry Cocoa framework linking"
)
return@whenReady
}

if (activeTargets.size > 1) {
project.logger.warn(

Check warning on line 107 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L107

Added line #L107 was not covered by tests
"Cannot set up Sentry Cocoa linking: " +
"expected exactly one Apple target but found ${activeTargets.size} ($activeTargets)."

Check warning on line 109 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L109

Added line #L109 was not covered by tests
)
return@whenReady
}

project.logger.lifecycle("Set up Sentry Cocoa linking for target: ${activeTargets.first().name}")

Check warning on line 114 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L114

Added line #L114 was not covered by tests

CocoaFrameworkLinker(
logger = project.logger,
pathResolver = FrameworkPathResolver(project),
binaryLinker = FrameworkLinker(project.logger)
).configure(appleTargets = activeTargets)

Check warning on line 120 in sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform-gradle-plugin/src/main/java/io/sentry/kotlin/multiplatform/gradle/SentryPlugin.kt#L116-L120

Added lines #L116 - L120 were not covered by tests
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package io.sentry.kotlin.multiplatform.gradle

import com.google.common.truth.Truth.assertThat
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
import org.gradle.testkit.runner.internal.io.SynchronizedOutputStream
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS
import org.junit.jupiter.api.io.TempDir
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.OutputStreamWriter

@EnabledOnOs(OS.MAC)
class CocoaFrameworkLinkerIntegrationTest {
/**
* Verifies that the Cocoa linker is **not** configured when the task graph
* contains only non-Apple targets.
*/
@Test
fun `linker is not configured when only non-Apple tasks are requested`(@TempDir projectDir: File) {
writeBuildFiles(projectDir)

val output = ByteArrayOutputStream()
defaultRunner(projectDir, output)
.withArguments("compileKotlinJvm", "--dry-run", "--info")
.build()

assertThat(output.toString())
.contains("No Apple compile task scheduled for this build - skipping Sentry Cocoa framework linking")
}

/**
* Verifies that the Cocoa linker **is** configured when at least one Apple
* task is present in the task graph.
*/
@Test
fun `linker is configured when an Apple task is requested`(@TempDir projectDir: File) {
writeBuildFiles(projectDir)

val output = ByteArrayOutputStream()
defaultRunner(projectDir, output)
.withArguments("compileKotlinIosSimulatorArm64", "--dry-run", "--info")
.build()

assertThat(output.toString())
.contains("Set up Sentry Cocoa linking for target: iosSimulatorArm64")
assertThat(output.toString())
.contains("Start resolving Sentry Cocoa framework paths for target: iosSimulatorArm64")
}

// ---------------------------------------------------------------------
// test-fixture helpers
// ---------------------------------------------------------------------

private fun writeBuildFiles(dir: File) {
// -----------------------------------------------------------------
// Create a fake XCFramework on disk so that the CustomPathStrategy
// can resolve a valid framework path even on CI machines where SPM
// (and hence DerivedData) is not available.
// -----------------------------------------------------------------
val fakeFrameworkDir = File(dir, "Sentry-Dynamic.xcframework").apply {
// Create minimal structure that satisfies path validation logic
val archDirName = "ios-arm64_x86_64-simulator" // architecture used for iosSimulatorArm64
val archDir = File(this, archDirName)
archDir.mkdirs()
}

File(dir, "settings.gradle").writeText("""rootProject.name = "fixture"""")

val pluginClasspath = PluginUnderTestMetadataReading
.readImplementationClasspath()
.joinToString(", ") { "\"${it.absolutePath.replace('\\', '/')}\"" }

File(dir, "build.gradle").writeText(
"""
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
classpath files($pluginClasspath)
}
}

apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'io.sentry.kotlin.multiplatform.gradle'

repositories {
google()
mavenCentral()
}

kotlin {
jvm() // non-Apple target
iosSimulatorArm64() // Apple target used in tests
}

// -----------------------------------------------------------------
// Configure the plugin to use the fake framework path created above.
// This makes the CustomPathStrategy succeed immediately, bypassing the
// DerivedData and manual search strategies that rely on an SPM setup.
// -----------------------------------------------------------------
sentryKmp {
linker {
frameworkPath.set("${fakeFrameworkDir.absolutePath.replace('\\', '/')}")
}
}
""".trimIndent()
)
}

/** Returns a pre-configured [GradleRunner] that logs into [out]. */
private fun defaultRunner(projectDir: File, out: ByteArrayOutputStream): GradleRunner =
GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.withGradleVersion(org.gradle.util.GradleVersion.current().version)
.forwardStdOutput(OutputStreamWriter(SynchronizedOutputStream(out)))
.forwardStdError(OutputStreamWriter(SynchronizedOutputStream(out)))
.withArguments("--stacktrace")

private companion object {
private const val KOTLIN_VERSION = "2.1.21"
}
}
Loading