From 6bc1cb1463064f9aeeb8dd99a77bab985c77183b Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Fri, 31 Mar 2017 21:19:53 -0700 Subject: [PATCH] Add java_deps attribute to kotlin_compile rule. --- README.md | 6 +-- WORKSPACE | 6 +++ examples/helloworld/BUILD | 18 +++++++- examples/helloworld/Main.java | 2 + examples/helloworld/main.kt | 5 ++- kotlin/rules.bzl | 78 ++++++++++++++++++----------------- 6 files changed, 71 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 93c0a2b..3649031 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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"], ) ``` @@ -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` | diff --git a/WORKSPACE b/WORKSPACE index 3736be6..d092cd5 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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", +) diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD index dd160d8..39e238e 100644 --- a/examples/helloworld/BUILD +++ b/examples/helloworld/BUILD @@ -10,7 +10,10 @@ 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) @@ -18,7 +21,10 @@ 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" @@ -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", + ], +) diff --git a/examples/helloworld/Main.java b/examples/helloworld/Main.java index 8a76d48..411be76 100644 --- a/examples/helloworld/Main.java +++ b/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) { diff --git a/examples/helloworld/main.kt b/examples/helloworld/main.kt index c913e46..bd4ee9a 100644 --- a/examples/helloworld/main.kt +++ b/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) { - 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()) } diff --git a/kotlin/rules.bzl b/kotlin/rules.bzl index eab4f42..51c9185 100644 --- a/kotlin/rules.bzl +++ b/kotlin/rules.bzl @@ -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 \ @@ -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, ), ) @@ -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, ), @@ -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 ) @@ -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,