summaryrefslogtreecommitdiff
path: root/native-image/kotlin-2.4.21-native-image.patch
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@opera.com>2026-11-24 00:55:37 +0100
committerJoel Klinghed <the_jk@opera.com>2026-11-24 00:55:37 +0100
commit8c126e9a0370ace22b9293ea7616a59a25e5e89d (patch)
treeee465d78a123aa320c3befc06f133dd190ee78ca /native-image/kotlin-2.4.21-native-image.patch
parent2871d7a506a12656adb36998be7684f664727503 (diff)
Document how to build the OFA Kotlin compiler native imageHEADmain
native-image/kotlin-2.4.21-native-image.patch is the change to the Kotlin repository at v2.4.21. It bundles parcelize and plugin jars from outside the repository, adds the constructor metadata that the plugin loader needs, and adds G1, PGO and GraalVM home options. It also has the reachability metadata collected from OFA modules. native-image/build.sh runs the plain, instrumented and PGO builds, and native-image/README.md describes the full procedure and the results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'native-image/kotlin-2.4.21-native-image.patch')
-rw-r--r--native-image/kotlin-2.4.21-native-image.patch522
1 files changed, 522 insertions, 0 deletions
diff --git a/native-image/kotlin-2.4.21-native-image.patch b/native-image/kotlin-2.4.21-native-image.patch
new file mode 100644
index 0000000..1708bcd
--- /dev/null
+++ b/native-image/kotlin-2.4.21-native-image.patch
@@ -0,0 +1,522 @@
+diff --git i/prepare/compiler-native-image/build.gradle.kts w/prepare/compiler-native-image/build.gradle.kts
+index 705e4e379..16648abae 100644
+--- i/prepare/compiler-native-image/build.gradle.kts
++++ w/prepare/compiler-native-image/build.gradle.kts
+@@ -31,6 +31,48 @@ val pluginsRuntime by configurations.creating {
+
+ val bundledCompilerPlugins = mutableListOf<BundledCompilerPluginInfo>()
+
++// Each entry: pluginId;registrarFqName;commandLineProcessorFqName;jarPrefixes(comma-separated);jarPath
++// Entries are separated by '|'. The plugin jar must be compiled against kotlin-compiler-embeddable.
++val extraCompilerPlugins = kotlinBuildProperties
++ .stringProperty("kotlin.build.native-image.extra-plugins")
++ .orNull
++ .orEmpty()
++ .split("|")
++ .map { it.trim() }
++ .filter { it.isNotEmpty() }
++ .map { entry ->
++ val fields = entry.split(";").map { it.trim() }
++ require(fields.size == 5) { "Malformed kotlin.build.native-image.extra-plugins entry: $entry" }
++ ExtraCompilerPluginInfo(
++ pluginId = fields[0],
++ registrarFqName = fields[1],
++ commandLineProcessorFqName = fields[2].ifEmpty { null },
++ jarPrefixes = fields[3].split(",").map { it.trim() }.filter { it.isNotEmpty() },
++ jar = file(fields[4]),
++ )
++ }
++
++val useG1Gc = kotlinBuildProperties
++ .booleanProperty("kotlin.build.native-image.use-g1", false)
++ .get()
++val pgoProfile = kotlinBuildProperties
++ .stringProperty("kotlin.build.native-image.pgo-profile")
++ .orNull
++val pgoInstrument = kotlinBuildProperties
++ .booleanProperty("kotlin.build.native-image.pgo-instrument", false)
++ .get()
++// For example -O3, or -march=x86-64-v3. Separated by spaces.
++val extraNativeImageArgs = kotlinBuildProperties
++ .stringProperty("kotlin.build.native-image.extra-args")
++ .orNull
++ .orEmpty()
++ .split(" ")
++ .filter { it.isNotBlank() }
++// Oracle GraalVM reports "Oracle Corporation" as vendor, so the GRAAL_VM toolchain spec does not match it.
++val graalVmHome = kotlinBuildProperties
++ .stringProperty("kotlin.build.native-image.graalvm-home")
++ .orNull
++
+ dependencies {
+ nativeImageClasspath(project(":kotlin-compiler-embeddable", configuration = "runtimeElements"))
+
+@@ -106,6 +148,27 @@ dependencies {
+ nativeImageClasspath(project(":plugins:compose-compiler-plugin:compiler"))
+ }
+
++ bundledCompilerPlugin(
++ pluginId = "org.jetbrains.kotlin.parcelize",
++ registrarFqName = "org.jetbrains.kotlin.parcelize.ParcelizeComponentRegistrar",
++ commandLineProcessorFqName = "org.jetbrains.kotlin.parcelize.ParcelizeCommandLineProcessor",
++ jarPrefixes = listOf("parcelize-compiler", "kotlin-parcelize-compiler"),
++ ) {
++ nativeImageClasspath(project(":kotlin-parcelize-compiler"))
++ }
++
++ // Plugins built outside of this repository, from -Pkotlin.build.native-image.extra-plugins.
++ for (extraPlugin in extraCompilerPlugins) {
++ bundledCompilerPlugin(
++ pluginId = extraPlugin.pluginId,
++ registrarFqName = extraPlugin.registrarFqName,
++ commandLineProcessorFqName = extraPlugin.commandLineProcessorFqName,
++ jarPrefixes = extraPlugin.jarPrefixes,
++ ) {
++ nativeImageClasspath(files(extraPlugin.jar))
++ }
++ }
++
+ // Tests
+ pluginsRuntime(libs.kotlinx.serialization.core)
+ pluginsRuntime(composeRuntime())
+@@ -129,7 +192,9 @@ sourceSets {
+
+ val graalLauncher = javaToolchains.launcherFor {
+ languageVersion.set(JavaLanguageVersion.of(JdkMajorVersion.JDK_25_0.targetName))
+- vendor.set(JvmVendorSpec.GRAAL_VM)
++ if (graalVmHome == null) {
++ vendor.set(JvmVendorSpec.GRAAL_VM)
++ }
+ }
+
+ projectTests {
+@@ -218,15 +283,46 @@ val generateBundledPluginsInfo = tasks.register("generateBundledPluginsInfo") {
+ }
+ }
+
++// PluginCliParser creates the registrar and the command line processor with Class.forName().newInstance().
++val generateBundledPluginsMetadata = tasks.register("generateBundledPluginsMetadata") {
++ description = "Generates reflection metadata for the constructors of the bundled compiler plugins"
++
++ val outputDir = layout.buildDirectory.dir("bundled-plugins-metadata")
++ val pluginClasses = bundledCompilerPlugins.flatMap { listOfNotNull(it.registrarFqName, it.commandLineProcessorFqName) }
++ inputs.property("pluginClasses", pluginClasses)
++ outputs.dir(outputDir)
++ doLast {
++ val file = outputDir.get().file("META-INF/native-image/bundled-compiler-plugins/reachability-metadata.json").asFile
++ file.parentFile.mkdirs()
++ val entries = pluginClasses.joinToString(",\n") { className ->
++ """ {"type": "$className", "methods": [{"name": "<init>", "parameterTypes": []}]}"""
++ }
++ file.writeText("{\n \"reflection\": [\n$entries\n ]\n}\n")
++ }
++}
++
+ val kotlincNativeImageTask = tasks.register<Exec>("kotlincNativeImage") {
+ description = "Build a native image of the kotlin-compiler-embeddable"
+
+ val launcher = graalLauncher
+ val resources = layout.projectDirectory.dir("resources")
+- val classpathFiles = files(nativeImageClasspath, resources)
+- inputs.files(nativeImageClasspath, resources, launcher.map { it.metadata.installationPath.asFile })
++ val classpathFiles = files(nativeImageClasspath, resources, generateBundledPluginsMetadata)
++ val graalVmHomeDir = graalVmHome?.let { file(it) }
++ inputs.files(nativeImageClasspath, resources, generateBundledPluginsMetadata)
+ .withNormalizer(ClasspathNormalizer::class)
+ .withPropertyName("nativeImageClasspath")
++ val graalVmRelease: Any = graalVmHomeDir?.resolve("release")
++ ?: launcher.map { it.metadata.installationPath.file("release") }
++ inputs.file(graalVmRelease).withPropertyName("graalVmRelease")
++ inputs.files(listOfNotNull(pgoProfile)).withPropertyName("pgoProfile")
++
++ val optimizationArgs = buildList {
++ if (useG1Gc) add("--gc=G1")
++ if (pgoProfile != null) add("--pgo=$pgoProfile")
++ if (pgoInstrument) add("--pgo-instrument")
++ addAll(extraNativeImageArgs)
++ }
++ inputs.property("optimizationArgs", optimizationArgs)
+
+ val isWindows = currentOs.isWindows
+ val mainClass = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
+@@ -238,7 +334,7 @@ val kotlincNativeImageTask = tasks.register<Exec>("kotlincNativeImage") {
+ outputs.file(executableFile)
+
+ doFirst {
+- val javaHome = launcher.get().executablePath.asFile.toPath().parent.parent
++ val javaHome = graalVmHomeDir?.toPath() ?: launcher.get().executablePath.asFile.toPath().parent.parent
+
+ val nativeImageName = if (isWindows) "native-image.exe" else "native-image"
+ val nativeImageBin = javaHome.resolve("lib/svm/bin/$nativeImageName")
+@@ -256,6 +352,7 @@ val kotlincNativeImageTask = tasks.register<Exec>("kotlincNativeImage") {
+ "-H:+AddAllCharsets",
+ "-H:+UnlockExperimentalVMOptions",
+ "-H:+AllowJRTFileSystem",
++ *optimizationArgs.toTypedArray(),
+ "-cp", fullClasspath,
+ "-o", outputFile.get().asFile.absolutePath,
+ mainClass,
+@@ -389,6 +486,14 @@ fun Test.useReachabilityMetadataResources() {
+ )
+ }
+
++data class ExtraCompilerPluginInfo(
++ val pluginId: String,
++ val registrarFqName: String,
++ val commandLineProcessorFqName: String?,
++ val jarPrefixes: List<String>,
++ val jar: File,
++)
++
+ data class BundledCompilerPluginInfo(
+ val pluginId: String,
+ val registrarFqName: String,
+diff --git i/prepare/compiler-native-image/resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json w/prepare/compiler-native-image/resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json
+index 55c4017a8..17bfbb292 100644
+--- i/prepare/compiler-native-image/resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json
++++ w/prepare/compiler-native-image/resources/META-INF/native-image/org/jetbrains/kotlin/kotlin-compiler-embeddable/reachability-metadata.json
+@@ -18,6 +18,24 @@
+ }
+ ]
+ },
++ {
++ "type": "com.opera.EmptyToStringCommandLineProcessor",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
++ {
++ "type": "com.opera.EmptyToStringCompilePluginRegistrar",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
+ {
+ "type": "gnu.trove.TObjectHashingStrategy"
+ },
+@@ -91,6 +109,9 @@
+ {
+ "type": "java.lang.Object"
+ },
++ {
++ "type": "java.lang.Object[]"
++ },
+ {
+ "type": "java.lang.Runtime",
+ "methods": [
+@@ -173,6 +194,9 @@
+ }
+ ]
+ },
++ {
++ "type": "java.util.ResourceBundle"
++ },
+ {
+ "type": "java.util.Set"
+ },
+@@ -382,6 +406,9 @@
+ {
+ "type": "kotlinx.coroutines.scheduling.CoroutineScheduler"
+ },
++ {
++ "type": "org.jetbrains.kotlin.KtPsiSourceElement"
++ },
+ {
+ "type": "org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor",
+ "methods": [
+@@ -448,6 +475,9 @@
+ "type": "org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments",
+ "allDeclaredMethods": true
+ },
++ {
++ "type": "org.jetbrains.kotlin.cli.common.arguments.RemovedCompilerArguments"
++ },
+ {
+ "type": "org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings",
+ "fields": [
+@@ -485,6 +515,9 @@
+ }
+ ]
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.openapi.editor.event.DocumentListener[]"
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.openapi.extensions.impl.ExtensionPointImpl"
+ },
+@@ -503,9 +536,45 @@
+ }
+ ]
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.openapi.vfs.transformer.TextPresentationTransformers",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.JavaModuleSystem"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.JavaResolveResult[]"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.LanguageSubstitutors",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.psi.PsiElementFinder"
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.PsiMethod[]"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.PsiRecordComponent[]"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.SingleRootFileViewProvider"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.augment.PsiAugmentProvider"
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.psi.compiled.ClassFileDecompilers",
+ "methods": [
+@@ -518,15 +587,39 @@
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.psi.compiled.ClassFileDecompilers$Decompiler"
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.impl.JavaClassSupersImpl",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.impl.cache.TypeInfo[]"
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.psi.impl.source.JavaFileElementType"
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.psi.stubs.StubBase"
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.util.KeyedLazyInstanceEP"
+ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.util.QueryExecutor"
+ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.util.containers.ConcurrentIntObjectHashMap"
++ },
++ {
++ "type": "org.jetbrains.kotlin.com.intellij.util.containers.ConcurrentIntObjectHashMap$CounterCell"
++ },
+ {
+ "type": "org.jetbrains.kotlin.com.intellij.util.containers.ConcurrentLongObjectHashMap",
+ "fields": [
+@@ -603,6 +696,9 @@
+ {
+ "type": "org.jetbrains.kotlin.load.java.JavaIncompatibilityRulesOverridabilityCondition"
+ },
++ {
++ "type": "org.jetbrains.kotlin.load.java.structure.impl.JavaClassifierTypeImpl"
++ },
+ {
+ "type": "org.jetbrains.kotlin.load.java.structure.impl.source.JavaFixedElementSourceFactory",
+ "methods": [
+@@ -648,6 +744,24 @@
+ }
+ ]
+ },
++ {
++ "type": "org.jetbrains.kotlin.parcelize.ParcelizeCommandLineProcessor",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
++ {
++ "type": "org.jetbrains.kotlin.parcelize.ParcelizeComponentRegistrar",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
++ }
++ ]
++ },
+ {
+ "type": "org.jetbrains.kotlin.powerassert.PowerAssertCommandLineProcessor",
+ "methods": [
+@@ -1515,11 +1629,98 @@
+ }
+ ],
+ "methods": [
++ {
++ "name": "arrayBaseOffset",
++ "parameterTypes": [
++ "java.lang.Class"
++ ]
++ },
++ {
++ "name": "arrayIndexScale",
++ "parameterTypes": [
++ "java.lang.Class"
++ ]
++ },
++ {
++ "name": "compareAndSwapInt",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "int",
++ "int"
++ ]
++ },
++ {
++ "name": "compareAndSwapLong",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "long",
++ "long"
++ ]
++ },
++ {
++ "name": "compareAndSwapObject",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "java.lang.Object",
++ "java.lang.Object"
++ ]
++ },
++ {
++ "name": "copyMemory",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "java.lang.Object",
++ "long",
++ "long"
++ ]
++ },
++ {
++ "name": "getAndAddInt",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "int"
++ ]
++ },
++ {
++ "name": "getObjectVolatile",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long"
++ ]
++ },
+ {
+ "name": "invokeCleaner",
+ "parameterTypes": [
+ "java.nio.ByteBuffer"
+ ]
++ },
++ {
++ "name": "objectFieldOffset",
++ "parameterTypes": [
++ "java.lang.reflect.Field"
++ ]
++ },
++ {
++ "name": "putObjectVolatile",
++ "parameterTypes": [
++ "java.lang.Object",
++ "long",
++ "java.lang.Object"
++ ]
++ }
++ ]
++ },
++ {
++ "type": "sun.net.www.protocol.jar.Handler",
++ "methods": [
++ {
++ "name": "<init>",
++ "parameterTypes": []
+ }
+ ]
+ },
+@@ -1603,11 +1804,47 @@
+ {
+ "glob": "kotlin/reflect/reflect.kotlin_builtins"
+ },
++ {
++ "glob": "localization/messages/JavaPsiBundle.properties"
++ },
++ {
++ "glob": "localization/messages/JavaPsiBundle_en.properties"
++ },
++ {
++ "glob": "localization/messages/JavaPsiBundle_en_US.properties"
++ },
++ {
++ "glob": "messages/JavaPsiBundle.properties"
++ },
++ {
++ "glob": "org/jetbrains/kotlin/org/fusesource/jansi/internal/native/Linux/x86_64/libjansi.so"
++ },
+ {
+ "glob": "org/jetbrains/kotlin/org/fusesource/jansi/internal/native/Mac/arm64/libjansi.jnilib"
+ },
+ {
+ "glob": "org/jetbrains/kotlin/org/fusesource/jansi/jansi.properties"
++ },
++ {
++ "bundle": "localization//messages/JavaPsiBundle"
++ },
++ {
++ "bundle": "messages.CoreBundle"
++ },
++ {
++ "bundle": "messages.CoreDeprecatedMessagesBundle"
++ },
++ {
++ "bundle": "messages.JavaErrorBundle"
++ },
++ {
++ "bundle": "messages.JavaOptionBundle"
++ },
++ {
++ "bundle": "messages.JavaPsiBundle"
++ },
++ {
++ "bundle": "messages.UtilBundle"
+ }
+ ]
+-}
++}
+\ No newline at end of file