Skip to content

Commit 0dab0b1

Browse files
committed
chore: distroless support for kotlin native app
1 parent e881821 commit 0dab0b1

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

backend/native/build.gradle.kts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import com.google.cloud.tools.jib.api.buildplan.ImageFormat
55
import com.google.cloud.tools.jib.gradle.extension.nativeimage.JibNativeImageExtension
66
import common.*
77
import common.Platform
8-
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
9-
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
8+
import org.jetbrains.kotlin.gradle.plugin.mpp.*
109
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
10+
import org.jetbrains.kotlin.konan.target.Family
1111

1212
plugins {
1313
dev.suresh.plugin.kotlin.mpp
@@ -24,12 +24,13 @@ kotlin {
2424

2525
targets.withType<KotlinNativeTarget>().configureEach {
2626
binaries {
27-
executable(setOf(DEBUG)) {
27+
executable(setOf(RELEASE)) {
2828
entryPoint = "main"
2929
// Fix for libcrypt.so.1 not-found error on distroless
30-
if (target.targetName.startsWith("linux")) {
31-
linkerOpts("--as-needed")
32-
freeCompilerArgs += "-Xoverride-konan-properties=linkerGccFlags.linux=-lgcc -lgcc_eh -lc"
30+
if (target.konanTarget.family == Family.LINUX) {
31+
linkerOpts("-Wl,--as-needed", "-Wl,-Bstatic", "-lz", "-Wl,-Bdynamic")
32+
// freeCompilerArgs += "-Xoverride-konan-properties=linkerGccFlags.linux=-lgcc -lgcc_eh
33+
// -lc"
3334
}
3435

3536
if (buildType == NativeBuildType.RELEASE) {
@@ -119,7 +120,7 @@ jib {
119120
sourceSets.maybeCreate("main")
120121

121122
tasks {
122-
val buildType = "Debug"
123+
val buildType = "Release"
123124
val macOsUniversalBinary by
124125
registering(Exec::class) {
125126
val macosX64 = named<KotlinNativeLink>("link${buildType}ExecutableMacosX64")

backend/native/src/nativeMain/kotlin/Main.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ fun main(args: Array<String>): Unit = runBlocking {
2727
val ps =
2828
Process.Builder(command = "ls")
2929
.args("-l")
30+
.onError { println("Error: $it") }
3031
.destroySignal(Signal.SIGKILL)
3132
.stdout(Stdio.Inherit)
3233
.stderr(Stdio.Inherit)
3334

34-
val exit = ps.spawn { it.waitForAsync(2.seconds) ?: -1 }
35-
println("Process exited: $exit")
35+
val exit = runCatching { ps.spawn { it.waitForAsync(2.seconds) ?: -1 } }
36+
println("Process exited: ${exit.getOrElse { -1 }}")
3637

3738
println("Reflection Simple name ${this::class.simpleName}")
3839
println("Reflection Simple name ${this::class.qualifiedName}")

0 commit comments

Comments
 (0)