Skip to content

Commit 8de6f19

Browse files
authored
Merge pull request #12 from pubref/java_jars
Add java_deps attribute to kotlin_compile rule.
2 parents 522a1e7 + 6bc1cb1 commit 8de6f19

File tree

6 files changed

+71
-44
lines changed

6 files changed

+71
-44
lines changed

README.md

Lines changed: 3 additions & 3 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.2", # update as needed
22+
tag = "v0.2.2", # update as needed
2323
)
2424
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")
2525
kotlin_repositories()
@@ -56,7 +56,7 @@ kotlin_library(
5656
name = "my_kotlin_lib",
5757
srcs = ["kotlin_source_file.kt"],
5858
deps = [":some_other_kotlin_library_rule"],
59-
java_deps = [":some_other_java_library_rule"],
59+
java_deps = [":some_other_java_library_rule", "@another_maven_jar//jar"],
6060
)
6161
```
6262

@@ -98,7 +98,7 @@ android_binary(
9898
| --- | --- | --- |
9999
| `srcs` | `label_list` | Kotlin source files `*.kt` |
100100
| `deps` | `label_list` | List of `kotlin_library` targets |
101-
| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`) |
101+
| `java_deps` | `label_list` | List of java provider targets (`java_library`, `java_import`, `...`) |
102102
| `jars` | `label_list` | List of jar file targets (`*.jar`) |
103103
| `x_opts` | `string_list` | List of additional `-X` options to `kotlinc` |
104104
| `plugin_opts` | `string_dict` | List of additional `-P` options to `kotlinc` |

WORKSPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ maven_jar(
1212
artifact = "io.reactivex:rxjava:jar:1.2.1",
1313
#sha1 = "ssa",
1414
)
15+
16+
# Used to demonstrate/test maven dependencies
17+
maven_jar(
18+
name = "com_google_guava_guava_21_0",
19+
artifact = "com.google.guava:guava:jar:21.0",
20+
)

examples/helloworld/BUILD

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ kotlin_binary(
1010
main_class = "examples.helloworld.MainKt",
1111
srcs = ["main.kt"],
1212
deps = [":rules"],
13-
java_deps = [":milk"]
13+
java_deps = [
14+
":milk",
15+
":guava",
16+
],
1417
)
1518

1619
# A java rule that depends on a kotlin rule (using kotlin within traditional java)
1720
java_binary(
1821
name = "main_java",
1922
main_class = "examples.helloworld.Main",
2023
srcs = ["Main.java"],
21-
deps = [":rules_kt"],
24+
deps = [
25+
":rules_kt",
26+
":guava",
27+
],
2228
)
2329

2430
# A simple kotlin rule that defines "data classes"
@@ -45,3 +51,11 @@ java_test(
4551
"@junit4//jar",
4652
]
4753
)
54+
55+
# Included to test dependent java providers
56+
java_library(
57+
name = "guava",
58+
exports = [
59+
"@com_google_guava_guava_21_0//jar",
60+
],
61+
)

examples/helloworld/Main.java

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

3+
import com.google.common.base.Joiner;
34
// Not actually a necessary import since both in same package
45
import examples.helloworld.KotlinLibraryRule;
56

7+
68
public class Main {
79

810
public static void main(String[] args) {

examples/helloworld/main.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package examples.helloworld
22

3+
import com.google.common.base.Joiner
34
import examples.helloworld.SoyMilk
45

56
fun main(args : Array<String>) {
6-
println("I am Kotlin!......")
7-
println("... But what is soy milk?")
7+
println(Joiner.on(' ').join(arrayOf("I", "am", "Kotlin!", "......")))
8+
println(Joiner.on(' ').join(arrayOf("...", "But", "what", "is", "soy", "milk?")))
89
println(SoyMilk())
910
}

kotlin/rules.bzl

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@ def _kotlin_compile_impl(ctx):
1818
args += ["-P"]
1919
args += ["plugin:%s=\"%s\"" % (k, v)]
2020

21-
# Make classpath if needed. Include those from this rule and from
22-
# dependent rules.
23-
jars = [] + ctx.attr.jars
21+
# Make classpath if needed. Include those from this and dependent rules.
22+
jars = []
23+
24+
# Populate from (transitive) java dependencies
25+
for dep in ctx.attr.java_deps:
26+
# Add-in all source and generated jar files
27+
for file in dep.files:
28+
jars.append(file)
29+
# Add-in transitive dependencies
30+
for file in dep.java.transitive_deps:
31+
jars.append(file)
32+
33+
# Populate from (transitive) kotlin dependencies
2434
for dep in ctx.attr.deps:
25-
jars += [jar.files for jar in dep.kt.transitive_jars]
35+
jars += [file for file in dep.kt.transitive_jars]
36+
37+
# Populate from jar dependencies
38+
for fileset in ctx.attr.jars:
39+
# The fileset object is either a ConfiguredTarget OR a depset.
40+
files = getattr(fileset, 'files', None)
41+
if files:
42+
for file in files:
43+
jars += [file]
44+
else:
45+
for file in fileset:
46+
jars += [file]
47+
2648
if jars:
27-
jarfiles = []
28-
for fileset in jars:
29-
# The fileset object is either a ConfiguredTarget OR a depset.
30-
files = getattr(fileset, 'files', None)
31-
if files:
32-
for file in files:
33-
jarfiles += [file.path]
34-
inputs += [file]
35-
else:
36-
for file in fileset:
37-
jarfiles += [file.path]
38-
inputs += [file]
39-
classpath = ":".join(jarfiles)
40-
args += ["-cp", classpath]
49+
# De-duplicate
50+
jarsetlist = list(set(jars))
51+
args += ["-cp", ":".join([file.path for file in jarsetlist])]
52+
inputs += jarsetlist
4153

4254
# Need to traverse back up to execroot, then down again
4355
kotlin_home = ctx.executable._kotlinc.dirname \
@@ -66,7 +78,7 @@ def _kotlin_compile_impl(ctx):
6678
kt = struct(
6779
srcs = ctx.attr.srcs,
6880
jar = kt_jar,
69-
transitive_jars = jars,
81+
transitive_jars = [kt_jar] + jars,
7082
home = kotlin_home,
7183
),
7284
)
@@ -91,13 +103,18 @@ _kotlin_compile_attrs = {
91103
providers = ["kt"],
92104
),
93105

106+
# Dependent java rules.
107+
"java_deps": attr.label_list(
108+
providers = ["java"],
109+
),
110+
94111
# Not really implemented yet.
95112
"data": attr.label_list(
96113
allow_files = True,
97114
cfg = 'data',
98115
),
99116

100-
# Jars to put on the kotlinc classpath
117+
# Additional jar files to put on the kotlinc classpath
101118
"jars": attr.label_list(
102119
allow_files = jar_filetype,
103120
),
@@ -135,24 +152,12 @@ kotlin_compile = rule(
135152
)
136153

137154

138-
def _make_jars_list_from_java_deps(deps = []):
139-
jars = []
140-
for dep in deps:
141-
path = ""
142-
basename = dep
143-
if dep.find(":") >= 0:
144-
parts = dep.split(':')
145-
path = parts[0] if len(parts) > 0 else ""
146-
basename = parts[1]
147-
jars.append("%s:lib%s.jar" % (path, basename))
148-
return jars
149-
150-
151155
def kotlin_library(name, jars = [], java_deps = [], **kwargs):
152156

153157
kotlin_compile(
154158
name = name,
155-
jars = jars + _make_jars_list_from_java_deps(java_deps),
159+
jars = jars,
160+
java_deps = java_deps,
156161
**kwargs
157162
)
158163

@@ -175,11 +180,10 @@ def kotlin_binary(name,
175180
java_deps = [],
176181
**kwargs):
177182

178-
java_library_jars = _make_jars_list_from_java_deps(java_deps)
179-
180183
kotlin_compile(
181184
name = name + "_kt",
182-
jars = jars + java_library_jars,
185+
jars = jars,
186+
java_deps = java_deps,
183187
srcs = srcs,
184188
deps = deps,
185189
x_opts = x_opts,

0 commit comments

Comments
 (0)