Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Last active February 16, 2024 05:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cy6erGn0m/134938242f28b27118039c038ea8c722 to your computer and use it in GitHub Desktop.
Save cy6erGn0m/134938242f28b27118039c038ea8c722 to your computer and use it in GitHub Desktop.
kotlin-native-libssh
plugins {
kotlin("multiplatform") version "1.6.21"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
nativeTarget.apply {
compilations["main"].cinterops {
val libssh by creating {
defFile("src/nativeMain/interop/ssh.def")
}
}
binaries {
executable {
entryPoint = "main"
}
}
}
sourceSets {
val nativeMain by getting
val nativeTest by getting
}
}
import kotlinx.cinterop.*
import org.ssh.*
fun main(): Unit = memScoped {
val session = ssh_new() ?: return
val port = alloc<IntVar>()
port.value = 22
val verbosity = alloc<UIntVar>()
verbosity.value = SSH_LOG_PROTOCOL
try {
ssh_options_set(session, ssh_options_e.SSH_OPTIONS_HOST, "host".utf8.getPointer(this))
ssh_options_set(session, ssh_options_e.SSH_OPTIONS_PORT, port.ptr)
ssh_options_set(session, ssh_options_e.SSH_OPTIONS_LOG_VERBOSITY, verbosity.ptr)
val rc = ssh_connect(session)
if (rc == SSH_OK) {
println("Connected successfully")
ssh_disconnect(session)
} else {
println("Failed to connect: ${ssh_get_error(session)?.toKStringFromUtf8()}")
}
} finally {
ssh_free(session)
}
}
headers = libssh/libssh.h
headerFilter = libssh/*
package = org.ssh
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
linkerOpts.linux = -L/usr/lib/x86_64-linux-gnu -lssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment