Skip to content

Commit

Permalink
KTOR-3248 Build Ktor on Apple Silicon
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd committed Nov 1, 2021
1 parent 7ace1e0 commit e9caf29
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 245 deletions.
13 changes: 0 additions & 13 deletions buildSrc/src/main/kotlin/IdeUtils.kt

This file was deleted.

3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/JvmConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ fun Project.configureJvm() {
api("junit:junit:$junit_version")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version")
val jna_version = "5.9.0"
api("net.java.dev.jna:jna:$jna_version")
api("net.java.dev.jna:jna-platform:$jna_version")
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions buildSrc/src/main/kotlin/KtorBuildProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ private val versionComponents = java_version

val IDEA_ACTIVE: Boolean = System.getProperty("idea.active") == "true"

val OS_NAME = System.getProperty("os.name").toLowerCase()

val HOST_NAME = when {
OS_NAME.startsWith("linux") -> "linux"
OS_NAME.startsWith("windows") -> "windows"
OS_NAME.startsWith("mac") -> "macos"
else -> error("Unknown os name `$OS_NAME`")
}

object KtorBuildProperties {
val jettyAlpnBootVersion: String? = when (java_version) {
"1.8.0_191",
Expand Down
39 changes: 7 additions & 32 deletions buildSrc/src/main/kotlin/NativeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import org.gradle.api.*
import org.gradle.api.publish.maven.tasks.*
import org.gradle.api.publish.tasks.*
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.konan.target.*

fun KotlinMultiplatformExtension.posixTargets(): Set<KotlinNativeTarget> =
nixTargets() + mingwX64()
Expand Down Expand Up @@ -35,33 +32,11 @@ fun KotlinMultiplatformExtension.darwinTargets(): Set<KotlinNativeTarget> = setO
macosArm64()
)

fun KotlinMultiplatformExtension.desktopTargets(): Set<KotlinNativeTarget> = setOf(
macosX64(),
linuxX64(),
mingwX64()
)

fun Project.disableCompilation(target: KotlinNativeTarget) {
target.apply {

compilations.all {
cinterops.all {
tasks.named(interopProcessingTaskName) { enabled = false }
}
compileKotlinTaskProvider.configure { enabled = false }
}

binaries.all {
linkTaskProvider.configure { enabled = false }
}

mavenPublication {
tasks.withType<AbstractPublishToMaven>().configureEach {
onlyIf { publication != this@mavenPublication }
}
tasks.withType<GenerateModuleMetadata>().configureEach {
onlyIf { publication.get() != this@mavenPublication }
}
}
fun KotlinMultiplatformExtension.desktopTarget(configure: KotlinNativeTarget.() -> Unit) {
when (HostManager.host) {
is KonanTarget.MACOS_ARM64 -> macosArm64(configure = configure)
is KonanTarget.MACOS_X64 -> macosX64(configure = configure)
is KonanTarget.LINUX_X64 -> linuxX64(configure = configure)
is KonanTarget.MINGW_X64 -> mingwX64(configure = configure)
}
}
13 changes: 5 additions & 8 deletions buildSrc/src/main/kotlin/Publication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import org.gradle.api.publish.maven.tasks.*
import org.gradle.jvm.tasks.*
import org.gradle.kotlin.dsl.*
import org.gradle.plugins.signing.*
import org.jetbrains.kotlin.konan.target.*

fun isAvailableForPublication(publication: Publication): Boolean {
val name = publication.name
if (name == "maven") return true

var result = false
val jvmAndCommon = setOf(
"jvm",
"androidRelease",
Expand All @@ -24,9 +24,9 @@ fun isAvailableForPublication(publication: Publication): Boolean {
"metadata",
"kotlinMultiplatform"
)
result = result || name in jvmAndCommon
result = result || (HOST_NAME == "linux" && name == "linuxX64")
result = result || (HOST_NAME == "windows" && name == "mingwX64")
if (name in jvmAndCommon) return true
if (HostManager.hostIsLinux && name == "linuxX64") return true
if (HostManager.hostIsMingw && name == "mingwX64") return true
val macPublications = setOf(
"iosX64",
"iosArm64",
Expand All @@ -46,10 +46,7 @@ fun isAvailableForPublication(publication: Publication): Boolean {
"macosX64",
"macosArm64"
)

result = result || (HOST_NAME == "macos" && name in macPublications)

return result
return HostManager.hostIsMac && name in macPublications
}

fun Project.configurePublication() {
Expand Down
63 changes: 19 additions & 44 deletions buildSrc/src/main/kotlin/TargetsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,24 @@ fun Project.configureTargets() {

if (hasPosix || hasDarwin) extra.set("hasNative", true)

if (IDEA_ACTIVE) {
if (hasPosix) createIdeaTarget("posix")
if (hasNix) createIdeaTarget("nix")
if (hasDarwin) createIdeaTarget("darwin")
if (hasDesktop) createIdeaTarget("desktop")
} else {
sourceSets {
if (hasPosix) {
val posixMain by creating
val posixTest by creating
}
if (hasNix) {
val nixMain by creating
val nixTest by creating
}
if (hasDarwin) {
val darwinMain by creating
val darwinTest by creating
}
if (hasDesktop) {
val desktopMain by creating
val desktopTest by creating
}
sourceSets {
if (hasPosix) {
val posixMain by creating
val posixTest by creating
}
if (hasNix) {
val nixMain by creating
val nixTest by creating
}
if (hasDarwin) {
val darwinMain by creating
val darwinTest by creating
}
if (hasDesktop) {
val desktopMain by creating
val desktopTest by creating
}
}

sourceSets {
if (hasCommon) {
val commonMain by getting {
dependencies {
Expand Down Expand Up @@ -109,10 +100,6 @@ fun Project.configureTargets() {
posixTargets().forEach {
getByName("${it.name}Main").dependsOn(posixMain)
getByName("${it.name}Test").dependsOn(posixTest)

if (!it.name.startsWith(HOST_NAME)) {
disableCompilation(it)
}
}
}
if (hasNix) {
Expand All @@ -128,10 +115,6 @@ fun Project.configureTargets() {
nixTargets().forEach {
getByName("${it.name}Main").dependsOn(nixMain)
getByName("${it.name}Test").dependsOn(nixTest)

if (!it.name.startsWith(HOST_NAME)) {
disableCompilation(it)
}
}
}
if (hasDarwin) {
Expand All @@ -144,10 +127,6 @@ fun Project.configureTargets() {
darwinTargets().forEach {
getByName("${it.name}Main").dependsOn(darwinMain)
getByName("${it.name}Test").dependsOn(darwinTest)

if (!it.name.startsWith(HOST_NAME)) {
disableCompilation(it)
}
}
}
if (hasDesktop) {
Expand All @@ -157,13 +136,9 @@ fun Project.configureTargets() {

val desktopTest by getting

desktopTargets().forEach {
getByName("${it.name}Main").dependsOn(desktopMain)
getByName("${it.name}Test").dependsOn(desktopTest)

if (!it.name.startsWith(HOST_NAME)) {
disableCompilation(it)
}
desktopTarget {
getByName("${name}Main").dependsOn(desktopMain)
getByName("${name}Test").dependsOn(desktopTest)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions ktor-client/ktor-client-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@ kotlin.sourceSets {
api(project(":ktor-client:ktor-client-mock"))
}
}

val jvmTest by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version")
}
}
}
19 changes: 6 additions & 13 deletions ktor-client/ktor-client-curl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.*

val serialization_version: String by project.extra

Expand All @@ -10,28 +8,23 @@ plugins {

kotlin {
targets.apply {
val current = mutableListOf<KotlinTarget>()
if (IDEA_ACTIVE) {
current.add(getByName("desktop"))
} else {
current.addAll(listOf(getByName("macosX64"), getByName("linuxX64"), getByName("mingwX64")))
}

val paths = listOf(
"C:/msys64/mingw64/include/curl",
"C:/Tools/msys64/mingw64/include/curl",
"C:/Tools/msys2/mingw64/include/curl"
)
current.filterIsInstance<KotlinNativeTarget>().forEach { platform ->
platform.compilations.getByName("main") {
desktopTarget {
compilations.getByName("main") {
val libcurl by cinterops.creating {
defFile = File(projectDir, "desktop/interop/libcurl.def")

if (platform.name == "mingwX64") {
if (name == "mingwX64") {
includeDirs.headerFilterOnly(paths)
} else {
includeDirs.headerFilterOnly(
listOf(
"/usr/bin/curl",
"/opt/homebrew/opt/curl/include/curl",
"/opt/local/include/curl",
"/usr/local/include/curl",
Expand All @@ -47,7 +40,7 @@ kotlin {
}

afterEvaluate {
if (platform.name == "mingwX64") {
if (name == "mingwX64") {
val winTests = tasks.getByName("mingwX64Test") as KotlinNativeTest
winTests.environment(
"PATH",
Expand Down
2 changes: 1 addition & 1 deletion ktor-client/ktor-client-curl/desktop/interop/libcurl.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package = libcurl
headers = curl.h
headerFilter = *
linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl
compilerOpts.osx = -I/opt/local/include/curl -I/usr/local/include/curl -I/usr/include/curl -I/usr/local/Cellar/curl/7.62.0/include/curl -I/usr/local/Cellar/curl/7.63.0/include/curl -I/usr/local/Cellar/curl/7.65.3/include/curl -I/usr/local/Cellar/curl/7.66.0/include/curl -I/opt/homebrew/opt/curl/include/curl
compilerOpts.osx = -I/opt/local/include/curl -I/usr/bin/curl -I/usr/local/include/curl -I/usr/include/curl -I/usr/local/Cellar/curl/7.62.0/include/curl -I/usr/local/Cellar/curl/7.63.0/include/curl -I/usr/local/Cellar/curl/7.65.3/include/curl -I/usr/local/Cellar/curl/7.66.0/include/curl -I/opt/homebrew/opt/curl/include/curl
linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lcurl
compilerOpts.linux = -I/usr/include/curl -I/usr/include/x86_64-linux-gnu/curl
linkerOpts.mingw_x64 = -LC:/msys64/mingw64/lib -LC:/Tools/msys64/mingw64/lib -LC:/Tools/msys2/mingw64/lib -lcurl

0 comments on commit e9caf29

Please sign in to comment.