diff --git a/compiler-plugin/build.gradle.kts b/compiler-plugin/build.gradle.kts index 04ea1d47..196cd9f1 100644 --- a/compiler-plugin/build.gradle.kts +++ b/compiler-plugin/build.gradle.kts @@ -3,9 +3,13 @@ */ import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode -import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension import util.configureMetaTasks +plugins { + alias(libs.plugins.conventions.jvm) + alias(libs.plugins.compiler.specific.module) +} + val kotlinVersion: String by extra val rpcVersion: String = libs.versions.kotlinx.rpc.get() @@ -14,17 +18,12 @@ allprojects { version = "$kotlinVersion-$rpcVersion" } -plugins { - alias(libs.plugins.conventions.jvm) - alias(libs.plugins.compiler.specific.module) +kotlin { + explicitApi = ExplicitApiMode.Disabled } -subprojects { - afterEvaluate { - configure { - explicitApi = ExplicitApiMode.Disabled - } - } +dependencies { + compileOnly(libs.kotlin.compiler.embeddable) } configureMetaTasks("cleanTest", "test") diff --git a/compiler-plugin/compiler-plugin-1_7/build.gradle.kts b/compiler-plugin/compiler-plugin-1_7/build.gradle.kts deleted file mode 100644 index df08eab9..00000000 --- a/compiler-plugin/compiler-plugin-1_7/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -plugins { - alias(libs.plugins.conventions.jvm) -} - -dependencies { - compileOnly(libs.kotlin.compiler.embeddable) -} diff --git a/compiler-plugin/compiler-plugin-1_7_2/build.gradle.kts b/compiler-plugin/compiler-plugin-1_7_2/build.gradle.kts deleted file mode 100644 index df08eab9..00000000 --- a/compiler-plugin/compiler-plugin-1_7_2/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -plugins { - alias(libs.plugins.conventions.jvm) -} - -dependencies { - compileOnly(libs.kotlin.compiler.embeddable) -} diff --git a/compiler-plugin/compiler-plugin-1_8/build.gradle.kts b/compiler-plugin/compiler-plugin-1_8/build.gradle.kts deleted file mode 100644 index df08eab9..00000000 --- a/compiler-plugin/compiler-plugin-1_8/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -plugins { - alias(libs.plugins.conventions.jvm) -} - -dependencies { - compileOnly(libs.kotlin.compiler.embeddable) -} diff --git a/compiler-plugin/compiler-plugin-1_9/build.gradle.kts b/compiler-plugin/compiler-plugin-1_9/build.gradle.kts deleted file mode 100644 index df08eab9..00000000 --- a/compiler-plugin/compiler-plugin-1_9/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -plugins { - alias(libs.plugins.conventions.jvm) -} - -dependencies { - compileOnly(libs.kotlin.compiler.embeddable) -} diff --git a/compiler-plugin/compiler-plugin-core/build.gradle.kts b/compiler-plugin/compiler-plugin-core/build.gradle.kts deleted file mode 100644 index df08eab9..00000000 --- a/compiler-plugin/compiler-plugin-core/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -plugins { - alias(libs.plugins.conventions.jvm) -} - -dependencies { - compileOnly(libs.kotlin.compiler.embeddable) -} diff --git a/compiler-plugin/gradle.properties b/compiler-plugin/gradle.properties deleted file mode 100644 index be31bf3c..00000000 --- a/compiler-plugin/gradle.properties +++ /dev/null @@ -1,5 +0,0 @@ -# -# Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -# - -compiler-specific-module=true diff --git a/compiler-plugin/settings.gradle.kts b/compiler-plugin/settings.gradle.kts index b24e1cc2..13d26bde 100644 --- a/compiler-plugin/settings.gradle.kts +++ b/compiler-plugin/settings.gradle.kts @@ -13,7 +13,6 @@ pluginManagement { plugins { id("settings-conventions") - id("compiler-specific-modules") } includeRootAsPublic() diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPluginCore.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/RPCCompilerPluginCore.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPluginCore.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/RPCCompilerPluginCore.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApi.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/VersionSpecificApi.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApi.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/VersionSpecificApi.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/IrUtils.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/IrUtils.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/IrUtils.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/IrUtils.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCDeclarationScanner.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCDeclarationScanner.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCDeclarationScanner.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCDeclarationScanner.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrContext.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrContext.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrContext.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrContext.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrExtension.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrExtension.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrExtension.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrExtension.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrServiceProcessor.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrServiceProcessor.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCIrServiceProcessor.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCIrServiceProcessor.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCStubGenerator.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCStubGenerator.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/RPCStubGenerator.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/RPCStubGenerator.kt diff --git a/compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/ServiceDeclaration.kt b/compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/ServiceDeclaration.kt similarity index 100% rename from compiler-plugin/compiler-plugin-core/src/main/kotlin/kotlinx/rpc/codegen/extension/ServiceDeclaration.kt rename to compiler-plugin/src/main/core/kotlinx/rpc/codegen/extension/ServiceDeclaration.kt diff --git a/compiler-plugin/compiler-plugin-1_9/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt b/compiler-plugin/src/main/latest/kotlinx/rpc/codegen/RPCCompilerPlugin.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_9/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt rename to compiler-plugin/src/main/latest/kotlinx/rpc/codegen/RPCCompilerPlugin.kt diff --git a/compiler-plugin/compiler-plugin-1_9/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt b/compiler-plugin/src/main/latest/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_9/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt rename to compiler-plugin/src/main/latest/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt diff --git a/compiler-plugin/compiler-plugin-1_7/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/compiler-plugin/src/main/resources/latest/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor similarity index 100% rename from compiler-plugin/compiler-plugin-1_7/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor rename to compiler-plugin/src/main/resources/latest/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor diff --git a/compiler-plugin/compiler-plugin-1_8/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar b/compiler-plugin/src/main/resources/latest/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar similarity index 100% rename from compiler-plugin/compiler-plugin-1_8/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar rename to compiler-plugin/src/main/resources/latest/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar diff --git a/compiler-plugin/compiler-plugin-1_7_2/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/compiler-plugin/src/main/resources/v_1_7/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor similarity index 100% rename from compiler-plugin/compiler-plugin-1_7_2/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor rename to compiler-plugin/src/main/resources/v_1_7/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor diff --git a/compiler-plugin/compiler-plugin-1_7/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar b/compiler-plugin/src/main/resources/v_1_7/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar similarity index 100% rename from compiler-plugin/compiler-plugin-1_7/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar rename to compiler-plugin/src/main/resources/v_1_7/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar diff --git a/compiler-plugin/compiler-plugin-1_8/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/compiler-plugin/src/main/resources/v_1_7_2/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor similarity index 100% rename from compiler-plugin/compiler-plugin-1_8/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor rename to compiler-plugin/src/main/resources/v_1_7_2/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor diff --git a/compiler-plugin/compiler-plugin-1_7_2/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar b/compiler-plugin/src/main/resources/v_1_7_2/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar similarity index 100% rename from compiler-plugin/compiler-plugin-1_7_2/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar rename to compiler-plugin/src/main/resources/v_1_7_2/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar diff --git a/compiler-plugin/compiler-plugin-1_9/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/compiler-plugin/src/main/resources/v_1_8/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor similarity index 100% rename from compiler-plugin/compiler-plugin-1_9/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor rename to compiler-plugin/src/main/resources/v_1_8/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor diff --git a/compiler-plugin/compiler-plugin-1_9/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar b/compiler-plugin/src/main/resources/v_1_8/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar similarity index 100% rename from compiler-plugin/compiler-plugin-1_9/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar rename to compiler-plugin/src/main/resources/v_1_8/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar diff --git a/compiler-plugin/compiler-plugin-1_7/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt b/compiler-plugin/src/main/v_1_7/kotlinx/rpc/codegen/RPCCompilerPlugin.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_7/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt rename to compiler-plugin/src/main/v_1_7/kotlinx/rpc/codegen/RPCCompilerPlugin.kt diff --git a/compiler-plugin/compiler-plugin-1_7/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt b/compiler-plugin/src/main/v_1_7/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_7/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt rename to compiler-plugin/src/main/v_1_7/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt diff --git a/compiler-plugin/compiler-plugin-1_7_2/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt b/compiler-plugin/src/main/v_1_7_2/kotlinx/rpc/codegen/RPCCompilerPlugin.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_7_2/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt rename to compiler-plugin/src/main/v_1_7_2/kotlinx/rpc/codegen/RPCCompilerPlugin.kt diff --git a/compiler-plugin/compiler-plugin-1_7_2/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt b/compiler-plugin/src/main/v_1_7_2/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_7_2/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt rename to compiler-plugin/src/main/v_1_7_2/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt diff --git a/compiler-plugin/compiler-plugin-1_8/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt b/compiler-plugin/src/main/v_1_8/kotlinx/rpc/codegen/RPCCompilerPlugin.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_8/src/main/kotlin/kotlinx/rpc/codegen/RPCCompilerPlugin.kt rename to compiler-plugin/src/main/v_1_8/kotlinx/rpc/codegen/RPCCompilerPlugin.kt diff --git a/compiler-plugin/compiler-plugin-1_8/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt b/compiler-plugin/src/main/v_1_8/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt similarity index 100% rename from compiler-plugin/compiler-plugin-1_8/src/main/kotlin/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt rename to compiler-plugin/src/main/v_1_8/kotlinx/rpc/codegen/VersionSpecificApiImpl.kt diff --git a/gradle-conventions/compiler-specific-module/src/main/kotlin/compiler-specific-module.gradle.kts b/gradle-conventions/compiler-specific-module/src/main/kotlin/compiler-specific-module.gradle.kts index ea8138ce..96b8ac19 100644 --- a/gradle-conventions/compiler-specific-module/src/main/kotlin/compiler-specific-module.gradle.kts +++ b/gradle-conventions/compiler-specific-module/src/main/kotlin/compiler-specific-module.gradle.kts @@ -2,77 +2,100 @@ * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet +import java.nio.file.Files +import java.nio.file.Path + +val kotlinVersion: String by extra + +fun NamedDomainObjectContainer.applyCompilerSpecificSourceSets() { + forEach { set -> + val sourceDirs = set.kotlin.sourceDirectories.toList() + val path = sourceDirs.firstOrNull() // one is ok in most cases because we need its parent directory + ?: error( + "Expected at least one source set dir for '${set.name}' source set (kotlin dir). " + + "Review the case and adjust the script" + ) + + val sourceSetPath = path.toPath().parent + if (!Files.exists(sourceSetPath)) { + return@forEach + } -// ### Utils ### + val core = sourceSetPath.resolve(Const.CORE_SOURCE_SET).toFile() + + // version-specific source sets + val vsSets = Files.newDirectoryStream(sourceSetPath).use { it.toList() }.filter { + Files.isDirectory(it) && it.name().matches(directoryNameRegex) + }.map { it.toFile() } + + // choose 'latest' if there are no more specific ones + val mostSpecificApplicable = vsSets.mostSpecificByVersionOrNull(kotlinVersion) + ?: vsSets.singleOrNull { it.name == Const.LATEST_SOURCE_SET } + ?: run { + logger.info("No version specific sources sets, but '${core.name}'") + set.kotlin.setSrcDirs(listOf(core)) // 'core' source set instead of 'kotlin' + set.configureResources(sourceSetPath, core.name) + return@forEach + } -fun capitalize(string: String): String { - if (string.isEmpty()) { - return "" - } - val firstChar = string[0] - return string.replaceFirst(firstChar, Character.toTitleCase(firstChar)) -} + logger.info( + "${project.name}: included version specific source sets: " + + "${core.name}, ${mostSpecificApplicable.name}" + ) -object CSM { - const val KOTLIN_MULTIPLATFORM_PLUGIN_ID = "org.jetbrains.kotlin.multiplatform" - const val KOTLIN_JVM_PLUGIN_ID = "org.jetbrains.kotlin.jvm" -} + set.kotlin.setSrcDirs(listOf(core, mostSpecificApplicable)) // 'core' source set instead of 'kotlin' + set.configureResources(sourceSetPath, core.name, mostSpecificApplicable.name) -val kotlinVersion = getKotlinPluginVersion() + val excluded = vsSets.filter { it != mostSpecificApplicable } + logger.info("${project.name}: excluded version specific source sets: [${excluded.joinToString { it.name }}]") + } +} -// ### Plugin Logic ### -// What happens here, is that root module is being made a complete compile specific module for current version of Kotlin -// We take -core submodule that contains compiler version independent code, add to it version specific module -// and the summary is presented to the outer world as a complete module, -// which is the root module for these two submodules. -// Root module takes its submodules as `api` gradle configurations, and root's jar will consist of -// two submodules' artifacts and will be presented to the world as it was always a one complete module. +fun KotlinSourceSet.configureResources(sourceSetPath: Path, vararg versionNames: String) { + val vsResources = sourceSetPath.resolve(Const.RESOURCES).toFile() + resources.setSrcDirs( + versionNames.map { vsResources.resolve(it) } + ) -val rootProjectPrefix = "$name-" -val coreProjectName = "${rootProjectPrefix}core" + // 'resources' property does not work alone in gradle 7.5.1 with kotlin 1.7.0 (no idea why), + // so we adjust task contents as well + tasks.withType().configureEach { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE -// configurations are available only when kotlin plugin is applied, so we use lazy resolving -fun Project.lazyDependency(baseConfigurationName: String, notation: Any) { - val kmpConfigurationName = "commonMain${capitalize(baseConfigurationName)}" - this.configurations.matching { it.name == kmpConfigurationName }.all { - this@lazyDependency.dependencies.add(kmpConfigurationName, notation) + from(versionNames.map { vsResources.resolve(it) }) + include { it.file.parentInAllowList(versionNames) } } +} - this.configurations.matching { it.name == baseConfigurationName }.all { - this@lazyDependency.dependencies.add(baseConfigurationName, notation) +fun File.parentInAllowList(allowList: Array): Boolean { + val parent = toPath().parent?.toFile() + // will skip v_1_7 for 1.7.0, as it's parent is resources + // but will allow META-INF, as it's parent is v_1_7 + if (parent?.name in allowList) { + return true } -} -fun Project.lazyApi(notation: Any) { - lazyDependency("api", notation) + // allow META-INF contents + return untilAllowedParentOrNull(allowList) != null } -val root = project - -subprojects { - afterEvaluate { - println("Compiler-specific module $name, version: $version") +tailrec fun File.untilAllowedParentOrNull(allowList: Array): File? { + if (name in allowList) { + return null } - when { - name == coreProjectName -> { - root.lazyApi(this) - } - - name.startsWith(rootProjectPrefix) -> { - val semVer = name - .removePrefix(rootProjectPrefix) - .replace('_', '.') + val parent = toPath().parent?.toFile() + return if (parent?.name in allowList) this else parent?.untilAllowedParentOrNull(allowList) +} - // resolve compiler specific submodule, for example compiler-plugin-1_7 for Kotlin version 1.7.22 - if (kotlinVersion.startsWith(semVer)) { - val coreProject = root.subprojects.singleOrNull { it.name == coreProjectName } - ?: error("Expected to find subproject with name '$coreProjectName'") - lazyApi(coreProject) +plugins.withId(Const.KOTLIN_JVM_PLUGIN_ID) { + the().sourceSets.applyCompilerSpecificSourceSets() +} - root.lazyApi(this) - } - } - } +plugins.withId(Const.KOTLIN_MULTIPLATFORM_PLUGIN_ID) { + the().sourceSets.applyCompilerSpecificSourceSets() } + diff --git a/gradle-conventions/compiler-specific-module/src/main/kotlin/dependency.kt b/gradle-conventions/compiler-specific-module/src/main/kotlin/dependency.kt new file mode 100644 index 00000000..3e5338ac --- /dev/null +++ b/gradle-conventions/compiler-specific-module/src/main/kotlin/dependency.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler +import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet + +/** + * Use this to add a specific dependency to a source set, depending on a Kotlin version + */ +fun KotlinSourceSet.vsDependencies( + vsSourceSetDir: String, + configure: KotlinDependencyHandler.() -> Unit, +) { + kotlin.srcDirs.find { it.name == vsSourceSetDir }?.apply { + dependencies(configure) + } +} diff --git a/gradle-conventions/compiler-specific-module/src/main/kotlin/util.kt b/gradle-conventions/compiler-specific-module/src/main/kotlin/util.kt new file mode 100644 index 00000000..cda3e63f --- /dev/null +++ b/gradle-conventions/compiler-specific-module/src/main/kotlin/util.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +import java.nio.file.Path + +object Const { + const val KOTLIN_MULTIPLATFORM_PLUGIN_ID = "org.jetbrains.kotlin.multiplatform" + const val KOTLIN_JVM_PLUGIN_ID = "org.jetbrains.kotlin.jvm" + + const val CORE_SOURCE_SET = "core" + const val LATEST_SOURCE_SET = "latest" + + const val RESOURCES = "resources" +} + +fun capitalize(string: String): String { + if (string.isEmpty()) { + return "" + } + val firstChar = string[0] + return string.replaceFirst(firstChar, Character.toTitleCase(firstChar)) +} + +fun Path.name() = fileName?.toString().orEmpty() diff --git a/gradle-conventions/compiler-specific-module/src/main/kotlin/version.kt b/gradle-conventions/compiler-specific-module/src/main/kotlin/version.kt new file mode 100644 index 00000000..c1d2d3fc --- /dev/null +++ b/gradle-conventions/compiler-specific-module/src/main/kotlin/version.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +import java.io.File + +// Versioning is used to sort version-specific source sets in the 'first comes more specific' order +// By 'more specific' we mean that '1.7.10' is more specific than '1.7'. +// So [1.7, 1.7.10, 1.9.10, 1.7.22, 1.9, 1, 1.7.0, latest, 1.8] +// will be sorted as [1.7.0, 1.7.10, 1.7.22, 1.7, 1.8, 1.9.10, 1.9, 1, latest] +// It's ok to have version '1'. +// For example, we may have '1.7' and '1' specific source sets. +// That would mean that all 1.7.* versions we compile with the '1.7' source set, +// and 1.8.+ up to 1.9.24 will be with the '1' source set +class CompilerModuleVersion(fullName: String, prefix: String) : Comparable { + // For example, "v_1_7_10" -> "1.7.10" + val version = fullName + .removePrefix(prefix) + .replace('_', '.') + + override fun compareTo(other: CompilerModuleVersion): Int { + return when { + version.length == other.version.length -> version.compareTo(other.version) + version.length < other.version.length -> 1 + else -> -1 + } + } +} + +fun Collection.mostSpecificByVersionOrNull(kotlinVersion: String): File? { + return map { it to CompilerModuleVersion(it.name, "v_") } + .sortedBy { (_, semVer) -> semVer } + .firstOrNull { (_, semVer) -> + kotlinVersion.startsWith(semVer.version) + }?.first +} + +// matches: +// - latest +// - v_1 +// - v_1_9 +// - v_1_9_2 +// - v_1_9_24 +val directoryNameRegex = "^(latest|v(_\\d){1,3}\\d?)$".toRegex() diff --git a/gradle-settings-conventions/src/main/kotlin/compiler-specific-modules.settings.gradle.kts b/gradle-settings-conventions/src/main/kotlin/compiler-specific-modules.settings.gradle.kts deleted file mode 100644 index 8b1c7cb7..00000000 --- a/gradle-settings-conventions/src/main/kotlin/compiler-specific-modules.settings.gradle.kts +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. - */ - -object CSM { - const val SETTINGS_FILE = "settings.gradle.kts" - const val BUILD_FILE = "build.gradle.kts" - const val PROPERTIES_FILE = "gradle.properties" - - const val CSM_PROPERTY_NAME = "compiler-specific-module" - - const val KOTLIN_VERSION_EXTRA = "kotlinVersion" -} - -// SemVer is used to sort compiler version specific submodules in the 'first comes more specific' order -// By 'more specific' we mean that '1.7.10' is more specific than '1.7'. -// So [1.7, 1.7.10, 1.9.10, 1.7.22, 1.9, 1, 1.7.0, 1.8] -// will be sorted as [1.7.0, 1.7.10, 1.7.22, 1.7, 1.8, 1.9.10, 1.9, 1] -// It's ok to have version '1'. For example, we may have '1.7' and '1' specific modules. -// That would mean, that all 1.7.* versions we compile with '1.7' module, and 1.8.+ up to 1.9.24 will be with '1' module -class CompilerModuleSemVer(fullName: String, prefix: String) : Comparable { - // For example, "compiler-plugin-1_7_10" -> "1.7.10" - val version = fullName - .removePrefix(prefix) - .replace('_', '.') - - override fun compareTo(other: CompilerModuleSemVer): Int { - return when { - version.length == other.version.length -> version.compareTo(other.version) - version.length < other.version.length -> 1 - else -> -1 - } - } -} - -// Passed from the `settings-conventions.settings.gradle.kts` plugin -val kotlinVersion = extra[CSM.KOTLIN_VERSION_EXTRA] as? String - ?: error("Expected `${CSM.KOTLIN_VERSION_EXTRA}` extra property to be provided by `settings-conventions` plugin") - -// dir - root dir for compiler specific module -// files - list of all files in that directory -// -// This function includes exactly two submodules of the root compiler specific module: -// 1. core submodule -// 2. version specific submodule that is the best suitable for the current Kotlin compiler version. -// -// IMPORTANT: it is expected that the root submodule is already included to the project, -// otherwise the exception will be thrown -fun includeCSM(dir: File, files: Array) { - val rootProjectName = dir.name - - val submodules = files.filter { - it.isDirectory && it.name.startsWith(rootProjectName) - }.toMutableSet() - - val core = submodules.singleOrNull { it.name == "$rootProjectName-core" } - if (core == null) { - error("Compiler Specific Module $rootProjectName should have `-core` module defined") - } - val compilerSubmodules = submodules - core - - val basePath = dir.absoluteFile - .relativeTo(settingsDir.absoluteFile).path - .replace(File.separator, ":") - .takeIf { it.isNotEmpty() }?.let { ":$it" } ?: "" - - includePublic("$basePath:$rootProjectName-core") - - val prefix = "$rootProjectName-" - - val currentCompilerModuleDirName = compilerSubmodules - .map { it.name to CompilerModuleSemVer(it.name, prefix) } - // example after sorted: [1.7.0, 1.7.10, 1.7.22, 1.7, 1.8, 1.9.10, 1.9, 1] - .sortedBy { (_, semVer) -> semVer } - .firstOrNull { (_, semVer) -> - kotlinVersion.startsWith(semVer.version) - }?.first - ?: error(""" - Unable to find compiler specific submodule for $rootProjectName and Kotlin $kotlinVersion. - Available modules: ${compilerSubmodules.joinToString { it.name }} - """.trimIndent()) - - includePublic("$basePath:$currentCompilerModuleDirName") - - gradle.projectsLoaded { - if (rootProject.name == rootProjectName) { - return@projectsLoaded - } - - rootProject.subprojects.find { it.name == rootProjectName } - ?: error("Expected root project '$rootProjectName' to be included to the build manually") - } -} - -// search through all gradle modules for compiler specific ones -// -// IMPORTANT: compiler specific modules MUST NOT have any nested modules other than core and version specific ones, -// otherwise, they will be skipped -settingsDir.walkTopDown().onEnter { dir -> - val isExcluded = dir.name.run { - startsWith(".") || - startsWith("build") || - startsWith("src") || - equals("gradle") || - equals("kotlin-js-store") || - equals("karma") || - equals("gradle") || - equals("sample") - } - - if (isExcluded) { - return@onEnter false - } - - val files = dir.listFiles() ?: return@onEnter false - val propertiesFile = files.singleOrNull { it.name == CSM.PROPERTIES_FILE } - - when { - // standalone projects are excluded unless they are root - files.any { it.name == CSM.SETTINGS_FILE && dir != settingsDir } -> false - - // check if it is a gradle module - propertiesFile != null && files.find { it.name == CSM.BUILD_FILE } != null -> { - val isCompilerSpecificModule = propertiesFile.readLines(Charsets.UTF_8) - .singleOrNull { it.startsWith(CSM.CSM_PROPERTY_NAME) } - ?.substringAfter("=") - ?.substringBefore("#") // avoid comments - ?.trim() - ?.toBoolean() - ?: return@onEnter true // continue traversing - - if (isCompilerSpecificModule) { - includeCSM(dir, files) - } - - // proper CS module or not, there should not be any nested modules - false - } - - else -> true // continue traversing - } -}.toList() diff --git a/runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/callException_json.gold b/krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/callException_json.gold similarity index 100% rename from runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/callException_json.gold rename to krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/callException_json.gold diff --git a/runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_json.gold b/krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_json.gold similarity index 100% rename from runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_json.gold rename to krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_json.gold diff --git a/runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_protobuf.gold b/krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_protobuf.gold similarity index 100% rename from runtime/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_protobuf.gold rename to krpc/krpc-test/src/jvmTest/resources/wire_dumps/0_1_1_SNAPSHOT/echo_protobuf.gold diff --git a/settings.gradle.kts b/settings.gradle.kts index 7e81c0f7..21be36ed 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,7 +28,6 @@ pluginManagement { plugins { id("settings-conventions") - id("compiler-specific-modules") } dependencyResolutionManagement {