The installation procedure of the build environment for macOS / Linux (* without Gradle of the build tool) of Kotlin / Native is summarized. However, as of January 2020, Kotlin / Native itself is still in beta.
A JDK (Java environment) is required to compile the Kotlin source code, so install it in advance.
https://github.com/JetBrains/kotlin/releases/latest Use the latest version of the official release at.
Since the wget command is used for download here, if you have not installed it, please install it if necessary.
macOS
$ brew install wget
Linux(Ubuntu)
$ sudo apt install wget
macOS
$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-macos-1.3.61.tar.gz
Linux(Ubuntu)
$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-linux-1.3.61.tar.gz
Unzip it to a suitable location and put it in the path of the environment variable. Here, we will place it in / usr / local / kotlin-native.
$ tar xzvf kotlin-native-macos-1.3.61.tar.gz
$ sudo mkdir -p /usr/local/kotlin-native
$ sudo mv kotlin-native-macos-1.3.61/* /usr/local/kotlin-native
Add the path to ~ / .bash_profile to pass the path to the compiler binary.
~/.bash_profile
$ export PATH=$PATH:/usr/local/kotlin-native/bin/
If you don't use Gradle, you'll have to find the additional packages you need (for example, kotlinx: kotlinx-coroutines-core-native for Coroutines) and specify them in the command line options. The problem with this is that you don't know where to download it. Therefore, it is convenient to have IntelliJ IDEA installed so that you can use the Gradle environment as well ...
Download from here. In the case of macOS environment, it is a dmg file, so install it by GUI operation. On the other hand, in the case of Linux environment, you can use it just by unzipping it to a suitable location and passing it through the path.
In addition, I think that the Kotlin plug-in is already installed, so you can use it as it is for creating a new project.
hello.kt
fun main(args: Array<String>) {
println("Hello, World!")
}
The first time it runs, it takes time to download and install dependent packages such as LLVM.
$ kotlinc-native hello.kt -o hello
In addition, Java options can be passed with the -D option, so if you need proxy settings in a network environment, execute as follows. The information source is here.
$ kotlinc-native hello.kt -o hello ¥
-Dhttp.proxyHost=hoge.host.co.jp -Dhttp.proxyPort=12345 ¥
-Dhttps.proxyHost=hoge.host.co.jp -Dhttps.proxyPort=12345
$ ./hello.kexe
Hello, World!
I was able to run the Kotlin program in the native environment of macOS / Linux!
By using the -p option, it is possible to create a library in several formats, and the header file is also automatically generated. It is compatible with the iOS / macOS framework and is fairly well made, but I feel that the header file for C / C ++ when dynamic is specified is quite poorly made ... I want you to make it a little more decent macro ...
$ kotlinc-native hello.kt -p dynamic
Optional arguments | Contents |
---|---|
program | Normal execution binary |
static | .a file |
dynamic | In Linux environment.so,on macOS.dylib |
framework | iOS/Framework format for macOS |
library | klib(Kotlin library format) |
bitcode | bc file(LLVM Bitcodebc) |
If you don't know how to use it, you can check the help message with the -h option.
$ kotlinc-native -h
Usage: kotlinc-native <options> <source files>
where possible options include:
-g Enable emitting debug information
-enable-assertions (-ea) Enable runtime assertions in generated code
-friend-modules <path> Paths to friend modules
-generate-no-exit-test-runner (-trn)
Produce a runner for unit tests not forcing exit
-generate-test-runner (-tr) Produce a runner for unit tests
-generate-worker-test-runner (-trw)
Produce a worker runner for unit tests
-include-binary (-ib) <path> Pack external binary within the klib
-library (-l) <path> Link with the library
-library-version (-lv) <version>
Set library version
-linker-options <arg> Pass arguments to linker
-list-targets List available hardware targets
-entry (-e) <name> Qualified entry point name
-manifest <path> Provide a maniferst addend file
-memory-model <model> Memory model to use, 'strict' and 'relaxed' are currently supported
-module-name <name> Specify a name for the compilation module
-native-library (-nl) <path> Include the native bitcode library
-no-default-libs Don't link the libraries from dist/klib automatically
-no-endorsed-libs Don't link the endorsed libraries from dist automatically
-nomain Assume 'main' entry point to be provided by external libraries
-nopack Don't pack the library into a klib file
-nostdlib Don't link with stdlib
-opt Enable optimizations during compilation
-output (-o) <name> Output name
-produce (-p) {program|static|dynamic|framework|library|bitcode}
Specify output file kind
-repo (-r) <path> Library search path
-linker-option <arg> Pass argument to linker
-target <target> Set hardware target
-Werror Report an error if there are any warnings
-api-version <version> Allow to use declarations only from the specified version of bundled libraries
-X Print a synopsis of advanced options
-help (-h) Print a synopsis of standard options
-kotlin-home <path> Path to Kotlin compiler home directory, used for runtime libraries discovery
-language-version <version> Provide source compatibility with specified language version
-P plugin:<pluginId>:<optionName>=<value>
Pass an option to a plugin
-progressive Enable progressive compiler mode.
In this mode, deprecations and bug fixes for unstable code take effect immediately,
instead of going through a graceful migration cycle.
Code written in the progressive mode is backward compatible; however, code written in
non-progressive mode may cause compilation errors in the progressive mode.
-nowarn Generate no warnings
-verbose Enable verbose logging output
-version Display compiler version
@<argfile> Read compiler arguments and file paths from the given file
As for the impression I used, it works normally, but the build is slow anyway ... It seems that Kotlin 1.4 will update the compiler, so I'm looking forward to it.
Recommended Posts