Skip to content

Commit c998eb5

Browse files
authored
Use preloader for worker process and introduce kotlin_test rule (#22)
1 parent 44a0f3f commit c998eb5

File tree

8 files changed

+211
-67
lines changed

8 files changed

+211
-67
lines changed

README.md

+57-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<table><tr>
2+
<td><img src="https://github.com/pubref/rules_protobuf/blob/master/images/bazel.png" width="120"/></td>
3+
<td><img src="https://kotlinlang.org/assets/images/open-graph/kotlin_250x250.png" width="120"/></td>
4+
</tr><tr>
5+
<td>Bazel</td>
6+
<td>Kotlin</td>
7+
</tr></table>
8+
19
# Kotlin Rules for Bazel
210
[![Build Status](https://travis-ci.org/pubref/rules_kotlin.svg?branch=master)](https://travis-ci.org/pubref/rules_kotlin)
311

@@ -9,6 +17,7 @@ These rules are for building [Kotlin][kotlin] source with with
917
1. [kotlin_repositories](#kotlin_repositories)
1018
1. [kotlin_library](#kotlin_library)
1119
1. [kotlin_binary](#kotlin_binary)
20+
1. [kotlin_test](#kotlin_test)
1221

1322
## Workspace rules
1423

@@ -18,7 +27,7 @@ Add the following to your `WORKSPACE` file:
1827
git_repository(
1928
name = "org_pubref_rules_kotlin",
2029
remote = "https://github.com/pubref/rules_kotlin.git",
21-
tag = "v0.3.0", # update as needed
30+
tag = "v0.3.1", # update as needed
2231
)
2332

2433
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")
@@ -40,7 +49,7 @@ dagger (used to build the `KotlinCompiler` bazel worker).
4049
Add the following to your BUILD file:
4150

4251
```python
43-
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_library", "kotlin_binary")
52+
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_library")
4453
```
4554

4655
### kotlin_library
@@ -95,18 +104,18 @@ android_binary(
95104
| `srcs` | `label_list` | Kotlin source files `*.kt` |
96105
| `deps` | `label_list` | List of `kotlin_library` targets |
97106
| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`, `...`) |
107+
| `android_deps` | `label_list` | List of android provider targets (`android_library`) |
98108
| `jars` | `label_list` | List of jar file targets (`*.jar`) |
99109
| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |
100110
| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |
101-
| `use_worker` | `boolean` | Assign to `False` to disable the use of [bazel workers](https://bazel.build/blog/2015/12/10/java-workers.html). |
102111

103112

104113
### kotlin_binary
105114

106-
A `kotlin_binary` rule takes the same arguments as a `kotlin_library`,
107-
plus a required `main_class` argument (the name of the compiled kotlin
108-
class to run, in java package notation). This class should have a
109-
`fun main(...)` entrypoint. Example:
115+
A `kotlin_binary` macro takes the same arguments as a
116+
`kotlin_library`, plus a required `main_class` argument (the name of
117+
the compiled kotlin class to run, in java package notation). This
118+
class should have a `fun main(...)` entrypoint. Example:
110119

111120
```python
112121
kotlin_binary(
@@ -128,6 +137,8 @@ Target :main_kt_deploy.jar up-to-date:
128137
$ java -jar ./bazel-bin/.../main_kt_deploy.jar
129138
```
130139

140+
> The `kotlin-runtime.jar` is implicitly included by the `kotlin_binary` rule.
141+
131142
#### kotlin_binary attributes
132143

133144
Includes all `kotlin_library` attributes as well as:
@@ -137,25 +148,52 @@ Includes all `kotlin_library` attributes as well as:
137148
| `main_class` | `string` | Main class to run with the `kotlin_binary` rule |
138149

139150

151+
### kotlin_test
152+
153+
The `kotlin_test` rule is nearly identical the `kotlin_binary` rule
154+
(other than calling `java_test` internally rather than `java_binary`).
155+
156+
157+
```python
158+
kotlin_test(
159+
name = "main_kt_test",
160+
test_class = "examples.helloworld.MainKtTest",
161+
srcs = ["MainKtTest.kt"],
162+
size = "small",
163+
deps = [
164+
":rules",
165+
],
166+
java_deps = [
167+
"@junit4//jar",
168+
],
169+
)
170+
```
171+
172+
```sh
173+
$ bazel test :main_kt_test.jar
174+
```
175+
176+
> The `kotlin-test.jar` is implicitly included by the `kotlin_test` rule.
177+
140178
### kotlin_compile
141179

142-
The `kotlin_compile` rule runs the `kotlinc` tool to generate a `.jar`
143-
file from a list of kotlin source files. The `kotlin_library` rule
144-
(actually, macro) calls this internally and then makes the jarfile
145-
available to other java rules via a `java_import` rule.
180+
> TL;DR; You most likely do not need to interact with the
181+
> `kotlin_compile` rule directly.
146182
147-
In summary, you most likely do not need to interact with the
148-
`kotlin_compile` rule directly.
183+
The `kotlin_compile` rule runs the kotlin compiler to generate a
184+
`.jar` file from a list of kotlin source files. The `kotlin_library`
185+
rule calls this internally and then makes the jarfile available to
186+
other java rules via a `java_import` rule.
149187

150188
# Summary
151189

152190
That's it! Hopefully these rules with make it easy to mix kotlin and
153191
traditional java code in your projects and take advantage of bazel's
154192
approach to fast, repeatable, and reliable builds.
155193

156-
> Note: if you have a bunch of maven (central) dependencies, consider
157-
> [rules_maven](https://github.com/pubref/rules_maven) for taming the
158-
> issue of transitive dependencies with your java/kotlin projects.
194+
> Note: Consider [rules_maven](https://github.com/pubref/rules_maven)
195+
> for handling transitive maven dependencies with your java/kotlin
196+
> projects.
159197
160198
## Examples
161199

@@ -167,13 +205,14 @@ $ cd rules_kotlin
167205
$ bazel query //... --output label_kind
168206
$ bazel run examples/helloworld:main_kt
169207
$ bazel run examples/helloworld:main_java
208+
$ bazel test examples/helloworld:main_test
209+
$ bazel test examples/helloworld:main_kt_test
170210
```
171211

172212
## TODO
173213

174-
1. Implement a `kotlin_test` rule.
175214
1. Proper `data` and runfiles support.
176-
2. Android support.
215+
2. Proper android support.
177216
4. kapt support.
178217
3. Incremental compilation.
179218

examples/helloworld/BUILD

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package(default_visibility = ["//visibility:public"])
22

3-
load("//kotlin:rules.bzl", "kotlin_library", "kotlin_binary")
3+
load("//kotlin:rules.bzl", "kotlin_library", "kotlin_binary", "kotlin_test")
44

55

66
# A kotlin binary that depends on another kotlin rule (using kotlin
@@ -52,6 +52,20 @@ java_test(
5252
]
5353
)
5454

55+
# A kotlin test rule that depends on a kotlin rule
56+
kotlin_test(
57+
name = "main_kt_test",
58+
test_class = "examples.helloworld.MainKtTest",
59+
srcs = ["MainKtTest.kt"],
60+
size = "small",
61+
deps = [
62+
":rules",
63+
],
64+
java_deps = [
65+
"@junit4//jar",
66+
],
67+
)
68+
5569
# Included to test dependent java providers
5670
java_library(
5771
name = "guava",

examples/helloworld/MainKtTest.kt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package examples.helloworld
2+
3+
import kotlin.test.assertEquals
4+
import org.junit.Test
5+
6+
public class MainKtTest {
7+
8+
@Test fun testRuleName(): Unit {
9+
val rule = KotlinLibraryRule("foo", emptyList(), emptyList())
10+
assertEquals("foo", rule.name)
11+
}
12+
13+
}

java/org/pubref/rules/kotlin/BUILD

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ java_binary(
77
visibility = ["//visibility:public"],
88
deps = [
99
":compiler",
10+
":preloader",
1011
"//java/io/bazel/rules/closure:BazelWorker",
1112
"//java/io/bazel/rules/closure/program",
1213
"@com_google_dagger",
@@ -27,3 +28,16 @@ java_library(
2728
"@com_github_jetbrains_kotlin//:compiler",
2829
],
2930
)
31+
32+
java_library(
33+
name = "preloader",
34+
srcs = [
35+
"KotlinPreloader.java",
36+
],
37+
visibility = ["//visibility:public"],
38+
deps = [
39+
"//java/io/bazel/rules/closure/program",
40+
"@com_google_guava",
41+
"@com_github_jetbrains_kotlin//:preloader",
42+
],
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2017 PubRef.org. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.pubref.rules.kotlin;
18+
19+
import com.google.common.collect.Iterables;
20+
import io.bazel.rules.closure.program.CommandLineProgram;
21+
import org.jetbrains.kotlin.preloading.Preloader;
22+
23+
/**
24+
* CommandLineProgram Wrapper for the Kotlin Preloader.
25+
*/
26+
public final class KotlinPreloader implements CommandLineProgram {
27+
28+
@Override
29+
public Integer apply(Iterable<String> args) {
30+
try {
31+
Preloader.main(Iterables.toArray(args, String.class));
32+
return 0;
33+
} catch (Exception ex) {
34+
ex.printStackTrace();
35+
return 1;
36+
}
37+
}
38+
39+
}

java/org/pubref/rules/kotlin/KotlinWorker.java

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public Integer apply(Iterable<String> args) {
5050
switch (head) {
5151
case "KotlinCompiler":
5252
return new KotlinCompiler().apply(tail);
53+
case "KotlinPreloader":
54+
return new KotlinPreloader().apply(tail);
5355
default:
5456
output.println(
5557
"\nERROR: First flag to KotlinWorker should be specific compiler to run, "

kotlin/kotlin_repositories.bzl

+12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ java_import(
1010
name = "runtime",
1111
jars = ["lib/kotlin-runtime.jar"],
1212
)
13+
java_import(
14+
name = "stdlib",
15+
jars = ["lib/kotlin-stdlib.jar"],
16+
)
1317
java_import(
1418
name = "compiler",
1519
jars = ["lib/kotlin-compiler.jar"],
1620
)
21+
java_import(
22+
name = "preloader",
23+
jars = ["lib/kotlin-preloader.jar"],
24+
)
25+
java_import(
26+
name = "test",
27+
jars = ["lib/kotlin-test.jar"],
28+
)
1729
sh_binary(
1830
name = "kotlin",
1931
srcs = ["bin/kotlin"],

0 commit comments

Comments
 (0)