From f5b9f8c8297464e1e5008bd0a2d20f443058989f Mon Sep 17 00:00:00 2001 From: George Smyrnaios Date: Wed, 3 Jul 2024 19:17:48 +0200 Subject: [PATCH] Published version 0.1.0 to maven central. --- README.md | 3 +- build.gradle.kts | 127 +++++++++++------- gradle.properties | 7 +- .../cinterop/aarch64-apple-darwin.def | 5 + .../cinterop/aarch64-unknown-linux-gnu.def | 5 + src/nativeInterop/cinterop/librust_lib.def | 0 .../cinterop/x86_64-apple-darwin.def | 5 + .../cinterop/x86_64-unknown-linux-gnu.def | 5 + 8 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 src/nativeInterop/cinterop/aarch64-apple-darwin.def create mode 100644 src/nativeInterop/cinterop/aarch64-unknown-linux-gnu.def delete mode 100644 src/nativeInterop/cinterop/librust_lib.def create mode 100644 src/nativeInterop/cinterop/x86_64-apple-darwin.def create mode 100644 src/nativeInterop/cinterop/x86_64-unknown-linux-gnu.def diff --git a/README.md b/README.md index 386ad6d1..e8cdbc80 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/build.gradle.kts b/build.gradle.kts index ad5b4f12..b8521375 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,4 @@ +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 @@ -5,6 +6,8 @@ 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" @@ -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" @@ -33,30 +39,14 @@ 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( @@ -64,55 +54,98 @@ kotlin { "--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() } diff --git a/gradle.properties b/gradle.properties index 487c2261..c7e3b250 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/nativeInterop/cinterop/aarch64-apple-darwin.def b/src/nativeInterop/cinterop/aarch64-apple-darwin.def new file mode 100644 index 00000000..3fe3aa00 --- /dev/null +++ b/src/nativeInterop/cinterop/aarch64-apple-darwin.def @@ -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 diff --git a/src/nativeInterop/cinterop/aarch64-unknown-linux-gnu.def b/src/nativeInterop/cinterop/aarch64-unknown-linux-gnu.def new file mode 100644 index 00000000..02522134 --- /dev/null +++ b/src/nativeInterop/cinterop/aarch64-unknown-linux-gnu.def @@ -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 diff --git a/src/nativeInterop/cinterop/librust_lib.def b/src/nativeInterop/cinterop/librust_lib.def deleted file mode 100644 index e69de29b..00000000 diff --git a/src/nativeInterop/cinterop/x86_64-apple-darwin.def b/src/nativeInterop/cinterop/x86_64-apple-darwin.def new file mode 100644 index 00000000..42b79b0f --- /dev/null +++ b/src/nativeInterop/cinterop/x86_64-apple-darwin.def @@ -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 diff --git a/src/nativeInterop/cinterop/x86_64-unknown-linux-gnu.def b/src/nativeInterop/cinterop/x86_64-unknown-linux-gnu.def new file mode 100644 index 00000000..c5d0f817 --- /dev/null +++ b/src/nativeInterop/cinterop/x86_64-unknown-linux-gnu.def @@ -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