Move implicits to toolchain and some other stuff (#92) * move implicits to kotlin toolchain, tidy up srcs and fix bug, remove proto from workspace till 0.16 * Update README.md straighten out comment.
diff --git a/.bazelproject b/.bazelproject index 03a69f4..73d0819 100644 --- a/.bazelproject +++ b/.bazelproject
@@ -17,6 +17,7 @@ . targets: + -//docs/... //kotlin/builder:for_ide //:all_tests //examples/...
diff --git a/README.md b/README.md index 9bfe029..2c8f572 100644 --- a/README.md +++ b/README.md
@@ -3,6 +3,8 @@ [Skydoc documentation](https://bazelbuild.github.io/rules_kotlin) # Announcements +* <b>Jun 29, 2018.</b> The commits from this date forward are compatible with bazel `>=0.14`. JDK9 host issues were + fixed as well some other deprecations. I recommend skipping `0.15.0` if you are on a Mac. * <b>May 25, 2018.</b> Test "friend" support. A single friend dep can be provided to `kt_jvm_test` which allows the test to access internal members of the module under test. * <b>February 15, 2018.</b> Toolchains for the JVM rules. Currently this allow tweaking:
diff --git a/WORKSPACE b/WORKSPACE index f7146f8..c69d24f 100644 --- a/WORKSPACE +++ b/WORKSPACE
@@ -14,11 +14,11 @@ workspace(name="io_bazel_rules_kotlin") load("//kotlin/internal:bootstrap.bzl",github_archive="github_archive") -github_archive( - name = "com_google_protobuf", - repo = "google/protobuf", - commit = "106ffc04be1abf3ff3399f54ccf149815b287dd9", -) +#github_archive( +# name = "com_google_protobuf", +# repo = "google/protobuf", +# commit = "106ffc04be1abf3ff3399f54ccf149815b287dd9", +#) github_archive( name = "build_bazel_rules_nodejs",
diff --git a/kotlin/builder/BUILD b/kotlin/builder/BUILD index a268bbf..b14900b 100644 --- a/kotlin/builder/BUILD +++ b/kotlin/builder/BUILD
@@ -23,7 +23,9 @@ # Common depset for the builder. _COMMON_DEPS = [ - "//kotlin/builder/proto", + "//kotlin/builder/proto:deps", + "//kotlin/builder/proto:kotlin_model", + "//kotlin/builder/proto:worker", "@com_github_jetbrains_kotlin//:preloader", "@io_bazel_rules_kotlin_com_google_protobuf_protobuf_java//jar", "@io_bazel_rules_kotlin_com_google_protobuf_protobuf_java_util//jar",
diff --git a/kotlin/builder/proto/BUILD b/kotlin/builder/proto/BUILD index 5d5550f..8bc369b 100644 --- a/kotlin/builder/proto/BUILD +++ b/kotlin/builder/proto/BUILD
@@ -12,45 +12,45 @@ # See the License for the specific language governing permissions and # limitations under the License. - -proto_library( - name = "kotlin_model_proto", - srcs = [":kotlin_model.proto"], - visibility=["//visibility:public"] -) - -java_proto_library( - name = "kotlin_model_java_proto", - deps = [":kotlin_model_proto"] -) - -_PROTO_LIBS=["deps", "worker_protocol"] - -[proto_library( - name="%s_proto" % lib, - srcs=["%s.proto" % lib] - ) for lib in _PROTO_LIBS] - -[java_proto_library( - name="%s_java_proto" % lib, - deps=["%s_proto" % lib], - ) for lib in _PROTO_LIBS] +# commented out till 0.16 is released. We aren't prebuilding the libraries at the moment so it's fine. +#proto_library( +# name = "kotlin_model_proto", +# srcs = [":kotlin_model.proto"], +# visibility=["//visibility:public"] +#) +# +#java_proto_library( +# name = "kotlin_model_java_proto", +# deps = [":kotlin_model_proto"] +#) +# +#_PROTO_LIBS=["deps", "worker_protocol"] +# +#[proto_library( +# name="%s_proto" % lib, +# srcs=["%s.proto" % lib] +# ) for lib in _PROTO_LIBS] +# +#[java_proto_library( +# name="%s_java_proto" % lib, +# deps=["%s_proto" % lib], +# ) for lib in _PROTO_LIBS] +# java_import( - name = "proto", - jars = [ - "jars/libdeps_proto-speed.jar", - "jars/libworker_protocol_proto-speed.jar", - "jars/libkotlin_model_proto-speed.jar" - ], + name = "deps", + jars = ["jars/libdeps_proto-speed.jar"], visibility = ["//kotlin/builder:__subpackages__"] ) -filegroup( - name = "files", - srcs = [ - "jars/libdeps_proto-speed.jar", - "jars/libworker_protocol_proto-speed.jar" - ], +java_import( + name = "worker", + jars = ["jars/libworker_protocol_proto-speed.jar"], + visibility = ["//kotlin/builder:__subpackages__"] +) + +java_import( + name = "kotlin_model", + jars = ["jars/libkotlin_model_proto-speed.jar"], visibility = ["//kotlin/builder:__subpackages__"] ) \ No newline at end of file
diff --git a/kotlin/internal/compile.bzl b/kotlin/internal/compile.bzl index b444c16..e08429a 100644 --- a/kotlin/internal/compile.bzl +++ b/kotlin/internal/compile.bzl
@@ -18,7 +18,7 @@ def _declare_output_directory(ctx, aspect, dir_name): return ctx.actions.declare_directory("_kotlinc/%s_%s/%s_%s" % (ctx.label.name, aspect, ctx.label.name, dir_name)) -def _kotlin_do_compile_action(ctx, rule_kind, output_jar, compile_jars, module_name, friend_paths, srcs, src_jars): +def _kotlin_do_compile_action(ctx, rule_kind, output_jar, compile_jars, module_name, friend_paths, srcs): """Internal macro that sets up a Kotlin compile action. This macro only supports a single Kotlin compile operation for a rule. @@ -35,8 +35,7 @@ sourcegen_directory=_declare_output_directory(ctx, "jvm", "sourcegenfiles") temp_directory=_declare_output_directory(ctx, "jvm", "temp") - - tc=ctx.toolchains[kt.defs.TOOLCHAIN_TYPE] + toolchain=ctx.toolchains[kt.defs.TOOLCHAIN_TYPE] args = [ "--target_label", ctx.label, @@ -51,21 +50,18 @@ "--output_jdeps", ctx.outputs.jdeps.path, "--classpath", "\n".join([f.path for f in compile_jars.to_list()]), "--kotlin_friend_paths", "\n".join(friend_paths.to_list()), - "--kotlin_jvm_target", tc.jvm_target, - "--kotlin_api_version", tc.api_version, - "--kotlin_language_version", tc.language_version, + "--kotlin_jvm_target", toolchain.jvm_target, + "--kotlin_api_version", toolchain.api_version, + "--kotlin_language_version", toolchain.language_version, "--kotlin_module_name", module_name, - "--kotlin_passthrough_flags", "-Xcoroutines=%s" % tc.coroutines + "--kotlin_passthrough_flags", "-Xcoroutines=%s" % toolchain.coroutines ] - if len(srcs) == 0 and len(src_jars) == 0: - fail("srcs did not contain kotlin/java files or any srcjars") + if (len(srcs.kt) + len(srcs.java)) > 0: + args += ["--sources", "\n".join([s.path for s in (srcs.kt + srcs.java)])] - if len(srcs) > 0: - args += ["--sources", "\n".join(srcs)] - - if len(src_jars) > 0: - args += ["--source_jars", "\n".join([sj.path for sj in src_jars])] + if len(srcs.src_jars) > 0: + args += ["--source_jars", "\n".join([sj.path for sj in srcs.src_jars])] # Collect and prepare plugin descriptor for the worker. plugin_info=plugins.merge_plugin_infos(ctx.attr.plugins + ctx.attr.deps) @@ -76,19 +72,17 @@ args_file = ctx.actions.declare_file(ctx.label.name + ".jar-2.params") ctx.actions.write(args_file, "\n".join(args)) - # When a stratetegy isn't provided for the worker and the workspace is fresh then certain deps are not available under - # external/@com_github_jetbrains_kotlin/... that is why the classpath is added explicetly. - compile_inputs = ( - depset([args_file]) + - ctx.files.srcs + - compile_jars + - ctx.files._kotlin_compiler_classpath + - ctx.files._kotlin_home + - ctx.files._jdk) + progress_message = "Compiling Kotlin %s { kt: %d, java: %d, srcjars: %d }" % ( + ctx.label, + len(srcs.kt), + len(srcs.java), + len(srcs.src_jars) + ) - ctx.action( + inputs, _, input_manifests = ctx.resolve_command(tools = [toolchain.kotlinbuilder]) + ctx.actions.run( mnemonic = "KotlinCompile", - inputs = compile_inputs, + inputs = depset([args_file]) + inputs + ctx.files.srcs + compile_jars, outputs = [ output_jar, ctx.outputs.jdeps, @@ -97,16 +91,17 @@ temp_directory, generated_classes_directory ], - executable = ctx.executable._kotlinw, + executable = toolchain.kotlinbuilder.files_to_run.executable, execution_requirements = {"supports-workers": "1"}, arguments = ["@" + args_file.path], - progress_message="Compiling %d Kotlin source files to %s" % (len(ctx.files.srcs), output_jar.short_path), + progress_message = progress_message, + input_manifests = input_manifests ) def _select_std_libs(ctx): - return ctx.files._kotlin_std + return ctx.toolchains[kt.defs.TOOLCHAIN_TYPE].jvm_stdlibs -def _make_java_provider(ctx, input_deps=[], auto_deps=[], src_jars=[]): +def _make_java_provider(ctx, srcs, input_deps=[], auto_deps=[]): """Creates the java_provider for a Kotlin target. This macro is distinct from the kotlin_make_providers as collecting the java_info is useful before the DefaultInfo is @@ -122,6 +117,7 @@ Returns: A JavaInfo provider. """ + runtime = ctx.toolchains[kt.defs.TOOLCHAIN_TYPE].jvm_runtime deps=utils.collect_all_jars(input_deps) exported_deps=utils.collect_all_jars(getattr(ctx.attr, "exports", [])) @@ -129,7 +125,7 @@ my_runtime_jars = exported_deps.transitive_runtime_jars + [ctx.outputs.jar] my_transitive_compile_jars = my_compile_jars + deps.transitive_compile_time_jars + exported_deps.transitive_compile_time_jars + auto_deps - my_transitive_runtime_jars = my_runtime_jars + deps.transitive_runtime_jars + exported_deps.transitive_runtime_jars + [ctx.file._kotlin_runtime] + auto_deps + my_transitive_runtime_jars = my_runtime_jars + deps.transitive_runtime_jars + exported_deps.transitive_runtime_jars + runtime + auto_deps # collect the runtime jars from the runtime_deps attribute. for jar in ctx.attr.runtime_deps: @@ -139,13 +135,13 @@ use_ijar = False, # A list or set of output source jars that contain the uncompiled source files including the source files # generated by annotation processors if the case. - source_jars= src_jars + utils.actions.maybe_make_srcsjar(ctx), + source_jars = utils.actions.maybe_make_srcsjar(ctx, srcs), # A list or a set of jars that should be used at compilation for a given target. compile_time_jars = my_compile_jars, # A list or a set of jars that should be used at runtime for a given target. - runtime_jars=my_runtime_jars, - transitive_compile_time_jars= my_transitive_compile_jars, - transitive_runtime_jars=my_transitive_runtime_jars + runtime_jars = my_runtime_jars, + transitive_compile_time_jars = my_transitive_compile_jars, + transitive_runtime_jars = my_transitive_runtime_jars ) def _make_providers(ctx, java_info, module_name, transitive_files=depset(order="default")): @@ -207,13 +203,10 @@ deps = ctx.attr.deps + getattr(ctx.attr, "friends", []) - srcs = [] - src_jars = [] - for f in ctx.files.srcs: - if f.path.endswith(".kt") or f.path.endswith(".java"): - srcs.append(f.path) - elif f.path.endswith(".srcjar"): - src_jars.append(f) + srcs = utils.partition_srcs(ctx.files.srcs) + + if (len(srcs.kt) + len(srcs.java) == 0) and len(srcs.src_jars) == 0: + fail("no sources provided") # setup the compile action. _kotlin_do_compile_action( @@ -223,8 +216,7 @@ compile_jars = utils.collect_jars_for_compile(deps) + kotlin_auto_deps, module_name = module_name, friend_paths = friend_paths, - srcs = srcs, - src_jars = src_jars + srcs = srcs ) # setup the merge action if needed. @@ -232,7 +224,7 @@ utils.actions.fold_jars(ctx, output_jar, output_merge_list) # create the java provider but the kotlin and default provider cannot be created here. - return _make_java_provider(ctx, deps, kotlin_auto_deps, src_jars) + return _make_java_provider(ctx, srcs, deps, kotlin_auto_deps) compile = struct( compile_action = _compile_action,
diff --git a/kotlin/internal/utils.bzl b/kotlin/internal/utils.bzl index d7890a4..fd34a66 100644 --- a/kotlin/internal/utils.bzl +++ b/kotlin/internal/utils.bzl
@@ -30,6 +30,25 @@ module_name = (ctx.label.package.lstrip("/").replace("/","_") + "-" + ctx.label.name.replace("/", "_")) return module_name +def _partition_srcs(srcs): + kt_srcs = [] + java_srcs = [] + src_jars = [] + + for f in srcs: + if f.path.endswith(".kt"): + kt_srcs.append(f) + elif f.path.endswith(".java"): + java_srcs.append(f) + elif f.path.endswith(".srcjar"): + src_jars.append(f) + + return struct ( + kt = kt_srcs, + java = java_srcs, + src_jars = src_jars + ) + # DEPSET UTILS ################################################################################################################################################# def _select_compile_jars(dep): """selects the correct compile time jar from a java provider""" @@ -141,25 +160,32 @@ return resources_jar_output # SRC JARS ##################################################################################################################################################### -def _maybe_make_srcsjar_action(ctx): - if len(ctx.files.srcs) > 0: +def _maybe_make_srcsjar_action(ctx, srcs): + source_files = srcs.kt + srcs.java + if (len(source_files) + len(srcs.src_jars)) > 0: output_srcjar = ctx.actions.declare_file(ctx.label.name + "-sources.jar") + args = ["--output", output_srcjar.path] - for i in ctx.files.srcs: - args += ["--resources", i.path] + + for sj in srcs.src_jars: + args += ["--sources", sj.path] + + for sf in source_files: + args += ["--resources", sf.path] ctx.action( mnemonic = "KotlinPackageSources", - inputs = ctx.files.srcs, + inputs = source_files + srcs.src_jars, outputs = [output_srcjar], executable = ctx.executable._singlejar, arguments = args, - progress_message="Creating Kotlin srcjar from %d srcs" % len(ctx.files.srcs), + progress_message="Creating Kotlin srcjar from %d srcs" % len(source_files), ) return [output_srcjar] else: return [] + # PACKAGE JARS ################################################################################################################################################# def _fold_jars_action(ctx, output_jar, input_jars): args=["--output", output_jar.path] @@ -213,5 +239,6 @@ collect_all_jars = _collect_all_jars, collect_jars_for_compile = _collect_jars_for_compile, restore_label = _restore_label, - derive_module_name = _derive_module_name + derive_module_name = _derive_module_name, + partition_srcs = _partition_srcs )
diff --git a/kotlin/kotlin.bzl b/kotlin/kotlin.bzl index 91da083..2ecd9a5 100644 --- a/kotlin/kotlin.bzl +++ b/kotlin/kotlin.bzl
@@ -119,52 +119,17 @@ ) load("//third_party/jvm:workspace.bzl", _maven_dependencies="maven_dependencies") -# _kt.defs.KT_COMPILER_REPO can't be used till skydoc is removed -KT_COMPILER_REPO="com_github_jetbrains_kotlin" - - ######################################################################################################################## # Rule Attributes ######################################################################################################################## _implicit_deps = { - "_kotlin_compiler_classpath": attr.label_list( - allow_files = True, - default = [ - Label("@" + KT_COMPILER_REPO + "//:compiler"), - Label("@" + KT_COMPILER_REPO + "//:reflect"), - Label("@" + KT_COMPILER_REPO + "//:script-runtime"), - ], - ), - "_kotlin_home": attr.label( - default = Label("@" + KT_COMPILER_REPO + "//:home"), - allow_files = True, - cfg = "host", - ), - "_kotlinw": attr.label( - default = Label("//kotlin/builder"), - executable = True, - cfg = "host", - ), - "_kotlin_runtime": attr.label( - single_file = True, - default = Label("@" + KT_COMPILER_REPO + "//:runtime"), - ), - "_kotlin_std": attr.label_list(default = [ - Label("@" + KT_COMPILER_REPO + "//:stdlib"), - Label("@" + KT_COMPILER_REPO + "//:stdlib-jdk7"), - Label("@" + KT_COMPILER_REPO + "//:stdlib-jdk8"), - ]), "_kotlin_toolchain": attr.label_list( default = [ Label("@io_bazel_rules_kotlin//kotlin:kt_toolchain_ide_info"), ], + cfg="host", allow_files = False, ), - "_kotlin_reflect": attr.label( - single_file = True, - default = - Label("@" + KT_COMPILER_REPO + "//:reflect"), - ), "_singlejar": attr.label( executable = True, cfg = "host", @@ -183,12 +148,10 @@ default = Label("@bazel_tools//tools/jdk:java"), allow_files = True, ), - "_jdk": attr.label( - default = Label("@bazel_tools//tools/jdk"), + "_java_stub_template": attr.label( cfg = "host", - allow_files = True, + default = Label("@kt_java_stub_template//file"), ), - "_java_stub_template": attr.label(default = Label("@kt_java_stub_template//file")), } _common_attr = dict(_implicit_deps.items() + {
diff --git a/kotlin/toolchains.bzl b/kotlin/toolchains.bzl index 9666b88..64359ab 100644 --- a/kotlin/toolchains.bzl +++ b/kotlin/toolchains.bzl
@@ -43,8 +43,21 @@ ``` """ +_KT_COMPILER_REPO="com_github_jetbrains_kotlin" + # The toolchain rules are not made private, at least the jvm ones so that they may be introspected in Intelij. _common_attrs = { + "kotlin_home": attr.label( + default = Label("@" + _KT_COMPILER_REPO + "//:home"), + allow_files = True, + cfg = "host" + ), + "kotlinbuilder": attr.label( + default = Label("//kotlin/builder"), + executable = True, + allow_files = True, + cfg = "host", + ), "language_version": attr.string( default = "1.2", values = [ @@ -70,6 +83,17 @@ } _kt_jvm_attrs = dict(_common_attrs.items() + { + "jvm_runtime": attr.label( + single_file = True, + default = Label("@" + _KT_COMPILER_REPO + "//:runtime"), + ), + "jvm_stdlibs": attr.label_list( + default = [ + Label("@" + _KT_COMPILER_REPO + "//:stdlib"), + Label("@" + _KT_COMPILER_REPO + "//:stdlib-jdk7"), + Label("@" + _KT_COMPILER_REPO + "//:stdlib-jdk8"), + ] + ), "jvm_target": attr.string( default = "1.8", values = [ @@ -84,8 +108,16 @@ label = _utils.restore_label(ctx.label), language_version = ctx.attr.language_version, api_version = ctx.attr.api_version, + coroutines = ctx.attr.coroutines, + jvm_target = ctx.attr.jvm_target, - coroutines = ctx.attr.coroutines + + + kotlinbuilder = ctx.attr.kotlinbuilder, + kotlin_home = ctx.files.kotlin_home, + + jvm_runtime = ctx.files.jvm_runtime, + jvm_stdlibs = ctx.files.jvm_stdlibs ) return struct(providers=[toolchain])