Skip to content

Commit 91a958f

Browse files
committed
v0.2.2 Release
1 parent 44c65f1 commit 91a958f

File tree

5 files changed

+100
-94
lines changed

5 files changed

+100
-94
lines changed

OLDREADME.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## The Problem
2+
3+
When you want to access C-etc code from Kotlin/Native, you point the cinterop process at it. That will create Kotlin code to enable
4+
calling the native code. That's only one part of the process, though. You *also* need to build and link binaries to implement
5+
the native calls. Configuring native builds can be complex because of the number of options that need to be correctly configured,
6+
as well as the need to package and link the complied binaries.
7+
8+
## The Solution
9+
10+
This problem is already kind of solved by Kotlin/Native itself. The platform is based largely on C and C++. There's a [Gradle
11+
plugin](https://github.com/JetBrains/kotlin/blob/7b73917217de6dc66330593887c44e67a4efb7d3/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt)
12+
and set of tasks embedded within Kotlin/Native and the broader Kotlin platform that configures and builds native C-etc code.
13+
That plugin is not published in an accessible way, unfortunately. CKlib extracts that Clang Gradle plugin to be used externally.
14+
15+
For packaging, the problem is already solved there as well. You can just insert the compiled binary into your klib. CKlib
16+
is configured to do that for you.
17+
18+
## Status
19+
20+
This plugin hasn't been designed for all use cases. We needed to compile and embed C code with no real external dependencies.
21+
You may want to build and embed C-etc code for other use cases we haven't considered, and this plugin will almost certainly
22+
need to be modified for your particular situation. Please start conversations and/or submit PRs if you add anything significant.
23+
24+
## Usage
25+
26+
Add the plugin to your buildscript path:
27+
28+
```kotlin
29+
buildscript {
30+
repositories {
31+
mavenCentral() // <- need this
32+
google()
33+
gradlePluginPortal()
34+
}
35+
dependencies {
36+
classpath("co.touchlab:cklib-gradle-plugin:1.5.31.3") // <- Replace with current version
37+
}
38+
}
39+
```
40+
41+
Apply the plugin. You will also need to have the Kotlin Multiplatform plugin applied. CKlib depends on it.
42+
43+
```kotlin
44+
plugins {
45+
kotlin("multiplatform")
46+
id("co.touchlab.cklib")
47+
}
48+
```
49+
50+
To create compilations, add the `cklib` block, set the Kotlin version, and then point at C-etc source.
51+
52+
```kotlin
53+
cklib {
54+
config.kotlinVersion = "1.6.0"
55+
create("somecode") {
56+
language = C
57+
compilerArgs.addAll(
58+
listOf(
59+
"-Wno-unused-function"
60+
)
61+
)
62+
}
63+
}
64+
```
65+
66+
By default, the C-etc code is built and packaged in compatible Kotlin/Native klibs. You can specify source folders and
67+
you will likely need to add some compiler args. Anything more custom will probably require tweaking the plugin itself, as
68+
it was really designed for a very particular use case.
69+
70+
## We're Hiring!
71+
72+
Touchlab is looking for a Mobile Developer, with Android/Kotlin experience, who is eager to dive into Kotlin Multiplatform Mobile (KMM) development. Come join the remote-first team putting KMM in production. [More info here](https://go.touchlab.co/careers-gh).
73+
74+
## Primary Maintainer
75+
76+
[Kevin Galligan](https://github.com/kpgalligan/)
77+
78+
![Image of Kevin](https://avatars.githubusercontent.com/u/68384?s=140&v=4)
79+
80+
*Ping me on twitter [@kpgalligan](https://twitter.com/kpgalligan/) if you don't get a timely reply!* -Kevin

README.md

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

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

5-
## The Problem
5+
# Note
66

7-
When you want to access C-etc code from Kotlin/Native, you point the cinterop process at it. That will create Kotlin code to enable
8-
calling the native code. That's only one part of the process, though. You *also* need to build and link binaries to implement
9-
the native calls. Configuring native builds can be complex because of the number of options that need to be correctly configured,
10-
as well as the need to package and link the complied binaries.
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).
1110

12-
## The Solution
13-
14-
This problem is already kind of solved by Kotlin/Native itself. The platform is based largely on C and C++. There's a [Gradle
15-
plugin](https://github.com/JetBrains/kotlin/blob/7b73917217de6dc66330593887c44e67a4efb7d3/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt)
16-
and set of tasks embedded within Kotlin/Native and the broader Kotlin platform that configures and builds native C-etc code.
17-
That plugin is not published in an accessible way, unfortunately. CKlib extracts that Clang Gradle plugin to be used externally.
18-
19-
For packaging, the problem is already solved there as well. You can just insert the compiled binary into your klib. CKlib
20-
is configured to do that for you.
21-
22-
## Status
23-
24-
This plugin hasn't been designed for all use cases. We needed to compile and embed C code with no real external dependencies.
25-
You may want to build and embed C-etc code for other use cases we haven't considered, and this plugin will almost certainly
26-
need to be modified for your particular situation. Please start conversations and/or submit PRs if you add anything significant.
27-
28-
## Usage
29-
30-
Add the plugin to your buildscript path:
31-
32-
```kotlin
33-
buildscript {
34-
repositories {
35-
mavenCentral() // <- need this
36-
google()
37-
gradlePluginPortal()
38-
}
39-
dependencies {
40-
classpath("co.touchlab:cklib-gradle-plugin:1.5.31.3") // <- Replace with current version
41-
}
42-
}
43-
```
44-
45-
Apply the plugin. You will also need to have the Kotlin Multiplatform plugin applied. CKlib depends on it.
46-
47-
```kotlin
48-
plugins {
49-
kotlin("multiplatform")
50-
id("co.touchlab.cklib")
51-
}
52-
```
53-
54-
To create compilations, add the `cklib` block, set the Kotlin version, and then point at C-etc source.
55-
56-
```kotlin
57-
cklib {
58-
config.kotlinVersion = "1.6.0"
59-
create("somecode") {
60-
language = C
61-
compilerArgs.addAll(
62-
listOf(
63-
"-Wno-unused-function"
64-
)
65-
)
66-
}
67-
}
68-
```
69-
70-
By default, the C-etc code is built and packaged in compatible Kotlin/Native klibs. You can specify source folders and
71-
you will likely need to add some compiler args. Anything more custom will probably require tweaking the plugin itself, as
72-
it was really designed for a very particular use case.
73-
74-
## We're Hiring!
75-
76-
Touchlab is looking for a Mobile Developer, with Android/Kotlin experience, who is eager to dive into Kotlin Multiplatform Mobile (KMM) development. Come join the remote-first team putting KMM in production. [More info here](https://go.touchlab.co/careers-gh).
77-
78-
## Primary Maintainer
79-
80-
[Kevin Galligan](https://github.com/kpgalligan/)
81-
82-
![Image of Kevin](https://avatars.githubusercontent.com/u/68384?s=140&v=4)
83-
84-
*Ping me on twitter [@kpgalligan](https://twitter.com/kpgalligan/) if you don't get a timely reply!* -Kevin
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.
8512

8613
License
8714
=======

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.1
18+
CKLIB_VERSION=0.2.2
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: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import javax.inject.Inject
2121
open class CKlibGradleExtension @Inject constructor(val project: Project) {
2222
private var _konanHome: String? = null
2323
private var _llvmHome: String? = null
24-
private var _llvmDir: String? = null
2524

2625
var kotlinVersion: String? = null
2726
var arch: String = hostArch
@@ -40,16 +39,10 @@ open class CKlibGradleExtension @Inject constructor(val project: Project) {
4039
}
4140

4241
var llvmHome: String
43-
get() = _llvmHome ?: "${System.getProperty("user.home")}/.konan/dependencies/${llvmDir}"
42+
get() = _llvmHome ?: "${System.getProperty("user.home")}/.cklib/clang-llvm-apple-8.0.0-darwin-macos"
4443
set(value) {
4544
_llvmHome = value
4645
}
47-
48-
var llvmDir: String
49-
get() = _llvmDir ?: llvmName
50-
set(value) {
51-
_llvmDir = value
52-
}
5346
}
5447

5548
internal val Project.platformManager: PlatformManager

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import java.util.*
2424
class CompileToBitcodePlugin : Plugin<Project> {
2525
override fun apply(target: Project) = with(target) {
2626
extensions.create(EXTENSION_NAME, CompileToBitcodeExtension::class.java, target)
27-
downloadIfNeeded()
27+
downloadIfNeeded(target)
2828

2929
target.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform"){ appliedPlugin ->
3030
extensions.getByType(KotlinMultiplatformExtension::class.java)
@@ -42,28 +42,34 @@ class CompileToBitcodePlugin : Plugin<Project> {
4242
}*/
4343
}
4444

45-
private fun downloadIfNeeded() {
45+
//This is pretty hacky, but the process changed in 1.6.0. We'll probably just split off and do our own thing
46+
//going forward, but need to be able to build for the next few weeks.
47+
private fun downloadIfNeeded(target: Project) {
4648
val cklibDir = File("${System.getProperty("user.home")}/.cklib")
4749
val localFile = File(cklibDir, "clang-llvm-apple-8.0.0-darwin-macos")
4850
val clangExists = localFile.exists()
4951
if(!clangExists){
52+
target.logger.info("cklib downloading dependencies (may take a while...)")
5053
cklibDir.mkdirs()
5154
val tempFileName = UUID.randomUUID().toString()
52-
val tempDl = File(cklibDir, "${tempFileName}.tar.gz")
55+
val extractDir = File(cklibDir, tempFileName)
56+
val tempDl = File(cklibDir, "${tempFileName}.zip")
57+
5358
try {
5459
val fos = FileOutputStream(tempDl)
55-
val inp = BufferedInputStream(URL("https://touchlab-deps-public.s3.us-east-2.amazonaws.com/clang-llvm-apple-8.0.0-darwin-macos.tar.gz").openStream())
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())
5661
inp.copyTo(fos)
5762
fos.close()
5863
inp.close()
5964

60-
val archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR)
65+
val archiver = ArchiverFactory.createArchiver(ArchiveFormat.ZIP)
6166

62-
val extractDir = File(cklibDir, tempFileName)
6367
archiver.extract(tempDl, extractDir)
64-
extractDir.renameTo(localFile)
68+
val extractChild = File(extractDir, "clang-llvm-apple-8.0.0-darwin-macos")
69+
extractChild.renameTo(localFile)
6570
} finally {
6671
tempDl.delete()
72+
extractDir.delete()
6773
}
6874
}
6975
}

0 commit comments

Comments
 (0)