Skip to content

Commit

Permalink
Kotlin/Native samples ported to MPP Gradle DSL (JetBrains#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddolovov authored Oct 26, 2018
1 parent 7d2aede commit daf1a5f
Show file tree
Hide file tree
Showing 255 changed files with 2,137 additions and 2,437 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/dependencies/all
dist
kotlin-native-*.tar.gz
kotlin-native-*.zip
translator/src/test/kotlin/tests/*/linked
out
tmp
Expand Down
35 changes: 7 additions & 28 deletions DISTRO_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,9 @@ virtual machines are not desirable or possible (such as iOS, embedded targets),
or where developer is willing to produce reasonably-sized self-contained program
without need to ship an additional execution runtime.

To get started with _Kotlin/Native_ take a look at the attached samples.

* `androidNativeActivity` - Android Native Activity rendering 3D graphics using OpenGLES
* `calculator` - iOS Swift application, using Kotlin/Native code compiled into the framework
* `csvparser` - simple CSV file parser and analyzer
* `gitchurn` - program interoperating with `libgit2` for GIT repository analysis
* `gtk` - GTK2 interoperability example
* `html5Canvas` - WebAssembly example
* `libcurl` - using of FTP/HTTP/HTTPS client library `libcurl`
* `nonBlockingEchoServer` - multi-client TCP/IP echo server using co-routines
* `objc` - AppKit Objective-C interoperability example for macOS
* `opengl` - OpenGL/GLUT teapot example
* `python_extension` - Python extension written in Kotlin/Native
* `socket` - TCP/IP echo server
* `tensorflow` - simple client for TensorFlow Machine Intelligence library
* `tetris` - Tetris game implementation (using SDL2 for rendering)
* `uikit` - UIKit Objective-C interoperability example for iOS
* `videoplayer` - SDL and FFMPEG-based video and audio player
* `win32` - trivial Win32 GUI application
* `workers` - example of using workers API


See `README.md` in each sample directory for more information and build instructions.

_Kotlin/Native_ could be used either as standalone compiler toolchain or as Gradle
plugin. See [`GRADLE_PLUGIN.md`](https://github.com/JetBrains/kotlin-native/blob/master/GRADLE_PLUGIN.md) for more details on how to use this plugin.
plugin. See [documentation](https://kotlinlang.org/docs/reference/native/gradle_plugin.html)
for more details on how to use this plugin.

Compile your programs like that:

Expand All @@ -45,10 +22,12 @@ For an optimized compilation use -opt:
kotlinc hello.kt -o hello -opt

To generate interoperability stubs create library definition file
(take a look on `samples/tetris/tetris.sdl`) and run `cinterop` tool like this:
(take a look on [Tetris sample](samples/tetris))
and run `cinterop` tool like this:

cinterop -def lib.def

See [`INTEROP.md`](https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md) for more information on how to use C libraries from _Kotlin/Native_.

See [C interop documentation](https://kotlinlang.org/docs/reference/native/c_interop.html)
for more information on how to use C libraries from _Kotlin/Native_.

See [`RELEASE_NOTES.md`](https://github.com/JetBrains/kotlin-native/blob/master/RELEASE_NOTES.md) for information on supported platforms and current limitations.
2 changes: 1 addition & 1 deletion GRADLE_PLUGIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### IMPORTANT NOTICE

This document describes Kotlin/Native experimental Gradle plugin, which is not the plugin yet supported by IDE
or in multiplatform projects. See MPP Gradle plugin [documentation](https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html
or in multiplatform projects. See MPP Gradle plugin [documentation](https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html)
for more information.

### Overview
Expand Down
62 changes: 38 additions & 24 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import groovy.io.FileType
import org.jetbrains.kotlin.konan.target.*
import org.jetbrains.kotlin.konan.properties.*
import org.jetbrains.kotlin.konan.util.*
import org.jetbrains.kotlin.CopySamples

buildscript {
apply from: "gradle/kotlinGradlePlugin.gradle"
Expand Down Expand Up @@ -390,40 +391,53 @@ task bundle(type: (isWindows()) ? Zip : Tar) {
}
from(project.rootDir) {
include 'RELEASE_NOTES.md'
include 'samples/**'
exclude '**/gradle.properties'
exclude '**/settings.gradle'
exclude '**/build'
exclude '**/.gradle'
exclude 'samples/**/*.kt.bc-build'
into baseName
}

from(project.file("samples")) {
include '**/settings.gradle'
into "$baseName/samples"
filter { it.startsWith("includeBuild") ? null : it }
destinationDir = file('.')
if (!isWindows()) {
extension = 'tar.gz'
compression = Compression.GZIP
}
}

from(project.file("samples")) {
task samples {
dependsOn 'samplesZip', 'samplesTar'
}

task samplesZip(type: Zip)
task samplesTar(type: Tar) {
extension = 'tar.gz'
compression = Compression.GZIP
}

configure([samplesZip, samplesTar]) {
baseName "kotlin-native-samples-$konanVersionFull"
destinationDir = projectDir
into(baseName)

from(file('samples')) {
// Process properties files separately.
exclude '**/gradle.properties'
}

from(file('samples')) {
include '**/gradle.properties'
into "$baseName/samples"
filter {
if (it.startsWith("konan.home=")) {
return it.replace("/dist", "")
}
if (it.startsWith("konan.plugin.version=")) {
return "konan.plugin.version=$gradlePluginVersion"
}
return it
it.startsWith('org.jetbrains.kotlin.native.home=') || it.startsWith('# Use custom Kotlin/Native home:') ? null : it
}
}

destinationDir = file('.')
if (!isWindows()) {
extension = 'tar.gz'
compression = Compression.GZIP
}
// Exclude build artifacts.
exclude '**/build'
exclude '**/.gradle'
exclude '**/.idea'
exclude '**/*.kt.bc-build/'
}

task copy_samples(dependsOn: 'copySamples')
task copySamples(type: CopySamples) {
destinationDir file('build/samples-under-test')
}

task uploadBundle {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jetbrains.kotlin

import groovy.lang.Closure
import org.gradle.api.Task
import org.gradle.api.tasks.Copy

/**
* A task that copies samples and replaces direct repository URLs with ones provided by the cache-redirector service.
*/
open class CopySamples: Copy() {

var samplesDir = project.file("samples")

init {
configureReplacements()
}

fun samplesDir(path: Any) {
samplesDir = project.file(path)
}

private fun configureReplacements() {
from(samplesDir) {
it.exclude("**/*.gradle")
}
from(samplesDir) {
it.include("**/*.gradle")
it.filter { line ->
replacements.forEach { (repo, replacement) ->
if (line.contains(repo)) {
return@filter line.replace(repo, replacement)
}
}
return@filter line
}
}
}

override fun configure(closure: Closure<Any>): Task {
super.configure(closure)
configureReplacements()
return this
}

companion object {
val replacements = listOf(
"mavenCentral()" to "maven { url 'https://cache-redirector.jetbrains.com/maven-central' }",
"jcenter()" to "maven { url 'https://cache-redirector.jetbrains.com/jcenter' }",
"https://dl.bintray.com/kotlin/kotlin-dev" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-dev",
"https://dl.bintray.com/kotlin/kotlin-eap" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-eap",
"https://dl.bintray.com/kotlin/ktor" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/ktor",
"https://plugins.gradle.org/m2" to "https://cache-redirector.jetbrains.com/plugins.gradle.org/m2"
)
}
}
40 changes: 26 additions & 14 deletions samples/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# Samples

This directory contains a set of samples demonstrating how one can work with Kotlin/Native. The samples can be
built using either command line tools (via `build.sh` script presented in each sample directory) or using a gradle build.
See `README.md` in sample directories to learn more about specific samples and the building process.
built using Gradle build tool. See `README.md` in sample directories to learn more about specific samples and
the building process.

**Note**: If the samples are built from a source tree (not from a distribution archive) the compiler and the gradle
plugin built from the sources are used. So one must build the compiler by running `./gradlew cross_dist` from the
Kotlin/Native root directory before building samples (see
[README.md](https://github.com/JetBrains/kotlin-native/blob/master/README.md) for details).
* `androidNativeActivity` - Android Native Activity rendering 3D graphics using OpenGLES
* `calculator` - iOS Swift application, using Kotlin/Native code compiled into the framework
* `csvparser` - simple CSV file parser and analyzer
* `echoServer` - TCP/IP echo server
* `gitchurn` - program interoperating with `libgit2` for GIT repository analysis
* `gtk` - GTK2 interoperability example
* `html5Canvas` - WebAssembly example
* `libcurl` - using of FTP/HTTP/HTTPS client library `libcurl`
* `nonBlockingEchoServer` - multi-client TCP/IP echo server using co-routines
* `objc` - AppKit Objective-C interoperability example for macOS
* `opengl` - OpenGL/GLUT teapot example
* `python_extension` - Python extension written in Kotlin/Native
* `tensorflow` - simple client for TensorFlow Machine Intelligence library
* `tetris` - Tetris game implementation (using SDL2 for rendering)
* `uikit` - UIKit Objective-C interoperability example for iOS
* `videoplayer` - SDL and FFMPEG-based video and audio player
* `win32` - trivial Win32 GUI application
* `workers` - example of using workers API

One may also build all the samples with one command. To build them using the command line tools run:

./build.sh

To build all the samples using the gradle build:
**Note**: If the samples are built from a source tree (not from a distribution archive) the compiler built from
the sources is used. So one must build the compiler and the necessary platform libraries by running
`./gradlew bundle` from the Kotlin/Native root directory before building samples (see
[README.md](https://github.com/JetBrains/kotlin-native/blob/master/README.md) for details).

./gradlew build

One also may launch the command line build via a gradle task `buildSh` (equivalent of `./build.sh` executing):
One may also build all the samples with one command. To build them using Gradle run:

./gradlew buildSh
./gradlew buildAllSamples
13 changes: 9 additions & 4 deletions samples/androidNativeActivity/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Android Native Activity
This example shows how to build an Android Native Activity. Also, we provide an example

This example shows how to build an Android Native Activity. Also, we provide an example
bridging mechanism for the Java APIs, callable from Native side.

The example will render a textured dodecahedron using OpenGL ES library. It can be rotated with fingers.
Please make sure that Android SDK version 25 is installed, using Android SDK manager in Android Studio.
Please make sure that Android SDK version 28 is installed, using Android SDK manager in Android Studio.
See https://developer.android.com/studio/index.html for more details on Android Studio or
`$ANDROID_HOME/tools/bin/sdkmanager "platforms;android-25" "build-tools;25.0.2"` from command line.
`$ANDROID_HOME/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"` from command line.
We use JniBridge to call vibration service on the Java side for short tremble on startup.

To build use `ANDROID_HOME=<your path to android sdk> ../gradlew build`.
To build use `ANDROID_HOME=<your path to android sdk> ../gradlew assemble`.

Run `$ANDROID_HOME/platform-tools/adb install -r build/outputs/apk/debug/androidNativeActivity-debug.apk`
to deploy the apk on the Android device or emulator (note that only ARM-based devices are currently supported).

Note: If you are importing project to IDEA for the first time, you might need to put `local.properties` file
with the following content:

sdk.dir=<your path to Android SDK>
Loading

0 comments on commit daf1a5f

Please sign in to comment.