Skip to content

Commit

Permalink
Fixes remaining resource combination issues for Android and iOS (#1584)
Browse files Browse the repository at this point in the history
* Make gradle kotlin version 1.7

* Move createCopyToExecutableTarget to a common package

* Bump XCodeGen to 2.35.0 and improve versioning

* Minor

* Fixes iOS resource copying

* Fixes Android resources with kproject submodules
  • Loading branch information
soywiz authored May 10, 2023
1 parent 14686c7 commit bc08620
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fun getKorgeProcessResourcesTaskName(targetName: String, compilationName: String
fun getProcessResourcesTaskName(targetName: String, compilationName: String): String =
"${targetName.decapitalize()}${if (compilationName == "main") "" else compilationName.capitalize()}ProcessResources"

fun Project.generateKorgeProcessedFromTask(task: ProcessResources?, taskName: String) {
val targetNameRaw = taskName.removeSuffix("ProcessResources")
fun Project.generateKorgeProcessedFromTask(task: ProcessResources) {
val targetNameRaw = task.name.removeSuffix("ProcessResources")
val isTest = targetNameRaw.endsWith("Test")
val targetName = targetNameRaw.removeSuffix("Test")
val target = kotlin.targets.findByName(targetName) ?: return
Expand Down Expand Up @@ -111,7 +111,7 @@ fun Project.addGenResourcesTasks() {

for (task in tasks.withType(ProcessResources::class.java).toList()) {
//println("TASK: $task : ${task::class}")
generateKorgeProcessedFromTask(task, task.name)
generateKorgeProcessedFromTask(task)
}
}
}
Expand Down
106 changes: 106 additions & 0 deletions buildSrc/src/main/kotlin/korlibs/korge/gradle/module/CopyResources.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package korlibs.korge.gradle.module

import korlibs.korge.gradle.*
import korlibs.korge.gradle.targets.*
import korlibs.korge.gradle.targets.desktop.*
import korlibs.korge.gradle.targets.native.*
import korlibs.korge.gradle.targets.windows.*
import korlibs.korge.gradle.util.*
import org.gradle.api.*
import org.gradle.api.file.*
import org.gradle.api.tasks.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*

fun Project.createCopyToExecutableTarget(target: String) {
for (build in KNTargetWithBuildType.buildList(project, target)) {
val copyTask = project.tasks.createThis<Copy>(build.copyTaskName) {
run {
val processResourcesTaskName = getProcessResourcesTaskName(build.target, build.compilation.name)
dependsOn(processResourcesTaskName)
afterEvaluate {
afterEvaluate {
afterEvaluate {
val korgeGenerateResourcesTask = tasks.findByName(processResourcesTaskName) as? Copy?
//korgeGenerateResourcesTask?.korgeGeneratedFolder?.let { from(it) }
from(korgeGenerateResourcesTask?.outputs)
}
}
}
}
run {
val processResourcesTaskName = getKorgeProcessResourcesTaskName(build.target, build.compilation.name)
dependsOn(processResourcesTaskName)
afterEvaluate {
afterEvaluate {
afterEvaluate {
val korgeGenerateResourcesTask =
tasks.findByName(processResourcesTaskName) as? KorgeGenerateResourcesTask?
//korgeGenerateResourcesTask?.korgeGeneratedFolder?.let { from(it) }
korgeGenerateResourcesTask?.addToCopySpec(this@createThis, addFrom = false)
}
}
}
}
dependsOn(build.compilation.compileKotlinTask)
duplicatesStrategy = DuplicatesStrategy.INCLUDE
for (sourceSet in project.gkotlin.sourceSets) {
from(sourceSet.resources)
}
//println("executableFile.parentFile: ${executableFile.parentFile}")
into(build.executableFile.parentFile)
if (build.isMingwX64) {
//if (false) {
doLast {
val bmp32 = project.korge.getIconBytes(32).decodeImage()
val bmp256 = project.korge.getIconBytes(256).decodeImage()

build.appIcoFile.writeBytes(ICO2.encode(listOf(bmp32, bmp256)))

val linkTask = build.compilation.getLinkTask(NativeOutputKind.EXECUTABLE, build.buildType, project)

val isConsole = korge.enableConsole ?: (build.kind == NativeBuildType.DEBUG)
val subsystem = if (isConsole) "console" else "windows"

run {
// @TODO: https://releases.llvm.org/9.0.0/tools/lld/docs/ReleaseNotes.html#id6
// @TODO: lld-link now rejects more than one resource object input files, matching link.exe. Previously, lld-link would silently ignore all but one. If you hit this: Don’t pass resource object files to the linker, instead pass res files to the linker directly. Don’t put resource files in static libraries, pass them on the command line. (r359749)
//appRcFile.writeText(WindowsRC.generate(korge))
//project.compileWindowsRC(appRcFile, appRcObjFile)
//linkTask.binary.linkerOpts(appRcObjFile.absolutePath, "-Wl,--subsystem,$subsystem")
}

run {
// @TODO: Not working either!
//appRcFile.writeText(WindowsRC.generate(korge))
//val appResFile = project.compileWindowsRES(appRcFile)
//linkTask.binary.linkerOpts(appResFile.absolutePath, "-Wl,--subsystem,$subsystem")
}

run {
build.appRcFile.writeText(WindowsRC.generate(korge))
project.compileWindowsRES(build.appRcFile, build.appResFile)
linkTask.binary.linkerOpts("-Wl,--subsystem,$subsystem")
}
}
//println("compilation:$compilation")
//compilation.linkerOpts(appRcObjFile.absolutePath, "-Wl,--subsystem,console")
afterEvaluate {
}
}
}

afterEvaluate {
try {
val linkTask = build.compilation.getLinkTask(NativeOutputKind.EXECUTABLE, build.buildType, project)
linkTask.dependsOn(copyTask)
if (build.isMingwX64) {
linkTask.doLast {
replaceExeWithRes(linkTask.outputFile.get(), build.appResFile)
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun Project.androidEmulatorStart() {
}
}


/*
fun Project.androidGetResourcesFolders(): Pair<List<File>, List<File>> {
val targets = listOf(kotlin.metadata())
val mainSourceSets = targets.flatMap { it.compilations["main"].allKotlinSourceSets }
Expand All @@ -98,7 +98,7 @@ fun isKorlibsDependency(cleanFullName: String): Boolean {
if (cleanFullName.startsWith("org.jogamp")) return false
return true
}

*/

//fun writeAndroidManifest(outputFolder: File, korge: KorgeExtension, info: AndroidInfo = AndroidInfo(null)) {
// val generated = AndroidGenerated(korge, info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ fun Project.configureAndroidDirect(projectType: ProjectType, isKorge: Boolean) {
java.srcDirs(AndroidConfig.getAndroidSrcFolder(project, isKorge = isKorge))
res.srcDirs(AndroidConfig.getAndroidResFolder(project, isKorge = isKorge))
assets.srcDirs(
"${project.projectDir}/src/commonMain/resources",
"${project.projectDir}/src/androidMain/resources",
"${project.projectDir}/src/main/resources",
"${project.projectDir}/build/commonMain/korgeProcessedResources/metadata/main",
"${project.projectDir}/build/korgeProcessedResources/android/main",
"${project.buildDir}/processedResources/jvm/main",
//"${project.projectDir}/src/commonMain/resources",
//"${project.projectDir}/src/androidMain/resources",
//"${project.projectDir}/src/main/resources",
//"${project.projectDir}/build/commonMain/korgeProcessedResources/metadata/main",
//"${project.projectDir}/build/korgeProcessedResources/android/main",
)
//assets.srcDirs(*resourcesSrcDirs.map { it.absoluteFile }.toTypedArray())
//java.srcDirs(*kotlinSrcDirs.map { it.absoluteFile }.toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ fun Project.installAndroidRun(dependsOnList: List<String>, direct: Boolean, isKo
val hasKotlinMultiplatformExtension = project.extensions.findByType(KotlinMultiplatformExtension::class.java) != null
if (hasKotlinMultiplatformExtension) {
afterEvaluate {
generateKorgeProcessedFromTask(null, "androidProcessResources")
//generateKorgeProcessedFromTask(null, "androidProcessResources")
}
}

val generateAndroidProcessedResources = getKorgeProcessResourcesTaskName("android", "main")
//val generateAndroidProcessedResources = getKorgeProcessResourcesTaskName("jvm", "main")
val generateAndroidProcessedResources = getProcessResourcesTaskName("jvm", "main")

afterEvaluate {
for (Type in listOf("Debug", "Release")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import korlibs.korge.gradle.targets.native.*
import korlibs.korge.gradle.targets.windows.*
import korlibs.korge.gradle.util.*
import korlibs.*
import korlibs.korge.gradle.module.*
import org.gradle.api.*
import org.gradle.api.file.*
import org.gradle.api.tasks.*
Expand Down Expand Up @@ -133,100 +134,6 @@ class KNTargetWithBuildType(val project: Project, val target: String, val kind:
val copyTaskName = "copyResourcesToExecutable${ctargetKind}"
}

fun Project.createCopyToExecutableTarget(target: String) {
for (build in KNTargetWithBuildType.buildList(project, target)) {
val copyTask = project.tasks.createThis<Copy>(build.copyTaskName) {
run {
val processResourcesTaskName = getProcessResourcesTaskName(build.target, build.compilation.name)
dependsOn(processResourcesTaskName)
afterEvaluate {
afterEvaluate {
afterEvaluate {
val korgeGenerateResourcesTask = tasks.findByName(processResourcesTaskName) as? Copy?
//korgeGenerateResourcesTask?.korgeGeneratedFolder?.let { from(it) }
from(korgeGenerateResourcesTask?.outputs)
}
}
}
}
run {
val processResourcesTaskName = getKorgeProcessResourcesTaskName(build.target, build.compilation.name)
dependsOn(processResourcesTaskName)
afterEvaluate {
afterEvaluate {
afterEvaluate {
val korgeGenerateResourcesTask =
tasks.findByName(processResourcesTaskName) as? KorgeGenerateResourcesTask?
//korgeGenerateResourcesTask?.korgeGeneratedFolder?.let { from(it) }
korgeGenerateResourcesTask?.addToCopySpec(this@createThis, addFrom = false)
}
}
}
}
dependsOn(build.compilation.compileKotlinTask)
duplicatesStrategy = DuplicatesStrategy.INCLUDE
for (sourceSet in project.gkotlin.sourceSets) {
from(sourceSet.resources)
}
//println("executableFile.parentFile: ${executableFile.parentFile}")
into(build.executableFile.parentFile)
if (build.isMingwX64) {
//if (false) {
doLast {
val bmp32 = project.korge.getIconBytes(32).decodeImage()
val bmp256 = project.korge.getIconBytes(256).decodeImage()

build.appIcoFile.writeBytes(ICO2.encode(listOf(bmp32, bmp256)))

val linkTask = build.compilation.getLinkTask(NativeOutputKind.EXECUTABLE, build.buildType, project)

val isConsole = korge.enableConsole ?: (build.kind == NativeBuildType.DEBUG)
val subsystem = if (isConsole) "console" else "windows"

run {
// @TODO: https://releases.llvm.org/9.0.0/tools/lld/docs/ReleaseNotes.html#id6
// @TODO: lld-link now rejects more than one resource object input files, matching link.exe. Previously, lld-link would silently ignore all but one. If you hit this: Don’t pass resource object files to the linker, instead pass res files to the linker directly. Don’t put resource files in static libraries, pass them on the command line. (r359749)
//appRcFile.writeText(WindowsRC.generate(korge))
//project.compileWindowsRC(appRcFile, appRcObjFile)
//linkTask.binary.linkerOpts(appRcObjFile.absolutePath, "-Wl,--subsystem,$subsystem")
}

run {
// @TODO: Not working either!
//appRcFile.writeText(WindowsRC.generate(korge))
//val appResFile = project.compileWindowsRES(appRcFile)
//linkTask.binary.linkerOpts(appResFile.absolutePath, "-Wl,--subsystem,$subsystem")
}

run {
build.appRcFile.writeText(WindowsRC.generate(korge))
project.compileWindowsRES(build.appRcFile, build.appResFile)
linkTask.binary.linkerOpts("-Wl,--subsystem,$subsystem")
}
}
//println("compilation:$compilation")
//compilation.linkerOpts(appRcObjFile.absolutePath, "-Wl,--subsystem,console")
afterEvaluate {
}
}
}

afterEvaluate {
try {
val linkTask = build.compilation.getLinkTask(NativeOutputKind.EXECUTABLE, build.buildType, project)
linkTask.dependsOn(copyTask)
if (build.isMingwX64) {
linkTask.doLast {
replaceExeWithRes(linkTask.outputFile.get(), build.appResFile)
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
}
}
}


fun KotlinNativeCompilation.getBinary(kind: NativeOutputKind, type: NativeBuildType): File {
return this.getLinkTask(kind, type, project).binary.outputFile.absoluteFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import korlibs.korge.gradle.targets.native.*
import korlibs.korge.gradle.targets.native.getCompileTask
import korlibs.korge.gradle.util.*
import korlibs.*
import korlibs.korge.gradle.module.*
import org.gradle.api.*
import org.gradle.api.internal.plugins.*
import org.gradle.api.reporting.*
Expand Down
23 changes: 6 additions & 17 deletions buildSrc/src/main/kotlin/korlibs/korge/gradle/targets/ios/Ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fun Project.configureNativeIos(projectType: ProjectType) {
kotlin.apply {
for (target in iosTargets) {
target.configureKotlinNativeTarget(project)
//createCopyToExecutableTarget(target.name)
//for (target in listOf(iosX64())) {
target.also { target ->
//target.attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.native)
Expand Down Expand Up @@ -74,30 +75,18 @@ fun Project.configureNativeIosRun() {
}

val combinedResourcesFolder = File(buildDir, "combinedResources/resources")
val processedResourcesFolder = File(buildDir, "processedResources/iosArm64/main")
val copyIosResources = tasks.createTyped<Copy>("copyIosResources") {
val targetName = "iosX64" // @TODO: Should be one per target?
val compilationName = "main"

val korgeProcessResourcesTaskName = getKorgeProcessResourcesTaskName(targetName, compilationName)
dependsOn(korgeProcessResourcesTaskName)
afterEvaluate {
afterEvaluate {
(tasks.findByName(korgeProcessResourcesTaskName) as? KorgeGenerateResourcesTask?)?.addToCopySpec(this@createTyped, addFrom = true)
}
}
//from(getCompilationKorgeProcessedResourcesFolder(targetName, compilationName))
from(File(project.projectDir, "src/commonMain/resources")) // @TODO: Use proper source sets to determine this?
val processResourcesTaskName = getProcessResourcesTaskName("iosArm64", "main")
dependsOn(processResourcesTaskName)
from(processedResourcesFolder)
into(combinedResourcesFolder)
duplicatesStrategy = DuplicatesStrategy.INCLUDE
doFirst {
//combinedResourcesFolder.mkdirs()
}
}

val prepareKotlinNativeIosProject = tasks.createThis<Task>("prepareKotlinNativeIosProject") {
dependsOn("installXcodeGen", "prepareKotlinNativeBootstrapIos", prepareKotlinNativeBootstrap, copyIosResources)
doLast {
// project.yml requires these folders to be available or it will fail
// project.yml requires these folders to be available, or it will fail
//File(rootDir, "src/commonMain/resources").mkdirs()

val folder = File(buildDir, "platforms/ios")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ val Project.iosXcodegenExt by projectExtension {
}

class IosXcodegen(val project: Project) {
//val xcodeGenGitTag = "2.25.0"
val xcodeGenGitTag = "2.35.0"
val korlibsFolder = File(System.getProperty("user.home") + "/.korlibs").apply { mkdirs() }
val xcodeGenFolder = File(korlibsFolder, "XcodeGen")
val xcodeGenFolder = File(korlibsFolder, "XcodeGen-$xcodeGenGitTag")
val xcodeGenLocalExecutable = File("/usr/local/bin/xcodegen")
val xcodeGenExecutable = FileList(
File(xcodeGenFolder, ".build/release/xcodegen"),
File(xcodeGenFolder, ".build/apple/Products/Release/xcodegen"),
)
val xcodeGenGitTag = "2.25.0"
val xcodeGenExe: File
get() = xcodeGenExecutable.takeIfExists() ?: xcodeGenLocalExecutable.takeIfExists() ?: error("Can't find xcodegen")

Expand All @@ -28,10 +29,14 @@ class IosXcodegen(val project: Project) {
if (!File(xcodeGenFolder, ".git").isDirectory) {
project.execLogger {
//it.commandLine("git", "clone", "--depth", "1", "--branch", xcodeGenGitTag, "https://github.com/yonaskolb/XcodeGen.git")
it.commandLine("git", "clone", "https://github.com/yonaskolb/XcodeGen.git")
it.commandLine("git", "clone", "https://github.com/yonaskolb/XcodeGen.git", xcodeGenFolder)
it.workingDir(korlibsFolder)
}
}
project.execLogger {
it.commandLine("git", "pull")
it.workingDir(xcodeGenFolder)
}
project.execLogger {
it.commandLine("git", "checkout", xcodeGenGitTag)
it.workingDir(xcodeGenFolder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import org.jetbrains.kotlin.gradle.tasks.*

fun KotlinNativeCompilation.getLinkTask(kind: NativeOutputKind, type: NativeBuildType, project: Project): KotlinNativeLink {
val taskName = "link${type.name.toLowerCase().capitalize()}${kind.name.toLowerCase().capitalize()}${target.name.capitalize()}"
val tasks = (project.getTasksByName(taskName, true) + project.getTasksByName(taskName, false)).toList()
return (tasks.firstOrNull() as? KotlinNativeLink) ?: error("Can't find $taskName from $tasks from ${project.tasks.map { it.name }}")
val taskName2 = "link${target.name.capitalize()}"

val tasks = listOf(
project.getTasksByName(taskName, true),
project.getTasksByName(taskName, false),
project.getTasksByName(taskName2, true),
project.getTasksByName(taskName2, false),
).flatMap { it }
return (tasks.firstOrNull() as? KotlinNativeLink)
?: error("Can't find [$taskName or $taskName2] from $tasks from ${project.tasks.map { it.name }}")
}

fun KotlinNativeCompilation.getCompileTask(kind: NativeOutputKind, type: NativeBuildType, project: Project): Task {
Expand Down
Loading

0 comments on commit bc08620

Please sign in to comment.