Skip to content

Commit 3a57b1b

Browse files
committed
Simplify jvm build
1 parent bbe0ecc commit 3a57b1b

File tree

13 files changed

+173
-273
lines changed

13 files changed

+173
-273
lines changed

core/build.gradle.kts

Lines changed: 3 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import app.cash.sqldelight.core.capitalize
21
import com.powersync.plugins.sonatype.setupGithubRepository
32
import de.undercouch.gradle.tasks.download.Download
43
import org.gradle.internal.os.OperatingSystem
54
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
65
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
76
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
87
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
9-
import java.util.*
108

119
plugins {
1210
alias(libs.plugins.kotlinMultiplatform)
@@ -140,6 +138,7 @@ kotlin {
140138
api(libs.kermit)
141139
}
142140

141+
androidMain.get()
143142
androidMain.dependencies {
144143
implementation(libs.ktor.client.okhttp)
145144
}
@@ -157,6 +156,7 @@ kotlin {
157156
implementation(libs.kotlin.test)
158157
implementation(libs.test.coroutines)
159158
implementation(libs.kermit.test)
159+
implementation(libs.test.turbine)
160160
}
161161
}
162162
}
@@ -218,110 +218,6 @@ if (binariesAreProvided && crossArch) {
218218
error("powersync.binaries.provided and powersync.binaries.cross-arch must not be both defined.")
219219
}
220220

221-
val getBinaries = if (binariesAreProvided) {
222-
// Binaries for all OS must be provided (manually or by the CI) in binaries/desktop
223-
224-
val verifyPowersyncBinaries = tasks.register("verifyPowersyncBinaries") {
225-
val directory = projectDir.resolve("binaries/desktop")
226-
val binaries = listOf(
227-
directory.resolve("libpowersync-sqlite_aarch64.so"),
228-
directory.resolve("libpowersync-sqlite_x64.so"),
229-
directory.resolve("libpowersync-sqlite_aarch64.dylib"),
230-
directory.resolve("libpowersync-sqlite_x64.dylib"),
231-
directory.resolve("powersync-sqlite_x64.dll"),
232-
)
233-
doLast {
234-
binaries.forEach {
235-
if (!it.exists()) error("File $it does not exist")
236-
if (!it.isFile) error("File $it is not a regular file")
237-
}
238-
}
239-
outputs.files(*binaries.toTypedArray())
240-
}
241-
verifyPowersyncBinaries
242-
} else {
243-
// Building locally for the current OS
244-
245-
val localProperties = Properties()
246-
val localPropertiesFile = rootProject.file("local.properties")
247-
if (localPropertiesFile.exists()) {
248-
localPropertiesFile.inputStream().use { localProperties.load(it) }
249-
}
250-
val cmakeExecutable = localProperties.getProperty("cmake.path") ?: "cmake"
251-
252-
fun registerCMakeTasks(
253-
suffix: String,
254-
vararg defines: String,
255-
): TaskProvider<Exec> {
256-
val cmakeConfigure = tasks.register<Exec>("cmakeJvmConfigure${suffix.capitalize()}") {
257-
dependsOn(unzipSQLiteSources)
258-
group = "cmake"
259-
workingDir = layout.buildDirectory.dir("cmake/$suffix").get().asFile
260-
inputs.files(
261-
"src/jvmMain/cpp",
262-
"src/jvmNative/cpp",
263-
sqliteSrcFolder,
264-
)
265-
outputs.dir(workingDir)
266-
executable = cmakeExecutable
267-
args(listOf(file("src/jvmMain/cpp/CMakeLists.txt").absolutePath, "-DSUFFIX=$suffix", "-DCMAKE_BUILD_TYPE=Release") + defines.map { "-D$it" })
268-
doFirst {
269-
workingDir.mkdirs()
270-
}
271-
}
272-
273-
val cmakeBuild = tasks.register<Exec>("cmakeJvmBuild${suffix.capitalize()}") {
274-
dependsOn(cmakeConfigure)
275-
group = "cmake"
276-
workingDir = layout.buildDirectory.dir("cmake/$suffix").get().asFile
277-
inputs.files(
278-
"src/jvmMain/cpp",
279-
"src/jvmNative/cpp",
280-
sqliteSrcFolder,
281-
workingDir,
282-
)
283-
outputs.dir(workingDir.resolve(if (os.isWindows) "output/Release" else "output"))
284-
executable = cmakeExecutable
285-
args("--build", ".", "--config", "Release")
286-
}
287-
288-
return cmakeBuild
289-
}
290-
291-
val (aarch64, x64) = when {
292-
os.isMacOsX -> {
293-
val aarch64 = registerCMakeTasks("aarch64", "CMAKE_OSX_ARCHITECTURES=arm64")
294-
val x64 = registerCMakeTasks("x64", "CMAKE_OSX_ARCHITECTURES=x86_64")
295-
aarch64 to x64
296-
}
297-
os.isLinux -> {
298-
val aarch64 = registerCMakeTasks("aarch64", "CMAKE_C_COMPILER=aarch64-linux-gnu-gcc", "CMAKE_CXX_COMPILER=aarch64-linux-gnu-g++")
299-
val x64 = registerCMakeTasks("x64", "CMAKE_C_COMPILER=x86_64-linux-gnu-gcc", "CMAKE_CXX_COMPILER=x86_64-linux-gnu-g++")
300-
aarch64 to x64
301-
}
302-
os.isWindows -> {
303-
val x64 = registerCMakeTasks("x64")
304-
null to x64
305-
}
306-
else -> error("Unknown operating system: $os")
307-
}
308-
309-
val arch = System.getProperty("os.arch")
310-
val cmakeJvmBuilds = when {
311-
crossArch -> listOfNotNull(aarch64, x64)
312-
arch == "aarch64" -> listOfNotNull(aarch64)
313-
arch == "amd64" || arch == "x86_64" -> listOfNotNull(x64)
314-
else -> error("Unsupported architecture: $arch")
315-
}
316-
317-
tasks.register<Copy>("cmakeJvmBuild") {
318-
dependsOn(cmakeJvmBuilds)
319-
group = "cmake"
320-
from(cmakeJvmBuilds)
321-
into(binariesFolder.map { it.dir("sqlite") })
322-
}
323-
}
324-
325221
val downloadPowersyncDesktopBinaries = tasks.register<Download>("downloadPowersyncDesktopBinaries") {
326222
val coreVersion = libs.versions.powersync.core.get()
327223
val linux_aarch64 = "https://github.com/powersync-ja/powersync-sqlite-core/releases/download/v$coreVersion/libpowersync_aarch64.so"
@@ -351,7 +247,7 @@ val downloadPowersyncDesktopBinaries = tasks.register<Download>("downloadPowersy
351247
}
352248

353249
tasks.named<ProcessResources>(kotlin.jvm().compilations["main"].processResourcesTaskName) {
354-
from(getBinaries, downloadPowersyncDesktopBinaries)
250+
from(downloadPowersyncDesktopBinaries)
355251
}
356252

357253
// We want to build with recent JDKs, but need to make sure we support Java 8. https://jakewharton.com/build-on-latest-java-test-through-lowest-java/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.powersync
2+
3+
internal interface UpdateHookReceiver {
4+
// These methods are used by native code
5+
@Suppress("unused")
6+
fun onTableUpdate(tableName: String)
7+
8+
@Suppress("unused")
9+
fun onTransactionResult(commit: Boolean)
10+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.powersync
2+
3+
import app.cash.turbine.turbineScope
4+
import com.powersync.db.SqlCursor
5+
import com.powersync.db.getString
6+
import com.powersync.db.schema.Column
7+
import com.powersync.db.schema.Schema
8+
import com.powersync.db.schema.Table
9+
import com.powersync.testutils.cleanup
10+
import com.powersync.testutils.factory
11+
import kotlinx.coroutines.test.runTest
12+
import kotlin.test.AfterTest
13+
import kotlin.test.BeforeTest
14+
import kotlin.test.Test
15+
import kotlin.test.assertEquals
16+
17+
class DatabaseTest {
18+
private lateinit var database: PowerSyncDatabase
19+
20+
@BeforeTest
21+
fun setupDatabase() {
22+
database = PowerSyncDatabase(
23+
factory = factory,
24+
schema = Schema(
25+
Table(name = "users", columns = listOf(Column.text("name"), Column.text("email")))
26+
),
27+
dbFilename = "testdb"
28+
)
29+
}
30+
31+
@AfterTest
32+
fun tearDown() {
33+
cleanup("testdb")
34+
}
35+
36+
@Test
37+
fun testLinksPowerSync() = runTest {
38+
database.get("SELECT powersync_rs_version() AS r;") { it.getString(0)!! }
39+
}
40+
41+
@Test
42+
fun testTableUpdates() = runTest {
43+
turbineScope {
44+
val query = database.watch("SELECT * FROM users") { User.from(it) }.testIn(this)
45+
46+
// Wait for initial query
47+
assertEquals(0, query.awaitItem().size)
48+
49+
database.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("Test", "[email protected]"))
50+
assertEquals(1, query.awaitItem().size)
51+
52+
database.writeTransaction {
53+
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("Test2", "[email protected]"))
54+
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("Test3", "[email protected]"))
55+
}
56+
57+
assertEquals(3, query.awaitItem().size)
58+
59+
try {
60+
database.writeTransaction {
61+
it.execute("DELETE FROM users;")
62+
it.execute("syntax error, revert please")
63+
}
64+
} catch (e: Exception) {
65+
// Ignore
66+
}
67+
68+
database.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("Test4", "[email protected]"))
69+
assertEquals(4, query.awaitItem().size)
70+
71+
query.expectNoEvents()
72+
query.cancel()
73+
}
74+
}
75+
76+
private data class User(val id: String, val name: String, val email: String) {
77+
companion object {
78+
fun from(cursor: SqlCursor): User = User(
79+
id = cursor.getString("id"),
80+
name = cursor.getString("name"),
81+
email = cursor.getString("email")
82+
)
83+
}
84+
}
85+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.powersync.testutils
2+
3+
import com.powersync.DatabaseDriverFactory
4+
5+
expect val factory: DatabaseDriverFactory
6+
expect fun cleanup(path: String)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.powersync.testutils
2+
3+
import com.powersync.DatabaseDriverFactory
4+
import kotlinx.io.files.Path
5+
import kotlinx.io.files.SystemFileSystem
6+
7+
actual val factory: DatabaseDriverFactory
8+
get() = DatabaseDriverFactory()
9+
10+
actual fun cleanup(path: String) {
11+
SystemFileSystem.delete(Path(path))
12+
}

core/src/jvmMain/cpp/CMakeLists.txt

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)