Skip to content

Commit

Permalink
fix: Make multiplatform actually compile the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 21, 2024
1 parent 0089dca commit 1c78089
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 37 deletions.
26 changes: 23 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
load("@rules_kotlin//kotlin:lint.bzl", "ktlint_fix", "ktlint_test")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//tools/project:build_defs.bzl", "project")

Expand Down Expand Up @@ -73,20 +75,27 @@ cc_binary(
],
)

kt_kotlinc_options(
name = "kotlinc_options",
x_multi_platform = True,
)

kt_jvm_library(
name = "jvm-toxcore-c",
srcs = glob([
"lib/src/*Main/**/*.java",
"lib/src/*Main/**/*.kt",
"lib/src/commonMain/**/*.kt",
"lib/src/jvmMain/**/*.java",
"lib/src/jvmMain/**/*.kt",
]),
data = ["libtox4j-c.so"],
kotlinc_opts = ":kotlinc_options",
deps = [":jni_java_proto"],
)

kt_jvm_test(
name = "ToxCoreTest",
size = "small",
srcs = ["lib/src/jvmTest/java/im/tox/tox4j/core/ToxCoreTest.kt"],
srcs = ["lib/src/commonTest/kotlin/im/tox/tox4j/core/ToxCoreTest.kt"],
jvm_flags = ["-Djava.library.path=jvm-toxcore-c"],
test_class = "im.tox.tox4j.core.ToxCoreTest",
deps = [
Expand All @@ -95,3 +104,14 @@ kt_jvm_test(
"@maven//:org_jetbrains_kotlinx_kotlinx_coroutines_core",
],
)

ktlint_fix(
name = "ktlint_fix",
srcs = glob(["lib/src/**/*.kt"]),
)

ktlint_test(
name = "ktlint_test",
size = "small",
srcs = glob(["lib/src/**/*.kt"]),
)
2 changes: 2 additions & 0 deletions docker/build-native
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ set -eux
cat <<EOF >native.txt
-p
library
-Xmulti-platform
EOF

find lib/src/commonMain -name '*.kt' -exec printf '{}\n-Xcommon-sources={}\n' ';' >>native.txt
find lib/src/nativeMain -name '*.kt' -exec printf '{}\n' ';' >>native.txt

kotlinc-native @native.txt
ls
1 change: 1 addition & 0 deletions docker/native.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ENV PATH=$PATH:/opt/kotlin/bin

RUN ["touch", "Boot.kt"]
RUN ["kotlinc-native", "-p", "library", "Boot.kt"]
#RUN ["kotlinc-native", "-help", "-X"]

WORKDIR /work/jvm-toxcore-c
COPY lib/ /work/jvm-toxcore-c/lib/
Expand Down
18 changes: 16 additions & 2 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,36 @@ repositories {
mavenCentral()
}

dependencies {
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
sourceSets["main"].proto {
srcDir("src/jvmMain/proto")
dependencies {
implementation("com.google.protobuf:protobuf-java:3.24.4")
}
}

kotlin {
jvm()

applyDefaultHierarchyTemplate()

sourceSets {
val jvmMain by getting {
kotlin.srcDir("/src/workspace/jvm-toxcore-c/lib/build/generated/source/proto/main/java")
dependencies {
implementation("com.google.protobuf:protobuf-java:3.24.4")
}
}
val commonTest by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.22")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}
}
}
}

tasks["compileKotlinJvm"].dependsOn("generateProto")

testing {
suites {
// Configure the built-in test suite
Expand Down
3 changes: 3 additions & 0 deletions lib/src/commonMain/kotlin/im/tox/tox4j/core/ToxCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import im.tox.tox4j.core.enums.ToxMessageType
import im.tox.tox4j.core.enums.ToxUserStatus
import im.tox.tox4j.core.options.ToxOptions

@kotlin.ExperimentalStdlibApi
expect fun newToxCore(options: ToxOptions): ToxCore

/**
* Interface for a basic wrapper of tox chat functionality.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package im.tox.tox4j.exceptions

abstract class ToxException constructor(val code: Enum<*>, private val extraMessage: String) :
Exception(extraMessage) {
final override val message: String
get() =
when (extraMessage) {
"" -> "Error code: " + code.name
else -> extraMessage + ", error code: " + code.name
}
}
final override val message: String
get() =
when (extraMessage) {
"" -> "Error code: " + code.name
else -> extraMessage + ", error code: " + code.name
}
}
7 changes: 7 additions & 0 deletions lib/src/jvmMain/kotlin/im/tox/tox4j/core/ToxCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package im.tox.tox4j.core

import im.tox.tox4j.core.options.ToxOptions
import im.tox.tox4j.impl.jni.ToxCoreImpl

@kotlin.ExperimentalStdlibApi
actual fun newToxCore(options: ToxOptions): ToxCore = ToxCoreImpl(options)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import im.tox.tox4j.core.callbacks.ToxCoreEventListener
import im.tox.tox4j.core.data.ToxFriendNumber
import im.tox.tox4j.core.enums.ToxConnection
import im.tox.tox4j.core.options.ToxOptions
import im.tox.tox4j.impl.jni.ToxCoreImpl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
Expand All @@ -13,36 +12,37 @@ import kotlin.coroutines.coroutineContext
import kotlin.test.Test

@kotlin.ExperimentalStdlibApi
class ToxCoreTest {
private class Toxes : CoroutineContext.Element {
private val list: MutableList<ToxCore> = mutableListOf()
class Toxes : CoroutineContext.Element {
private val list: MutableList<ToxCore> = mutableListOf()

override val key = Toxes
override val key = Toxes

companion object : CoroutineContext.Key<Toxes> {
suspend fun add(makeTox: () -> ToxCore): ToxCore {
val ctx = coroutineContext.get(Toxes)
if (ctx == null) {
throw IllegalStateException("coroutine context has no Toxes object")
}
val tox = makeTox()
ctx.list.add(tox)
return tox
companion object : CoroutineContext.Key<Toxes> {
suspend fun add(makeTox: () -> ToxCore): ToxCore {
val ctx = coroutineContext.get(Toxes)
if (ctx == null) {
throw IllegalStateException("coroutine context has no Toxes object")
}
val tox = makeTox()
ctx.list.add(tox)
return tox
}

suspend fun close() {
val ctx = coroutineContext.get(Toxes)
if (ctx == null) {
throw IllegalStateException("coroutine context has no Toxes object")
}
for (tox in ctx.list) {
tox.close()
}
suspend fun close() {
val ctx = coroutineContext.get(Toxes)
if (ctx == null) {
throw IllegalStateException("coroutine context has no Toxes object")
}
for (tox in ctx.list) {
tox.close()
}
}
}
}

private suspend fun newToxCore(options: ToxOptions): ToxCore = Toxes.add { ToxCoreImpl(options) }
@kotlin.ExperimentalStdlibApi
class ToxCoreTest {
private suspend fun localToxCore(options: ToxOptions): ToxCore = Toxes.add { newToxCore(options) }

private fun runTox(block: suspend CoroutineScope.() -> Unit): Unit =
runBlocking(Toxes()) {
Expand All @@ -56,8 +56,8 @@ class ToxCoreTest {
@Test
fun addFriendNorequest_shouldConnectTwoToxes() =
runTox {
val tox1 = newToxCore(ToxOptions())
val tox2 = newToxCore(ToxOptions())
val tox1 = localToxCore(ToxOptions())
val tox2 = localToxCore(ToxOptions())

tox2.bootstrap("localhost", tox1.getUdpPort, tox1.getDhtId)

Expand Down
7 changes: 7 additions & 0 deletions lib/src/nativeMain/kotlin/im/tox/tox4j/core/ToxCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package im.tox.tox4j.core

import im.tox.tox4j.core.options.ToxOptions
import im.tox.tox4j.impl.cinterop.ToxCoreImpl

@kotlin.ExperimentalStdlibApi
actual fun newToxCore(options: ToxOptions): ToxCore = ToxCoreImpl(options)
Loading

0 comments on commit 1c78089

Please sign in to comment.