|
| 1 | +import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink |
| 2 | +import java.io.File |
| 3 | +import kotlin.reflect.full.declaredMemberProperties |
| 4 | + |
| 5 | +plugins { |
| 6 | + kotlin("multiplatform") |
| 7 | + id("org.jetbrains.compose") |
| 8 | + id("dev.icerock.mobile.multiplatform-resources") |
| 9 | +} |
| 10 | + |
| 11 | +version = "0.1.0" |
| 12 | + |
| 13 | +kotlin { |
| 14 | + macosX64 { |
| 15 | + binaries { |
| 16 | + executable { |
| 17 | + entryPoint = "main" |
| 18 | + freeCompilerArgs += listOf( |
| 19 | + "-linker-option", "-framework", "-linker-option", "Metal" |
| 20 | + ) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + macosArm64 { |
| 26 | + binaries { |
| 27 | + executable { |
| 28 | + entryPoint = "main" |
| 29 | + freeCompilerArgs += listOf( |
| 30 | + "-linker-option", "-framework", "-linker-option", "Metal" |
| 31 | + ) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Suppress("UNUSED_VARIABLE") |
| 37 | + sourceSets { |
| 38 | + val commonMain by getting { |
| 39 | + dependencies { |
| 40 | + api(project(":shared")) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +compose.desktop.nativeApplication { |
| 47 | + targets(kotlin.targets.getByName("macosX64"), kotlin.targets.getByName("macosArm64")) |
| 48 | + distributions { |
| 49 | + targetFormats(org.jetbrains.compose.desktop.application.dsl.TargetFormat.Dmg) |
| 50 | + packageName = "dev.icerock.moko.resources.sample" |
| 51 | + packageVersion = "0.1.0" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +kotlin { |
| 56 | + targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> { |
| 57 | + binaries.all { |
| 58 | + // TODO: the current compose binary surprises LLVM, so disable checks for now. |
| 59 | + freeCompilerArgs += "-Xdisable-phases=VerifyBitcode" |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +multiplatformResources { |
| 65 | + multiplatformResourcesPackage = "dev.icerock.moko.resources.sample" |
| 66 | +} |
| 67 | + |
| 68 | +// TODO move to moko-resources gradle plugin |
| 69 | +// copy .bundle from all .klib to .kexe |
| 70 | +tasks.withType<KotlinNativeLink>() |
| 71 | + .configureEach { |
| 72 | + val linkTask: KotlinNativeLink = this |
| 73 | + val outputDir: File = this.outputFile.get().parentFile |
| 74 | + |
| 75 | + @Suppress("ObjectLiteralToLambda") // lambda broke up-to-date |
| 76 | + val action = object : Action<Task> { |
| 77 | + override fun execute(t: Task) { |
| 78 | + (linkTask.libraries + linkTask.sources) |
| 79 | + .filter { library -> library.extension == "klib" } |
| 80 | + .filter(File::exists) |
| 81 | + .forEach { inputFile -> |
| 82 | + val klibKonan = org.jetbrains.kotlin.konan.file.File(inputFile.path) |
| 83 | + val klib = org.jetbrains.kotlin.library.impl.KotlinLibraryLayoutImpl( |
| 84 | + klib = klibKonan, |
| 85 | + component = "default" |
| 86 | + ) |
| 87 | + val layout = klib.extractingToTemp |
| 88 | + |
| 89 | + // extracting bundles |
| 90 | + layout |
| 91 | + .resourcesDir |
| 92 | + .absolutePath |
| 93 | + .let(::File) |
| 94 | + .listFiles { file: File -> file.extension == "bundle" } |
| 95 | + // copying bundles to app |
| 96 | + ?.forEach { |
| 97 | + logger.info("${it.absolutePath} copying to $outputDir") |
| 98 | + it.copyRecursively( |
| 99 | + target = File(outputDir, it.name), |
| 100 | + overwrite = true |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + doLast(action) |
| 107 | + } |
| 108 | + |
| 109 | +// TODO move to moko-resources gradle plugin |
| 110 | +// copy .bundle from .kexe to .app |
| 111 | +tasks.withType<org.jetbrains.compose.experimental.uikit.tasks.ExperimentalPackComposeApplicationForXCodeTask>() |
| 112 | + .configureEach { |
| 113 | + val packTask: org.jetbrains.compose.experimental.uikit.tasks.ExperimentalPackComposeApplicationForXCodeTask = |
| 114 | + this |
| 115 | + |
| 116 | + val kclass = |
| 117 | + org.jetbrains.compose.experimental.uikit.tasks.ExperimentalPackComposeApplicationForXCodeTask::class |
| 118 | + val kotlinBinaryField = |
| 119 | + kclass.declaredMemberProperties.single { it.name == "kotlinBinary" } |
| 120 | + val destinationDirField = |
| 121 | + kclass.declaredMemberProperties.single { it.name == "destinationDir" } |
| 122 | + val executablePathField = |
| 123 | + kclass.declaredMemberProperties.single { it.name == "executablePath" } |
| 124 | + |
| 125 | + @Suppress("ObjectLiteralToLambda") // lambda broke up-to-date |
| 126 | + val action = object : Action<Task> { |
| 127 | + override fun execute(t: Task) { |
| 128 | + val kotlinBinary: RegularFile = |
| 129 | + (kotlinBinaryField.get(packTask) as RegularFileProperty).get() |
| 130 | + val destinationDir: Directory = |
| 131 | + (destinationDirField.get(packTask) as DirectoryProperty).get() |
| 132 | + val executablePath: String = |
| 133 | + (executablePathField.get(packTask) as Provider<*>).get().toString() |
| 134 | + |
| 135 | + val outputDir: File = File(destinationDir.asFile, executablePath).parentFile |
| 136 | + |
| 137 | + val bundleSearchDir: File = kotlinBinary.asFile.parentFile |
| 138 | + bundleSearchDir |
| 139 | + .listFiles { file: File -> file.extension == "bundle" } |
| 140 | + ?.forEach { file -> |
| 141 | + file.copyRecursively(File(outputDir, file.name), true) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + doLast(action) |
| 146 | + } |
0 commit comments