Skip to content

Commit

Permalink
Provide jar_snapshot for classpath in kotlincd
Browse files Browse the repository at this point in the history
Summary:
## Context
To enable Kotlin Incremental Compilation for a target we need to provide a snapshot of all the `.jar` files in its classpath.
See more context in this [doc](https://docs.google.com/document/d/1EpyHaCcOHxGgFWw59vK2HZgcJERReOp6_j-8bCWkIN0/edit?usp=sharing)

## Steps
1 . Create a utility to generate snapshot  for given `.jar`
2 . Provide jar_snapshot in JavaClasspathEntry
➜ 3 . Pass jar_snapshot for all `.jar` files in the classpath to KotlinCD
4 . In KotlinCD, deserialize and maintain a map of classpath to snapshot
5 . Provide classpath->snapshot map to incremental compilation

## This Diff
When building a target incrementally, pass jar_snapshot for all `.jar` files in the classpath to KotlinCD

Reviewed By: IanChilds

Differential Revision: D62533759

fbshipit-source-id: bb17d15db9e5df069c53d79ab7c5bee20df49771
  • Loading branch information
navidqar authored and facebook-github-bot committed Oct 4, 2024
1 parent 20b0164 commit ef94481
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions jvm/cd_jar_creator_util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,20 @@ def encode_base_jar_command(
manifest_file: Artifact | None,
extra_arguments: cmd_args,
source_only_abi_compiling_deps: list[JavaClasspathEntry],
track_class_usage: bool) -> struct:
track_class_usage: bool,
is_incremental: bool = False) -> struct:
library_jar_params = encode_jar_params(remove_classes, output_paths, manifest_file)
qualified_name = get_qualified_name(label, target_type)
if target_type == TargetType("source_only_abi"):
compiling_classpath = classpath_jars_tag.tag_artifacts([dep.abi for dep in source_only_abi_compiling_deps])
compiling_classpath_snapshot = {}
else:
expect(len(source_only_abi_compiling_deps) == 0)
compiling_deps_list = filter(None, list(compiling_deps_tset.traverse())) if compiling_deps_tset else []
compiling_classpath = classpath_jars_tag.tag_artifacts(
compiling_deps_tset.project_as_json("javacd_json") if compiling_deps_tset else None,
[dep.abi for dep in compiling_deps_list],
)
compiling_classpath_snapshot = {dep.abi: dep.abi_jar_snapshot for dep in compiling_deps_list if dep.abi_jar_snapshot} if is_incremental else {}

build_target_value = struct(
fullyQualifiedName = qualified_name,
Expand Down Expand Up @@ -328,6 +332,7 @@ def encode_base_jar_command(
return struct(
outputPathsValue = encode_output_paths(label, output_paths, target_type),
compileTimeClasspathPaths = compiling_classpath,
compileTimeClasspathSnapshotPaths = compiling_classpath_snapshot,
javaSrcs = srcs,
# TODO(cjhopman): populate jar infos. I think these are only used for unused dependencies (and appear to be broken in buck1 w/javacd anyway).
fullJarInfos = [],
Expand Down
10 changes: 5 additions & 5 deletions kotlin/kotlincd_jar_creator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def create_jar_artifact_kotlincd(
jvm_abi_gen = None
should_use_jvm_abi_gen = False

should_kotlinc_run_incrementally = kotlin_toolchain.enable_incremental_compilation and incremental
incremental_state_dir = declare_prefixed_output(actions, actions_identifier, "incremental_state", dir = True) if should_kotlinc_run_incrementally else None

def encode_kotlin_extra_params(kotlin_compiler_plugins, incremental_state_dir = None):
kosabiPluginOptionsMap = {}
if kotlin_toolchain.kosabi_stubs_gen_plugin != None:
Expand Down Expand Up @@ -147,7 +150,7 @@ def create_jar_artifact_kotlincd(
shouldRemoveKotlinCompilerFromClassPath = True,
depTrackerPlugin = kotlin_toolchain.track_class_usage_plugin,
shouldKotlincRunViaBuildToolsApi = kotlin_toolchain.kotlinc_run_via_build_tools_api,
shouldKotlincRunIncrementally = incremental_state_dir != None,
shouldKotlincRunIncrementally = should_kotlinc_run_incrementally,
incrementalStateDir = incremental_state_dir.as_output() if incremental_state_dir else None,
shouldUseStandaloneKosabi = kotlin_toolchain.kosabi_standalone,
)
Expand Down Expand Up @@ -183,6 +186,7 @@ def create_jar_artifact_kotlincd(
extra_arguments = cmd_args(extra_arguments),
source_only_abi_compiling_deps = [],
track_class_usage = track_class_usage,
is_incremental = should_kotlinc_run_incrementally,
)

return struct(
Expand Down Expand Up @@ -359,10 +363,6 @@ def create_jar_artifact_kotlincd(
)

library_classpath_jars_tag = actions.artifact_tag()
incremental_state_dir = None
shouldKotlincRunIncrementally = kotlin_toolchain.enable_incremental_compilation and incremental
if shouldKotlincRunIncrementally:
incremental_state_dir = declare_prefixed_output(actions, actions_identifier, "incremental_state", dir = True)
command = encode_library_command(output_paths, path_to_class_hashes_out, library_classpath_jars_tag, incremental_state_dir)
define_kotlincd_action(
category_prefix = "",
Expand Down

0 comments on commit ef94481

Please sign in to comment.