From 534ef50a6727e4a6c984934fba7d2ab07311dbba Mon Sep 17 00:00:00 2001 From: Ruslan Latypov Date: Fri, 14 Feb 2025 08:22:13 -0800 Subject: [PATCH] pass language version as a parameter 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 --- prelude/kotlin/kotlin_library.bzl | 25 ++++++++++++++++--------- prelude/kotlin/kotlincd_jar_creator.bzl | 7 ++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/prelude/kotlin/kotlin_library.bzl b/prelude/kotlin/kotlin_library.bzl index ae37dd1bf67a0..83a2f72a766da 100644 --- a/prelude/kotlin/kotlin_library.bzl +++ b/prelude/kotlin/kotlin_library.bzl @@ -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 @@ -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]) @@ -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 @@ -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, @@ -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, diff --git a/prelude/kotlin/kotlincd_jar_creator.bzl b/prelude/kotlin/kotlincd_jar_creator.bzl index 9b6a545e8df38..6603893100773 100644 --- a/prelude/kotlin/kotlincd_jar_creator.bzl +++ b/prelude/kotlin/kotlincd_jar_creator.bzl @@ -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, @@ -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( @@ -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, @@ -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 @@ -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(