Skip to content

Commit

Permalink
pass language version as a parameter
Browse files Browse the repository at this point in the history
Summary:
We have many places parsing -language-version parameter. The diff makes language version as a parameter to Kotlincd. This allows us to better control the parameter by:
1. Making it possible to set only one language version
2. Removing multiple reparsing from the string and serialisizations back to the string

Reviewed By: hick209

Differential Revision: D69399086

fbshipit-source-id: c40baeb6d327a951ff35e8ecdc9489d8631cccc5
  • Loading branch information
RuslanLa authored and facebook-github-bot committed Feb 14, 2025
1 parent 540821c commit 534ef50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
25 changes: 16 additions & 9 deletions prelude/kotlin/kotlin_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _create_kotlin_sources(
module_name,
"-no-stdlib",
"-no-reflect",
] + ctx.attrs.extra_kotlinc_arguments + get_language_version(ctx),
] + ctx.attrs.extra_kotlinc_arguments + get_language_version_arg(ctx),
)

jvm_target = get_kotlinc_compatible_target(ctx.attrs.target) if ctx.attrs.target else None
Expand Down Expand Up @@ -271,9 +271,8 @@ def _add_plugins(

return _PluginCmdArgs(kotlinc_cmd_args = kotlinc_cmd_args, compile_kotlin_cmd = compile_kotlin_cmd)

def get_language_version(ctx: AnalysisContext) -> list:
def get_language_version(ctx: AnalysisContext) -> str:
kotlin_toolchain = ctx.attrs._kotlin_toolchain[KotlinToolchainInfo]
language_version_kotlinc_arguments = []

# kotlin compiler expects relase version of format 1.6, 1.7, etc. Don't include patch version
current_kotlin_release_version = ".".join(kotlin_toolchain.kotlin_version.split(".")[:2])
Expand All @@ -287,17 +286,24 @@ def get_language_version(ctx: AnalysisContext) -> list:
if ctx.attrs.k2 == True and kotlin_toolchain.allow_k2_usage:
if not current_language_version or current_language_version < "2.0":
if current_kotlin_release_version < "2.0":
language_version_kotlinc_arguments.append("-language-version=2.0")
current_language_version = "2.0"
else:
language_version_kotlinc_arguments.append("-language-version=" + current_kotlin_release_version)
current_language_version = current_kotlin_release_version
else: # use K1
if not current_language_version or current_language_version >= "2.0":
if current_kotlin_release_version >= "2.0":
language_version_kotlinc_arguments.append("-language-version=1.9")
current_language_version = "1.9"
else:
language_version_kotlinc_arguments.append("-language-version=" + current_kotlin_release_version)
current_language_version = current_kotlin_release_version

return language_version_kotlinc_arguments
return current_language_version

def get_language_version_arg(ctx: AnalysisContext) -> list[str]:
language_version = get_language_version(ctx)
return ["-language-version=" + language_version]

def filter_out_language_version(extra_arguments: list) -> list:
return [arg for arg in extra_arguments if not (isinstance(arg, str) and "-language-version" in arg)]

def kotlin_library_impl(ctx: AnalysisContext) -> list[Provider]:
packaging_deps = ctx.attrs.deps + ctx.attrs.exported_deps + ctx.attrs.runtime_deps
Expand Down Expand Up @@ -442,7 +448,7 @@ def build_kotlin_library(
"debug_port": getattr(ctx.attrs, "debug_port", None),
"deps": deps + [kotlin_toolchain.kotlin_stdlib],
"enable_used_classes": ctx.attrs.enable_used_classes,
"extra_kotlinc_arguments": (ctx.attrs.extra_kotlinc_arguments or []) + get_language_version(ctx),
"extra_kotlinc_arguments": filter_out_language_version(ctx.attrs.extra_kotlinc_arguments or []),
"friend_paths": ctx.attrs.friend_paths,
"is_building_android_binary": ctx.attrs._is_building_android_binary,
"jar_postprocessor": ctx.attrs.jar_postprocessor[RunInfo] if hasattr(ctx.attrs, "jar_postprocessor") and ctx.attrs.jar_postprocessor else None,
Expand All @@ -451,6 +457,7 @@ def build_kotlin_library(
"kotlin_compiler_plugins": ctx.attrs.kotlin_compiler_plugins,
"kotlin_toolchain": kotlin_toolchain,
"label": ctx.label,
"language_version": get_language_version(ctx),
"manifest_file": ctx.attrs.manifest_file,
"remove_classes": ctx.attrs.remove_classes,
"required_for_source_only_abi": ctx.attrs.required_for_source_only_abi,
Expand Down
7 changes: 6 additions & 1 deletion prelude/kotlin/kotlincd_jar_creator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create_jar_artifact_kotlincd(
extra_kotlinc_arguments: list,
incremental: bool,
enable_used_classes: bool,
language_version: str,
is_creating_subtarget: bool = False,
optional_dirs: list[OutputArtifact] = [],
jar_postprocessor: [RunInfo, None] = None,
Expand Down Expand Up @@ -158,6 +159,7 @@ def create_jar_artifact_kotlincd(
actual_abi_generation_mode = actual_abi_generation_mode,
should_kotlinc_run_incrementally = should_kotlinc_run_incrementally,
incremental_state_dir = incremental_state_dir,
language_version = language_version,
)

library_command_builder = command_builder(
Expand Down Expand Up @@ -210,6 +212,7 @@ def create_jar_artifact_kotlincd(
actual_abi_generation_mode = actual_abi_generation_mode,
should_kotlinc_run_incrementally = False,
incremental_state_dir = None,
language_version = language_version,
)
abi_command_builder = command_builder(
kotlin_extra_params = kotlin_extra_params,
Expand Down Expand Up @@ -269,7 +272,8 @@ def _encode_kotlin_extra_params(
should_use_jvm_abi_gen: bool,
actual_abi_generation_mode: AbiGenerationMode,
should_kotlinc_run_incrementally: bool,
incremental_state_dir: Artifact | None):
incremental_state_dir: Artifact | None,
language_version: str):
kosabiPluginOptionsMap = {}
if kotlin_toolchain.kosabi_stubs_gen_plugin != None:
kosabiPluginOptionsMap["kosabi_stubs_gen_plugin"] = kotlin_toolchain.kosabi_stubs_gen_plugin
Expand Down Expand Up @@ -303,6 +307,7 @@ def _encode_kotlin_extra_params(
shouldKotlincRunIncrementally = should_kotlinc_run_incrementally,
incrementalStateDir = incremental_state_dir.as_output() if incremental_state_dir else None,
shouldUseStandaloneKosabi = kotlin_toolchain.kosabi_standalone,
languageVersion = language_version,
)

def _command_builder(
Expand Down

0 comments on commit 534ef50

Please sign in to comment.