Skip to content

Commit

Permalink
aarch64 support in build logic
Browse files Browse the repository at this point in the history
Signed-off-by: Martmists <martmists@gmail.com>
  • Loading branch information
Martmists-GH committed Jun 2, 2024
1 parent d97e179 commit 1c4afdf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
18 changes: 16 additions & 2 deletions buildSrc/src/main/kotlin/DownloadPythonTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,26 @@ abstract class DownloadPythonTask : DefaultTask() {
MacOS,
}

enum class Architecture {
X86_64,
ARM64,
}

@get:Input
abstract val version: Property<Version>

@get:Input
abstract val platform: Property<Platform>

@get:Input
abstract val architecture: Property<Architecture>

@get:OutputFile
abstract val tarFile: Property<File>

init {
tarFile.convention(Providers.changing {
project.layout.buildDirectory.file("python-${version.get().str}-${platform.get()}.tar.gz").get().asFile
project.layout.buildDirectory.file("python-${version.get().str}-${platform.get()}-${architecture.get()}.tar.gz").get().asFile
})
}

Expand All @@ -60,12 +68,18 @@ abstract class DownloadPythonTask : DefaultTask() {
Platform.MacOS -> "apple-darwin"
else -> throw IllegalArgumentException("Unsupported platform: $platform")
}
val arch = when (architecture.get()) {
Architecture.X86_64 -> "x86_64"
Architecture.ARM64 -> "aarch64"
else -> throw IllegalArgumentException("Unsupported architecture: ${architecture.get()}")
}

val tmpFile by tarFile
tmpFile.parentFile.mkdirs()

val url = data["assets"]!!.jsonArray.map { obj ->
obj.jsonObject["browser_download_url"]!!.jsonPrimitive.content
}.first { it.contains("/cpython-${version.str}") && it.endsWith("x86_64-$hostOs-install_only.tar.gz") }
}.first { it.contains("/cpython-${version.str}") && it.endsWith("$arch-$hostOs-install_only.tar.gz") }

client.get(url).bodyAsChannel().also { channel ->
tmpFile.writeChannel().use {
Expand Down
19 changes: 14 additions & 5 deletions kpy-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ kotlin {

val targets = listOfNotNull(
if (hostOs == "Mac OS X") macosX64() else null,
if (hostOs == "Mac OS X") macosArm64() else null,
if (!isMingwX64) linuxX64() else null,
mingwX64()
if (!isMingwX64) linuxArm64() else null,
mingwX64(),
)


Expand All @@ -36,10 +38,17 @@ kotlin {

val downloadSourcesTask = tasks.register("downloadPython${targetName}", DownloadPythonTask::class) {
version = pythonVersion

architecture = when (konanTarget) {
KonanTarget.MINGW_X64, KonanTarget.LINUX_X64, KonanTarget.MACOS_X64 -> DownloadPythonTask.Architecture.X86_64
KonanTarget.LINUX_ARM64, KonanTarget.MACOS_ARM64 -> DownloadPythonTask.Architecture.ARM64
else -> throw IllegalArgumentException("Unsupported target: $targetName")
}

platform = when (konanTarget) {
KonanTarget.MINGW_X64 -> DownloadPythonTask.Platform.Windows
KonanTarget.LINUX_X64 -> DownloadPythonTask.Platform.Linux
KonanTarget.MACOS_X64 -> DownloadPythonTask.Platform.MacOS
KonanTarget.LINUX_X64, KonanTarget.LINUX_ARM64 -> DownloadPythonTask.Platform.Linux
KonanTarget.MACOS_X64, KonanTarget.MACOS_ARM64 -> DownloadPythonTask.Platform.MacOS
else -> throw IllegalArgumentException("Unsupported target: $targetName")
}
}
Expand All @@ -66,12 +75,12 @@ kotlin {
linkerOpts("-L${outDir.resolve("python").absolutePath} -lpython3")
}

KonanTarget.LINUX_X64 -> {
KonanTarget.LINUX_X64, KonanTarget.LINUX_ARM64 -> {
includeDirs(outDir.resolve("python/include/python${pythonVersion.str}"))
linkerOpts("-L${outDir.resolve("python/lib").absolutePath} -lpython3")
}

KonanTarget.MACOS_X64 -> {
KonanTarget.MACOS_X64, KonanTarget.MACOS_ARM64 -> {
includeDirs(outDir.resolve("python/include/python${pythonVersion.str}"))
linkerOpts("-L${outDir.resolve("python/lib").absolutePath} -lpython3")
}
Expand Down

0 comments on commit 1c4afdf

Please sign in to comment.