Skip to content

Commit

Permalink
Published version 0.1.0 to maven central.
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrgeorge committed Jul 3, 2024
1 parent b4fda65 commit f5b9f8c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 54 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Sqlx4k

![Build](https://github.com/smyrgeorge/sqlx4k/actions/workflows/ci.yml/badge.svg)
![Maven Central](https://img.shields.io/maven-central/v/io.github.smyrgeorge/sqlx4k)
![GitHub License](https://img.shields.io/github/license/smyrgeorge/sqlx4k)
![GitHub commit activity](https://img.shields.io/github/commit-activity/w/smyrgeorge/sqlx4k)
![GitHub issues](https://img.shields.io/github/issues/smyrgeorge/sqlx4k)
Expand Down Expand Up @@ -77,7 +78,7 @@ tx1.commit().getOrThrow()
- [x] Transactions
- [x] Named parameters
- [ ] Transaction isolation level
- [ ] Publish to maven central
- [x] Publish to maven central
- [x] Better error handling (in progress)
- [x] Check for memory leaks
- [ ] Testing
Expand Down
127 changes: 80 additions & 47 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import java.lang.System.getenv

plugins {
// https://plugins.gradle.org/plugin/org.jetbrains.kotlin.multiplatform
kotlin("multiplatform") version "2.0.0"
// https://github.com/vanniktech/gradle-maven-publish-plugin
id("com.vanniktech.maven.publish") version "0.28.0"
}

group = "io.github.smyrgeorge"
Expand All @@ -17,6 +20,9 @@ repositories {
private val os = DefaultNativePlatform.getCurrentOperatingSystem()
private val arch = DefaultNativePlatform.getCurrentArchitecture()

fun projectFile(path: String): String =
projectDir.resolve(path).absolutePath

private val exeExt: String
get() = when {
os.isWindows -> ".exe"
Expand All @@ -33,86 +39,113 @@ private val cargo: String
?.absolutePath
?: throw GradleException("Rust cargo binary is required to build project but it wasn't found.")

kotlin {
fun projectFile(path: String): String =
projectDir.resolve(path).absolutePath

applyDefaultHierarchyTemplate()

val host: Host = when {
os.isMacOsX && arch.isArm64 -> Host(macosArm64(), "aarch64-apple-darwin")
os.isMacOsX && arch.isAmd64 -> Host(macosX64(), "x86_64-apple-darwin")
os.isLinux && arch.isArm64 -> Host(linuxArm64(), "aarch64-unknown-linux-gnu")
os.isLinux && arch.isAmd64 -> Host(linuxX64(), "x86_64-unknown-linux-gnu")
os.isWindows && arch.isAmd64 -> Host(mingwX64())
else -> throw GradleException("OS: $os and architecture: $arch is not supported in script configuration.")
}

tasks.create("binaries") {
dependsOn("${host.target.targetName}Binaries")
doLast { host.renameBinaries() }
}
val chosenTargets = (properties["targets"] as? String)?.split(",")
?: listOf("macosArm64", "macosX64", "linuxArm64", "linuxX64")

host.target {
kotlin {
fun KotlinNativeTarget.rust(target: String) {
compilations.getByName("main").cinterops {
create("librust_lib") {
val buildRustLib by tasks.creating {
val cargo = tasks.create("cargo-$target") {
exec {
executable = cargo
args(
"build",
"--manifest-path", projectFile("rust_lib/Cargo.toml"),
"--package", "rust_lib",
"--lib",
// Had to specify manually here.
// When running/building from IntelliJ the arch is amd64 (not arm64),
// but cargo runs with arm64.
host.rustTarget?.let { "--target=$it" } ?: "",
"--target=$target",
"--release"
)
}
}

tasks.getByName(interopProcessingTaskName) {
dependsOn(buildRustLib)
dependsOn(cargo)
}
header(projectFile("rust_lib/target/rust_lib.h"))

definitionFile.set(projectDir.resolve("src/nativeInterop/cinterop/$target.def"))
}
}

// TODO: remove when we start building proper tests.
binaries.executable {
entryPoint = "main"
baseName = "sqlx4k"
linkerOpts += projectFile(
path = when {
os.isWindows -> "rust_lib/target/${host.rustTarget}/release/rust_lib.lib"
else -> "rust_lib/target/${host.rustTarget}/release/librust_lib.a"
os.isWindows -> "rust_lib/target/$target/release/rust_lib.lib"
else -> "rust_lib/target/$target/release/librust_lib.a"
}
)
}

}

val availableTargets = mapOf(
Pair("macosArm64") { macosArm64 { rust("aarch64-apple-darwin") } },
Pair("macosX64") { macosX64 { rust("x86_64-apple-darwin") } },
Pair("linuxArm64") { linuxArm64 { rust("aarch64-unknown-linux-gnu") } },
Pair("linuxX64") { linuxX64 { rust("x86_64-unknown-linux-gnu") } },
)
chosenTargets.forEach {
println("Enabling target $it")
availableTargets[it]?.invoke()
}

applyDefaultHierarchyTemplate()

sourceSets {
getByName("nativeMain").dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
configureEach {
languageSettings.progressiveMode = true
}
val nativeMain by getting {
dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}

class Host(
val target: KotlinNativeTarget,
val rustTarget: String? = null
) {

fun target(configure: KotlinNativeTarget.() -> Unit): Unit =
target.run(configure)

fun renameBinaries() {
project.layout.buildDirectory.get().asFile
.resolve("bin/${target.name}")
.walkTopDown().forEach {
if (it.extension != "kexe") return@forEach
val renamed = it.parentFile.resolve("${it.nameWithoutExtension}$exeExt")
it.renameTo(renamed)
mavenPublishing {
coordinates(
groupId = group as String,
artifactId = name,
version = version as String
)

pom {
name = "sqlx4k"
description = "A small non-blocking database driver written in Kotlin for the Native platform."
url = "https://github.com/smyrgeorge/sqlx4k"

licenses {
license {
name = "MIT License"
url = "https://github.com/smyrgeorge/sqlx4k/blob/main/LICENSE"
}
}

developers {
developer {
id = "smyrgeorge"
name = "Yorgos S."
email = "smyrgoerge@gmail.com"
url = "https://smyrgeorge.github.io/"
}
}

scm {
url = "https://github.com/smyrgeorge/sqlx4k"
connection = "scm:git:https://github.com/smyrgeorge/sqlx4k.git"
developerConnection = "scm:git:git@github.com:smyrgeorge/sqlx4k.git"
}
}

// Configure publishing to Maven Central
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

// Enable GPG signing for all publications
signAllPublications()
}
7 changes: 1 addition & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
#Kotlin
kotlin.code.style=official
kotlin.native.cacheKind.mingwX64=none
kotlin.native.cacheKind.linuxX64=none
kotlin.native.cacheKind.linuxArm64=none
kotlin.native.cacheKind.macosX64=none
kotlin.native.cacheKind.macosArm64=none
kotlin.mpp.enableCInteropCommonization=true
5 changes: 5 additions & 0 deletions src/nativeInterop/cinterop/aarch64-apple-darwin.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers = rust_lib.h
package = librust_lib

compilerOpts = -I./rust_lib/target
linkerOpts = -L./rust_lib/target/aarch64-apple-darwin/release/librust_lib.a
5 changes: 5 additions & 0 deletions src/nativeInterop/cinterop/aarch64-unknown-linux-gnu.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers = rust_lib.h
package = librust_lib

compilerOpts = -I./rust_lib/target
linkerOpts = -L./rust_lib/target/aarch64-unknown-linux-gnu/release/librust_lib.a
Empty file.
5 changes: 5 additions & 0 deletions src/nativeInterop/cinterop/x86_64-apple-darwin.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers = rust_lib.h
package = librust_lib

compilerOpts = -I./rust_lib/target
linkerOpts = -L./rust_lib/target/x86_64-apple-darwin/release/librust_lib.a
5 changes: 5 additions & 0 deletions src/nativeInterop/cinterop/x86_64-unknown-linux-gnu.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers = rust_lib.h
package = librust_lib

compilerOpts = -I./rust_lib/target
linkerOpts = -L./rust_lib/target/x86_64-unknown-linux-gnu/release/librust_lib.a

0 comments on commit f5b9f8c

Please sign in to comment.