From 736c6c4d2961dc4850325493242c5d40866345e0 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 3 Mar 2022 17:11:13 +0900 Subject: [PATCH] Pill: Import Java toolchain configuration correctly Before, all project modules used 'inheritedJdk'. However, some Kotlin modules now require JDK 8, while others depend on new API in JDK 11. --- plugins/pill/pill-importer/src/ModelParser.kt | 11 +++++++++++ plugins/pill/pill-importer/src/model/PModule.kt | 1 + plugins/pill/pill-importer/src/render.kt | 5 ++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/pill/pill-importer/src/ModelParser.kt b/plugins/pill/pill-importer/src/ModelParser.kt index 60a894d4772..0acf8e7a234 100644 --- a/plugins/pill/pill-importer/src/ModelParser.kt +++ b/plugins/pill/pill-importer/src/ModelParser.kt @@ -19,6 +19,7 @@ import org.gradle.api.internal.file.copy.CopySpecInternal import org.gradle.api.internal.file.copy.SingleParentCopySpec import org.gradle.api.provider.Property import org.gradle.jvm.tasks.Jar +import org.gradle.jvm.toolchain.JavaToolchainService import org.gradle.kotlin.dsl.extra import org.gradle.kotlin.dsl.findByType import org.gradle.language.jvm.tasks.ProcessResources @@ -122,6 +123,8 @@ class ModelParser(private val variant: Variant, private val modulePrefix: String val embeddedDependencies = project.configurations.findByName(EMBEDDED_CONFIGURATION_NAME) ?.let { parseDependencies(it) } ?: emptyList() + val javaLanguageVersion = getJavaLanguageVersion(project) + val sourceSets = parseSourceSets(project).sortedBy { it.forTests } for (sourceSet in sourceSets) { val sourceRoots = mutableListOf() @@ -158,6 +161,7 @@ class ModelParser(private val variant: Variant, private val modulePrefix: String moduleFile = getModuleFile(name), contentRoots = contentRoots, orderRoots = orderRoots, + javaLanguageVersion = javaLanguageVersion, kotlinOptions = sourceSet.kotlinOptions, moduleForProductionSources = productionModule, embeddedDependencies = embeddedDependencies @@ -177,6 +181,7 @@ class ModelParser(private val variant: Variant, private val modulePrefix: String moduleFile = mainModuleFileRelativePath, contentRoots = listOf(PContentRoot(project.projectDir, listOf(), getExcludedDirs(project, excludedProjects))), orderRoots = emptyList(), + javaLanguageVersion = null, kotlinOptions = null, moduleForProductionSources = null, embeddedDependencies = emptyList() @@ -185,6 +190,12 @@ class ModelParser(private val variant: Variant, private val modulePrefix: String return modules } + private fun getJavaLanguageVersion(project: Project): Int? { + val javaPluginExtension = project.extensions.findByType() ?: return null + val javaToolchainService = project.extensions.findByType() ?: return null + return javaToolchainService.launcherFor(javaPluginExtension.toolchain).orNull?.metadata?.languageVersion?.asInt() + } + private fun getExcludedDirs(project: Project, excludedProjects: List): List { fun getJavaExcludedDirs() = project.plugins.findPlugin(IdeaPlugin::class.java) ?.model?.module?.excludeDirs?.toList() ?: emptyList() diff --git a/plugins/pill/pill-importer/src/model/PModule.kt b/plugins/pill/pill-importer/src/model/PModule.kt index 678bb9a3f11..44453f6989b 100644 --- a/plugins/pill/pill-importer/src/model/PModule.kt +++ b/plugins/pill/pill-importer/src/model/PModule.kt @@ -16,6 +16,7 @@ data class PModule( val moduleFile: File, val contentRoots: List, val orderRoots: List, + val javaLanguageVersion: Int?, val kotlinOptions: PSourceRootKotlinOptions?, val moduleForProductionSources: PModule? = null, val embeddedDependencies: List diff --git a/plugins/pill/pill-importer/src/render.kt b/plugins/pill/pill-importer/src/render.kt index 4c64668eb8b..859d10e2b0c 100644 --- a/plugins/pill/pill-importer/src/render.kt +++ b/plugins/pill/pill-importer/src/render.kt @@ -130,7 +130,10 @@ private fun renderModule(project: PProject, module: PModule) = PFile( } } - xml("orderEntry", "type" to "inheritedJdk") + when (val javaLanguageVersion = module.javaLanguageVersion) { + null -> xml("orderEntry", "type" to "inheritedJdk") + else -> xml("orderEntry", "type" to "jdk", "jdkName" to javaLanguageVersion.toString(), "jdkType" to "JavaSDK") + } xml("orderEntry", "type" to "sourceFolder", "forTests" to "false")