Skip to content

Commit a4d6e96

Browse files
johanbaynikita-nazarov
authored andcommitted
WIP: Introduce JKlib entry point
1 parent 900eaca commit a4d6e96

File tree

11 files changed

+2641
-0
lines changed

11 files changed

+2641
-0
lines changed

compiler/cli/bin/kotlinc-jklib

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
# Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
5+
6+
export KOTLIN_COMPILER=org.jetbrains.kotlin.cli.jklib.K2JKlibCompiler
7+
8+
DIR="${BASH_SOURCE[0]%/*}"
9+
: ${DIR:="."}
10+
11+
"${DIR}"/kotlinc "$@"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@file:Suppress("unused", "DuplicatedCode")
2+
3+
// DO NOT EDIT MANUALLY!
4+
// Generated by generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateCompilerArgumentsCopy.kt
5+
// To regenerate run 'generateCompilerArgumentsCopy' task
6+
7+
package org.jetbrains.kotlin.cli.common.arguments
8+
9+
@OptIn(org.jetbrains.kotlin.utils.IDEAPluginsCompatibilityAPI::class)
10+
fun copyK2JKlibCompilerArguments(from: K2JKlibCompilerArguments, to: K2JKlibCompilerArguments): K2JKlibCompilerArguments {
11+
copyCommonCompilerArguments(from, to)
12+
13+
to.classpath = from.classpath
14+
to.destination = from.destination
15+
to.friendPaths = from.friendPaths?.copyOf()
16+
to.klibLibraries = from.klibLibraries
17+
to.moduleName = from.moduleName
18+
to.noReflect = from.noReflect
19+
to.noStdlib = from.noStdlib
20+
21+
return to
22+
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.cli.common.arguments
7+
8+
import org.jetbrains.kotlin.config.*
9+
10+
class K2JKlibCompilerArguments : CommonCompilerArguments() {
11+
companion object {
12+
@JvmStatic private val serialVersionUID = 0L
13+
}
14+
15+
@Argument(value = "-d", valueDescription = "<klib>", description = "Destination for generated files.")
16+
var destination: String? = null
17+
set(value) {
18+
checkFrozen()
19+
field = if (value.isNullOrEmpty()) null else value
20+
}
21+
22+
@Argument(value = "-p", valueDescription = "<library|ir>", description = "")
23+
var produce: String? = null
24+
set(value) {
25+
checkFrozen()
26+
field = if (value.isNullOrEmpty()) null else value
27+
}
28+
29+
@Argument(
30+
value = "-classpath",
31+
shortName = "-cp",
32+
valueDescription = "<path>",
33+
description = "List of directories and JAR/ZIP archives to search for user .kotlin_metadata files."
34+
)
35+
var classpath: String? = null
36+
set(value) {
37+
checkFrozen()
38+
field = if (value.isNullOrEmpty()) null else value
39+
}
40+
41+
@Argument(value = "-module-name", valueDescription = "<name>", description = "Name of the generated .kotlin_module file.")
42+
var moduleName: String? = null
43+
set(value) {
44+
checkFrozen()
45+
field = if (value.isNullOrEmpty()) null else value
46+
}
47+
48+
@Argument(
49+
value = "-Xfriend-paths",
50+
valueDescription = "<path>",
51+
description = "Paths to output directories for friend modules (modules whose internals should be visible)."
52+
)
53+
var friendPaths: Array<String>? = null
54+
set(value) {
55+
checkFrozen()
56+
field = value
57+
}
58+
59+
@Argument(
60+
value = "-Xklib",
61+
valueDescription = "<path>",
62+
description = "Paths to cross-platform libraries in the .klib format."
63+
)
64+
var klibLibraries: String? = null
65+
set(value) {
66+
checkFrozen()
67+
field = if (value.isNullOrEmpty()) null else value
68+
}
69+
70+
@Argument(
71+
value = "-no-stdlib",
72+
description = "Don't automatically include the Kotlin/JVM stdlib and Kotlin reflection dependencies in the classpath."
73+
)
74+
var noStdlib = false
75+
set(value) {
76+
checkFrozen()
77+
field = value
78+
}
79+
80+
@Argument(
81+
value = "-Xcompile-builtins-as-part-of-stdlib",
82+
description = "Enable behaviour needed to compile builtins as part of JVM stdlib"
83+
)
84+
var expectBuiltinsAsPartOfStdlib = false
85+
set(value) {
86+
checkFrozen()
87+
field = value
88+
}
89+
90+
91+
@Argument(value = "-no-jdk", description = "Don't automatically include the Java runtime in the classpath.")
92+
var noJdk = false
93+
set(value) {
94+
checkFrozen()
95+
field = value
96+
}
97+
98+
99+
@Argument(value = "-no-reflect", description = "Don't automatically include the Kotlin reflection dependency in the classpath.")
100+
var noReflect = false
101+
set(value) {
102+
checkFrozen()
103+
field = value
104+
}
105+
@Argument(
106+
value = "-Xtype-enhancement-improvements-strict-mode",
107+
description = """Enable strict mode for improvements to type enhancement for loaded Java types based on nullability annotations,
108+
including the ability to read type-use annotations from class files.
109+
See KT-45671 for more details."""
110+
)
111+
var typeEnhancementImprovementsInStrictMode = false
112+
set(value) {
113+
checkFrozen()
114+
field = value
115+
}
116+
117+
@Argument(
118+
value = "-Xenhance-type-parameter-types-to-def-not-null",
119+
description = "Enhance not-null-annotated type parameter types to definitely-non-nullable types ('@NotNull T' => 'T & Any')."
120+
)
121+
var enhanceTypeParameterTypesToDefNotNull = false
122+
set(value) {
123+
checkFrozen()
124+
field = value
125+
}
126+
127+
@Argument(
128+
value = "-Xjvm-default",
129+
valueDescription = "{all|all-compatibility|disable}",
130+
description = """Emit JVM default methods for interface declarations with bodies. The default is 'disable'.
131+
-Xjvm-default=all Generate JVM default methods for all interface declarations with bodies in the module.
132+
Do not generate 'DefaultImpls' stubs for interface declarations with bodies. If an interface inherits a method with a
133+
body from an interface compiled in 'disable' mode and doesn't override it, then a 'DefaultImpls' stub will be
134+
generated for it.
135+
This BREAKS BINARY COMPATIBILITY if some client code relies on the presence of 'DefaultImpls' classes.
136+
Note that if interface delegation is used, all interface methods are delegated.
137+
-Xjvm-default=all-compatibility Like 'all', but additionally generate compatibility stubs in the 'DefaultImpls' classes.
138+
Compatibility stubs can help library and runtime authors maintain backward binary compatibility
139+
for existing clients compiled against previous library versions.
140+
'all' and 'all-compatibility' modes change the library ABI surface that will be used by clients after
141+
the recompilation of the library. Because of this, clients might be incompatible with previous library
142+
versions. This usually means that proper library versioning is required, for example with major version increases in SemVer.
143+
In subtypes of Kotlin interfaces compiled in 'all' or 'all-compatibility' mode, 'DefaultImpls'
144+
compatibility stubs will invoke the default method of the interface with standard JVM runtime resolution semantics.
145+
Perform additional compatibility checks for classes inheriting generic interfaces where in some cases an
146+
additional implicit method with specialized signatures was generated in 'disable' mode.
147+
Unlike in 'disable' mode, the compiler will report an error if such a method is not overridden explicitly
148+
and the class is not annotated with '@JvmDefaultWithoutCompatibility' (see KT-39603 for more details).
149+
-Xjvm-default=disable Default behavior. Do not generate JVM default methods."""
150+
)
151+
var jvmDefault: String = JvmDefaultMode.DISABLE.description
152+
set(value) {
153+
checkFrozen()
154+
field = value
155+
}
156+
157+
@Argument(
158+
value = "-Xvalue-classes",
159+
description = "Enable experimental value classes."
160+
)
161+
var valueClasses = false
162+
set(value) {
163+
checkFrozen()
164+
field = value
165+
}
166+
167+
@Argument(
168+
value = "-Xjsr305",
169+
deprecatedName = "-Xjsr305-annotations",
170+
valueDescription =
171+
"{ignore/strict/warn}" +
172+
"|under-migration:{ignore/strict/warn}" +
173+
"|@<fq.name>:{ignore/strict/warn}",
174+
description =
175+
"""Specify the behavior of 'JSR-305' nullability annotations:
176+
-Xjsr305={ignore/strict/warn} global (all non-@UnderMigration annotations)
177+
-Xjsr305=under-migration:{ignore/strict/warn} all @UnderMigration annotations
178+
-Xjsr305=@<fq.name>:{ignore/strict/warn} annotation with the given fully qualified class name
179+
Modes:
180+
* ignore
181+
* strict (experimental; treat like other supported nullability annotations)
182+
* warn (report a warning)""",
183+
)
184+
var jsr305: Array<String>? = null
185+
set(value) {
186+
checkFrozen()
187+
field = value
188+
}
189+
190+
@Argument(
191+
value = "-Xsupport-compatqual-checker-framework-annotations",
192+
valueDescription = "enable|disable",
193+
description =
194+
"""Specify the behavior for Checker Framework 'compatqual' annotations ('NullableDecl'/'NonNullDecl').
195+
The default value is 'enable'.""",
196+
)
197+
var supportCompatqualCheckerFrameworkAnnotations: String? = null
198+
set(value) {
199+
checkFrozen()
200+
field = if (value.isNullOrEmpty()) null else value
201+
}
202+
203+
@Argument(
204+
value = "-Xjspecify-annotations",
205+
valueDescription = "ignore|strict|warn",
206+
description =
207+
"""Specify the behavior of 'jspecify' annotations.
208+
The default value is 'warn'.""",
209+
)
210+
var jspecifyAnnotations: String? = null
211+
set(value) {
212+
checkFrozen()
213+
field = value
214+
}
215+
216+
@Argument(
217+
value = "Xmultifile-parts-inherit",
218+
description = "Compile multifile classes as a hierarchy of parts and a facade."
219+
)
220+
var inheritMultifileParts: Boolean = false
221+
set(value) {
222+
checkFrozen()
223+
field = value
224+
}
225+
226+
@Argument(
227+
value = "-Xoutput-builtins-metadata",
228+
description = "Output builtins metadata as .kotlin_builtins files",
229+
)
230+
var outputBuiltinsMetadata: Boolean = false
231+
set(value) {
232+
checkFrozen()
233+
field = value
234+
}
235+
236+
@Argument(
237+
value = "-Xnullability-annotations",
238+
valueDescription = "@<fq.name>:{ignore/strict/warn}",
239+
description =
240+
"""Specify the behavior for specific Java nullability annotations (provided with fully qualified package name).
241+
Modes:
242+
* ignore
243+
* strict
244+
* warn (report a warning)""",
245+
)
246+
var nullabilityAnnotations: Array<String>? = null
247+
set(value) {
248+
checkFrozen()
249+
field = value
250+
}
251+
252+
override fun copyOf(): Freezable = TODO() // copyK2JKlibCompilerArguments(this, K2JKlibCompilerArguments())
253+
254+
override val configurator: CommonCompilerArgumentsConfigurator = K2JKlibCompilerArgumentsConfigurator()
255+
256+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.cli.common.arguments
7+
8+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
9+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
10+
import org.jetbrains.kotlin.config.*
11+
12+
class K2JKlibCompilerArgumentsConfigurator : CommonCompilerArgumentsConfigurator() {
13+
override fun configureAnalysisFlags(
14+
arguments: CommonCompilerArguments,
15+
collector: MessageCollector,
16+
languageVersion: LanguageVersion,
17+
): MutableMap<AnalysisFlag<*>, Any> = with(arguments) {
18+
require(this is K2JKlibCompilerArguments)
19+
val result = super.configureAnalysisFlags(arguments, collector, languageVersion)
20+
result[JvmAnalysisFlags.javaTypeEnhancementState] = JavaTypeEnhancementStateParser(collector, languageVersion.toKotlinVersion())
21+
.parse(jsr305, supportCompatqualCheckerFrameworkAnnotations, jspecifyAnnotations, nullabilityAnnotations)
22+
23+
result[JvmAnalysisFlags.inheritMultifileParts] = inheritMultifileParts
24+
result[JvmAnalysisFlags.outputBuiltinsMetadata] = outputBuiltinsMetadata
25+
if (expectBuiltinsAsPartOfStdlib && !stdlibCompilation) {
26+
collector.report(
27+
CompilerMessageSeverity.ERROR,
28+
"-Xcompile-builtins-as-part-of-stdlib must not be used without -Xstdlib-compilation"
29+
)
30+
}
31+
result[JvmAnalysisFlags.expectBuiltinsAsPartOfStdlib] = expectBuiltinsAsPartOfStdlib
32+
return result
33+
}
34+
35+
private fun K2JVMCompilerArguments.configureJvmDefaultMode(collector: MessageCollector?): JvmDefaultMode? = when {
36+
jvmDefaultStable != null -> JvmDefaultMode.fromStringOrNull(jvmDefaultStable).also {
37+
if (it == null) {
38+
collector?.report(
39+
CompilerMessageSeverity.ERROR,
40+
"Unknown -jvm-default mode: $jvmDefaultStable, supported modes: " +
41+
"${JvmDefaultMode.entries.map(JvmDefaultMode::description)}"
42+
)
43+
}
44+
}
45+
jvmDefault != null -> JvmDefaultMode.fromStringOrNullOld(jvmDefault).also {
46+
if (it == null) {
47+
collector?.report(
48+
CompilerMessageSeverity.ERROR,
49+
"Unknown -Xjvm-default mode: $jvmDefault, supported modes: " +
50+
"${JvmDefaultMode.entries.map(JvmDefaultMode::oldDescription)}"
51+
)
52+
}
53+
}
54+
else -> null
55+
}
56+
57+
override fun configureLanguageFeatures(
58+
arguments: CommonCompilerArguments,
59+
collector: MessageCollector,
60+
): MutableMap<LanguageFeature, LanguageFeature.State> = with(arguments) {
61+
require(this is K2JKlibCompilerArguments)
62+
val result = super.configureLanguageFeatures(arguments, collector)
63+
if (typeEnhancementImprovementsInStrictMode) {
64+
result[LanguageFeature.TypeEnhancementImprovementsInStrictMode] = LanguageFeature.State.ENABLED
65+
}
66+
if (enhanceTypeParameterTypesToDefNotNull) {
67+
result[LanguageFeature.ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated] = LanguageFeature.State.ENABLED
68+
}
69+
if (valueClasses) {
70+
result[LanguageFeature.ValueClasses] = LanguageFeature.State.ENABLED
71+
}
72+
return result
73+
}
74+
}

0 commit comments

Comments
 (0)