From 66411a92f066ca58b0bdd7534c3fa15353aca857 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Wed, 19 Apr 2023 19:09:16 +0200 Subject: [PATCH] [Gradle] Cleanup: remove unused classes - CompilationFreeArgsValidator got accidentally unused at some point, and for now we don't see a reason to enable it back. A proper solution includes exposing K/N linker options more explicitly via DSL, which would address the initial problem of passing linker options to compiler accidentally - With ComplationFreeArgsValidator removal and migration of DisabledNativeTargetsReporter to diagnostics infra, AggregateReporter becomes unused so it can be removed as well --- .../native/CompilationFreeArgsValidator.kt | 137 ------------------ .../native/DisabledNativeTargetsReporter.kt | 28 ---- 2 files changed, 165 deletions(-) delete mode 100644 libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/CompilationFreeArgsValidator.kt delete mode 100644 libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/DisabledNativeTargetsReporter.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/CompilationFreeArgsValidator.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/CompilationFreeArgsValidator.kt deleted file mode 100644 index e6c725581d1..00000000000 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/CompilationFreeArgsValidator.kt +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2010-2019 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.gradle.targets.native - -import org.gradle.api.Project -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget -import org.jetbrains.kotlin.gradle.utils.appendLine - -internal object CompilationFreeArgsValidator : AggregateReporter() { - - private const val EXTRA_PROPERTY_NAME = "org.jetbrains.kotlin.native.incorrectFreeArgs" - - private data class IncorrectArgumentsReport(val compilation: KotlinNativeCompilation, val incorrectArgs: List) { - val target: KotlinNativeTarget - get() = compilation.target - - val project: Project - get() = target.project - } - - private fun String.startsWithIncorrectPrefix() = incorrectArgPrefixes.any { startsWith(it) } - private fun String.disablesDevirtualization() = startsWith("-Xdisable-phases=") && contains("Devirtualization") - - private fun Project.getOrRegisterIncorrectArguments(): MutableList = - getOrRegisterData(this, EXTRA_PROPERTY_NAME) - - private fun String.withIndent(indent: Int) = prependIndent(" ".repeat(indent)) - - fun validate(compilation: KotlinNativeCompilation) { - val incorrectArgs = compilation.kotlinOptions.freeCompilerArgs.filter { arg -> - arg.startsWithIncorrectPrefix() || arg.disablesDevirtualization() - } - if (incorrectArgs.isNotEmpty()) { - compilation.target.project - .getOrRegisterIncorrectArguments() - .add(IncorrectArgumentsReport(compilation, incorrectArgs)) - } - } - - /** - * The message format is the following: - * - * The following free compiler arguments must be specified for a binary instead of a compilation: - * * In project ':': - * * In target 'macos': - * Compilation: 'main', arguments: [-opt] - * * In project ':test': - * * In target 'macos': - * Compilation: 'main', arguments: [-opt, -g] - * Compilation: 'test', arguments: [-opt, -g, -Xdisable-phases=Devirtualization,BuildDFG] - */ - override fun printWarning(project: Project) { - // filterIsInstance helps against potential class loaders conflict or misconfiguration. - @Suppress("UselessCallOnCollection") - val incorrectArgs = project - .getOrRegisterIncorrectArguments() - .filterIsInstance() - .groupBy { it.project } - .toSortedMap(compareBy { it.path }) - - if (incorrectArgs.isEmpty()) { - return - } - - val message = buildString { - appendLine() - appendLine("The following free compiler arguments must be specified for a binary instead of a compilation:") - incorrectArgs.forEach { (project, reports) -> - appendLine("* In project '${project.path}':".withIndent(1)) - val groupedReports = reports - .groupBy { it.target } - .toSortedMap(compareBy { it.name }) - groupedReports.forEach { (target, reports) -> - appendLine("* In target '${target.name}':".withIndent(2)) - reports.forEach { - appendLine( - "* Compilation: '${it.compilation.name}', arguments: [${it.incorrectArgs.joinToString()}]".withIndent(3) - ) - } - } - } - appendLine() - appendLine( - """ - Please move them into final binary declarations. E.g. binaries.executable { freeCompilerArgs += "..." } - See more about final binaries: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-final-native-binaries. - """.trimIndent() - ) - } - project.logger.warn(message) - } - - private val incorrectArgPrefixes = listOf( - // Optimizations/debug info. - "-opt", - "-g", - "-ea", - "-enable-assertions", - // Test runners. - "-trn", - "-generate-no-exit-test-runner", - "-tr", - "-generate-test-runner", - "-trw", - "-generate-worker-test-runner", - // Linker parameters and entry point. - "-linker-option", - "-linker-options", - "-e", - "-entry", - "-nomain", - // Runtime settings. - "-memory-model", - - // Coverage. - "-Xcoverage", - "-Xcoverage-file", - "-Xlibrary-to-cover", - // Reverse ObjC interop and framework producing. - "-Xembed-bitcode", - "-Xembed-bitcode-marker", - "-Xexport-library", - "-Xframework-import-header", - "-Xobjc-generics", - "-Xstatic-framework", - // Advanced debug info. - "-Xdebug-info-version", - "-Xg0", - // Other. - "-Xinclude", - "-Xruntime" - ) -} diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/DisabledNativeTargetsReporter.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/DisabledNativeTargetsReporter.kt deleted file mode 100644 index 931f1a8d7e1..00000000000 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/DisabledNativeTargetsReporter.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2010-2019 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.gradle.targets.native - -import org.gradle.api.Project -import org.gradle.api.plugins.ExtraPropertiesExtension -import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget -import org.jetbrains.kotlin.gradle.utils.appendLine -import org.jetbrains.kotlin.konan.target.HostManager -import org.jetbrains.kotlin.konan.target.KonanTarget - -internal abstract class AggregateReporter { - @Suppress("UNCHECKED_CAST") - protected fun getOrRegisterData(project: Project, propertyName: String): MutableList = - project.rootProject.extensions.getByType(ExtraPropertiesExtension::class.java).run { - if (!has(propertyName)) { - set(propertyName, mutableListOf()) - project.gradle.taskGraph.whenReady { printWarning(project) } - } - get(propertyName) - } as MutableList - - protected abstract fun printWarning(project: Project) -}