Skip to content

Commit

Permalink
Fix Gradle Configuration Cache support.
Browse files Browse the repository at this point in the history
  • Loading branch information
TadeasKriz committed Apr 16, 2024
1 parent 86ec7e5 commit 9d293f2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ internal abstract class CreateSkieConfigurationTask : DefaultTask() {

fun registerTask(target: SkieTarget) {
val createConfiguration = target.registerSkieTargetBasedTask<CreateSkieConfigurationTask>("createConfiguration") {
val skieExtension = project.skieExtension
configurationFile.set(target.skieBuildDirectory.skieConfiguration)
configuration.set(project.provider { project.skieExtension.buildConfiguration(target.outputKind) })
configuration.set(project.provider { skieExtension.buildConfiguration(target.outputKind) })

dependsOn(target.createSkieBuildDirectoryTask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
import org.jetbrains.kotlin.gradle.tasks.FrameworkLayout
import org.jetbrains.kotlin.konan.target.Architecture
import org.jetbrains.kotlin.konan.target.Family
import java.io.File

internal object FatFrameworkConfigurator {

fun configureSkieForFatFrameworks(project: Project) {
project.fixFatFrameworkNameForCocoaPodsPlugin()
project.configureFatFrameworkPatching()
fixFatFrameworkNameForCocoaPodsPlugin(project)
configureFatFrameworkPatching(project)
}

private fun Project.fixFatFrameworkNameForCocoaPodsPlugin() {
pluginManager.withPlugin("kotlin-native-cocoapods") {
tasks.withType<FatFrameworkTask>().matching { it.name == "fatFramework" }.configureEach {
private fun fixFatFrameworkNameForCocoaPodsPlugin(project: Project) {
project.pluginManager.withPlugin("kotlin-native-cocoapods") {
project.tasks.withType<FatFrameworkTask>().matching { it.name == "fatFramework" }.configureEach {
// Unfortunately has to be done in `doFirst` to make sure the task is already configured by the plugin when we run our code
doFirstOptimized {
val commonFrameworkName = frameworks.map { it.name }.distinct().singleOrNull() ?: return@doFirstOptimized
Expand All @@ -28,8 +26,9 @@ internal object FatFrameworkConfigurator {
}
}

private fun Project.configureFatFrameworkPatching() {
tasks.withType<FatFrameworkTask>().configureEach {
private fun configureFatFrameworkPatching(project: Project) {
val injectedFileSystemOperations = project.objects.newInstance<InjectedFileSystemOperations>()
project.tasks.withType<FatFrameworkTask>().configureEach {
doLastOptimized {
// There shouldn't be any real difference between the frameworks except for architecture, so we consider the first one "primary"
val primaryFramework = frameworks.first()
Expand All @@ -38,7 +37,7 @@ internal object FatFrameworkConfigurator {
isMacosFramework = primaryFramework.files.isMacosFramework,
)

// FatFramewrokTask writes its own
// FatFrameworkTask writes its own
target.moduleFile.writeText(
primaryFramework.files.moduleFile.readText()
)
Expand Down Expand Up @@ -74,12 +73,12 @@ internal object FatFrameworkConfigurator {
target.swiftModuleDir.mkdirs()

frameworksByArchs.toList().forEach { (_, framework) ->
project.copy {
injectedFileSystemOperations.fileSystemOperations.copy {
from(framework.files.apiNotes)
into(target.headerDir)
}
framework.files.swiftModuleFiles(framework.darwinTarget.targetTriple).forEach { swiftmoduleFile ->
project.copy {
injectedFileSystemOperations.fileSystemOperations.copy {
from(swiftmoduleFile)
into(target.swiftModuleDir)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ internal data class DarwinTarget(
DarwinTarget(KonanTarget.TVOS_SIMULATOR_ARM64, "arm64-apple-tvos-simulator", "appletvsimulator"),
DarwinTarget(KonanTarget.MACOS_X64, "x86_64-apple-macos", "macosx"),
DarwinTarget(KonanTarget.MACOS_ARM64, "arm64-apple-macos", "macosx"),
).associateBy { it.konanTarget }
).associateBy { it.konanTarget.name }
}
}

internal val FrameworkDescriptor.darwinTarget: DarwinTarget
get() = target.darwinTarget

internal val KonanTarget.darwinTarget: DarwinTarget
get() = DarwinTarget.allTargets[this] ?: error("Unknown konan target: $this")
// We can't use `KotlinTarget` directly as the instance can differ when Gradle Configuration Cache is used
get() = DarwinTarget.allTargets[name] ?: error("Unknown konan target: $this")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.touchlab.skie.plugin.util

import org.gradle.api.file.FileSystemOperations
import javax.inject.Inject

interface InjectedFileSystemOperations {
@get:Inject
val fileSystemOperations: FileSystemOperations
}
26 changes: 26 additions & 0 deletions test-runner/.run/Test Gradle Configuration Cache.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test Gradle Configuration Cache" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="-Pmatrix.linkModes=static,dynamic -Pmatrix.targets=macosArm64 -PtestLevel=smoke -Pmatrix.configurations=debug -PtestType=gradle" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value=":test" />
<option value="--tests" />
<option value="&quot;co.touchlab.skie.test.suite.gradle.configurationcache.GradleConfigurationCacheTests&quot;" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import co.touchlab.skie.test.annotation.filter.Smoke
import co.touchlab.skie.test.annotation.type.GradleTests
import co.touchlab.skie.test.base.BaseGradleTests
import co.touchlab.skie.test.runner.BuildConfiguration
import co.touchlab.skie.test.template.Templates
import co.touchlab.skie.test.util.KotlinTarget
import co.touchlab.skie.test.util.KotlinVersion
import co.touchlab.skie.test.util.LinkMode
Expand All @@ -21,9 +22,9 @@ import kotlin.test.assertEquals
class GradleConfigurationCacheTests: BaseGradleTests() {

// We don't need to run the whole build to check configuration cache
private val gradleArguments = arrayOf("dependencies")
private val gradleArguments = arrayOf("assemble")
private val assertGradleResult = { result: BuildResult ->
assertEquals(TaskOutcome.SUCCESS, result.task(":dependencies")?.outcome)
assertEquals(TaskOutcome.SUCCESS, result.task(":assemble")?.outcome)
}

@MatrixTest
Expand All @@ -36,6 +37,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() {
}
}

copyToCommonMain(Templates.basic)

runGradle(
arguments = gradleArguments,
assertResult = assertGradleResult,
Expand All @@ -60,6 +63,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() {
}
}

copyToCommonMain(Templates.basic)

runGradle(
arguments = gradleArguments,
assertResult = assertGradleResult,
Expand Down Expand Up @@ -89,6 +94,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() {
}
}

copyToCommonMain(Templates.basic)

runGradle(
arguments = gradleArguments,
assertResult = assertGradleResult,
Expand Down Expand Up @@ -116,6 +123,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() {
}
}

copyToCommonMain(Templates.basic)

runGradle(
arguments = gradleArguments,
assertResult = assertGradleResult,
Expand Down

0 comments on commit 9d293f2

Please sign in to comment.