Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
[feature] Enhance Kotlinc Opts support | #BAZEL-461
Browse files Browse the repository at this point in the history
Merge-request: BAZEL-MR-505
Merged-by: Xuan Son Trinh <xuanson.trinh@jetbrains.com>
  • Loading branch information
xuansontrinh authored and qodana-bot committed Sep 8, 2023
1 parent 0e429a0 commit 6b45eb6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Features 🎉

- The server generates `extensions.bzl` based on languages (external rules) relevant to the project.
- Enhance Kotlinc Opts support.

### Fixes 🛠️

Expand Down
17 changes: 11 additions & 6 deletions aspects/rules/kt/kt_info.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@io_bazel_rules_kotlin//kotlin/internal:defs.bzl", "KtJvmInfo")
load("@io_bazel_rules_kotlin//kotlin/internal:opts.bzl", "KotlincOptions")
load("//aspects:utils/utils.bzl", "create_proto", "create_struct")
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_proto", "create_struct")

KOTLIN_TOOLCHAIN_TYPE = "@io_bazel_rules_kotlin//kotlin/internal:kt_toolchain_type"

def extract_kotlin_info(target, ctx, **kwargs):
if KtJvmInfo not in target:
Expand All @@ -14,19 +16,22 @@ def extract_kotlin_info(target, ctx, **kwargs):
associates = getattr(ctx.rule.attr, "associates", [])
associates_labels = [str(associate.label) for associate in associates]

kotlin_toolchain = ctx.toolchains[KOTLIN_TOOLCHAIN_TYPE]
toolchain_kotlinc_opts = kotlin_toolchain.kotlinc_options
kotlinc_opts_target = getattr(ctx.rule.attr, "kotlinc_opts", None)
kotlinc_opts = None
if kotlinc_opts_target != None and KotlincOptions in kotlinc_opts_target:
kotlinc_opts = kotlinc_opts_target[KotlincOptions]
kotlinc_opts = kotlinc_opts_target[KotlincOptions] if kotlinc_opts_target and KotlincOptions in kotlinc_opts_target else toolchain_kotlinc_opts

kotlin_info = dict(
language_version = language_version,
api_version = api_version,
associates = associates_labels,
)

if kotlinc_opts != None:
kotlin_info["kotlinc_opts"] = kotlinc_opts
kotlinc_opts_dict = convert_struct_to_dict(kotlinc_opts)
if not kotlinc_opts_dict.get("jvm_target"):
kotlinc_opts_dict["jvm_target"] = getattr(kotlin_toolchain, "jvm_target", "")

kotlin_info["kotlinc_opts"] = create_struct(**kotlinc_opts_dict)

kotlin_target_info = create_struct(**kotlin_info)
info_file = create_proto(target, ctx, kotlin_target_info, "kotlin_target_info")
Expand Down
11 changes: 11 additions & 0 deletions aspects/utils/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ def create_proto(target, ctx, data, name):

def is_external(target):
return not str(target.label).startswith("@//") and not str(target.label).startswith("//")

def convert_struct_to_dict(s):
attrs = dir(s)

# two deprecated methods of struct
if "to_json" in attrs:
attrs.remove("to_json")
if "to_proto" in attrs:
attrs.remove("to_proto")

return {key: getattr(s, key) for key in attrs}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class BazelBspEnvironmentManager(
when (it) {
Language.Scala -> """"@io_bazel_rules_scala//scala:toolchain_type""""
Language.Java -> """"@bazel_tools//tools/jdk:runtime_toolchain_type""""
Language.Kotlin -> """"@io_bazel_rules_kotlin//kotlin/internal:kt_toolchain_type""""
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ class KotlinLanguagePlugin(
val kotlincOpts = kotlinTargetInfo.kotlincOpts
with(kotlincOpts) {
kotlincOptsRes = KotlincOpts(
includeStdlibs = includeStdlibs,
javaParameters = javaParameters,
jvmTarget = jvmTarget,
warn = warn,
xAllowResultReturnType = xAllowResultReturnType,
xBackendThreads = xBackendThreads,
xEmitJvmTypeAnnotations = xEmitJvmTypeAnnotations,
xEnableIncrementalCompilation = xEnableIncrementalCompilation,
xExplicitApiMode = xExplicitApiMode,
xInlineClasses = xInlineClasses,
xJvmDefault = xJvmDefault,
xLambdas = xLambdas,
xMultiPlatform = xMultiPlatform,
xNoCallAssertions = xNoCallAssertions,
xNoOptimize = xNoOptimize,
xNoOptimizedCallableReferences = xNoOptimizedCallableReferences,
xNoParamAssertions = xNoParamAssertions,
xNoReceiverAssertions = xNoReceiverAssertions,
xOptinList = xOptinList,
xReportPerf = xReportPerf,
xSamConversions = xSamConversions,
xSkipPrereleaseCheck = xSkipPrereleaseCheck,
xUseFirLt = xUseFirLt,
xUseK2 = xUseK2
includeStdlibs = if (hasIncludeStdlibs()) includeStdlibs else null,
javaParameters = if (hasJavaParameters()) javaParameters else null,
jvmTarget = if (hasJvmTarget()) jvmTarget else null,
warn = if (hasWarn()) warn else null,
xAllowResultReturnType = if (hasXAllowResultReturnType()) xAllowResultReturnType else null,
xBackendThreads = if (hasXBackendThreads()) xBackendThreads else null,
xEmitJvmTypeAnnotations = if (hasXEmitJvmTypeAnnotations()) xEmitJvmTypeAnnotations else null,
xEnableIncrementalCompilation = if (hasXEnableIncrementalCompilation()) xEnableIncrementalCompilation else null,
xExplicitApiMode = if (hasXExplicitApiMode()) xExplicitApiMode else null,
xInlineClasses = if (hasXInlineClasses()) xInlineClasses else null,
xJvmDefault = if (hasXJvmDefault()) xJvmDefault else null,
xLambdas = if (hasXLambdas()) xLambdas else null,
xMultiPlatform = if (hasXMultiPlatform()) xMultiPlatform else null,
xNoCallAssertions = if (hasXNoCallAssertions()) xNoCallAssertions else null,
xNoOptimize = if (hasXNoOptimize()) xNoOptimize else null,
xNoOptimizedCallableReferences = if (hasXNoOptimizedCallableReferences()) xNoOptimizedCallableReferences else null,
xNoParamAssertions = if (hasXNoParamAssertions()) xNoParamAssertions else null,
xNoReceiverAssertions = if (hasXNoReceiverAssertions()) xNoReceiverAssertions else null,
xOptinList = xOptinList.ifEmpty { null },
xReportPerf = if (hasXReportPerf()) xReportPerf else null,
xSamConversions = if (hasXSamConversions()) xSamConversions else null,
xSkipPrereleaseCheck = if (hasXSkipPrereleaseCheck()) xSkipPrereleaseCheck else null,
xUseFirLt = if (hasXUseFirLt()) xUseFirLt else null,
xUseK2 = if (hasXUseK2()) xUseK2 else null
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ data class KotlinModule(
) : LanguageData

data class KotlincOpts(
val includeStdlibs: String,
val javaParameters: Boolean,
val jvmTarget: String,
val warn : String,
val xAllowResultReturnType : Boolean,
val xBackendThreads: Int,
val xEmitJvmTypeAnnotations: Boolean,
val xEnableIncrementalCompilation: Boolean,
val xExplicitApiMode: String,
val xInlineClasses: Boolean,
val xJvmDefault: String,
val xLambdas: String,
val xMultiPlatform: Boolean,
val xNoCallAssertions: Boolean,
val xNoOptimize: Boolean,
val xNoOptimizedCallableReferences: Boolean,
val xNoParamAssertions: Boolean,
val xNoReceiverAssertions: Boolean,
val xOptinList: List<String>,
val xReportPerf: Boolean,
val xSamConversions: String,
val xSkipPrereleaseCheck: Boolean,
val xUseFirLt: Boolean,
val xUseK2: Boolean,
val includeStdlibs: String?,
val javaParameters: Boolean?,
val jvmTarget: String?,
val warn : String?,
val xAllowResultReturnType : Boolean?,
val xBackendThreads: Int?,
val xEmitJvmTypeAnnotations: Boolean?,
val xEnableIncrementalCompilation: Boolean?,
val xExplicitApiMode: String?,
val xInlineClasses: Boolean?,
val xJvmDefault: String?,
val xLambdas: String?,
val xMultiPlatform: Boolean?,
val xNoCallAssertions: Boolean?,
val xNoOptimize: Boolean?,
val xNoOptimizedCallableReferences: Boolean?,
val xNoParamAssertions: Boolean?,
val xNoReceiverAssertions: Boolean?,
val xOptinList: List<String>?,
val xReportPerf: Boolean?,
val xSamConversions: String?,
val xSkipPrereleaseCheck: Boolean?,
val xUseFirLt: Boolean?,
val xUseK2: Boolean?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,30 @@ message CppTargetInfo {
}

message KotlincOpts {
string include_stdlibs = 1;
bool java_parameters = 2;
string jvm_target = 3;
string warn = 4;
bool x_allow_result_return_type = 5;
int32 x_backend_threads = 6;
bool x_emit_jvm_type_annotations = 7;
bool x_enable_incremental_compilation = 8;
string x_explicit_api_mode = 9;
bool x_inline_classes = 10;
string x_jvm_default = 11;
string x_lambdas = 12;
bool x_multi_platform = 13;
bool x_no_call_assertions = 14;
bool x_no_optimize = 15;
bool x_no_optimized_callable_references = 16;
bool x_no_param_assertions = 18;
bool x_no_receiver_assertions = 19;
optional string include_stdlibs = 1;
optional bool java_parameters = 2;
optional string jvm_target = 3;
optional string warn = 4;
optional bool x_allow_result_return_type = 5;
optional int32 x_backend_threads = 6;
optional bool x_emit_jvm_type_annotations = 7;
optional bool x_enable_incremental_compilation = 8;
optional string x_explicit_api_mode = 9;
optional bool x_inline_classes = 10;
optional string x_jvm_default = 11;
optional string x_lambdas = 12;
optional bool x_multi_platform = 13;
optional bool x_no_call_assertions = 14;
optional bool x_no_optimize = 15;
optional bool x_no_optimized_callable_references = 16;
optional bool x_no_param_assertions = 18;
optional bool x_no_receiver_assertions = 19;
repeated string x_optin = 20;
bool x_report_perf = 21;
string x_sam_conversions = 22;
bool x_skip_prerelease_check = 23;
bool x_use_fir_lt = 24;
bool x_use_k2 = 25;
optional bool x_report_perf = 21;
optional string x_sam_conversions = 22;
optional bool x_skip_prerelease_check = 23;
optional bool x_use_fir_lt = 24;
optional bool x_use_k2 = 25;
}

message KotlinTargetInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BazelBspEnvironmentManagerTest {
load("//aspects:rules/kt/kt_info.bzl","extract_kotlin_info")
load("//aspects:rules/scala/scala_info.bzl","extract_scala_info","extract_scala_toolchain_info")
EXTENSIONS=[extract_java_toolchain,extract_java_runtime,extract_jvm_info,extract_python_info,extract_cpp_info,extract_kotlin_info,extract_scala_info,extract_scala_toolchain_info]
TOOLCHAINS=["@bazel_tools//tools/jdk:runtime_toolchain_type","@io_bazel_rules_scala//scala:toolchain_type"]
TOOLCHAINS=["@bazel_tools//tools/jdk:runtime_toolchain_type","@io_bazel_rules_kotlin//kotlin/internal:kt_toolchain_type","@io_bazel_rules_scala//scala:toolchain_type"]
""".replace(" ", "").replace("\n", "")
private lateinit var dotBazelBspAspectsPath: Path
private lateinit var internalAspectsResolverMock: InternalAspectsResolver
Expand Down

0 comments on commit 6b45eb6

Please sign in to comment.