Important
These rules are in a bit of a rough state. They work for my needs but lack proper documentation and certain features. Pull requests are welcome!
Only bzlmod is supported right now. Pick the latest commit and add the following to your MODULE.bazel file
bazel_dep(name = "rules_kotlin_native")
archive_override(
module_name = "rules_kotlin_native",
url = "https://github.com/kitterion/rules_kotlin_native/archive/<commit>.zip",
strip_prefix = "rules_kotlin_native-<commit>",
)
You can customize the compiler version, the language version and the api version. See for example here for an explanation.
Note, that currently only a few compiler versions can be specified (2.0.21 and 2.1.21). Kotlin Native compiler has a few host-specific dependencies that it downloads on the first launch which doesn't quite work with bazel. To side-step this and to integrate with the bazel downloader, these dependencies are currently manually specified for each known compiler version. There are plans to support other versions including versions unknown to rules_kotlin_native.
kotlin_native = use_extension("@rules_kotlin_native//kotlin_native:extensions.bzl", "kotlin_native")
kotlin_native.toolchain(
version = "2.1.21",
language_version = "2.0",
api_version = "2.0",
)
The rules have have been written with toolchains and platforms in mind. Linux and macos should work, windows has not been tested (sorry, windows users).
Remote caching has been tested to work. Remote execution might work but is untested.
Supported rules are:
kt_native_library
load("@rules_kotlin_native//kotlin_native:kt_native_library.bzl", "kt_native_library") kt_native_library( name = "lib", srcs = ["Lib.kt"], )
kt_native_binary
.load("@rules_kotlin_native//kotlin_native:kt_native_binary.bzl", "kt_native_binary") kt_native_binary( name = "bin", srcs = ["Main.kt"], # Fully-qualified path to the main function. The default is "main". entry_point = "package.name.main", deps = [":lib"], )
kt_native_cinterop
. Seeexamples/hello_world_cinterop
load("@rules_kotlin_native//kotlin_native:kt_native_cinterop.bzl", "kt_native_cinterop") kt_native_cinterop( name = "hello_world_cinterop", src = "hello_world.def", # The supported deps are cc_library, swift_library, objc_library. # In fact, anything providing CcInfo or SwiftInfo should work. deps = [":hello_world_cc"], )
kt_native_static_framework
. Useful when building for apple platforms. Generates an apple framework that can be dependent on by swift or objc rules.load("@rules_kotlin_native//kotlin_native:kt_native_static_framework.bzl", "kt_native_static_framework") kt_native_static_framework( name = "framework", # This is the name of the framework (and the generated header). bundle_name = "KotlinNative" # The targets in deps define what gets exported from the framework. deps = [ "//lib_a", "//lib_b", ], )
kt_native_import
. Import a prebuilt .klib file.load("@rules_kotlin_native//kotlin_native:kt_native_import.bzl", "kt_native_import") kt_native_import( name = "", klib = "file.klib", # Just like with java_import, deps get propagated to the targets that depend on this one. deps = [ ... ], )
Compiler plugins from rules_kotlin should work.
Caution
Make sure you are using compatible versions of the Kotlin JVM compiler from rules_kotlin and Kotlin Native compiler. Incompatibilities may and will cause weird build failures with the compiler crashing.
kt_compile_plugin(
name = "serialization_plugin",
compile_phase = True,
id = "org.jetbrains.kotlinx.serialization",
stubs_phase = True,
deps = [
"@rules_kotlin//kotlin/compiler:kotlinx-serialization-compiler-plugin",
],
)
kt_native_library(
...
plugins = [":serialization_plugin"],
)