Skip to content

Commit

Permalink
[Wasm] Support linux arm
Browse files Browse the repository at this point in the history
^KT-70100 fixed
  • Loading branch information
ilgonmic authored and qodana-bot committed Aug 14, 2024
1 parent 4ef6046 commit 1b9a321
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
package org.jetbrains.kotlin.gradle.targets.js.binaryen

import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import org.gradle.api.provider.Provider

/**
* Provides platform and architecture names that is used to download Binaryen.
*/
internal object BinaryenPlatform {
private val props = System.getProperties()
private fun property(name: String) = props.getProperty(name) ?: System.getProperty(name)

const val WIN = "windows"
const val LINUX = "linux"
const val DARWIN = "macos"

val name: String = run {
val name = property("os.name").toLowerCaseAsciiOnly()
when {
name.contains("windows") -> WIN
name.contains("mac") -> DARWIN
name.contains("linux") -> LINUX
name.contains("freebsd") -> LINUX
else -> throw IllegalArgumentException("Unsupported OS: $name")
}
internal data class BinaryenPlatform(
val name: String,
val arch: String,
) {
val platform: String
get() = "$arch-$name"

fun isWindows(): Boolean {
return name == WIN
}

const val ARM64 = "arm64"
const val X64 = "x86_64"
const val X86 = "x86_86"
companion object {
const val WIN = "windows"
const val LINUX = "linux"
const val DARWIN = "macos"

const val X64 = "x86_64"
const val X86 = "x86_86"

val architecture: String
get() {
val arch = property("os.arch")
internal fun parseBinaryenPlatform(name: String, arch: String, uname: Provider<String>): BinaryenPlatform {
return BinaryenPlatform(
parseOsName(name.toLowerCase()),
parseOsArch(
arch.toLowerCase(),
uname
)
)
}

private fun parseOsName(name: String): String {
return when {
arch == "aarch64" -> ARM64
name.contains("windows") -> WIN
name.contains("mac") -> DARWIN
name.contains("linux") -> LINUX
name.contains("freebsd") -> LINUX
else -> throw IllegalArgumentException("Unsupported OS: $name")
}
}

private fun parseOsArch(arch: String, uname: Provider<String>): String {
return when {
arch == "arm" || arch.startsWith("aarch") -> uname.get()
arch.contains("64") -> X64
else -> X86
}
}

val platform: String
get() = "$architecture-$name"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.logging.kotlinInfo
import org.jetbrains.kotlin.gradle.targets.js.AbstractSettings
import org.jetbrains.kotlin.gradle.tasks.internal.CleanableStore
import org.jetbrains.kotlin.gradle.utils.property

open class BinaryenRootExtension(
@Transient val rootProject: Project
Expand All @@ -31,12 +32,14 @@ open class BinaryenRootExtension(
val setupTaskProvider: TaskProvider<BinaryenSetupTask>
get() = rootProject.tasks.withType(BinaryenSetupTask::class.java).named(BinaryenSetupTask.NAME)

internal val platform: org.gradle.api.provider.Property<BinaryenPlatform> = rootProject.objects.property<BinaryenPlatform>()

override fun finalizeConfiguration(): BinaryenEnv {
val platform = BinaryenPlatform.platform
val platform = platform.get()
val requiredVersionName = "binaryen-version_$version"
val cleanableStore = CleanableStore[installationDir.absolutePath]
val targetPath = cleanableStore[requiredVersionName].use()
val isWindows = BinaryenPlatform.name == BinaryenPlatform.WIN
val isWindows = platform.isWindows()

fun getExecutable(command: String, customCommand: String, windowsExtension: String): String {
val finalCommand = if (isWindows && customCommand == command) "$command.$windowsExtension" else customCommand
Expand All @@ -52,7 +55,7 @@ open class BinaryenRootExtension(
return BinaryenEnv(
download = download,
downloadBaseUrl = downloadBaseUrl,
ivyDependency = "com.github.webassembly:binaryen:$version:$platform@tar.gz",
ivyDependency = "com.github.webassembly:binaryen:$version:${platform.platform}@tar.gz",
executable = getExecutable("wasm-opt", command, "exe"),
dir = targetPath,
cleanableStore = cleanableStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ package org.jetbrains.kotlin.gradle.targets.js.binaryen
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.jetbrains.kotlin.gradle.plugin.internal.configurationTimePropertiesAccessor
import org.jetbrains.kotlin.gradle.plugin.internal.usedAtConfigurationTime
import org.jetbrains.kotlin.gradle.plugin.variantImplementationFactory
import org.jetbrains.kotlin.gradle.targets.js.MultiplePluginDeclarationDetector
import org.jetbrains.kotlin.gradle.targets.js.binaryen.BinaryenPlatform.Companion.parseBinaryenPlatform
import org.jetbrains.kotlin.gradle.targets.js.binaryen.BinaryenRootExtension.Companion.EXTENSION_NAME
import org.jetbrains.kotlin.gradle.targets.js.nodejs.UnameExecutor
import org.jetbrains.kotlin.gradle.tasks.CleanDataTask
import org.jetbrains.kotlin.gradle.tasks.registerTask
import org.jetbrains.kotlin.gradle.utils.castIsolatedKotlinPluginClassLoaderAware
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly

open class BinaryenRootPlugin : Plugin<Project> {
override fun apply(project: Project) {
Expand All @@ -26,6 +32,8 @@ open class BinaryenRootPlugin : Plugin<Project> {

val settings = project.extensions.create(EXTENSION_NAME, BinaryenRootExtension::class.java, project)

addPlatform(project, settings)

project.registerTask<BinaryenSetupTask>(BinaryenSetupTask.NAME) {
it.group = TASKS_GROUP_NAME
it.description = "Download and install a binaryen"
Expand All @@ -42,6 +50,23 @@ open class BinaryenRootPlugin : Plugin<Project> {
}
}

private fun addPlatform(project: Project, extension: BinaryenRootExtension) {
val uname = project.variantImplementationFactory<UnameExecutor.UnameExecutorVariantFactory>()
.getInstance(project)
.unameExecResult

extension.platform.value(
project.providers.systemProperty("os.name")
.usedAtConfigurationTime(project.configurationTimePropertiesAccessor)
.zip(
project.providers.systemProperty("os.arch")
.usedAtConfigurationTime(project.configurationTimePropertiesAccessor)
) { name, arch ->
parseBinaryenPlatform(name.toLowerCaseAsciiOnly(), arch, uname)
}
).disallowChanges()
}

companion object {
const val TASKS_GROUP_NAME: String = "binaryen"

Expand Down

0 comments on commit 1b9a321

Please sign in to comment.