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

Kpg/for kotlin 160 #4

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@

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

# Note
# Usage

Add gradle plugins

```kotlin
plugins {
kotlin("multiplatform")
id("co.touchlab.cklib")
}
```

Add Kotlin version and define some C-like source:

The main Kotlin project has changed how locally embedded C-like code is included in libraries, so we'll probably remove
this project from public access soon until we land on a complete answer to handling this (our current solution is kind
of a copy/paste of the main Kotlin solution).
```kotlin
cklib {
config.kotlinVersion = KOTLIN_VERSION
create("objcsample") {
language = Language.OBJC
}
}
```

See example in [Kermit](https://github.com/touchlab/Kermit/blob/main/kermit-crashlytics-test/build.gradle.kts#L69)

# Note

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.
The main Kotlin project has changed how locally embedded C-like code is included in libraries. Use
this project if you'd like, but outside of private projects we won't really be supporting it much.

License
=======
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ org.gradle.jvmargs=-Xmx2g

GROUP=co.touchlab
KOTLIN_VERSION=1.6.0
CKLIB_VERSION=0.2.2
CKLIB_VERSION=0.2.4

POM_NAME=CKlib
POM_DESCRIPTION=C/C++ Bitcode Into Klib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ open class CKlibGradleExtension @Inject constructor(val project: Project) {
}

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

internal val defaultCklibDir:String
get() = "${System.getProperty("user.home")}/.cklib"

internal val Project.platformManager: PlatformManager
get() {
val cklibExtension = extensions.getByType(CompileToBitcodeExtension::class.java)
Expand Down Expand Up @@ -77,13 +80,18 @@ internal val osName: String
internal val llvmName: String
get() {
return when (osName) {
"osx" -> llvm_macos_x64
"osx" -> if (hostArch == "aarch64") {
llvm_macos_arm64
} else {
llvm_macos_x64
}
"linux" -> llvm_linux_x64
"windows" -> llvm_mingw_x64
else -> throw TargetSupportException("Unknown operating system: $osName")
}
}

//https://download.jetbrains.com/kotlin/native/clang-llvm-8.0.0-linux-x86-64.tar.gz
internal val llvm_linux_x64 = "clang-llvm-8.0.0-linux-x86-64"
internal val llvm_mingw_x64 = "msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1"
internal val llvm_macos_x64 = "clang-llvm-apple-8.0.0-darwin-macos"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

package co.touchlab.cklib.gradle

import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.rauschig.jarchivelib.ArchiveFormat
import org.rauschig.jarchivelib.ArchiverFactory
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
Expand All @@ -23,6 +22,9 @@ import java.util.*

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

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

try {
val fos = FileOutputStream(tempDl)
val inp = BufferedInputStream(URL("https://touchlab-deps-public.s3.us-east-2.amazonaws.com/clang-llvm-apple-8.0.0-darwin-macos.zip").openStream())

val inp = BufferedInputStream(URL("https://download.jetbrains.com/kotlin/native/${llvmName}.tar.gz").openStream())
inp.copyTo(fos)
fos.close()
inp.close()

val archiver = ArchiverFactory.createArchiver(ArchiveFormat.ZIP)

archiver.extract(tempDl, extractDir)
val extractChild = File(extractDir, "clang-llvm-apple-8.0.0-darwin-macos")
extractChild.renameTo(localFile)
target.exec {
it.workingDir = cklibDir.absoluteFile
it.executable = "tar"
it.args = listOf("-xf", tempFileNameWithExtension)
}
} finally {
tempDl.delete()
extractDir.delete()
}
}
}
Expand Down