Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use autolinking on Apple platforms to prevent the need to link with sqlite3 explicitly #120

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion sqliter-driver/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.konan.target.HostManager
import java.io.ByteArrayOutputStream

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -26,7 +29,7 @@ fun configInterop(target: org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTar
)

HostManager.hostIsMingw -> listOf("-linker-options", "-lsqlite3 -Lc:\\msys64\\mingw64\\lib")
else -> listOf("-linker-options", "-lsqlite3")
else -> emptyList()
}
}
}
Expand Down Expand Up @@ -55,6 +58,18 @@ kotlin {
linuxArm64(),
)

if (HostManager.hostIsMac) {
val externalLinkageTask = tasks.register("testExternalLinkage")
tasks.named("allTests").configure {
dependsOn(externalLinkageTask)
}
testExternalLinkage(macosArm64(), "arm64-apple-macosx", "macosx", externalLinkageTask)
testExternalLinkage(macosX64(), "x86_64-apple-macosx", "macosx", externalLinkageTask)
testExternalLinkage(iosArm64(), "arm64-apple-ios", "iphoneos", externalLinkageTask)
testExternalLinkage(iosSimulatorArm64(), "arm64-apple-ios-simulator", "iphonesimulator", externalLinkageTask)
testExternalLinkage(iosX64(), "x86_64-apple-ios-simulator", "iphonesimulator", externalLinkageTask)
}

knTargets
.forEach { target ->
configInterop(target)
Expand Down Expand Up @@ -134,3 +149,46 @@ listOf(
"mingwX64Test",
"linkDebugTestMingwX64",
).forEach { tasks.findByName(it)?.enabled = false }

fun testExternalLinkage(
target: KotlinNativeTarget,
clangTriple: String,
sdkName: String,
aggregateTask: TaskProvider<*>,
) {
target.binaries.framework(buildTypes = listOf(NativeBuildType.DEBUG)) {
val framework = this
baseName = "sqliteDriver"
isStatic = true

val testTask = tasks.register("testExternalLinkage${target.name}") {
val sourceFilesDirectory = layout.projectDirectory.dir("externalLinkageSources")
inputs.files(framework.linkTask, sourceFilesDirectory)
val output = layout.buildDirectory.file("testExternalLinkage/${target.name}")
outputs.dir(output)
doLast {
val sysrootPath: String = ByteArrayOutputStream().use { outputStream ->
project.exec {
commandLine("xcrun", "-sdk", sdkName, "--show-sdk-path")
standardOutput = outputStream
}
outputStream.toString().dropLast(1)
}

exec {
commandLine(
"clang", "-target", clangTriple, "-fmodules", "-ObjC",
"-isysroot", sysrootPath,
"-F", framework.linkTask.outputFile.get().parentFile,
sourceFilesDirectory.file("lib.m"),
"-dynamiclib", "-o", output.get().asFile.resolve("output.dylib")
)
}
}
}

aggregateTask.configure {
dependsOn(testTask)
}
}
}
10 changes: 10 additions & 0 deletions sqliter-driver/externalLinkageSources/lib.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import sqliteDriver;

void forceSqliteSymbolsLoading(NSString * path, SqliteDriverDatabaseConfiguration * configuration) {
SqliteDriverNativeDatabaseManager * foo = [
[SqliteDriverNativeDatabaseManager alloc]
initWithPath: path
configuration: configuration
];
[foo createSingleThreadedConnection];
}
14 changes: 14 additions & 0 deletions sqliter-driver/src/nativeInterop/cinterop/sqlite3.def
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ headerFilter = sqlite3*.h
linkerOpts.linux_x64 = -lpthread -ldl
linkerOpts.macos_x64 = -lpthread -ldl

compilerOpts.ios_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.ios_x64 = -Xclang --linker-option=-lsqlite3
compilerOpts.ios_simulator_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.watchos_arm32 = -Xclang --linker-option=-lsqlite3
compilerOpts.watchos_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.watchos_x64 = -Xclang --linker-option=-lsqlite3
compilerOpts.watchos_simulator_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.watchos_device_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.tvos_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.tvos_x64 = -Xclang --linker-option=-lsqlite3
compilerOpts.tvos_simulator_arm64 = -Xclang --linker-option=-lsqlite3
compilerOpts.macos_x64 = -Xclang --linker-option=-lsqlite3
compilerOpts.macos_arm64 = -Xclang --linker-option=-lsqlite3

noStringConversion = sqlite3_prepare_v2 sqlite3_prepare_v3