Skip to content

Commit

Permalink
[KT] Use direct deps for parsing package-info files.
Browse files Browse the repository at this point in the history
This reduces parsing time from 600ms to 300ms for the test target.

PiperOrigin-RevId: 713053259
  • Loading branch information
gkdn authored and copybara-github committed Jan 7, 2025
1 parent 403e99f commit 864b0a5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
27 changes: 11 additions & 16 deletions build_defs/internal_do_not_use/j2cl_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def _compile(
_j2cl_transpile(
ctx,
jvm_provider,
jvm_deps,
get_jdk_system(java_toolchain, javac_opts),
js_srcs,
output_js,
Expand Down Expand Up @@ -266,6 +267,7 @@ def _strip_incompatible_annotation(ctx, name, java_srcs, mnemonic, strip_annotat
def _j2cl_transpile(
ctx,
jvm_provider,
jvm_deps,
jdk_system,
js_srcs,
output_dir,
Expand All @@ -280,30 +282,23 @@ def _j2cl_transpile(
# In the Kotlin case, source_jars also include the common sources.
srcs = jvm_provider.source_jars + js_srcs

bootclasspath = _get_java_toolchain(ctx).bootclasspath
direct_deps = depset(transitive = [bootclasspath] + [d.compile_jars for d in jvm_deps])

if jvm_provider.compilation_info:
classpath = depset(
jvm_provider.compilation_info.boot_classpath,
transitive = [jvm_provider.compilation_info.compilation_classpath],
)
compilation_classpath = [jvm_provider.compilation_info.compilation_classpath]
else:
# TODO(b/214609427): JavaInfo created through Starlark does not have compilation_info set.
# We will compute the classpath manually using transitive_compile_time_jars (note that
# transitive_compile_time_jars contains current compiled code which should be excluded.)
compiled_jars = [output.compile_jar for output in jvm_provider.java_outputs if output.compile_jar]
compilation_classpath = [
jar
for jar in jvm_provider.transitive_compile_time_jars.to_list()
if jar not in compiled_jars
]
classpath = depset(
_get_java_toolchain(ctx).bootclasspath.to_list(),
transitive = [depset(compilation_classpath)],
)
# We will compute the classpath manually using transitive_compile_time_jars.
compilation_classpath = [d.transitive_compile_time_jars for d in jvm_deps]

classpath = depset(transitive = [bootclasspath] + compilation_classpath)

args = ctx.actions.args()
args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")
args.add_joined("-classpath", classpath, join_with = ctx.configuration.host_path_separator)
args.add_joined("-directdeps", direct_deps, join_with = ctx.configuration.host_path_separator)
args.add_all("-system", jdk_system, expand_directories = False)

# Explicitly format this as Bazel target labels can start with a @, which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ final class BazelJ2clBuilder extends BazelWorker {
usage = "Specifies where to find user class files and annotation processors.")
String classPath;

@Option(name = "-directdeps", metaVar = "<path>", usage = "Specifies direct dependency jars.")
String directDeps = "";

@Option(
name = "-system",
metaVar = "<path>",
Expand Down Expand Up @@ -200,6 +203,7 @@ private J2clTranspilerOptions createOptions(Output output, Problems problems) {
.build())
.setNativeSources(allNativeSources)
.setClasspaths(getPathEntries(this.classPath))
.setDirectDeps(getPathEntries(this.directDeps))
.setSystem(this.system)
.setOutput(output)
.setTargetLabel(targetLabel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public abstract static class Builder {

public abstract Builder setClasspaths(List<String> entries);

public abstract Builder setDirectDeps(List<String> entries);

public abstract Builder setSystem(String jdkSystem);

public abstract Builder setOutput(Output output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface FrontendOptions {

ImmutableList<String> getClasspaths();

@Nullable
ImmutableList<String> getDirectDeps();

String getSystem();

boolean getGenerateKytheIndexingMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class KotlinParser(private val problems: Problems) {
return PackageAnnotationsResolver.create(
packageInfoSources,
JdtParser(problems),
options.classpaths,
options.directDeps,
)
}

Expand Down

0 comments on commit 864b0a5

Please sign in to comment.