Skip to content

Commit

Permalink
Added support for mingwX64 target.
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrgeorge committed Sep 10, 2024
1 parent e44c158 commit f171ed5
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 29 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ We support the following targets:
- macosX64
- linuxArm64
- linuxX64
- mingwX64 (soon)
- wasmWasi (potential future support)
- mingwX64
- wasmJs (in progress)

## Why not a pure kotlin implementation?

Expand Down Expand Up @@ -150,11 +150,10 @@ db.listen("chan0") { notification: Postgres.Notification ->
- [x] Transactions
- [x] Listen/Notify Postgres.
- [x] Named parameters (needs enhancements)
- [ ] Reduce duplicate code (in progress)
- [ ] SQLDelight (in progress)
- [ ] Documentation (in progress)
- [ ] SQLDelight
- [ ] Transaction isolation level
- [ ] Testing
- [ ] Documentation (in progress)

## Compilation

Expand All @@ -163,7 +162,7 @@ Check here: https://rustup.rs/

Also, make sure that you have installed all the necessary targets:

```text
```shell
rustup target add aarch64-apple-ios
rustup target add aarch64-apple-darwin
rustup target add x86_64-apple-darwin
Expand All @@ -174,6 +173,12 @@ rustup target add x86_64-unknown-linux-gnu
rustup target add x86_64-pc-windows-gnu
```

We also need to install `cross` (tool that helps with cross-compiling)

```shell
cargo install cross --git https://github.com/cross-rs/cross
```

Then, run the build.

```shell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MultiplatformConventions : Plugin<Project> {
else -> getenv("HOME")
}?.let(::File)
?.resolve(".cargo/bin/cross$exeExt")?.absolutePath
?: throw GradleException("Rust cargo binary is required to build project but it wasn't found.")
?: throw GradleException("Rust cross binary is required to build project but it wasn't found.")


override fun apply(project: Project) {
Expand Down Expand Up @@ -111,7 +111,7 @@ class MultiplatformConventions : Plugin<Project> {
"macosX64",
"linuxArm64",
"linuxX64",
// "mingwX64"
"mingwX64"
)

else -> it.split(",").map { t -> t.trim() }
Expand All @@ -124,19 +124,21 @@ class MultiplatformConventions : Plugin<Project> {

compilations["main"].cinterops {
create("ffi") {
definitionFile.set(file("src/nativeInterop/cinterop/sqlx4k.def"))
if (project.name == "sqlx4k") return@create

if (project.name == "sqlx4k") {
definitionFile.set(file("src/nativeInterop/cinterop/sqlx4k.def"))
return@create
}

if (target == "x86_64-pc-windows-gnu") {
definitionFile.set(file("src/nativeInterop/cinterop/sqlx4k-mingwX64.def"))
} else {
definitionFile.set(file("src/nativeInterop/cinterop/sqlx4k.def"))
}

val cargo = tasks.create("cargo-$target") {
val exec = project.serviceOf<ExecOperations>()
doLast {
if (useCross) {
project.exec {
executable = cargo
args("install", "cross", "--git", "https://github.com/cross-rs/cross")
}
}

exec.exec {
executable = if (useCross) cross else cargo
args(
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "io.github.smyrgeorge"
version = "0.14.0"
version = "0.15.0"

plugins {
alias(libs.plugins.kotlin.multiplatform) apply false
Expand Down
5 changes: 5 additions & 0 deletions sqlx4k-mysql/src/nativeInterop/cinterop/sqlx4k-mingwX64.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package = sqlx4k
headers = sqlx4k_mysql.h
compilerOpts = -I./rust_lib/target
staticLibraries = libsqlx4k_mysql.a
libraryPaths = ./rust_lib/target/x86_64-pc-windows-gnu/release
1 change: 0 additions & 1 deletion sqlx4k-mysql/src/nativeInterop/cinterop/sqlx4k.def
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ libraryPaths.macos_arm64 = ./rust_lib/target/aarch64-apple-darwin/release
libraryPaths.macos_x64 = ./rust_lib/target/x86_64-apple-darwin/release
libraryPaths.linux_arm64 = ./rust_lib/target/aarch64-unknown-linux-gnu/release
libraryPaths.linux_x64 = ./rust_lib/target/x86_64-unknown-linux-gnu/release
libraryPaths.minigw_x64 = ./rust_lib/target/x86_64-pc-windows-gnu/release
6 changes: 3 additions & 3 deletions sqlx4k-postgres/rust_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sqlx::postgres::{
use sqlx::{Column, Executor, Postgres, Row, Transaction, TypeInfo, ValueRef};
use sqlx4k::{c_chars_to_str, sqlx4k_error_result_of, Ptr, Sqlx4kColumn, Sqlx4kResult, Sqlx4kRow};
use std::{
ffi::{c_char, c_int, c_long, c_void, CString},
ffi::{c_char, c_int, c_void, CString},
sync::OnceLock,
};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -278,8 +278,8 @@ pub extern "C" fn sqlx4k_tx_fetch_all(
#[no_mangle]
pub extern "C" fn sqlx4k_listen(
channels: *const c_char,
notify_id: c_long,
notify: unsafe extern "C" fn(c_long, *mut Sqlx4kResult),
notify_id: c_int,
notify: unsafe extern "C" fn(c_int, *mut Sqlx4kResult),
callback: *mut c_void,
fun: unsafe extern "C" fn(Ptr, *mut Sqlx4kResult),
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package = sqlx4k
headers = sqlx4k_postgres.h
compilerOpts = -I./rust_lib/target
staticLibraries = libsqlx4k_postgres.a
libraryPaths = ./rust_lib/target/x86_64-pc-windows-gnu/release
1 change: 0 additions & 1 deletion sqlx4k-postgres/src/nativeInterop/cinterop/sqlx4k.def
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ libraryPaths.macos_arm64 = ./rust_lib/target/aarch64-apple-darwin/release
libraryPaths.macos_x64 = ./rust_lib/target/x86_64-apple-darwin/release
libraryPaths.linux_arm64 = ./rust_lib/target/aarch64-unknown-linux-gnu/release
libraryPaths.linux_x64 = ./rust_lib/target/x86_64-unknown-linux-gnu/release
libraryPaths.minigw_x64 = ./rust_lib/target/x86_64-pc-windows-gnu/release
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PostgreSQL(
}

suspend fun listen(channels: List<String>, f: (Notification) -> Unit) {
val channelId: Long = listenerId()
val channelId: Int = listenerId()
val channel = Channel<Notification>(capacity = Channel.UNLIMITED)

// Store the channel.
Expand Down Expand Up @@ -153,10 +153,11 @@ class PostgreSQL(
)

companion object {
private val channels: MutableMap<Long, Channel<Notification>> by lazy { mutableMapOf() }
private val channels: MutableMap<Int, Channel<Notification>> by lazy { mutableMapOf() }
private val listenerMutex = Mutex()
private var listenerId: Long = 0
private suspend fun listenerId(): Long = listenerMutex.withLock {
private var listenerId: Int = 0
private suspend fun listenerId(): Int = listenerMutex.withLock {
// Will eventually overflow, but it doesn't matter, is the desired behaviour.
listenerId += 1
listenerId
}
Expand All @@ -181,7 +182,7 @@ class PostgreSQL(
}
}

private val notify = staticCFunction<Long, CPointer<Sqlx4kResult>?, Unit> { c, r ->
private val notify = staticCFunction<Int, CPointer<Sqlx4kResult>?, Unit> { c, r ->
channels[c]?.let {
val notification: Notification = r.notify()
runBlocking { it.send(notification) }
Expand Down
5 changes: 5 additions & 0 deletions sqlx4k-sqlite/src/nativeInterop/cinterop/sqlx4k-mingwX64.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package = sqlx4k
headers = sqlx4k_sqlite.h
compilerOpts = -I./rust_lib/target
staticLibraries = libsqlx4k_sqlite.a
libraryPaths = ./rust_lib/target/x86_64-pc-windows-gnu/release
1 change: 0 additions & 1 deletion sqlx4k-sqlite/src/nativeInterop/cinterop/sqlx4k.def
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ libraryPaths.macos_arm64 = ./rust_lib/target/aarch64-apple-darwin/release
libraryPaths.macos_x64 = ./rust_lib/target/x86_64-apple-darwin/release
libraryPaths.linux_arm64 = ./rust_lib/target/aarch64-unknown-linux-gnu/release
libraryPaths.linux_x64 = ./rust_lib/target/x86_64-unknown-linux-gnu/release
libraryPaths.minigw_x64 = ./rust_lib/target/x86_64-pc-windows-gnu/release

0 comments on commit f171ed5

Please sign in to comment.