aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornickreid <nickreid@google.com>2022-11-04 10:40:18 -0700
committerCopybara-Service <copybara-worker@google.com>2022-11-04 10:40:49 -0700
commitcee077b3778bcc13413f7462556d8eeeb23115b7 (patch)
tree968c6b1a3048167e78feeaa48af7eb849424fd81
parentce0ab38b978c9d3a5f95149464077cc4bde7736a (diff)
downloadbazelbuild-kotlin-rules-cee077b3778bcc13413f7462556d8eeeb23115b7.tar.gz
Create and apply FileFactory, a class for declaring files relative to some base file.
This prevents filename conflicts if kt_jvm_compile is used multiple times in the same rule + aspect. PiperOrigin-RevId: 486176842 Change-Id: Ifd515009761f8b77dfd9ec84da8cc39ebb26f1e2
-rw-r--r--kotlin/common.bzl63
-rw-r--r--kotlin/file_factory.bzl48
-rw-r--r--tests/analysis/jvm_library/friends/BUILD8
-rw-r--r--tests/analysis/jvm_library/friends/sub/BUILD4
-rw-r--r--tests/analysis/jvm_library/friends/testing/BUILD8
5 files changed, 91 insertions, 40 deletions
diff --git a/kotlin/common.bzl b/kotlin/common.bzl
index 69ef66b..1b59472 100644
--- a/kotlin/common.bzl
+++ b/kotlin/common.bzl
@@ -18,6 +18,7 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
load("@bazel_skylib//lib:structs.bzl", "structs")
load("//bazel:stubs.bzl", "BASE_JVMOPTS")
load("//bazel:stubs.bzl", "DEFAULT_BUILTIN_PROCESSORS")
+load(":file_factory.bzl", "FileFactory")
# TODO: Remove the _ALLOWED_*_RULES lists to determine which rules
# are accepted dependencies to Kotlin rules as the approach does not scale
@@ -106,6 +107,7 @@ def _common_kapt_and_kotlinc_args(ctx, toolchain):
# Runs KAPT in two separate actions so annotation processors only rerun when Kotlin stubs changed.
def _kapt(
ctx,
+ file_factory,
kt_srcs = [],
common_srcs = [],
java_srcs = [],
@@ -124,7 +126,7 @@ def _kapt(
# just run turbine if there are no .kt sources.
stub_srcjars = []
if kt_srcs or common_srcs:
- stubs_dir = ctx.actions.declare_directory(ctx.label.name + "/kapt/gen/stubs")
+ stubs_dir = file_factory.declare_directory("/kapt/gen/stubs")
_kapt_stubs(
ctx,
stubs_dir,
@@ -143,14 +145,14 @@ def _kapt(
stub_srcjars.append(_create_jar(
ctx,
toolchain,
- ctx.actions.declare_file("stubs-srcjar.jar", sibling = stubs_dir),
+ file_factory.declare_file("stubs-srcjar.jar"),
kt_inputs = [stubs_dir],
ignore_not_allowed_files = True,
))
- output_jar = ctx.actions.declare_file(ctx.label.name + "-kapt.jar")
- output_srcjar = ctx.actions.declare_file(ctx.label.name + "-kapt.srcjar")
- output_manifest = ctx.actions.declare_file(ctx.label.name + "-kapt.jar_manifest_proto")
+ output_jar = file_factory.declare_file("-kapt.jar")
+ output_srcjar = file_factory.declare_file("-kapt.srcjar")
+ output_manifest = file_factory.declare_file("-kapt.jar_manifest_proto")
_run_turbine(
ctx,
toolchain,
@@ -335,7 +337,7 @@ def _derive_gen_class_jar(
# Run GenClass tool to derive gen_class_jar by filtering hand-written sources.
# cf. Bazel's JavaCompilationHelper#createGenJarAction
- result = ctx.actions.declare_file(ctx.label.name + "-gen.jar")
+ result = FileFactory(ctx, javac_jar).declare_file("-gen.jar")
genclass_args = ctx.actions.args()
genclass_args.add("--manifest_proto", manifest_proto)
@@ -371,7 +373,7 @@ def _kt_plugins_map(
def _run_kotlinc(
ctx,
- output_basename,
+ file_factory,
kt_srcs = [],
common_srcs = [],
coverage_srcs = [],
@@ -404,7 +406,7 @@ def _run_kotlinc(
kotlinc_args.add(toolchain.jvm_abi_gen_plugin, format = "-Xplugin=%s")
direct_inputs.append(toolchain.jvm_abi_gen_plugin)
- kt_ijar = ctx.actions.declare_file(output_basename + "-ijar.jar")
+ kt_ijar = file_factory.declare_file("-ijar.jar")
kotlinc_args.add("-P", kt_ijar, format = "plugin:org.jetbrains.kotlin.jvm.abi:outputDir=%s")
outputs.append(kt_ijar)
@@ -420,7 +422,7 @@ def _run_kotlinc(
kotlinc_args.add_all(common_srcs, format_each = "-Xcommon-sources=%s")
direct_inputs.extend(common_srcs)
- output = ctx.actions.declare_file(output_basename + ".jar")
+ output = file_factory.declare_file(".jar")
kotlinc_args.add("-d", output)
outputs.append(output)
kotlinc_args.add_all(kt_srcs)
@@ -456,7 +458,7 @@ def _run_kotlinc(
srcjar = _create_jar(
ctx,
toolchain,
- ctx.actions.declare_file(output_basename + "-kt-src.jar"),
+ file_factory.declare_file("-kt-src.jar"),
kt_inputs = kt_srcs,
common_inputs = common_srcs,
)
@@ -529,15 +531,15 @@ def _run_import_deps_checker(
def _offline_instrument_jar(ctx, toolchain, jar, srcs = []):
if not jar.basename.endswith(".jar"):
fail("Expect JAR input but got %s" % jar)
- output_basename = jar.basename[:-4]
+ file_factory = FileFactory(ctx, jar)
- paths_for_coverage_file = ctx.actions.declare_file(output_basename + "-kt-paths-for-coverage.txt", sibling = jar)
+ paths_for_coverage_file = file_factory.declare_file("-kt-paths-for-coverage.txt")
paths = ctx.actions.args()
paths.set_param_file_format("multiline") # don't shell-quote, just list file names
paths.add_all([src for src in srcs if src.is_source])
ctx.actions.write(paths_for_coverage_file, paths)
- output = ctx.actions.declare_file(output_basename + "-instrumented.jar", sibling = jar)
+ output = file_factory.declare_file("-instrumented.jar")
args = ctx.actions.args()
args.add(jar)
args.add(output)
@@ -586,8 +588,8 @@ def _singlejar(
progress_message = "Merging %s: %s" % (content, label),
)
-def _merge_jdeps(ctx, kt_jvm_toolchain, jdeps_files, output_suffix = ""):
- merged_jdeps_file = ctx.actions.declare_file(ctx.label.name + output_suffix + ".jdeps")
+def _merge_jdeps(ctx, kt_jvm_toolchain, jdeps_files, file_factory):
+ merged_jdeps_file = file_factory.declare_file("-merged.jdeps")
args = ctx.actions.args()
args.add("--kind=jdeps")
@@ -711,7 +713,7 @@ def _create_jar_from_tree_artifacts(ctx, jar_tool, output_jar, input_dirs):
)
return output_jar
-def _DirSrcjarSyncer(ctx, kt_toolchain, name):
+def _DirSrcjarSyncer(ctx, kt_toolchain, file_factory):
_dirs = []
_srcjars = []
@@ -724,9 +726,7 @@ def _DirSrcjarSyncer(ctx, kt_toolchain, name):
_create_jar_from_tree_artifacts(
ctx,
kt_toolchain.jar_tool,
- ctx.actions.declare_file(
- "%s/%s%s.srcjar" % (ctx.label.name, name, len(_srcjars)),
- ),
+ file_factory.declare_file("%s.srcjar" % len(_srcjars)),
dirs,
),
)
@@ -740,9 +740,7 @@ def _DirSrcjarSyncer(ctx, kt_toolchain, name):
_expand_zip(
ctx,
kt_toolchain,
- ctx.actions.declare_directory(
- "%s/%s%s.expand" % (ctx.label.name, name, len(_dirs)),
- ),
+ file_factory.declare_directory("%s.expand" % len(_dirs)),
srcjar,
),
)
@@ -836,12 +834,13 @@ def _kt_jvm_library(
if not kt_toolchain:
fail("Missing or invalid kt_toolchain")
+ file_factory = FileFactory(ctx, output)
deps = list(deps) # Defensive copy
# Split sources, as java requires a separate compile step.
kt_srcs = [s for s in srcs if _is_kt_src(s)]
java_srcs = [s for s in srcs if s.path.endswith(_EXT.JAVA)]
- java_syncer = _DirSrcjarSyncer(ctx, kt_toolchain, "java")
+ java_syncer = _DirSrcjarSyncer(ctx, kt_toolchain, file_factory)
java_syncer.add_dirs([s for s in srcs if _is_dir(s, "java")])
java_syncer.add_srcjars([s for s in srcs if s.path.endswith(_EXT.SRCJAR)])
@@ -901,6 +900,7 @@ def _kt_jvm_library(
if kt_srcs and plugin_processors:
kapt_outputs = _kapt(
ctx,
+ file_factory = file_factory,
kt_srcs = kt_srcs,
common_srcs = common_srcs,
java_srcs = java_srcs,
@@ -933,7 +933,7 @@ def _kt_jvm_library(
common_srcs = common_srcs,
coverage_srcs = coverage_srcs,
java_srcs_and_dirs = java_srcs + java_syncer.dirs,
- output_basename = ctx.label.name + "-kt",
+ file_factory = file_factory.derive("-kt"),
kotlincopts = kotlincopts,
compile_jdeps = compile_jdeps,
toolchain = kt_toolchain,
@@ -966,7 +966,7 @@ def _kt_jvm_library(
_create_jar_from_tree_artifacts(
ctx,
kt_toolchain.jar_tool,
- ctx.actions.declare_file(ctx.label.name + "-dir-res.jar"),
+ file_factory.declare_file("-dir-res.jar"),
classpath_resources_dirs,
),
)
@@ -977,7 +977,7 @@ def _kt_jvm_library(
java_genjar = None
is_android_library_without_kt_srcs = rule_family == _RULE_FAMILY.ANDROID_LIBRARY and not kt_srcs
if java_srcs or java_syncer.srcjars or classpath_resources:
- javac_out = output if is_android_library_without_kt_srcs else ctx.actions.declare_file(ctx.label.name + "-java.jar")
+ javac_out = output if is_android_library_without_kt_srcs else file_factory.declare_file("-java.jar")
javac_java_info = java_common.compile(
ctx,
source_files = java_srcs,
@@ -1033,8 +1033,8 @@ def _kt_jvm_library(
blocking_action_outs = []
if output_srcjar == None:
- output_srcjar = ctx.actions.declare_file("lib%s-src.jar" % ctx.label.name)
- compile_jar = ctx.actions.declare_file(ctx.label.name + "-compile.jar")
+ output_srcjar = file_factory.declare_file("-src.jar")
+ compile_jar = file_factory.declare_file("-compile.jar")
single_jar = java_toolchain.single_jar
_singlejar(ctx, out_srcjars, output_srcjar, single_jar, mnemonic = "KtMergeSrcjar", content = "srcjar", preserve_compression = True)
@@ -1094,12 +1094,15 @@ def _kt_jvm_import(
if not jars:
fail("Must import at least one JAR")
- deps += kt_toolchain.kotlin_libs
+ file_factory = FileFactory(ctx, jars[0])
+ deps += list(deps) # Defensive copy
+
+ deps.extend(kt_toolchain.kotlin_libs)
merged_deps = java_common.merge(deps)
# Check that any needed deps are declared unless neverlink, in which case Jars won't be used
# at runtime so we skip the check, though we'll populate jdeps either way.
- jdeps_output = ctx.actions.declare_file(ctx.label.name + ".jdeps")
+ jdeps_output = file_factory.declare_file(".jdeps")
_run_import_deps_checker(
ctx,
jars_to_check = jars,
diff --git a/kotlin/file_factory.bzl b/kotlin/file_factory.bzl
new file mode 100644
index 0000000..25c2837
--- /dev/null
+++ b/kotlin/file_factory.bzl
@@ -0,0 +1,48 @@
+# Copyright 2022 Google LLC. 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.
+
+"""FileFactory"""
+
+def FileFactory(ctx, base, suffix = None):
+ """Creates files with names derived from some base file
+
+ Including the name of a rule is not always enough to guarantee unique filenames. For example,
+ helper functions that declare their own output files may be called multiple times in the same
+ rule impl.
+
+ Args:
+ ctx: ctx
+ base: [File] The file to derive other filenames from
+ suffix: [Optional[string]] An additional suffix to differentiate declared files
+
+ Returns:
+ FileFactory
+ """
+
+ base_name = base.basename.rsplit(".", 1)[0] + (suffix or "")
+
+ def declare_directory(suffix):
+ return ctx.actions.declare_directory(base_name + suffix, sibling = base)
+
+ def declare_file(suffix):
+ return ctx.actions.declare_file(base_name + suffix, sibling = base)
+
+ def derive(suffix):
+ return FileFactory(ctx, base, suffix)
+
+ return struct(
+ declare_directory = declare_directory,
+ declare_file = declare_file,
+ derive = derive,
+ )
diff --git a/tests/analysis/jvm_library/friends/BUILD b/tests/analysis/jvm_library/friends/BUILD
index b0460f9..707b589 100644
--- a/tests/analysis/jvm_library/friends/BUILD
+++ b/tests/analysis/jvm_library/friends/BUILD
@@ -40,7 +40,7 @@ jvm_library_test(
name = "has_java_exported_friend_in_package_test",
expected_friend_jar_names = [
"libjava_exports_friend-hjar.jar",
- "friend-compile.jar",
+ "libfriend-compile.jar",
],
target_under_test = kt_jvm_library_under_test(
name = "has_java_exported_friend_in_package",
@@ -52,8 +52,8 @@ jvm_library_test(
jvm_library_test(
name = "has_kt_exported_friend_in_package_test",
expected_friend_jar_names = [
- "kt_exports_friend-compile.jar",
- "friend-compile.jar",
+ "libkt_exports_friend-compile.jar",
+ "libfriend-compile.jar",
],
target_under_test = kt_jvm_library_under_test(
name = "has_kt_exported_friend_in_package",
@@ -84,7 +84,7 @@ jvm_library_test(
jvm_library_test(
name = "has_direct_friend_in_package_test",
- expected_friend_jar_names = ["friend-compile.jar"],
+ expected_friend_jar_names = ["libfriend-compile.jar"],
target_under_test = kt_jvm_library_under_test(
name = "has_direct_friend_in_package",
srcs = ["Input.kt"],
diff --git a/tests/analysis/jvm_library/friends/sub/BUILD b/tests/analysis/jvm_library/friends/sub/BUILD
index 292eeae..3a77ed3 100644
--- a/tests/analysis/jvm_library/friends/sub/BUILD
+++ b/tests/analysis/jvm_library/friends/sub/BUILD
@@ -26,8 +26,8 @@ licenses(["notice"])
jvm_library_test(
name = "no_kt_exported_friend_cross_package_test",
expected_friend_jar_names = [
- "kt_exports_friend-compile.jar",
- # Absent # "friend-compile.jar"
+ "libkt_exports_friend-compile.jar",
+ # Absent # "libfriend-compile.jar"
],
target_under_test = kt_jvm_library_under_test(
name = "no_kt_exported_friend_cross_package",
diff --git a/tests/analysis/jvm_library/friends/testing/BUILD b/tests/analysis/jvm_library/friends/testing/BUILD
index dda8cac..8f08a3d 100644
--- a/tests/analysis/jvm_library/friends/testing/BUILD
+++ b/tests/analysis/jvm_library/friends/testing/BUILD
@@ -26,8 +26,8 @@ licenses(["notice"])
jvm_library_test(
name = "has_kt_exported_friend_impl_package_test",
expected_friend_jar_names = [
- "kt_exports_friend-compile.jar",
- "friend-compile.jar",
+ "libkt_exports_friend-compile.jar",
+ "libfriend-compile.jar",
],
target_under_test = kt_jvm_library_under_test(
name = "has_kt_exported_friend_impl_package",
@@ -39,7 +39,7 @@ jvm_library_test(
jvm_library_test(
name = "has_direct_friend_impl_package_test",
expected_friend_jar_names = [
- "friend-compile.jar",
+ "libfriend-compile.jar",
],
target_under_test = kt_jvm_library_under_test(
name = "has_direct_friend_impl_package",
@@ -51,7 +51,7 @@ jvm_library_test(
jvm_library_test(
name = "no_kt_exported_friend_sibling_package_test",
expected_friend_jar_names = [
- "kt_exports_subfriend-compile.jar",
+ "libkt_exports_subfriend-compile.jar",
# Absent # "subfriend-compile.jar"
],
target_under_test = kt_jvm_library_under_test(