1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 **/
16+ import co.touchlab.cklib.gradle.CKlibGradleExtension
1617import co.touchlab.cklib.gradle.CompileToBitcode.Language.C
1718import co.touchlab.cklib.gradle.CompileToBitcodeExtension
19+ import org.gradle.accessors.dm.LibrariesForLibs
1820import org.gradle.jvm.tasks.Jar
1921import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated
2022import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
2123import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
2224import org.jetbrains.kotlin.konan.target.Architecture.*
2325import org.jetbrains.kotlin.konan.target.Family.*
26+ import org.jetbrains.kotlin.konan.target.HostManager
2427import org.jetbrains.kotlin.konan.target.KonanTarget
28+ import org.jetbrains.kotlin.konan.target.TargetSupportException
29+ import org.jetbrains.kotlin.konan.util.ArchiveType
30+ import org.jetbrains.kotlin.konan.util.DependencyProcessor
31+ import org.jetbrains.kotlin.konan.util.DependencySource
2532import java.io.IOException
2633import java.io.InputStream
2734import java.io.OutputStream
@@ -37,7 +44,6 @@ private val jdbcRepack = JdbcRepack()
3744
3845kmpConfiguration {
3946 configureShared {
40-
4147 androidLibrary(namespace = " io.toxicity.sqlite.mc.driver" ) {
4248 target { publishLibraryVariants(" release" ) }
4349
@@ -65,6 +71,12 @@ kmpConfiguration {
6571 implementation(files(jdbcRepack.jarSQLiteJDBCAndroid))
6672 }
6773 }
74+ sourceSetTestInstrumented {
75+ dependencies {
76+ implementation(libs.androidx.test.core)
77+ implementation(libs.androidx.test.runner)
78+ }
79+ }
6880 }
6981
7082 jvm {
@@ -126,7 +138,7 @@ kmpConfiguration {
126138 }
127139
128140 project.extensions.configure<CompileToBitcodeExtension >(" cklib" ) {
129- config.kotlinVersion = libs.versions.gradle.kotlin.get( )
141+ config.configure(libs )
130142
131143 create(" sqlite3mc" ) {
132144 language = C
@@ -159,6 +171,7 @@ kmpConfiguration {
159171 // Warning/Error suppression flags
160172 buildList {
161173 add(" -Wno-sign-compare" )
174+ add(" -Wno-unused-but-set-variable" )
162175 add(" -Wno-unused-function" )
163176 add(" -Wno-unused-parameter" )
164177 add(" -Wno-unused-variable" )
@@ -257,7 +270,8 @@ kmpConfiguration {
257270 }
258271}
259272
260- tasks.getByName(" clean" ) {
273+ tasks.all {
274+ if (name != " clean" ) return @all
261275 doLast {
262276 projectDir
263277 .resolve(" src" )
@@ -570,3 +584,76 @@ private class JdbcRepack {
570584 }
571585 }
572586}
587+
588+ // CKLib uses too old of a version of LLVM for current version of Kotlin which produces errors for android
589+ // native due to unsupported link arguments. Below is a supplemental implementation to download and use
590+ // the -dev llvm compiler for the current kotlin version.
591+ //
592+ // The following info can be found in ~/.konan/kotlin-native-prebuild-{os}-{arch}-{kotlin version}/konan/konan.properties
593+ private object LLVM {
594+ const val URL : String = " https://download.jetbrains.com/kotlin/native/resources/llvm"
595+ const val VERSION : String = " 16.0.0"
596+
597+ // llvm-{llvm version}-{arch}-{host}-dev-{id}
598+ object DevID {
599+ object Linux {
600+ const val x86_64: Int = 80
601+ }
602+ object MacOS {
603+ const val aarch64: Int = 65
604+ const val x86_64: Int = 56
605+ }
606+ object MinGW {
607+ const val x86_64: Int = 56
608+ }
609+ }
610+ }
611+
612+ private fun CKlibGradleExtension.configure (libs : LibrariesForLibs ) {
613+ kotlinVersion = libs.versions.gradle.kotlin.get()
614+ check(kotlinVersion == " 2.1.21" ) {
615+ " Kotlin version out of date! Download URLs for LLVM need to be updated for ${project.path} "
616+ }
617+
618+ val host = HostManager .simpleOsName()
619+ val arch = HostManager .hostArch()
620+ val (id, archive) = when (host) {
621+ " linux" -> when (arch) {
622+ " x86_64" -> LLVM .DevID .Linux .x86_64 to ArchiveType .TAR_GZ
623+ else -> null
624+ }
625+ " macos" -> when (arch) {
626+ " aarch64" -> LLVM .DevID .MacOS .aarch64 to ArchiveType .TAR_GZ
627+ " x86_64" -> LLVM .DevID .MacOS .x86_64 to ArchiveType .TAR_GZ
628+ else -> null
629+ }
630+ " windows" -> when (arch) {
631+ " x86_64" -> LLVM .DevID .MinGW .x86_64 to ArchiveType .ZIP
632+ else -> null
633+ }
634+ else -> null
635+ } ? : throw TargetSupportException (" Unsupported host[$host ] or arch[$arch ]" )
636+
637+ val llvmDev = " llvm-${LLVM .VERSION } -${arch} -${host} -dev-${id} "
638+ val cklibDir = File (System .getProperty(" user.home" )).resolve(" .cklib" )
639+ llvmHome = cklibDir.resolve(llvmDev).path
640+
641+ val source = DependencySource .Remote .Public (subDirectory = " ${LLVM .VERSION } -${arch} -${host} " )
642+
643+ DependencyProcessor (
644+ dependenciesRoot = cklibDir,
645+ dependenciesUrl = LLVM .URL ,
646+ dependencyToCandidates = mapOf (llvmDev to listOf (source)),
647+ homeDependencyCache = cklibDir.resolve(" cache" ),
648+ customProgressCallback = { _, currentBytes, totalBytes ->
649+ val total = totalBytes.toString()
650+ var current = currentBytes.toString()
651+ while (current.length < 15 && current.length < total.length) {
652+ current = " $current "
653+ }
654+
655+ println (" Downloading[$llvmDev ] - $current / $total " )
656+ },
657+ archiveType = archive,
658+ ).run ()
659+ }
0 commit comments