Skip to content

Commit 82e64fe

Browse files
authored
Merge pull request #4 from touchlab/kpg/for_kotlin_160
Kpg/for kotlin 160
2 parents 91a958f + be147bf commit 82e64fe

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@
22

33
CKlib is a gradle plugin that will build and package C/C++/Objective-C code for Kotlin/Native.
44

5-
# Note
5+
# Usage
6+
7+
Add gradle plugins
8+
9+
```kotlin
10+
plugins {
11+
kotlin("multiplatform")
12+
id("co.touchlab.cklib")
13+
}
14+
```
15+
16+
Add Kotlin version and define some C-like source:
617

7-
The main Kotlin project has changed how locally embedded C-like code is included in libraries, so we'll probably remove
8-
this project from public access soon until we land on a complete answer to handling this (our current solution is kind
9-
of a copy/paste of the main Kotlin solution).
18+
```kotlin
19+
cklib {
20+
config.kotlinVersion = KOTLIN_VERSION
21+
create("objcsample") {
22+
language = Language.OBJC
23+
}
24+
}
25+
```
26+
27+
See example in [Kermit](https://github.com/touchlab/Kermit/blob/main/kermit-crashlytics-test/build.gradle.kts#L69)
28+
29+
# Note
1030

11-
You can use this, but we won't be supporting it publicly as it's kind of brittle to set up and debug. Just FYI.
31+
The main Kotlin project has changed how locally embedded C-like code is included in libraries. Use
32+
this project if you'd like, but outside of private projects we won't really be supporting it much.
1233

1334
License
1435
=======

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ org.gradle.jvmargs=-Xmx2g
1515

1616
GROUP=co.touchlab
1717
KOTLIN_VERSION=1.6.0
18-
CKLIB_VERSION=0.2.2
18+
CKLIB_VERSION=0.2.4
1919

2020
POM_NAME=CKlib
2121
POM_DESCRIPTION=C/C++ Bitcode Into Klib

plugin/src/main/kotlin/co/touchlab/cklib/gradle/CKlibGradleExtension.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ open class CKlibGradleExtension @Inject constructor(val project: Project) {
3939
}
4040

4141
var llvmHome: String
42-
get() = _llvmHome ?: "${System.getProperty("user.home")}/.cklib/clang-llvm-apple-8.0.0-darwin-macos"
42+
get() = _llvmHome ?: "$defaultCklibDir/${llvmName}"
4343
set(value) {
4444
_llvmHome = value
4545
}
4646
}
4747

48+
internal val defaultCklibDir:String
49+
get() = "${System.getProperty("user.home")}/.cklib"
50+
4851
internal val Project.platformManager: PlatformManager
4952
get() {
5053
val cklibExtension = extensions.getByType(CompileToBitcodeExtension::class.java)
@@ -77,13 +80,18 @@ internal val osName: String
7780
internal val llvmName: String
7881
get() {
7982
return when (osName) {
80-
"osx" -> llvm_macos_x64
83+
"osx" -> if (hostArch == "aarch64") {
84+
llvm_macos_arm64
85+
} else {
86+
llvm_macos_x64
87+
}
8188
"linux" -> llvm_linux_x64
8289
"windows" -> llvm_mingw_x64
8390
else -> throw TargetSupportException("Unknown operating system: $osName")
8491
}
8592
}
8693

94+
//https://download.jetbrains.com/kotlin/native/clang-llvm-8.0.0-linux-x86-64.tar.gz
8795
internal val llvm_linux_x64 = "clang-llvm-8.0.0-linux-x86-64"
8896
internal val llvm_mingw_x64 = "msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1"
8997
internal val llvm_macos_x64 = "clang-llvm-apple-8.0.0-darwin-macos"

plugin/src/main/kotlin/co/touchlab/cklib/gradle/CompileToBitcodePlugin.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010

1111
package co.touchlab.cklib.gradle
1212

13+
import org.gradle.api.GradleException
1314
import org.gradle.api.Plugin
1415
import org.gradle.api.Project
1516
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
16-
import org.rauschig.jarchivelib.ArchiveFormat
17-
import org.rauschig.jarchivelib.ArchiverFactory
1817
import java.io.BufferedInputStream
1918
import java.io.File
2019
import java.io.FileOutputStream
@@ -23,6 +22,9 @@ import java.util.*
2322

2423
class CompileToBitcodePlugin : Plugin<Project> {
2524
override fun apply(target: Project) = with(target) {
25+
if (osName == "windows") {
26+
throw GradleException("Windows is currently not compatible with cklib")
27+
}
2628
extensions.create(EXTENSION_NAME, CompileToBitcodeExtension::class.java, target)
2729
downloadIfNeeded(target)
2830

@@ -45,31 +47,31 @@ class CompileToBitcodePlugin : Plugin<Project> {
4547
//This is pretty hacky, but the process changed in 1.6.0. We'll probably just split off and do our own thing
4648
//going forward, but need to be able to build for the next few weeks.
4749
private fun downloadIfNeeded(target: Project) {
48-
val cklibDir = File("${System.getProperty("user.home")}/.cklib")
49-
val localFile = File(cklibDir, "clang-llvm-apple-8.0.0-darwin-macos")
50+
val cklibDir = File(defaultCklibDir)
51+
val localFile = File(cklibDir, llvmName)
5052
val clangExists = localFile.exists()
5153
if(!clangExists){
5254
target.logger.info("cklib downloading dependencies (may take a while...)")
5355
cklibDir.mkdirs()
5456
val tempFileName = UUID.randomUUID().toString()
55-
val extractDir = File(cklibDir, tempFileName)
56-
val tempDl = File(cklibDir, "${tempFileName}.zip")
57+
val tempFileNameWithExtension = "${tempFileName}.tar.gz"
58+
val tempDl = File(cklibDir, tempFileNameWithExtension)
5759

5860
try {
5961
val fos = FileOutputStream(tempDl)
60-
val inp = BufferedInputStream(URL("https://touchlab-deps-public.s3.us-east-2.amazonaws.com/clang-llvm-apple-8.0.0-darwin-macos.zip").openStream())
62+
63+
val inp = BufferedInputStream(URL("https://download.jetbrains.com/kotlin/native/${llvmName}.tar.gz").openStream())
6164
inp.copyTo(fos)
6265
fos.close()
6366
inp.close()
6467

65-
val archiver = ArchiverFactory.createArchiver(ArchiveFormat.ZIP)
66-
67-
archiver.extract(tempDl, extractDir)
68-
val extractChild = File(extractDir, "clang-llvm-apple-8.0.0-darwin-macos")
69-
extractChild.renameTo(localFile)
68+
target.exec {
69+
it.workingDir = cklibDir.absoluteFile
70+
it.executable = "tar"
71+
it.args = listOf("-xf", tempFileNameWithExtension)
72+
}
7073
} finally {
7174
tempDl.delete()
72-
extractDir.delete()
7375
}
7476
}
7577
}

0 commit comments

Comments
 (0)