Skip to content

Commit 6582abe

Browse files
authored
Merge pull request #8 from pubref/java_deps
Adds 'java_deps' attribute
2 parents 2e52fb9 + fdf3099 commit 6582abe

File tree

6 files changed

+114
-121
lines changed

6 files changed

+114
-121
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ os:
99

1010
env:
1111
- V=HEAD
12+
- V=0.4.5
13+
- V=0.4.4
1214
- V=0.4.3
1315
- V=0.4.2
1416
- V=0.4.1

README.md

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Add the following to your WORKSPACE file:
1919
git_repository(
2020
name = "org_pubref_rules_kotlin",
2121
remote = "https://github.com/pubref/rules_kotlin.git",
22-
tag = "v0.1.1", # update as needed
22+
tag = "v0.2", # update as needed
2323
)
2424
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")
2525
kotlin_repositories()
@@ -39,17 +39,6 @@ sh_binary rule @com_github_jetbrains_kotlin//:kotlinc
3939
sh_binary rule @com_github_jetbrains_kotlin//:kotlin
4040
```
4141

42-
## bazel.rc
43-
44-
Add the following line to your `tools/bazel.rc` file:
45-
46-
```
47-
build --strategy=KotlinCompile=standalone
48-
```
49-
50-
Alternatively, you can also add `--strategy=KotlinCompile=standalone` parameters
51-
to every `bazel run`, `bazel build`, etc. commands involving a kotlin rule.
52-
5342
## Package (BUILD file) rules
5443

5544
Add the following to your BUILD file:
@@ -67,14 +56,14 @@ kotlin_library(
6756
name = "my_kotlin_lib",
6857
srcs = ["kotlin_source_file.kt"],
6958
deps = [":some_other_kotlin_library_rule"],
70-
jars = [":some_other_java_library_rule"],
59+
java_deps = [":some_other_java_library_rule"],
7160
)
7261
```
7362

7463
Use the `deps` attribute to name other `kotlin_library` targets as jar
75-
providers for this rule. Use the `jars` attribute to name other
76-
`java_library` targets (to expose traditional java classes in your
77-
kotlin source).
64+
providers for this rule. Use the `java_deps` attribute to name other
65+
`java_library` or `java_import` targets (to expose traditional java
66+
classes in your kotlin source).
7867

7968
To compile a set of kotlin sources files with the `kotlinc` tool and
8069
emit the corresponding jar file, use:
@@ -103,6 +92,18 @@ android_binary(
10392
)
10493
```
10594

95+
### kotlin_library attributes
96+
97+
| Name | Type | Description |
98+
| --- | --- | --- |
99+
| `srcs` | `label_list` | Kotlin source files `*.kt` |
100+
| `deps` | `label_list` | List of `kotlin_library` targets |
101+
| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`) |
102+
| `jars` | `label_list` | List of jar file targets (`*.jar`) |
103+
| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |
104+
| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |
105+
106+
106107
### kotlin_binary
107108

108109
A `kotlin_binary` rule takes the same arguments as a `kotlin_library`,
@@ -116,50 +117,80 @@ kotlin_binary(
116117
main_class = "my.project.MainKt",
117118
srcs = ["main.kt"],
118119
deps = [":my_kotlin_lib"]
119-
jars = [":javalib"]
120+
java_deps = [":javalib"]
120121
)
121122
```
122123

123-
This will prepare command line arguments for the `kotlin` runner and
124-
run it. It does not at this time support packaging up a fat
125-
executable jar.
124+
To create a self-contained executable jar, invoke the implicit
125+
`_deploy.jar` target. For example:
126+
127+
```sh
128+
$ bazel build :main_kt_deploy.jar
129+
Target :main_kt_deploy.jar up-to-date:
130+
bazel-bin/.../main_kt_deploy.jar
131+
$ java -jar ./bazel-bin/.../main_kt_deploy.jar
132+
```
133+
134+
#### kotlin_binary attributes
126135

127-
### Rule attributes
136+
Includes all `kotlin_library` attributes as well as:
128137

129138
| Name | Type | Description |
130139
| --- | --- | --- |
131-
| `srcs` | `label_list` | Kotlin source files `*.kt` |
132-
| `deps` | `label_list` | List of `kotlin_library` targets |
133-
| `jars` | `label_list` | List of jar provider targets (`java_library`, `java_import`) |
134-
| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |
135-
| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |
136140
| `main_class` | `string` | Main class to run with the `kotlin_binary` rule |
137141

138142

143+
### kotlin_compile
144+
145+
The `kotlin_compile` rule runs the `kotlinc` tool to generate a `.jar`
146+
file from a list of kotlin source files. The `kotlin_library` rule
147+
(actually, macro) calls this internally and then makes the jarfile
148+
available to other java rules via a `java_import` rule.
149+
150+
In summary, you most likely do not need to interact with the
151+
`kotlin_compile` rule directly.
152+
153+
154+
## bazel.rc
155+
156+
With older versions of bazel, you may need to add the following line
157+
to your `tools/bazel.rc` file:
158+
159+
```
160+
build --strategy=KotlinCompile=standalone
161+
```
162+
163+
Alternatively, you can also add `--strategy=KotlinCompile=standalone`
164+
parameters to every `bazel run`, `bazel build`, etc. commands
165+
involving a kotlin rule.
166+
139167
# Summary
140168

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

173+
> Note: if you have a bunch of maven (central) dependencies, consider
174+
> [rules_maven](https://github.com/pubref/rules_maven) for taming the
175+
> issue of transitive dependencies with your java/kotlin projects.
176+
145177
## Examples
146178

147179
To run the examples in this repository, clone the repo:
148180

149181
```sh
150182
$ git clone https://github.com/pubref/rules_kotlin
151183
$ cd rules_kotlin
184+
$ bazel query //... --output label_kind
152185
$ bazel run examples/helloworld:main_kt
153186
$ bazel run examples/helloworld:main_java
154187
```
155188

156189
## TODO
157190

158191
1. Implement a `kotlin_test` rule.
159-
2. Fix the semantics of a `kotlin_binary` rule to make an executable
160-
target rather than as a runner library.
161-
3. Proper `data` and runfiles support.
162-
3. Research incremental compilation and bazel integration.
192+
2. Proper `data` and runfiles support.
193+
3. Research incremental compilation and bazel worker integration.
163194

164195
[bazel]: http://www.bazel.io
165196
[kotlin]: http://www.kotlinlang.org

examples/helloworld/BUILD

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ kotlin_binary(
1010
main_class = "examples.helloworld.MainKt",
1111
srcs = ["main.kt"],
1212
deps = [":rules"],
13-
jars = [":milk"]
13+
java_deps = [":milk"]
1414
)
1515

1616
# A java rule that depends on a kotlin rule (using kotlin within traditional java)
1717
java_binary(
1818
name = "main_java",
1919
main_class = "examples.helloworld.Main",
2020
srcs = ["Main.java"],
21-
deps = [":rules_kt"]
21+
deps = [":rules_kt"],
2222
)
2323

2424
# A simple kotlin rule that defines "data classes"
2525
kotlin_library(
2626
name = "rules",
2727
srcs = ["rules.kt"],
28+
java_deps = [":milk"]
2829
)
2930

3031
# A simple java class that defines soy milk
@@ -33,12 +34,12 @@ java_library(
3334
srcs = ["SoyMilk.java"],
3435
)
3536

36-
3737
# A java rule that depends on a kotlin rule (using kotlin within traditional java)
3838
java_test(
3939
name = "main_test",
4040
test_class = "examples.helloworld.MainTest",
4141
srcs = ["MainTest.java"],
42+
size = "small",
4243
deps = [
4344
":rules_kt",
4445
"@junit4//jar",

examples/helloworld/Main.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import examples.helloworld.KotlinLibraryRule;
55

66
public class Main {
7+
78
public static void main(String[] args) {
89
KotlinLibraryRule rule = new KotlinLibraryRule(
910
"foo",
1011
new java.util.ArrayList(),
1112
new java.util.ArrayList());
1213
System.out.println("A bazel kotlin_library rule looks something like: ");
1314
System.out.println(rule.toString());
14-
15-
1615
}
1716

1817
void testRuleName() {

examples/helloworld/rules.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package examples.helloworld
22

3-
// using go-mode in my emacs here. Also a kotlin newbie.
4-
53
data class KotlinLibraryRule(
64
val name: String,
75
val jars: List<String> = listOf<String>(),

0 commit comments

Comments
 (0)