Skip to content

Commit

Permalink
[fix] kotlin stdlibs now are obtained from toolchain instead of class…
Browse files Browse the repository at this point in the history
…path

now its project level lib

review fix

[fix] kotlin stdlibs now are obtained from toolchain instead of classpath


Merge-request: BAZEL-MR-667
Merged-by: Marcin Abramowicz <marcin.abramowicz@jetbrains.com>
  • Loading branch information
abrams27 authored and Space Team committed Nov 27, 2023
1 parent a10ecb0 commit e6ad2a0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
3 changes: 0 additions & 3 deletions aspects/rules/jvm/jvm_info.bzl
Expand Up @@ -136,8 +136,6 @@ def extract_jvm_info(target, ctx, output_groups, **kwargs):
resolve_files += compile_jars
resolve_files += source_jars

compile_classpath = map(file_location, compile_jars)

javac_opts = getattr(ctx.rule.attr, "javacopts", [])
jvm_flags = getattr(ctx.rule.attr, "jvm_flags", [])
args = getattr(ctx.rule.attr, "args", [])
Expand All @@ -149,7 +147,6 @@ def extract_jvm_info(target, ctx, output_groups, **kwargs):
info = create_struct(
jars = jars,
generated_jars = generated_jars,
compile_classpath = compile_classpath,
javac_opts = javac_opts,
jvm_flags = jvm_flags,
main_class = main_class,
Expand Down
6 changes: 5 additions & 1 deletion aspects/rules/kt/kt_info.bzl.template
@@ -1,6 +1,6 @@
load("@${ruleName}//kotlin/internal:defs.bzl", "KtJvmInfo")
load("@${ruleName}//kotlin/internal:opts.bzl", "KotlincOptions")
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_proto", "create_struct")
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_proto", "create_struct", "map", "file_location")

KOTLIN_TOOLCHAIN_TYPE = "@${ruleName}//kotlin/internal:kt_toolchain_type"

Expand All @@ -21,10 +21,14 @@ def extract_kotlin_info(target, ctx, **kwargs):
kotlinc_opts_target = getattr(ctx.rule.attr, "kotlinc_opts", None)
kotlinc_opts = kotlinc_opts_target[KotlincOptions] if kotlinc_opts_target and KotlincOptions in kotlinc_opts_target else toolchain_kotlinc_opts

stdlibs_files = kotlin_toolchain.jvm_stdlibs.compile_jars.to_list()
stdlibs = map(file_location, stdlibs_files)

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

kotlinc_opts_dict = convert_struct_to_dict(kotlinc_opts)
Expand Down
Expand Up @@ -30,10 +30,6 @@ import java.nio.file.Paths
import kotlin.io.path.exists
import kotlin.io.path.notExists

val KOTLIN_STDLIB_ROOT_EXECUTION_REGEX =
"""external/(rules_kotlin~.*~rules_kotlin_extensions~|)com_github_jetbrains_kotlin""".toRegex()
const val KOTLIN_STDLIB_RELATIVE_PATH_PREFIX = "lib/"

class BazelProjectMapper(
private val languagePluginsService: LanguagePluginsService,
private val bazelPathsResolver: BazelPathsResolver,
Expand Down Expand Up @@ -125,14 +121,34 @@ class BazelProjectMapper(
}

private fun calculateKotlinStdlibsMapper(targetsToImport: Sequence<TargetInfo>): Map<String, List<Library>> {
val projectLevelKotlinStdlibs = calculateProjectLevelKotlinStdlibs(targetsToImport)
val rulesKotlinTargets = targetsToImport
.filter { targetInfo -> targetInfo.jvmTargetInfo.compileClasspathList.any { it.isKotlinStdlibPath() } }
.map { it.id }
.toSet()
return rulesKotlinTargets.associateWith { listOf(projectLevelKotlinStdlibs) }
val projectLevelKotlinStdlibsLibrary = calculateProjectLevelKotlinStdlibsLibrary(targetsToImport)
val kotlinTargetsIds = targetsToImport.filter { it.hasKotlinTargetInfo() }.map { it.id }

return projectLevelKotlinStdlibsLibrary
?.let { stdlibsLibrary -> kotlinTargetsIds.associateWith { listOf(stdlibsLibrary) } }
.orEmpty()
}

private fun calculateProjectLevelKotlinStdlibsLibrary(targetsToImport: Sequence<TargetInfo>): Library? {
val kotlinStdlibsJars = calculateProjectLevelKotlinStdlibsJars(targetsToImport)

return if (kotlinStdlibsJars.isNotEmpty()) {
Library(
label = "rules_kotlin_kotlin-stdlibs",
outputs = kotlinStdlibsJars,
sources = emptySet(),
dependencies = emptyList(),
)
} else null
}

private fun calculateProjectLevelKotlinStdlibsJars(targetsToImport: Sequence<TargetInfo>): Set<URI> =
targetsToImport
.filter { it.hasKotlinTargetInfo() }
.map { it.kotlinTargetInfo.stdlibsList }
.flatMap { it.resolveUris() }
.toSet()

/**
* In some cases, the jar dependencies of a target might be injected by bazel or rules and not are not
* available via `deps` field of a target. For this reason, we read JavaOutputInfo's jdeps file and
Expand Down Expand Up @@ -226,23 +242,6 @@ class BazelProjectMapper(
return Paths.get(lib).fileName.toString().replace("[^0-9a-zA-Z]".toRegex(), "-") + "-" + shaOfPath
}

private fun calculateProjectLevelKotlinStdlibs(targets: Sequence<TargetInfo>) =
Library(
label = "rules_kotlin_kotlin-stdlibs",
outputs = targets
.flatMap { it.jvmTargetInfo.compileClasspathList }
.filter { it.isKotlinStdlibPath() }
.map { bazelPathsResolver.resolveUri(it) }
.toSet(),
sources = emptySet(),
dependencies = emptyList(),
)

private fun FileLocation.isKotlinStdlibPath() =
KOTLIN_STDLIB_ROOT_EXECUTION_REGEX.matches(this.rootExecutionPathFragment) &&
this.relativePath.startsWith(KOTLIN_STDLIB_RELATIVE_PATH_PREFIX)


private fun createLibraries(targets: Map<String, TargetInfo>): Map<String, Library> {
return targets.mapValues { entry ->
val targetId = entry.key
Expand Down
Expand Up @@ -39,7 +39,7 @@ class JavaLanguagePlugin(
it.interfaceJarsList + it.binaryJarsList
}.map(bazelPathsResolver::resolveUri)
val mainClass = getMainClass(this)
val compileClasspath = bazelPathsResolver.resolveUris(compileClasspathList + generatedJarsList.flatMap { it.binaryJarsList }, true)
val compileClasspath = bazelPathsResolver.resolveUris(generatedJarsList.flatMap { it.binaryJarsList }, true)
val runtimeJdk = jdkResolver.resolveJdk(targetInfo)

JavaModule(
Expand Down
Expand Up @@ -30,7 +30,6 @@ message JvmOutputs {
message JvmTargetInfo {
repeated JvmOutputs jars = 1;
repeated JvmOutputs generated_jars = 2;
repeated FileLocation compile_classpath = 4;
repeated string javac_opts = 6;
repeated string jvm_flags = 7;
string main_class = 8;
Expand Down Expand Up @@ -68,6 +67,7 @@ message KotlinTargetInfo {
string api_version = 2;
repeated string associates = 3;
repeated string kotlinc_opts = 4;
repeated FileLocation stdlibs = 5;
}

message PythonTargetInfo {
Expand Down

0 comments on commit e6ad2a0

Please sign in to comment.