diff --git a/compiler/build-tools/kotlin-build-tools-api/api/kotlin-build-tools-api.api b/compiler/build-tools/kotlin-build-tools-api/api/kotlin-build-tools-api.api index ca7a66f553f..4abc73abcb5 100644 --- a/compiler/build-tools/kotlin-build-tools-api/api/kotlin-build-tools-api.api +++ b/compiler/build-tools/kotlin-build-tools-api/api/kotlin-build-tools-api.api @@ -1,13 +1,21 @@ public abstract interface class org/jetbrains/kotlin/buildtools/api/CompilationService { public static final field Companion Lorg/jetbrains/kotlin/buildtools/api/CompilationService$Companion; - public abstract fun compile ()V + public abstract fun calculateClasspathSnapshot (Ljava/io/File;)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathEntrySnapshot; + public abstract fun compileJvm (Lorg/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration;Lorg/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration;Ljava/util/List;Ljava/util/List;)V public static fun loadImplementation (Ljava/lang/ClassLoader;)Lorg/jetbrains/kotlin/buildtools/api/CompilationService; + public abstract fun makeCompilerExecutionStrategyConfiguration ()Lorg/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration; + public abstract fun makeJvmCompilationConfiguration ()Lorg/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration; } public final class org/jetbrains/kotlin/buildtools/api/CompilationService$Companion { public final fun loadImplementation (Ljava/lang/ClassLoader;)Lorg/jetbrains/kotlin/buildtools/api/CompilationService; } +public abstract interface class org/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration { + public abstract fun useDaemonStrategy (Ljava/io/File;Ljava/util/List;)Lorg/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration; + public abstract fun useInProcessStrategy ()Lorg/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration; +} + public abstract interface class org/jetbrains/kotlin/buildtools/api/KotlinLogger { public abstract fun debug (Ljava/lang/String;)V public abstract fun error (Ljava/lang/String;Ljava/lang/Throwable;)V @@ -22,3 +30,71 @@ public final class org/jetbrains/kotlin/buildtools/api/SharedApiClassesClassLoad public static final fun newInstance ()Ljava/lang/ClassLoader; } +public abstract interface class org/jetbrains/kotlin/buildtools/api/SourcesChanges { +} + +public final class org/jetbrains/kotlin/buildtools/api/SourcesChanges$Known : org/jetbrains/kotlin/buildtools/api/SourcesChanges { + public fun (Ljava/util/List;Ljava/util/List;)V + public final fun getModifiedFiles ()Ljava/util/List; + public final fun getRemovedFiles ()Ljava/util/List; +} + +public final class org/jetbrains/kotlin/buildtools/api/SourcesChanges$ToBeCalculated : org/jetbrains/kotlin/buildtools/api/SourcesChanges { + public static final field INSTANCE Lorg/jetbrains/kotlin/buildtools/api/SourcesChanges$ToBeCalculated; +} + +public final class org/jetbrains/kotlin/buildtools/api/SourcesChanges$Unknown : org/jetbrains/kotlin/buildtools/api/SourcesChanges { + public static final field INSTANCE Lorg/jetbrains/kotlin/buildtools/api/SourcesChanges$Unknown; +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/ClassSnapshot { +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/ClasspathEntrySnapshot { + public abstract fun getClassSnapshots ()Ljava/util/LinkedHashMap; + public abstract fun saveSnapshot (Ljava/io/File;)V +} + +public final class org/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalCompilationApproachParameters : org/jetbrains/kotlin/buildtools/api/jvm/IncrementalCompilationApproachParameters { + public fun (Ljava/util/List;Ljava/io/File;)V + public final fun getNewClasspathSnapshotFiles ()Ljava/util/List; + public final fun getShrunkClasspathSnapshot ()Ljava/io/File; +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration : org/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration { + public abstract fun assureNoClasspathSnapshotsChanges (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public static synthetic fun assureNoClasspathSnapshotsChanges$default (Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration;ZILjava/lang/Object;)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public abstract fun forceNonIncrementalMode (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public abstract fun getAssuredNoClasspathSnapshotsChanges ()Z + public abstract fun keepIncrementalCompilationCachesInMemory (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public abstract fun usePreciseCompilationResultsBackup (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public abstract fun usePreciseJavaTracking (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public abstract fun useProjectDir (Ljava/io/File;)Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/IncrementalCompilationApproachParameters { +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration { + public abstract fun forceNonIncrementalMode (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; + public static synthetic fun forceNonIncrementalMode$default (Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration;ZILjava/lang/Object;)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; + public abstract fun getForcedNonIncrementalMode ()Z + public abstract fun getIncrementalCompilationCachesKeptInMemory ()Z + public abstract fun getPreciseCompilationResultsBackupEnabled ()Z + public abstract fun getPreciseJavaTrackingEnabled ()Z + public abstract fun getProjectDir ()Ljava/io/File; + public abstract fun keepIncrementalCompilationCachesInMemory (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; + public abstract fun usePreciseCompilationResultsBackup (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; + public abstract fun usePreciseJavaTracking (Z)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; + public abstract fun useProjectDir (Ljava/io/File;)Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration; +} + +public abstract interface class org/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration { + public abstract fun getKotlinScriptFilenameExtensions ()Ljava/util/Set; + public abstract fun getLogger ()Lorg/jetbrains/kotlin/buildtools/api/KotlinLogger; + public abstract fun makeClasspathSnapshotBasedIncrementalCompilationConfiguration ()Lorg/jetbrains/kotlin/buildtools/api/jvm/ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration; + public fun useIncrementalCompilation (Ljava/io/File;Lorg/jetbrains/kotlin/buildtools/api/SourcesChanges;Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalCompilationApproachParameters;Lorg/jetbrains/kotlin/buildtools/api/jvm/IncrementalJvmCompilationConfiguration;)V + public abstract fun useKotlinScriptFilenameExtensions (Ljava/util/Collection;)Lorg/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration; + public abstract fun useLogger (Lorg/jetbrains/kotlin/buildtools/api/KotlinLogger;)Lorg/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration; +} + diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilationService.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilationService.kt index 5b2cf758d16..fff42737669 100644 --- a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilationService.kt +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilationService.kt @@ -5,12 +5,54 @@ package org.jetbrains.kotlin.buildtools.api +import org.jetbrains.kotlin.buildtools.api.jvm.ClasspathEntrySnapshot +import org.jetbrains.kotlin.buildtools.api.jvm.JvmCompilationConfiguration +import java.io.File + /** - * A facade for invoking compilation in Kotlin compiler. It allows to use compiler in different modes. - * TODO: add a mention where to see the available modes after implementing them + * A facade for invoking compilation and related stuff (such as [calculateClasspathSnapshot]) in Kotlin compiler. + * + * An example of the basic usage is: + * ``` + * val service = CompilationService.loadImplementation(ClassLoader.getSystemClassLoader()) + * val executionConfig = service.makeCompilerExecutionStrategyConfiguration() + * val compilationConfig = service.makeJvmCompilationConfiguration() + * service.compileJvm(executionConfig, compilationConfig, listOf(File("src/a.kt")), listOf("-Xexplicit-api=strict")) + * ``` + * + * This interface is not intended to be implemented by the API consumers. An instance of [CompilationService] is expected to be obtained from [loadImplementation]. */ public interface CompilationService { - public fun compile() + /** + * TODO KT-57565 + */ + public fun calculateClasspathSnapshot(classpathEntry: File): ClasspathEntrySnapshot + + /** + * Provides a default [CompilerExecutionStrategyConfiguration] allowing to use it as is or customizing for specific requirements. + * Could be used as an overview to default values of the options (as they are implementation-specific). + */ + public fun makeCompilerExecutionStrategyConfiguration(): CompilerExecutionStrategyConfiguration + + /** + * Provides a default [CompilerExecutionStrategyConfiguration] allowing to use it as is or customizing for specific requirements. + * Could be used as an overview to default values of the options (as they are implementation-specific). + */ + public fun makeJvmCompilationConfiguration(): JvmCompilationConfiguration + + /** + * Compiles Kotlin code targeting JVM platform and using specified options. + * @param strategyConfig an instance of [CompilerExecutionStrategyConfiguration] initially obtained from [makeCompilerExecutionStrategyConfiguration] + * @param compilationConfig an instance of [JvmCompilationConfiguration] initially obtained from [makeJvmCompilationConfiguration] + * @param sources a list of all sources of the compilation unit + * @param arguments a list of Kotlin JVM compiler arguments + */ + public fun compileJvm( + strategyConfig: CompilerExecutionStrategyConfiguration, + compilationConfig: JvmCompilationConfiguration, + sources: List, + arguments: List + ) public companion object { @JvmStatic diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration.kt new file mode 100644 index 00000000000..e4da82ae0fd --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/CompilerExecutionStrategyConfiguration.kt @@ -0,0 +1,32 @@ +/* + * 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.buildtools.api + +import java.io.File + +/** + * Allows users to customize the compiler execution strategy. + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface CompilerExecutionStrategyConfiguration { + /** + * Marks the compilation to be run inside the same JVM as the caller. + * The default strategy. + */ + public fun useInProcessStrategy(): CompilerExecutionStrategyConfiguration + + /** + * Marks the compilation to be run in Kotlin daemon launched as a separate process and shared across similar compilation requests. + * See Kotlin daemon documentation here: https://kotlinlang.org/docs/gradle-compilation-and-caches.html#the-kotlin-daemon-and-how-to-use-it-with-gradle + * @param sessionDir a directory for storing data related to the daemon work session (should not be shared with other compilations) + * @param jvmArguments a list of JVM startup arguments for the daemon + */ + public fun useDaemonStrategy( + sessionDir: File, + jvmArguments: List, + ): CompilerExecutionStrategyConfiguration +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/KotlinLogger.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/KotlinLogger.kt index d35693037ab..6283e116f4e 100644 --- a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/KotlinLogger.kt +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/KotlinLogger.kt @@ -5,6 +5,9 @@ package org.jetbrains.kotlin.buildtools.api +/** + * A logger object used to log messages from the Build Tools API. + */ public interface KotlinLogger { public val isDebugEnabled: Boolean public fun error(msg: String, throwable: Throwable? = null) diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/SourcesChanges.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/SourcesChanges.kt new file mode 100644 index 00000000000..63a40878197 --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/SourcesChanges.kt @@ -0,0 +1,33 @@ +/* + * 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.buildtools.api + +import java.io.File + +/** + * A hierarchy representing source files changes for incremental compilation + */ +public sealed interface SourcesChanges { + /** + * A marker object stating that the API consumer cannot calculate changes (either because it's an initial build or for some other reason). + * The Build Tools API will not enable its source file changes detector in this mode, expecting the API consumer to provide file changes as [Known] for the consequent builds. + */ + public object Unknown : SourcesChanges + + /** + * A marker object stating that the API consumer is not capable of calculating source file changes. + * In this mode, the Build Tools API will enable its source file changes detector and detect changes itself. + */ + public object ToBeCalculated : SourcesChanges + + /** + * A class containing [modifiedFiles] and [removedFiles] calculated from source file changes by the API consumer. + */ + public class Known( + public val modifiedFiles: List, + public val removedFiles: List, + ) : SourcesChanges +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/ClasspathEntrySnapshot.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/ClasspathEntrySnapshot.kt new file mode 100644 index 00000000000..f62d3b4a1cb --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/ClasspathEntrySnapshot.kt @@ -0,0 +1,28 @@ +/* + * 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.buildtools.api.jvm + +import java.io.File + +/** + * TODO add docs KT-57565 + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface ClasspathEntrySnapshot { + public val classSnapshots: LinkedHashMap + + public fun saveSnapshot(path: File) +} + +/** + * TODO add docs KT-57565 + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface ClassSnapshot { + // ... TODO: KT-57565, it will expose some part of org.jetbrains.kotlin.incremental.classpathDiff.ClassSnapshot hierarchy +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration.kt b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration.kt new file mode 100644 index 00000000000..4ce005bca16 --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-api/src/main/kotlin/org/jetbrains/kotlin/buildtools/api/jvm/JvmCompilationConfiguration.kt @@ -0,0 +1,199 @@ +/* + * 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.buildtools.api.jvm + +import org.jetbrains.kotlin.buildtools.api.KotlinLogger +import org.jetbrains.kotlin.buildtools.api.SourcesChanges +import java.io.File + +/** + * Allows users to customize the compilation process and to observe the configured settings (or the default ones). + * + * This interface defines a set of properties and methods that allow users to customize the compilation process. + * It provides control over various aspects of compilation, such as incremental compilation, logging customization and other. + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface JvmCompilationConfiguration { + /** + * A logger used during the compilation. + * + * Managed by [useLogger] + * Default logger is a logger just printing messages to stdin and stderr. + */ + public val logger: KotlinLogger + + /** + * @see [JvmCompilationConfiguration.logger] + */ + public fun useLogger(logger: KotlinLogger): JvmCompilationConfiguration + + /** + * A set of additional to the `.kt` and `.kts` Kotlin script extensions. + * + * Managed by [useKotlinScriptFilenameExtensions] + * Default value is an empty set. + */ + public val kotlinScriptFilenameExtensions: Set + + /** + * @see [JvmCompilationConfiguration.kotlinScriptFilenameExtensions] + */ + public fun useKotlinScriptFilenameExtensions(kotlinScriptExtensions: Collection): JvmCompilationConfiguration + + /** + * Provides a default [ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration] allowing to use it as is or customizing for specific requirements. + * Could be used as an overview to default values of the options (as they are implementation-specific). + * @see [useIncrementalCompilation] + */ + public fun makeClasspathSnapshotBasedIncrementalCompilationConfiguration(): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + /** + * Configures usage of incremental compilation. + * @param workingDirectory a working directory for incremental compilation internal state + * @param sourcesChanges an instance of [SourcesChanges] + * @param approachParameters an object representing mandatory parameters specific for the selected incremental compilation approach + * @param options an object representing optional parameters and handles specific for the selected incremental compilation approach + * @see [makeClasspathSnapshotBasedIncrementalCompilationConfiguration] + */ + public fun

useIncrementalCompilation( + workingDirectory: File, + sourcesChanges: SourcesChanges, + approachParameters: P, + options: IncrementalJvmCompilationConfiguration

, + ) { + error("This version of the Build Tools API does not support incremental compilation") + } +} + +/** + * Allows to observe and customize general JVM incremental compilation settings. + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface IncrementalJvmCompilationConfiguration

{ + /** + * A directory used as a base path for computing relative paths in the incremental compilation caches. + * + * If is not specified, incremental compilation caches will be non-relocatable + * + * Managed by [useProjectDir] + * Default values is `null` + */ + public val projectDir: File? + + /** + * @see [IncrementalJvmCompilationConfiguration.projectDir] + */ + public fun useProjectDir(projectDir: File): IncrementalJvmCompilationConfiguration

+ + /** + * An indicator whether incremental compilation will analyze Java files precisely for better changes detection + * + * Managed by [usePreciseJavaTracking] + * Default value is defined by implementation of the API + */ + public val preciseJavaTrackingEnabled: Boolean + + /** + * @see [IncrementalJvmCompilationConfiguration.preciseJavaTrackingEnabled] + */ + public fun usePreciseJavaTracking(value: Boolean): IncrementalJvmCompilationConfiguration

+ + /** + * An indicator whether incremental compilation should perform file-by-file backup of files and revert them in the case of a failure + * + * Managed by [usePreciseCompilationResultsBackup] + * Default value is defined by implementation of the API + */ + public val preciseCompilationResultsBackupEnabled: Boolean + + /** + * @see [IncrementalJvmCompilationConfiguration.preciseCompilationResultsBackupEnabled] + */ + public fun usePreciseCompilationResultsBackup(value: Boolean): IncrementalJvmCompilationConfiguration

+ + /** + * Incremental compilation uses the PersistentHashMap of the intellij platform for storing caches. + * An indicator whether the changes should remain in memory and not being flushed to the disk until we could mark the compilation as successful. + * + * Managed by [keepIncrementalCompilationCachesInMemory] + * Default value is defined by implementation of the API + */ + public val incrementalCompilationCachesKeptInMemory: Boolean + + /** + * @see [IncrementalJvmCompilationConfiguration.incrementalCompilationCachesKeptInMemory] + */ + public fun keepIncrementalCompilationCachesInMemory(value: Boolean): IncrementalJvmCompilationConfiguration

+ + /** + * An indicator whether the non-incremental mode of the incremental compiler is forced. + * The non-incremental mode of the incremental compiler means that during the non-incremental compilation + * the compiler will collect enough information to perform the following builds incrementally. + * + * Manager by [forceNonIncrementalMode] + * By default, the compilation is considered incremental + */ + public val forcedNonIncrementalMode: Boolean + + /** + * @see [forcedNonIncrementalMode] + */ + public fun forceNonIncrementalMode(value: Boolean = true): IncrementalJvmCompilationConfiguration

+} + +/** + * Allows to observe and customize JVM incremental compilation settings specific to the classpath snapshots based approach. + * + * This interface is not intended to be implemented by the API consumers. + */ +public interface ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration : + IncrementalJvmCompilationConfiguration { + /** + * An indicator whether classpath snapshots comparing should be avoided. + * Could be used if the check is already performed by the API consumer for the sake of optimization + * + * Managed by [assureNoClasspathSnapshotsChanges] + * By default, the incremental compiler will compare the snapshots itself. + */ + public val assuredNoClasspathSnapshotsChanges: Boolean + + /** + * @see [assuredNoClasspathSnapshotsChanges] + */ + public fun assureNoClasspathSnapshotsChanges(value: Boolean = true): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + override fun useProjectDir(projectDir: File): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + override fun usePreciseJavaTracking(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + override fun usePreciseCompilationResultsBackup(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + override fun keepIncrementalCompilationCachesInMemory(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration + + override fun forceNonIncrementalMode(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration +} + +/** + * Mandatory parameters for an incremental compilation approach + */ +public sealed interface IncrementalCompilationApproachParameters + +/** + * Mandatory parameters of the classpath snapshots based incremental compilation approach + */ +public class ClasspathSnapshotBasedIncrementalCompilationApproachParameters( + /** + * The classpath snapshots files actual at the moment of compilation + */ + public val newClasspathSnapshotFiles: List, + /** + * The shrunk classpath snapshot, a result of the previous compilation. Could point to a non-existent file. + * At the successful end of the compilation, the shrunk version of the [newClasspathSnapshotFiles] will be stored at this path. + */ + public val shrunkClasspathSnapshot: File, +) : IncrementalCompilationApproachParameters \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilationServiceImpl.kt b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilationServiceImpl.kt index fc2ba32c439..bfd58cfe75b 100644 --- a/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilationServiceImpl.kt +++ b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilationServiceImpl.kt @@ -6,9 +6,25 @@ package org.jetbrains.kotlin.buildtools.internal import org.jetbrains.kotlin.buildtools.api.CompilationService +import org.jetbrains.kotlin.buildtools.api.CompilerExecutionStrategyConfiguration +import org.jetbrains.kotlin.buildtools.api.jvm.* +import java.io.File internal class CompilationServiceImpl : CompilationService { - override fun compile() { + override fun calculateClasspathSnapshot(classpathEntry: File): ClasspathEntrySnapshot { + TODO("Calculating classpath snapshots via the Build Tools API is not yet implemented: KT-57565") + } + + override fun makeCompilerExecutionStrategyConfiguration() = CompilerExecutionStrategyConfigurationImpl() + + override fun makeJvmCompilationConfiguration() = JvmCompilationConfigurationImpl() + + override fun compileJvm( + strategyConfig: CompilerExecutionStrategyConfiguration, + compilationConfig: JvmCompilationConfiguration, + sources: List, + arguments: List + ) { println("I'm simulating compilation, nothing more yet") } } \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilerExecutionStrategyConfigurationImpl.kt b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilerExecutionStrategyConfigurationImpl.kt new file mode 100644 index 00000000000..41b82a5b176 --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/CompilerExecutionStrategyConfigurationImpl.kt @@ -0,0 +1,19 @@ +/* + * 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.buildtools.internal + +import org.jetbrains.kotlin.buildtools.api.CompilerExecutionStrategyConfiguration +import java.io.File + +class CompilerExecutionStrategyConfigurationImpl : CompilerExecutionStrategyConfiguration { + override fun useInProcessStrategy(): CompilerExecutionStrategyConfiguration { + return this + } + + override fun useDaemonStrategy(sessionDir: File, jvmArguments: List): CompilerExecutionStrategyConfiguration { + TODO("Daemon strategy is not yet supported in the Build Tools API") + } +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/DefaultKotlinLogger.kt b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/DefaultKotlinLogger.kt new file mode 100644 index 00000000000..2c182768b94 --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/DefaultKotlinLogger.kt @@ -0,0 +1,33 @@ +/* + * 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.buildtools.internal + +import org.jetbrains.kotlin.buildtools.api.KotlinLogger + +internal object DefaultKotlinLogger : KotlinLogger { + override val isDebugEnabled: Boolean + get() = TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + + override fun error(msg: String, throwable: Throwable?) { + TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + } + + override fun warn(msg: String) { + TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + } + + override fun info(msg: String) { + TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + } + + override fun debug(msg: String) { + TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + } + + override fun lifecycle(msg: String) { + TODO("Default KotlinLogger is not yet implemented in the Build Tools API") + } +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/JvmCompilationConfigurationImpl.kt b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/JvmCompilationConfigurationImpl.kt new file mode 100644 index 00000000000..be29f295a20 --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-impl/src/main/kotlin/org/jetbrains/kotlin/buildtools/internal/JvmCompilationConfigurationImpl.kt @@ -0,0 +1,104 @@ +/* + * 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.buildtools.internal + +import org.jetbrains.kotlin.buildtools.api.KotlinLogger +import org.jetbrains.kotlin.buildtools.api.SourcesChanges +import org.jetbrains.kotlin.buildtools.api.jvm.* +import java.io.File + +internal class JvmCompilationConfigurationImpl( + override var kotlinScriptFilenameExtensions: Set = emptySet(), + override var logger: KotlinLogger = DefaultKotlinLogger, +) : JvmCompilationConfiguration { + override fun useLogger(logger: KotlinLogger): JvmCompilationConfiguration { + this.logger = logger + return this + } + + override fun useKotlinScriptFilenameExtensions(kotlinScriptExtensions: Collection): JvmCompilationConfiguration { + this.kotlinScriptFilenameExtensions = kotlinScriptExtensions.toSet() + return this + } + + override fun makeClasspathSnapshotBasedIncrementalCompilationConfiguration() = ClasspathSnapshotBasedIncrementalJvmCompilationConfigurationImpl() + + override fun

useIncrementalCompilation( + workingDirectory: File, + sourcesChanges: SourcesChanges, + approachParameters: P, + options: IncrementalJvmCompilationConfiguration

, + ) = TODO("Incremental compilation is not yet supported to run via the Build Tools API") +} + +internal abstract class JvmIncrementalCompilationConfigurationImpl

( + override var preciseJavaTrackingEnabled: Boolean = true, + override var preciseCompilationResultsBackupEnabled: Boolean = false, + override var incrementalCompilationCachesKeptInMemory: Boolean = false, + override var projectDir: File? = null, + override var forcedNonIncrementalMode: Boolean = false, +) : IncrementalJvmCompilationConfiguration

{ + override fun useProjectDir(projectDir: File): IncrementalJvmCompilationConfiguration

{ + this.projectDir = projectDir + return this + } + + override fun usePreciseJavaTracking(value: Boolean): IncrementalJvmCompilationConfiguration

{ + preciseJavaTrackingEnabled = value + return this + } + + override fun usePreciseCompilationResultsBackup(value: Boolean): IncrementalJvmCompilationConfiguration

{ + preciseCompilationResultsBackupEnabled = value + return this + } + + override fun keepIncrementalCompilationCachesInMemory(value: Boolean): IncrementalJvmCompilationConfiguration

{ + incrementalCompilationCachesKeptInMemory = value + return this + } + + override fun forceNonIncrementalMode(value: Boolean): IncrementalJvmCompilationConfiguration

{ + forcedNonIncrementalMode = value + return this + } +} + +internal class ClasspathSnapshotBasedIncrementalJvmCompilationConfigurationImpl( + override var assuredNoClasspathSnapshotsChanges: Boolean = false, +) : + JvmIncrementalCompilationConfigurationImpl(), + ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + override fun useProjectDir(projectDir: File): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + super.useProjectDir(projectDir) + return this + } + + override fun usePreciseJavaTracking(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + super.usePreciseJavaTracking(value) + return this + } + + override fun usePreciseCompilationResultsBackup(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + super.usePreciseCompilationResultsBackup(value) + return this + } + + override fun keepIncrementalCompilationCachesInMemory(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + super.keepIncrementalCompilationCachesInMemory(value) + return this + } + + override fun forceNonIncrementalMode(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + super.forceNonIncrementalMode(value) + return this + } + + override fun assureNoClasspathSnapshotsChanges(value: Boolean): ClasspathSnapshotBasedIncrementalJvmCompilationConfiguration { + assuredNoClasspathSnapshotsChanges = value + return this + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt index e110ffae0af..d2d48c4eec2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/btapi/BuildToolsApiCompilationWork.kt @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.compilerRunner.GradleKotlinCompilerWorkArguments import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService import org.jetbrains.kotlin.gradle.internal.ParentClassLoaderProvider import java.io.File -import java.util.* internal abstract class BuildToolsApiCompilationWork : WorkAction { internal interface BuildToolsApiCompilationParameters : WorkParameters { @@ -36,7 +35,16 @@ internal abstract class BuildToolsApiCompilationWork : WorkAction