[CLI] Add CLI arguments to pass HMPP module structure to the compiler

^KT-56209
This commit is contained in:
Dmitriy Novozhilov
2023-02-08 18:14:12 +02:00
committed by Space Team
parent ec59cc050c
commit 77caa31640
31 changed files with 504 additions and 0 deletions
@@ -716,6 +716,30 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
field = value
}
@Argument(
value = "-Xmodule",
valueDescription = "<module name>;<source file[,source file...]>",
description = "Describes module with specific sources. Usage of this arguments requires to specify module for each source file from free args",
delimiter = ""
)
var modulesDescription: Array<String>? = null
set(value) {
checkFrozen()
field = value
}
@Argument(
value = "-XdependsOn",
valueDescription = "<fromModuleName>:<onModuleName>",
description = "Declares that <fromModuleName> depends on <onModuleName> with dependsOn relation",
delimiter = ""
)
var dependsOnDependencies: Array<String>? = null
set(value) {
checkFrozen()
field = value
}
@OptIn(IDEAPluginsCompatibilityAPI::class)
open fun configureAnalysisFlags(collector: MessageCollector, languageVersion: LanguageVersion): MutableMap<AnalysisFlag<*>, Any> {
return HashMap<AnalysisFlag<*>, Any>().apply {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.cli.common
import com.intellij.ide.highlighter.JavaFileType
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.cli.common.arguments.ManualLanguageFeatureSetting
@@ -12,6 +13,7 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.KotlinPaths
import org.jetbrains.kotlin.utils.KotlinPathsFromHomeDir
import org.jetbrains.kotlin.utils.PathUtil
@@ -47,6 +49,7 @@ fun <A : CommonCompilerArguments> CompilerConfiguration.setupCommonArguments(
val usesK2 = arguments.useK2 || languageVersionSettings.languageVersion.usesK2
put(CommonConfigurationKeys.USE_FIR, usesK2)
put(CommonConfigurationKeys.USE_LIGHT_TREE, arguments.useFirLT)
buildHmppModuleStructure(arguments)?.let { put(CommonConfigurationKeys.HMPP_MODULE_STRUCTURE, it) }
}
fun <A : CommonCompilerArguments> CompilerConfiguration.setupLanguageVersionSettings(arguments: A) {
@@ -125,3 +128,134 @@ private fun <A : CommonToolArguments> MessageCollector.reportUnsafeInternalArgum
)
}
}
private fun CompilerConfiguration.buildHmppModuleStructure(arguments: CommonCompilerArguments): HmppCliModuleStructure? {
val rawModules = arguments.modulesDescription
val rawDependencies = arguments.dependsOnDependencies
val messageCollector = getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
fun reportError(message: String) {
messageCollector.report(CompilerMessageSeverity.ERROR, message)
}
fun reportWarning(message: String) {
messageCollector.report(CompilerMessageSeverity.WARNING, message)
}
if (rawModules == null) {
if (rawDependencies != null) {
reportError("-XdependsOn flag can not be used without -Xmodule")
}
return null
}
if (!languageVersionSettings.languageVersion.usesK2) {
reportWarning("-Xmodule flag is not supported for language version < 2.0")
return null
}
var modules = rawModules.mapNotNull { arg ->
val split = arg.split(";")
val name = split.first()
when (split.size) {
1 -> reportWarning("Incorrect syntax for -Xmodule argument. Module $name has no sources")
2 -> {} // OK
else -> {
reportError("Incorrect syntax for -Xmodule argument. `<module name>;<source file[,source file...]>` expected but got `$arg`")
return@mapNotNull null
}
}
val sources = split.getOrNull(1)?.split(",")?.toSet().orEmpty()
HmppCliModule(name, sources)
}
var wasError = false
// check sources mapping
for (i in modules.indices) {
val m1 = modules[i]
for (j in (i + 1) until modules.size) {
val m2 = modules[j]
val commonFiles = m1.sources.intersect(m2.sources)
if (commonFiles.isNotEmpty()) {
val message = buildString {
if (commonFiles.size == 1) {
append("File '${commonFiles.single()}'")
} else {
append("Files ")
append(commonFiles.joinToString(", ") { "'$it'" })
}
append(" can be a part of only one module, but is listed as a source for both `${m1.name}` and `${m2.name}`, please check you -Xmodule options.")
}
reportError(message)
wasError = true
}
}
}
for (source in arguments.freeArgs) {
if (source.endsWith(JavaFileType.DOT_DEFAULT_EXTENSION)) continue
if (modules.none { source in it.sources }) {
reportError("Source '$source' does not belong to any module")
wasError = true
}
}
if (wasError) {
return null
}
if (modules.size == 1) {
if (rawDependencies?.isNotEmpty() == true) {
reportError("-XdependsOn flag is specified but there is only one module declared")
}
return HmppCliModuleStructure(modules, emptyMap())
}
val duplicatedModules = modules.filter { module -> modules.count { it.name == module.name } > 1 }
if (duplicatedModules.isNotEmpty()) {
reportError("There are multiple modules with same name(s): ${duplicatedModules.distinct().joinToString(", ") { it.name }}")
return null
}
val moduleByName = modules.associateBy { it.name }
val dependenciesMap = rawDependencies.orEmpty().mapNotNull {
val split = it.split(":")
if (split.size != 2) {
reportError("Incorrect syntax for -XdependsOn argument. Expected <fromModuleName>:<onModuleName> but got `$it`")
return@mapNotNull null
}
val moduleName1 = split[0]
val moduleName2 = split[1]
fun findModule(name: String): HmppCliModule? {
return moduleByName[name].also { module ->
if (module == null) {
reportError("Module `$name` not found in -Xmodule arguments")
}
}
}
val module1 = findModule(moduleName1)
val module2 = findModule(moduleName2)
if (module1 == null || module2 == null) return@mapNotNull null
module1 to module2
}.groupBy(
keySelector = { it.first },
valueTransform = { it.second }
)
modules = DFS.topologicalOrder(modules) { dependenciesMap[it].orEmpty() }.asReversed()
modules.forEachIndexed { i, module ->
val dependencies = dependenciesMap[module].orEmpty()
val previousModules = modules.subList(0, i)
if (dependencies.any { it !in previousModules }) {
reportError("There is a cycle in dependencies of module `${module.name}`")
}
}
return HmppCliModuleStructure(modules, dependenciesMap)
}
@@ -56,6 +56,9 @@ object CommonConfigurationKeys {
@JvmField
val USE_LIGHT_TREE = CompilerConfigurationKey.create<Boolean>("light tree")
@JvmField
val HMPP_MODULE_STRUCTURE = CompilerConfigurationKey.create<HmppCliModuleStructure>("HMPP module structure")
@JvmField
val EXPECT_ACTUAL_LINKER = CompilerConfigurationKey.create<Boolean>("Experimental expect/actual linker")
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.config
class HmppCliModule(val name: String, val sources: Set<String>) {
override fun toString(): String {
return "Module $name"
}
}
/**
* All [modules] are sorted in reversed topological order
* (module without dependencies will be the first)
*/
class HmppCliModuleStructure(
val modules: List<HmppCliModule>,
val dependenciesMap: Map<HmppCliModule, List<HmppCliModule>>
)
+4
View File
@@ -63,6 +63,8 @@ where advanced options include:
-Xcommon-sources=<path> Sources of the common module that need to be compiled together with this module in the multi-platform mode.
Should be a subset of sources passed as free arguments
-Xcontext-receivers Enable experimental context receivers
-XdependsOn=<fromModuleName>:<onModuleName>
Declares that <fromModuleName> depends on <onModuleName> with dependsOn relation
-Xdisable-default-scripting-plugin
Do not enable scripting plugin by default
-Xdisable-phases Disable backend phases
@@ -89,6 +91,8 @@ where advanced options include:
-Xlegacy-smart-cast-after-try Allow var smart casts despite assignment in try block
-Xlist-phases List backend phases
-Xmetadata-version Change metadata version of the generated binary files
-Xmodule=<module name>;<source file[,source file...]>
Describes module with specific sources. Usage of this arguments requires to specify module for each source file from free args
-Xmulti-platform Enable experimental language support for multi-platform projects
-Xnew-inference Enable new experimental generic type inference algorithm
-Xno-check-actual Do not check presence of 'actual' modifier in multi-platform projects
+4
View File
@@ -169,6 +169,8 @@ where advanced options include:
-Xcommon-sources=<path> Sources of the common module that need to be compiled together with this module in the multi-platform mode.
Should be a subset of sources passed as free arguments
-Xcontext-receivers Enable experimental context receivers
-XdependsOn=<fromModuleName>:<onModuleName>
Declares that <fromModuleName> depends on <onModuleName> with dependsOn relation
-Xdisable-default-scripting-plugin
Do not enable scripting plugin by default
-Xdisable-phases Disable backend phases
@@ -195,6 +197,8 @@ where advanced options include:
-Xlegacy-smart-cast-after-try Allow var smart casts despite assignment in try block
-Xlist-phases List backend phases
-Xmetadata-version Change metadata version of the generated binary files
-Xmodule=<module name>;<source file[,source file...]>
Describes module with specific sources. Usage of this arguments requires to specify module for each source file from free args
-Xmulti-platform Enable experimental language support for multi-platform projects
-Xnew-inference Enable new experimental generic type inference algorithm
-Xno-check-actual Do not check presence of 'actual' modifier in multi-platform projects
+14
View File
@@ -0,0 +1,14 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
$TESTDATA_DIR$/src/c.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-Xmodule=b;$TESTDATA_DIR$/src/b.kt
-Xmodule=c;$TESTDATA_DIR$/src/c.kt
-XdependsOn=b\:a
-XdependsOn=c\:b
-XdependsOn=a\:c
+12
View File
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: there is a cycle in dependencies of module `b`
COMPILATION_ERROR
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/src/a.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-XdependsOn=b\:a
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: -XdependsOn flag is specified but there is only one module declared
COMPILATION_ERROR
@@ -0,0 +1,7 @@
$TESTDATA_DIR$/src/a.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-XdependsOn=a\:b
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: -XdependsOn flag can not be used without -Xmodule
COMPILATION_ERROR
+10
View File
@@ -0,0 +1,10 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-Xmodule=b;$TESTDATA_DIR$/src/b.kt
-Xmodule=a;$TESTDATA_DIR$/src/c.kt
+12
View File
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: there are multiple modules with same name(s): a, a
COMPILATION_ERROR
+11
View File
@@ -0,0 +1,11 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
$TESTDATA_DIR$/src/c.kt
-d
$TEMP_DIR$
-language-version
1.9
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-Xmodule=b;$TESTDATA_DIR$/src/b.kt,$TESTDATA_DIR$/src/c.kt
-Xcommon-sources=$TESTDATA_DIR$/src/a.kt,$TESTDATA_DIR$/src/b.kt
+11
View File
@@ -0,0 +1,11 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: -Xmodule flag is not supported for language version < 2.0
OK
+10
View File
@@ -0,0 +1,10 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-Xmodule=b;$TESTDATA_DIR$/src/b.kt
-XdependsOn=c\:a
+12
View File
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: module `c` not found in -Xmodule arguments
COMPILATION_ERROR
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;a.kt;b.kt
@@ -0,0 +1,14 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: incorrect syntax for -Xmodule argument. `<module name>;<source file[,source file...]>` expected but got `a;a.kt;b.kt`
error: source '$TESTDATA_DIR$/src/a.kt' does not belong to any module
error: source '$TESTDATA_DIR$/src/b.kt' does not belong to any module
COMPILATION_ERROR
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/src/a.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a
-Xmodule=b;
+12
View File
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: source '$TESTDATA_DIR$/src/a.kt' does not belong to any module
COMPILATION_ERROR
@@ -0,0 +1,9 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
-Xmodule=b;$TESTDATA_DIR$/src/a.kt
@@ -0,0 +1,13 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: file '$TESTDATA_DIR$/src/a.kt' can be a part of only one module, but is listed as a source for both `a` and `b`, please check you -Xmodule options.
error: source '$TESTDATA_DIR$/src/b.kt' does not belong to any module
COMPILATION_ERROR
@@ -0,0 +1,8 @@
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xmodule=a;$TESTDATA_DIR$/src/a.kt
+12
View File
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
warning: language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
error: source '$TESTDATA_DIR$/src/b.kt' does not belong to any module
COMPILATION_ERROR
+7
View File
@@ -0,0 +1,7 @@
expect class A {
fun foo()
}
expect class B {
fun bar()
}
+13
View File
@@ -0,0 +1,13 @@
actual class A {
actual fun foo() {}
fun actFoo() {}
}
fun acceptB(b: B) {
b.bar()
}
fun acceptA(a: A) {
a.foo()
a.actFoo()
}
+15
View File
@@ -0,0 +1,15 @@
actual class B {
actual fun bar() {}
fun actBar() {}
}
fun actualAcceptB(b: B) {
b.bar()
b.actBar()
}
fun test() {
acceptA(A())
acceptB(B())
actualAcceptB(B())
}
@@ -250,6 +250,7 @@ fun generateJUnit3CompilerTests(args: Array<String>) {
testClass<AbstractCliTest> {
model("cli/jvm/readingConfigFromEnvironment", extension = "args", testMethod = "doJvmTest", recursive = false)
model("cli/jvm/plugins", extension = "args", testMethod = "doJvmTest", recursive = false)
model("cli/jvm/hmpp", extension = "args", testMethod = "doJvmTest", recursive = false)
model("cli/jvm", extension = "args", testMethod = "doJvmTest", recursive = false)
model("cli/js", extension = "args", testMethod = "doJsTest", recursive = false)
model("cli/js-dce", extension = "args", testMethod = "doJsDceTest", recursive = false)
@@ -105,6 +105,69 @@ public class CliTestGenerated extends AbstractCliTest {
}
}
@TestMetadata("compiler/testData/cli/jvm/hmpp")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Hmpp extends AbstractCliTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doJvmTest, this, testDataFilePath);
}
public void testAllFilesPresentInHmpp() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/cli/jvm/hmpp"), Pattern.compile("^(.+)\\.args$"), null, false);
}
@TestMetadata("cycleInDependencies.args")
public void testCycleInDependencies() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/cycleInDependencies.args");
}
@TestMetadata("dependsOnSingleModule.args")
public void testDependsOnSingleModule() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/dependsOnSingleModule.args");
}
@TestMetadata("dependsOnWithoutModules.args")
public void testDependsOnWithoutModules() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/dependsOnWithoutModules.args");
}
@TestMetadata("duplicatedModules.args")
public void testDuplicatedModules() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/duplicatedModules.args");
}
@TestMetadata("lowLanguageVersion.args")
public void testLowLanguageVersion() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/lowLanguageVersion.args");
}
@TestMetadata("missingModule.args")
public void testMissingModule() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/missingModule.args");
}
@TestMetadata("moduleIncorrectSyntax.args")
public void testModuleIncorrectSyntax() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/moduleIncorrectSyntax.args");
}
@TestMetadata("moduleWithoutSources.args")
public void testModuleWithoutSources() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/moduleWithoutSources.args");
}
@TestMetadata("sameSourceInDifferentModules.args")
public void testSameSourceInDifferentModules() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/sameSourceInDifferentModules.args");
}
@TestMetadata("sourceNotInAnyModule.args")
public void testSourceNotInAnyModule() throws Exception {
runTest("compiler/testData/cli/jvm/hmpp/sourceNotInAnyModule.args");
}
}
@TestMetadata("compiler/testData/cli/jvm")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)