@@ -19,7 +19,7 @@ Add the following to your WORKSPACE file:
1919git_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)
2424load(" @org_pubref_rules_kotlin//kotlin:rules.bzl" , " kotlin_repositories" )
2525kotlin_repositories()
@@ -39,17 +39,6 @@ sh_binary rule @com_github_jetbrains_kotlin//:kotlinc
3939sh_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
5544Add 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
7463Use 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
7968To compile a set of kotlin sources files with the ` kotlinc ` tool and
8069emit 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
108109A ` 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
141169That's it! Hopefully these rules with make it easy to mix kotlin and
142170traditional java code in your projects and take advantage of bazel's
143171approach 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
147179To 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
1581911 . 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
0 commit comments