Skip to content

Commit

Permalink
Merge pull request #12 from pubref/java_jars
Browse files Browse the repository at this point in the history
Add java_deps attribute to kotlin_compile rule.
  • Loading branch information
pcj committed Apr 1, 2017
2 parents 522a1e7 + 6bc1cb1 commit 8de6f19
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README.md
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.2", # update as needed
tag = "v0.2.2", # update as needed
)
load("@org_pubref_rules_kotlin//kotlin:rules.bzl", "kotlin_repositories")
kotlin_repositories()
Expand Down Expand Up @@ -56,7 +56,7 @@ kotlin_library(
name = "my_kotlin_lib",
srcs = ["kotlin_source_file.kt"],
deps = [":some_other_kotlin_library_rule"],
java_deps = [":some_other_java_library_rule"],
java_deps = [":some_other_java_library_rule", "@another_maven_jar//jar"],
)
```

Expand Down Expand Up @@ -98,7 +98,7 @@ android_binary(
| --- | --- | --- |
| `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`) |
| `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` |
Expand Down
6 changes: 6 additions & 0 deletions WORKSPACE
Expand Up @@ -12,3 +12,9 @@ maven_jar(
artifact = "io.reactivex:rxjava:jar:1.2.1",
#sha1 = "ssa",
)

# Used to demonstrate/test maven dependencies
maven_jar(
name = "com_google_guava_guava_21_0",
artifact = "com.google.guava:guava:jar:21.0",
)
18 changes: 16 additions & 2 deletions examples/helloworld/BUILD
Expand Up @@ -10,15 +10,21 @@ kotlin_binary(
main_class = "examples.helloworld.MainKt",
srcs = ["main.kt"],
deps = [":rules"],
java_deps = [":milk"]
java_deps = [
":milk",
":guava",
],
)

# 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",
":guava",
],
)

# A simple kotlin rule that defines "data classes"
Expand All @@ -45,3 +51,11 @@ java_test(
"@junit4//jar",
]
)

# Included to test dependent java providers
java_library(
name = "guava",
exports = [
"@com_google_guava_guava_21_0//jar",
],
)
2 changes: 2 additions & 0 deletions examples/helloworld/Main.java
@@ -1,8 +1,10 @@
package examples.helloworld;

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


public class Main {

public static void main(String[] args) {
Expand Down
5 changes: 3 additions & 2 deletions examples/helloworld/main.kt
@@ -1,9 +1,10 @@
package examples.helloworld

import com.google.common.base.Joiner
import examples.helloworld.SoyMilk

fun main(args : Array<String>) {
println("I am Kotlin!......")
println("... But what is soy milk?")
println(Joiner.on(' ').join(arrayOf("I", "am", "Kotlin!", "......")))
println(Joiner.on(' ').join(arrayOf("...", "But", "what", "is", "soy", "milk?")))
println(SoyMilk())
}
78 changes: 41 additions & 37 deletions kotlin/rules.bzl
Expand Up @@ -18,26 +18,38 @@ def _kotlin_compile_impl(ctx):
args += ["-P"]
args += ["plugin:%s=\"%s\"" % (k, v)]

# Make classpath if needed. Include those from this rule and from
# dependent rules.
jars = [] + ctx.attr.jars
# Make classpath if needed. Include those from this and dependent rules.
jars = []

# Populate from (transitive) java dependencies
for dep in ctx.attr.java_deps:
# Add-in all source and generated jar files
for file in dep.files:
jars.append(file)
# Add-in transitive dependencies
for file in dep.java.transitive_deps:
jars.append(file)

# Populate from (transitive) kotlin dependencies
for dep in ctx.attr.deps:
jars += [jar.files for jar in dep.kt.transitive_jars]
jars += [file for file in dep.kt.transitive_jars]

# Populate from jar dependencies
for fileset in ctx.attr.jars:
# The fileset object is either a ConfiguredTarget OR a depset.
files = getattr(fileset, 'files', None)
if files:
for file in files:
jars += [file]
else:
for file in fileset:
jars += [file]

if jars:
jarfiles = []
for fileset in jars:
# The fileset object is either a ConfiguredTarget OR a depset.
files = getattr(fileset, 'files', None)
if files:
for file in files:
jarfiles += [file.path]
inputs += [file]
else:
for file in fileset:
jarfiles += [file.path]
inputs += [file]
classpath = ":".join(jarfiles)
args += ["-cp", classpath]
# De-duplicate
jarsetlist = list(set(jars))
args += ["-cp", ":".join([file.path for file in jarsetlist])]
inputs += jarsetlist

# Need to traverse back up to execroot, then down again
kotlin_home = ctx.executable._kotlinc.dirname \
Expand Down Expand Up @@ -66,7 +78,7 @@ def _kotlin_compile_impl(ctx):
kt = struct(
srcs = ctx.attr.srcs,
jar = kt_jar,
transitive_jars = jars,
transitive_jars = [kt_jar] + jars,
home = kotlin_home,
),
)
Expand All @@ -91,13 +103,18 @@ _kotlin_compile_attrs = {
providers = ["kt"],
),

# Dependent java rules.
"java_deps": attr.label_list(
providers = ["java"],
),

# Not really implemented yet.
"data": attr.label_list(
allow_files = True,
cfg = 'data',
),

# Jars to put on the kotlinc classpath
# Additional jar files to put on the kotlinc classpath
"jars": attr.label_list(
allow_files = jar_filetype,
),
Expand Down Expand Up @@ -135,24 +152,12 @@ kotlin_compile = rule(
)


def _make_jars_list_from_java_deps(deps = []):
jars = []
for dep in deps:
path = ""
basename = dep
if dep.find(":") >= 0:
parts = dep.split(':')
path = parts[0] if len(parts) > 0 else ""
basename = parts[1]
jars.append("%s:lib%s.jar" % (path, basename))
return jars


def kotlin_library(name, jars = [], java_deps = [], **kwargs):

kotlin_compile(
name = name,
jars = jars + _make_jars_list_from_java_deps(java_deps),
jars = jars,
java_deps = java_deps,
**kwargs
)

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

java_library_jars = _make_jars_list_from_java_deps(java_deps)

kotlin_compile(
name = name + "_kt",
jars = jars + java_library_jars,
jars = jars,
java_deps = java_deps,
srcs = srcs,
deps = deps,
x_opts = x_opts,
Expand Down

0 comments on commit 8de6f19

Please sign in to comment.