-
Notifications
You must be signed in to change notification settings - Fork 235
Add initial kotlin native toolchain and kt_library
rule
#1351
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
Open
smocherla-brex
wants to merge
41
commits into
bazelbuild:master
Choose a base branch
from
smocherla-brex:smocherla/kotlin-native-klib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
913b1ad
Initial changes to support kotlin-native toolchain
smocherla-brex e5eb0a5
KtKlibInfo
smocherla-brex 94abd2d
Add initial support for kotlin-native toolchain and klib compilation
smocherla-brex 3553ace
Fix cache marker for other platforms
smocherla-brex d2a3ca9
buildifier
smocherla-brex f0385a3
remove unused stuff
smocherla-brex b108308
remove opts.native.bzl
smocherla-brex 5d6557a
Fix builder tests
smocherla-brex fa6291e
Add capabilties.bzl to release archive
smocherla-brex 685e4d7
Add BUILD.kotlin-native_capabilities.bazel to release archive
smocherla-brex 126931f
Feedback: Remove duplicate code and pass transitive klibs
smocherla-brex cefedcb
wip: Remove copy_to_directory and create a dedicated native toolchain
smocherla-brex a564355
Feedback: Pass konan_home as an arg, and attempt to remove marker files
smocherla-brex a0c2284
Remove some flags that don't seem needed and don't link with stdlib
smocherla-brex a57be2e
Fix path to konan_home in toolchain
smocherla-brex 809270e
lint and fix starlark tests
smocherla-brex 59b64c6
Rename klib package to native under kotlin/internal
smocherla-brex ce96a95
Update docs
smocherla-brex e902025
Make toolchain names and impl names consistent
smocherla-brex de83c33
Fix starlark tests
smocherla-brex 3d06d53
Fix more references
smocherla-brex e940430
Fix more tests and buildifier
smocherla-brex f5b6641
Add -Xklib-relative-path-base and -Xdebug-prefix-map
smocherla-brex 3def59a
Rename to kt_library and structure accordingly
smocherla-brex bc060d4
Remove konan_home attribute on the JVM toolchain
smocherla-brex eda9930
Add runfiles support with tests
smocherla-brex 3dafe36
Mark targets within starkark tests as manual
smocherla-brex 53bda70
Be explicit about --target and propagate it from toolchain to builder
smocherla-brex 2e33d76
Set default target correctly
smocherla-brex 0d6850e
Create CLI toolchain for kotlinc-native and use that for builder
smocherla-brex b1fba97
Fix build/test failures due to stale references
smocherla-brex cd60552
Fix checksum for windows
smocherla-brex bba1ea4
Regen docs
smocherla-brex 96eb77a
Need an alias target in kotlin/compiler sadly to make runfiles work
smocherla-brex 4906239
buildifier and fix one reference
smocherla-brex 89c25cd
Fix some integration tests
smocherla-brex 911f49d
Update docs
smocherla-brex c23bc5f
lint
smocherla-brex 1cac1b1
Add tests for transitive outputs
smocherla-brex f244d54
Remove unused loads
smocherla-brex 702ff09
Empty commit to retrigger build and deal with android flake
smocherla-brex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("//kotlin/internal/native:toolchains.bzl", "kt_configure_native_toolchains") | ||
|
||
kt_configure_native_toolchains() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
load("//kotlin/internal:defs.bzl", _KtKlibInfo = "KtKlibInfo", _NATIVE_TOOLCHAIN_TYPE = "NATIVE_TOOLCHAIN_TYPE", _TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE") | ||
load("//kotlin/internal/utils:utils.bzl", "utils") | ||
|
||
def _kt_library_impl(ctx): | ||
module_name = utils.derive_module_name(ctx) | ||
builder_args = utils.init_args(ctx, "kt_library", module_name) | ||
|
||
klib = ctx.actions.declare_file("{}.klib".format(ctx.label.name)) | ||
outputs = [klib] | ||
|
||
toolchains = ctx.toolchains[_TOOLCHAIN_TYPE] | ||
|
||
# Retrieve konan.home from the chosen toolchain's distribution | ||
native_toolchain_info = ctx.toolchains[_NATIVE_TOOLCHAIN_TYPE].kotlin_native_info | ||
konan_home = native_toolchain_info.konan_home | ||
|
||
deps_klibs = [] | ||
transitive_klibs = [] | ||
for dep in ctx.attr.deps: | ||
deps_klibs.append(dep[_KtKlibInfo].klibs) | ||
transitive_klibs.append(dep[_KtKlibInfo].transitive_klibs) | ||
|
||
libraries = depset(transitive = deps_klibs) | ||
builder_args.add_all("--sources", ctx.files.srcs) | ||
builder_inputs, _, input_manifests = ctx.resolve_command(tools = [toolchains.kotlinbuilder]) | ||
|
||
builder_args.add("--strict_kotlin_deps", "off") | ||
builder_args.add("--reduced_classpath_mode", "off") | ||
builder_args.add("--output_klib", klib.path) | ||
|
||
libraries = depset(transitive = deps_klibs) | ||
builder_args.add_all("--klibs", libraries) | ||
builder_args.add("--konan_home", konan_home.path) | ||
|
||
ctx.actions.run( | ||
mnemonic = "KotlinKlibCompile", | ||
inputs = depset(builder_inputs + ctx.files.srcs, transitive = [libraries, native_toolchain_info.konan_home_files]), | ||
outputs = outputs, | ||
executable = toolchains.kotlinbuilder.files_to_run.executable, | ||
tools = [ | ||
toolchains.kotlinbuilder.files_to_run, | ||
], | ||
execution_requirements = {"supports-workers": "1"}, | ||
arguments = [ctx.actions.args().add_all(toolchains.builder_args), builder_args], | ||
progress_message = "Compiling Kotlin to Klib %%{label} { kt: %d }" % len(ctx.files.srcs), | ||
input_manifests = input_manifests, | ||
env = { | ||
"REPOSITORY_NAME": utils.builder_workspace_name(ctx), | ||
}, | ||
) | ||
|
||
return [ | ||
DefaultInfo(files = depset(outputs)), | ||
_KtKlibInfo( | ||
klibs = depset(outputs), | ||
transitive_klibs = depset(transitive = transitive_klibs), | ||
), | ||
] | ||
|
||
kt_library = rule( | ||
implementation = _kt_library_impl, | ||
doc = """ | ||
This rule is intended to leverage the new Kotlin IR backend to allow for compiling platform-independent Kotlin code | ||
to be shared between Kotlin code for different platforms (JS/JVM/WASM etc.). It produces a klib file as the output. | ||
""", | ||
attrs = { | ||
"srcs": attr.label_list( | ||
doc = "A list of source files to be compiled to klib", | ||
allow_files = [".kt"], | ||
), | ||
"deps": attr.label_list( | ||
doc = "A list of other kt_klib_library targets that this library depends on for compilation", | ||
providers = [_KtKlibInfo], | ||
), | ||
}, | ||
toolchains = [_TOOLCHAIN_TYPE, _NATIVE_TOOLCHAIN_TYPE], | ||
provides = [_KtKlibInfo], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo") | ||
load("//src/main/starlark/core/repositories/kotlin:artifacts.bzl", "KOTLIN_NATIVE_ARTIFACTS_AND_TARGETS") | ||
|
||
KotlinNativeToolchainInfo = provider(fields = { | ||
"konan_home": "The path to konan.home directory", | ||
"konan_properties": "The kokna.properties file corresponding to this distribution", | ||
"konan_home_files": "A depset containing all the files in konan.home", | ||
"targets": "A list of Kotlin native targets this toolchain supports", | ||
}) | ||
|
||
def _kt_native_toolchain_impl(ctx): | ||
konan_home_info = ctx.attr.konan_home[DirectoryInfo] | ||
konan_properties = konan_home_info.get_file("konan/konan.properties") | ||
konan_home = konan_home_info | ||
|
||
return [ | ||
platform_common.ToolchainInfo( | ||
kotlin_native_info = KotlinNativeToolchainInfo( | ||
konan_home = konan_home, | ||
konan_home_files = depset(transitive = [konan_home_info.transitive_files]), | ||
konan_properties = konan_properties, | ||
targets = ctx.attr.targets, | ||
), | ||
), | ||
] | ||
|
||
kt_native_toolchain = rule( | ||
implementation = _kt_native_toolchain_impl, | ||
attrs = { | ||
"konan_home": attr.label( | ||
providers = [DirectoryInfo], | ||
doc = "The directory containing konan.home", | ||
), | ||
"targets": attr.string_list( | ||
doc = "The list of targets supported by this toolchain. Each target can be passed to kotlinc-native with -target option", | ||
default = [], | ||
), | ||
}, | ||
provides = [platform_common.ToolchainInfo], | ||
) | ||
|
||
def kt_configure_native_toolchains(): | ||
"""Defines and registers the default toolchains for the kotlin-native compiler for all the platforms and targets supported.""" | ||
native.toolchain_type( | ||
name = "kt_native_toolchain_type", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# Create toolchains for each exec platform and their supported targets | ||
for exec_platform, artifacts_and_targets in KOTLIN_NATIVE_ARTIFACTS_AND_TARGETS.items(): | ||
for target_constraint_tuple, kotlin_native_targets in artifacts_and_targets.targets.items(): | ||
target_os, target_cpu = target_constraint_tuple | ||
|
||
# Create a unique name for this toolchain | ||
toolchain_name = "default_kt_native_toolchain_{}_to_{}_{}".format( | ||
exec_platform, | ||
target_os.split("//")[1].replace(":", "_").replace("/", "_"), | ||
target_cpu.split("//")[1].replace(":", "_").replace("/", "_"), | ||
) | ||
|
||
toolchain_impl = "default_kt_native_{}_to_{}_{}".format( | ||
exec_platform, | ||
target_os.split("//")[1].replace(":", "_").replace("/", "_"), | ||
target_cpu.split("//")[1].replace(":", "_").replace("/", "_"), | ||
) | ||
|
||
# Create the toolchain implementation | ||
kt_native_toolchain( | ||
name = toolchain_impl, | ||
konan_home = "@com_github_jetbrains_kotlin_native_{}//:konan_home".format(exec_platform), | ||
targets = kotlin_native_targets, | ||
) | ||
|
||
# Register the toolchain | ||
native.toolchain( | ||
name = toolchain_name, | ||
exec_compatible_with = artifacts_and_targets.exec_compatible_with, | ||
target_compatible_with = [target_os, target_cpu], | ||
toolchain = ":" + toolchain_impl, | ||
toolchain_type = ":kt_native_toolchain_type", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the usecase for this? It's far preferable to use specific toolchains.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was used to pass the kotlin-native.jar to KotlinBuilder but I've created a toolchain now, so will update to reference it from there directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried moving this to a dedicated CLI toolchain but unfortunately we need the runfiles path to
kotlin-native.jar
as a jvm flag to the builderjava_binary
target here and it doesn't seem to be possible to provide it through the toolchain (since it usesjava_binary
withjvm_flags
). The builder compilation still uses a dedicated toolchain (kotlin_native_cli_toolchain
) and the jar from there but I've not found a way to re-use that with the binary runfiles.