Move everything under kotlin-native folder
I was forced to manually do update the following files, because otherwise they would be ignored according .gitignore settings. Probably they should be deleted from repo. Interop/.idea/compiler.xml Interop/.idea/gradle.xml Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml Interop/.idea/modules.xml Interop/.idea/modules/Indexer/Indexer.iml Interop/.idea/modules/Runtime/Runtime.iml Interop/.idea/modules/StubGenerator/StubGenerator.iml backend.native/backend.native.iml backend.native/bc.frontend/bc.frontend.iml backend.native/cli.bc/cli.bc.iml backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt backend.native/tests/link/lib/foo.kt backend.native/tests/link/lib/foo2.kt backend.native/tests/teamcity-test.property
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# Curl interop library
|
||||
|
||||
This example shows how to build and publish an interop library to communicate with the libcurl,
|
||||
HTTP/HTTPS/FTP/etc client library.
|
||||
|
||||
Install libcurl development files. For Mac - `brew install curl`. For Debian-like Linux - use `apt-get install libcurl4-openssl-dev` or `apt-get install libcurl4-gnutls-dev`.
|
||||
For Windows - `pacman -S mingw-w64-x86_64-curl` in MinGW64 console, if you do
|
||||
not have MSYS2-MinGW64 installed - install it first as described in http://www.msys2.org
|
||||
|
||||
To build use `../gradlew assemble`.
|
||||
|
||||
To publish the library into a local repo use `../gradlew publish`.
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
group = "org.jetbrains.kotlin.sample.native"
|
||||
version = "1.0"
|
||||
|
||||
val localRepo = rootProject.file("build/.m2-local")
|
||||
|
||||
publishing {
|
||||
repositories {
|
||||
maven("file://$localRepo")
|
||||
}
|
||||
}
|
||||
|
||||
val cleanLocalRepo by tasks.creating(Delete::class) {
|
||||
delete(localRepo)
|
||||
}
|
||||
|
||||
val mingwPath = File(System.getenv("MINGW64_DIR") ?: "C:/msys64/mingw64")
|
||||
|
||||
kotlin {
|
||||
|
||||
// Determine host preset.
|
||||
val hostOs = System.getProperty("os.name")
|
||||
|
||||
// Create target for the host platform.
|
||||
val hostTarget = when {
|
||||
hostOs == "Mac OS X" -> macosX64("libcurl")
|
||||
hostOs == "Linux" -> linuxX64("libcurl")
|
||||
hostOs.startsWith("Windows") -> mingwX64("libcurl")
|
||||
else -> throw GradleException("Host OS '$hostOs' is not supported in Kotlin/Native $project.")
|
||||
}
|
||||
|
||||
hostTarget.apply {
|
||||
compilations["main"].cinterops {
|
||||
val libcurl by creating {
|
||||
when (preset) {
|
||||
presets["macosX64"] -> includeDirs.headerFilterOnly("/opt/local/include", "/usr/local/include")
|
||||
presets["linuxX64"] -> includeDirs.headerFilterOnly("/usr/include", "/usr/include/x86_64-linux-gnu")
|
||||
presets["mingwX64"] -> includeDirs.headerFilterOnly(mingwPath.resolve("include"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mavenPublication {
|
||||
pom {
|
||||
withXml {
|
||||
val root = asNode()
|
||||
root.appendNode("name", "libcurl interop library")
|
||||
root.appendNode("description", "A library providing interoperability with host libcurl")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enable experimental stdlib API used by the sample.
|
||||
sourceSets.all {
|
||||
languageSettings.useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.import.noCommonSourceSets=true
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package sample.libcurl
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.size_t
|
||||
import libcurl.*
|
||||
|
||||
class CUrl(url: String) {
|
||||
private val stableRef = StableRef.create(this)
|
||||
|
||||
private val curl = curl_easy_init()
|
||||
|
||||
init {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url)
|
||||
val header = staticCFunction(::header_callback)
|
||||
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header)
|
||||
curl_easy_setopt(curl, CURLOPT_HEADERDATA, stableRef.asCPointer())
|
||||
val writeData = staticCFunction(::write_callback)
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData)
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stableRef.asCPointer())
|
||||
}
|
||||
|
||||
val header = Event<String>()
|
||||
val body = Event<String>()
|
||||
|
||||
fun nobody(){
|
||||
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L)
|
||||
}
|
||||
|
||||
fun fetch() {
|
||||
val res = curl_easy_perform(curl)
|
||||
if (res != CURLE_OK)
|
||||
println("curl_easy_perform() failed: ${curl_easy_strerror(res)?.toKString()}")
|
||||
}
|
||||
|
||||
fun close() {
|
||||
curl_easy_cleanup(curl)
|
||||
stableRef.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
fun CPointer<ByteVar>.toKString(length: Int): String {
|
||||
val bytes = this.readBytes(length)
|
||||
return bytes.decodeToString()
|
||||
}
|
||||
|
||||
fun header_callback(buffer: CPointer<ByteVar>?, size: size_t, nitems: size_t, userdata: COpaquePointer?): size_t {
|
||||
if (buffer == null) return 0u
|
||||
if (userdata != null) {
|
||||
val header = buffer.toKString((size * nitems).toInt()).trim()
|
||||
val curl = userdata.asStableRef<CUrl>().get()
|
||||
curl.header(header)
|
||||
}
|
||||
return size * nitems
|
||||
}
|
||||
|
||||
|
||||
fun write_callback(buffer: CPointer<ByteVar>?, size: size_t, nitems: size_t, userdata: COpaquePointer?): size_t {
|
||||
if (buffer == null) return 0u
|
||||
if (userdata != null) {
|
||||
val data = buffer.toKString((size * nitems).toInt()).trim()
|
||||
val curl = userdata.asStableRef<CUrl>().get()
|
||||
curl.body(data)
|
||||
}
|
||||
return size * nitems
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package sample.libcurl
|
||||
|
||||
typealias EventHandler<T> = (T) -> Unit
|
||||
class Event<T : Any> {
|
||||
private var handlers = emptyList<EventHandler<T>>()
|
||||
|
||||
fun subscribe(handler: EventHandler<T>) {
|
||||
handlers += handler
|
||||
}
|
||||
|
||||
fun unsubscribe(handler: EventHandler<T>) {
|
||||
handlers -= handler
|
||||
}
|
||||
|
||||
operator fun plusAssign(handler: EventHandler<T>) = subscribe(handler)
|
||||
operator fun minusAssign(handler: EventHandler<T>) = unsubscribe(handler)
|
||||
|
||||
operator fun invoke(value: T) {
|
||||
var exception: Throwable? = null
|
||||
for (handler in handlers) {
|
||||
try {
|
||||
handler(value)
|
||||
} catch (e: Throwable) {
|
||||
exception = e
|
||||
}
|
||||
}
|
||||
exception?.let { throw it }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
headers = curl/curl.h
|
||||
headerFilter = curl/*
|
||||
linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl
|
||||
linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lcurl
|
||||
linkerOpts.mingw = -lcurl
|
||||
Reference in New Issue
Block a user