Skip to content

Commit e2055c1

Browse files
committed
Implement linux target and actual implementations
1 parent 4cda0af commit e2055c1

File tree

5 files changed

+729
-30
lines changed

5 files changed

+729
-30
lines changed

sqliter-driver/build.gradle.kts

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,30 @@ fun configInterop(target: org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTar
1818
val onWindows = org.jetbrains.kotlin.konan.target.HostManager.hostIsMingw
1919

2020
kotlin {
21-
val knTargets = listOf(
22-
macosX64(),
23-
iosX64(),
24-
iosArm64(),
25-
iosArm32(),
26-
watchosArm32(),
27-
watchosArm64(),
28-
watchosX86(),
29-
watchosX64(),
30-
tvosArm64(),
31-
tvosX64(),
32-
mingwX64("mingw") {
33-
compilations.forEach {
34-
it.kotlinOptions.freeCompilerArgs += listOf("-linker-options", "-Lc:\\msys64\\mingw64\\lib")
35-
}
36-
}
37-
)
21+
val knTargets = listOf(
22+
macosX64(),
23+
iosX64(),
24+
iosArm64(),
25+
iosArm32(),
26+
watchosArm32(),
27+
watchosArm64(),
28+
watchosX86(),
29+
watchosX64(),tvosArm64(),
30+
tvosX64(),
31+
mingwX64("mingw") {
32+
compilations.forEach {
33+
it.kotlinOptions.freeCompilerArgs += listOf("-linker-options", "-Lc:\\msys64\\mingw64\\lib")
34+
}
35+
},
36+
linuxX64()
37+
)
3838

39-
knTargets.forEach { configInterop(it) }
39+
knTargets.forEach { configInterop(it) }
4040

4141
knTargets.forEach { target ->
42-
val test by target.compilations.getting
43-
test.kotlinOptions.freeCompilerArgs += listOf("-linker-options", "-lsqlite3")
42+
configInterop(target)
43+
val test by target.compilations.getting
44+
test.kotlinOptions.freeCompilerArgs += listOf("-linker-options", "-lsqlite3", "-L/usr/lib")
4445
}
4546

4647
sourceSets {
@@ -63,20 +64,32 @@ kotlin {
6364
dependsOn(nativeCommonMain)
6465
}
6566

67+
val linuxMain = sourceSets.maybeCreate("linuxX64Main").apply {
68+
dependsOn(nativeCommonMain)
69+
}
70+
6671
val mingwMain = sourceSets.maybeCreate("mingwMain").apply {
6772
dependsOn(nativeCommonMain)
6873
}
6974
knTargets.forEach { target ->
70-
if (target.name.startsWith("mingw")) {
71-
target.compilations.getByName("main").source(mingwMain)
72-
target.compilations.getByName("test").source(nativeCommonTest)
73-
} else {
74-
target.compilations.getByName("main").source(appleMain)
75-
target.compilations.getByName("test").source(nativeCommonTest)
76-
}
77-
}
75+
when {
76+
target.name.startsWith("mingw") -> {
77+
target.compilations.getByName("main").source(mingwMain)
78+
target.compilations.getByName("test").source(nativeCommonTest)
79+
}
80+
target.name.startsWith("linux") -> {
81+
target.compilations.getByName("main").source(linuxMain)
82+
target.compilations.getByName("test").source(nativeCommonTest)
83+
}
84+
else -> {
85+
target.compilations.getByName("main").source(appleMain)
86+
target.compilations.getByName("test").source(nativeCommonTest)
87+
}
88+
}
7889

79-
}
90+
}
91+
}
8092
}
8193

82-
apply(from = "../gradle/gradle-mvn-mpp-push.gradle")
94+
apply(from = "../gradle/gradle-mvn-mpp-push.gradle")
95+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package co.touchlab.sqliter
2+
3+
import co.touchlab.sqliter.internal.File
4+
import co.touchlab.sqliter.internal.FileFilter
5+
import kotlinx.cinterop.memScoped
6+
import kotlinx.cinterop.refTo
7+
import kotlinx.cinterop.toKString
8+
import platform.posix.NULL
9+
import platform.posix.PATH_MAX
10+
import platform.posix.getcwd
11+
import platform.posix.getenv
12+
13+
actual object DatabaseFileContext {
14+
actual fun deleteDatabase(name: String, basePath: String?) {
15+
deleteDatabaseFile(databaseFile(name, basePath))
16+
}
17+
18+
actual fun databasePath(databaseName: String, datapathPath: String?): String {
19+
return databaseFile(databaseName, datapathPath).path
20+
}
21+
22+
internal fun databaseDirPath(): String {
23+
return getHomeDirPath() ?: memScoped {
24+
val buff = ByteArray(PATH_MAX)
25+
if (getcwd(buff.refTo(0), buff.size.toULong()) != NULL) {
26+
return buff.toKString()
27+
}
28+
throw IllegalStateException("Cannot get home dir or current dir")
29+
}
30+
}
31+
32+
internal fun getHomeDirPath(): String? = getenv("HOME")?.toKString()
33+
34+
internal fun databaseFile(databaseName: String, datapathPath: String?): File =
35+
File(datapathPath ?: databaseDirPath(), databaseName)
36+
37+
internal fun deleteDatabaseFile(file: File): Boolean {
38+
var deleted = false
39+
deleted = deleted or file.delete()
40+
deleted = deleted or File(file.getPath() + "-journal").delete()
41+
deleted = deleted or File(file.getPath() + "-shm").delete()
42+
deleted = deleted or File(file.getPath() + "-wal").delete()
43+
44+
//TODO: Implement file list
45+
val dir = file.getParentFile()
46+
if (dir != null) {
47+
val prefix = file.getName() + "-mj"
48+
val files = dir.listFiles(object : FileFilter {
49+
override fun accept(candidate: File): Boolean {
50+
return candidate.getName().startsWith(prefix)
51+
}
52+
})
53+
if (files != null) {
54+
for (masterJournal in files) {
55+
deleted = deleted or masterJournal.delete()
56+
}
57+
}
58+
}
59+
return deleted
60+
}
61+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package co.touchlab.sqliter.concurrency
2+
3+
import kotlinx.cinterop.Arena
4+
import kotlinx.cinterop.alloc
5+
import kotlinx.cinterop.ptr
6+
import platform.posix.*
7+
import kotlin.native.concurrent.freeze
8+
9+
/**
10+
* A simple lock.
11+
* Implementations of this class should be re-entrant.
12+
*/
13+
actual class Lock actual constructor() {
14+
private val arena = Arena()
15+
private val attr = arena.alloc<pthread_mutexattr_t>()
16+
private val mutex = arena.alloc<pthread_mutex_t>()
17+
18+
init {
19+
pthread_mutexattr_init(attr.ptr)
20+
pthread_mutexattr_settype(attr.ptr, PTHREAD_MUTEX_RECURSIVE.toInt())
21+
pthread_mutex_init(mutex.ptr, attr.ptr)
22+
freeze()
23+
}
24+
25+
actual fun lock() {
26+
pthread_mutex_lock(mutex.ptr)
27+
}
28+
29+
actual fun unlock() {
30+
pthread_mutex_unlock(mutex.ptr)
31+
}
32+
33+
actual fun tryLock(): Boolean = pthread_mutex_trylock(mutex.ptr) == 0
34+
35+
fun internalClose() {
36+
pthread_mutex_destroy(mutex.ptr)
37+
pthread_mutexattr_destroy(attr.ptr)
38+
arena.clear()
39+
}
40+
}
41+
42+
actual inline fun Lock.close() {
43+
internalClose()
44+
}

0 commit comments

Comments
 (0)