Skip to content

Commit

Permalink
Merge pull request #8 from pubref/java_deps
Browse files Browse the repository at this point in the history
Adds 'java_deps' attribute
  • Loading branch information
pcj authored Mar 19, 2017
2 parents 2e52fb9 + fdf3099 commit 6582abe
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ os:

env:
- V=HEAD
- V=0.4.5
- V=0.4.4
- V=0.4.3
- V=0.4.2
- V=0.4.1
Expand Down
91 changes: 61 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add the following to your WORKSPACE file:
git_repository(
name = "org_pubref_rules_kotlin",
remote = "https://github.com/pubref/rules_kotlin.git",
tag = "v0.1.1", # update as needed
tag = "v0.2", # update as needed
)
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")
kotlin_repositories()
Expand All @@ -39,17 +39,6 @@ sh_binary rule @com_github_jetbrains_kotlin//:kotlinc
sh_binary rule @com_github_jetbrains_kotlin//:kotlin
```

## bazel.rc

Add the following line to your `tools/bazel.rc` file:

```
build --strategy=KotlinCompile=standalone
```

Alternatively, you can also add `--strategy=KotlinCompile=standalone` parameters
to every `bazel run`, `bazel build`, etc. commands involving a kotlin rule.

## Package (BUILD file) rules

Add the following to your BUILD file:
Expand All @@ -67,14 +56,14 @@ kotlin_library(
name = "my_kotlin_lib",
srcs = ["kotlin_source_file.kt"],
deps = [":some_other_kotlin_library_rule"],
jars = [":some_other_java_library_rule"],
java_deps = [":some_other_java_library_rule"],
)
```

Use the `deps` attribute to name other `kotlin_library` targets as jar
providers for this rule. Use the `jars` attribute to name other
`java_library` targets (to expose traditional java classes in your
kotlin source).
providers for this rule. Use the `java_deps` attribute to name other
`java_library` or `java_import` targets (to expose traditional java
classes in your kotlin source).

To compile a set of kotlin sources files with the `kotlinc` tool and
emit the corresponding jar file, use:
Expand Down Expand Up @@ -103,6 +92,18 @@ android_binary(
)
```

### kotlin_library attributes

| Name | Type | Description |
| --- | --- | --- |
| `srcs` | `label_list` | Kotlin source files `*.kt` |
| `deps` | `label_list` | List of `kotlin_library` targets |
| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`) |
| `jars` | `label_list` | List of jar file targets (`*.jar`) |
| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |
| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |


### kotlin_binary

A `kotlin_binary` rule takes the same arguments as a `kotlin_library`,
Expand All @@ -116,50 +117,80 @@ kotlin_binary(
main_class = "my.project.MainKt",
srcs = ["main.kt"],
deps = [":my_kotlin_lib"]
jars = [":javalib"]
java_deps = [":javalib"]
)
```

This will prepare command line arguments for the `kotlin` runner and
run it. It does not at this time support packaging up a fat
executable jar.
To create a self-contained executable jar, invoke the implicit
`_deploy.jar` target. For example:

```sh
$ bazel build :main_kt_deploy.jar
Target :main_kt_deploy.jar up-to-date:
bazel-bin/.../main_kt_deploy.jar
$ java -jar ./bazel-bin/.../main_kt_deploy.jar
```

#### kotlin_binary attributes

### Rule attributes
Includes all `kotlin_library` attributes as well as:

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


### kotlin_compile

The `kotlin_compile` rule runs the `kotlinc` tool to generate a `.jar`
file from a list of kotlin source files. The `kotlin_library` rule
(actually, macro) calls this internally and then makes the jarfile
available to other java rules via a `java_import` rule.

In summary, you most likely do not need to interact with the
`kotlin_compile` rule directly.


## bazel.rc

With older versions of bazel, you may need to add the following line
to your `tools/bazel.rc` file:

```
build --strategy=KotlinCompile=standalone
```

Alternatively, you can also add `--strategy=KotlinCompile=standalone`
parameters to every `bazel run`, `bazel build`, etc. commands
involving a kotlin rule.

# Summary

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

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

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

```sh
$ git clone https://github.com/pubref/rules_kotlin
$ cd rules_kotlin
$ bazel query //... --output label_kind
$ bazel run examples/helloworld:main_kt
$ bazel run examples/helloworld:main_java
```

## TODO

1. Implement a `kotlin_test` rule.
2. Fix the semantics of a `kotlin_binary` rule to make an executable
target rather than as a runner library.
3. Proper `data` and runfiles support.
3. Research incremental compilation and bazel integration.
2. Proper `data` and runfiles support.
3. Research incremental compilation and bazel worker integration.

[bazel]: http://www.bazel.io
[kotlin]: http://www.kotlinlang.org
7 changes: 4 additions & 3 deletions examples/helloworld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ kotlin_binary(
main_class = "examples.helloworld.MainKt",
srcs = ["main.kt"],
deps = [":rules"],
jars = [":milk"]
java_deps = [":milk"]
)

# A java rule that depends on a kotlin rule (using kotlin within traditional java)
java_binary(
name = "main_java",
main_class = "examples.helloworld.Main",
srcs = ["Main.java"],
deps = [":rules_kt"]
deps = [":rules_kt"],
)

# A simple kotlin rule that defines "data classes"
kotlin_library(
name = "rules",
srcs = ["rules.kt"],
java_deps = [":milk"]
)

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


# A java rule that depends on a kotlin rule (using kotlin within traditional java)
java_test(
name = "main_test",
test_class = "examples.helloworld.MainTest",
srcs = ["MainTest.java"],
size = "small",
deps = [
":rules_kt",
"@junit4//jar",
Expand Down
3 changes: 1 addition & 2 deletions examples/helloworld/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import examples.helloworld.KotlinLibraryRule;

public class Main {

public static void main(String[] args) {
KotlinLibraryRule rule = new KotlinLibraryRule(
"foo",
new java.util.ArrayList(),
new java.util.ArrayList());
System.out.println("A bazel kotlin_library rule looks something like: ");
System.out.println(rule.toString());


}

void testRuleName() {
Expand Down
2 changes: 0 additions & 2 deletions examples/helloworld/rules.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package examples.helloworld

// using go-mode in my emacs here. Also a kotlin newbie.

data class KotlinLibraryRule(
val name: String,
val jars: List<String> = listOf<String>(),
Expand Down
Loading

0 comments on commit 6582abe

Please sign in to comment.