Skip to content

Commit

Permalink
Replace KotlinCompilerArguments with CompilerCommonOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ting-yuan committed Oct 11, 2022
1 parent e73051e commit 0bf44d1
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 Google LLC
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")

package com.google.devtools.ksp.gradle

import org.gradle.api.model.ObjectFactory
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptionsDefault
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptionsDefault
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptionsDefault
import org.jetbrains.kotlin.gradle.utils.newInstance

// TODO: to be replaced by KotlinJvmFactory, etc.
class CompilerOptionsFactory {
companion object {
fun createCompilerJvmOptions(objectFactory: ObjectFactory): CompilerJvmOptions =
objectFactory.newInstance<CompilerJvmOptionsDefault>()

fun createCompilerJsOptions(objectFactory: ObjectFactory): CompilerJsOptions =
objectFactory.newInstance<CompilerJsOptionsDefault>()

fun createCompilerCommonOptions(objectFactory: ObjectFactory): CompilerCommonOptions =
objectFactory.newInstance<CompilerCommonOptionsDefault>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Internal
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy
import org.jetbrains.kotlin.gradle.utils.newInstance
import java.io.File
Expand All @@ -49,37 +52,54 @@ interface KotlinCompilerRunner {

interface KotlinJvmCompilerRunner : KotlinCompilerRunner {
fun runJvmCompilerAsync(
args: KotlinJvmCompilerArguments,
options: CompilerJvmOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
)
}

interface KotlinJsCompilerRunner : KotlinCompilerRunner {
fun runJsCompilerAsync(
args: KotlinJsCompilerArguments,
options: CompilerJsOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
)
}

interface KotlinMetadataCompilerRunner : KotlinCompilerRunner {
fun runMetadataCompilerAsync(
args: KotlinMetadataCompilerArguments,
options: CompilerCommonOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
)
}

interface KotlinNativeCompilerRunner : KotlinCompilerRunner {
fun runNativeCompilerAsync(
args: KotlinNativeCompilerArguments,
options: CompilerCommonOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File,
target: String
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.gradle.process.ExecOperations
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporter
import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporterImpl
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
Expand All @@ -52,6 +51,13 @@ import org.jetbrains.kotlin.utils.JsLibraryUtils
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.io.File
import javax.inject.Inject
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptionsDefault
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptionsDefault
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptionsDefault
import org.jetbrains.kotlin.gradle.logging.GradleErrorMessageCollector

internal inline fun <reified T : Any?> ObjectFactory.property() = property(T::class.java)
internal inline fun <reified T : Any?> ObjectFactory.property(initialValue: T) = property<T>().value(initialValue)
Expand Down Expand Up @@ -86,9 +92,10 @@ abstract class KotlinCompilerRunnerImpl @Inject constructor(
@Internal
internal fun prepareEnvironment(allWarningsAsErrors: Boolean, outputs: List<File>): GradleCompilerEnvironment {
val messageCollector = GradlePrintingMessageCollector(GradleKotlinLogger(logger), allWarningsAsErrors)
val errorMessageCollector = GradleErrorMessageCollector(messageCollector)
val outputItemCollector = OutputItemsCollectorImpl()
return GradleCompilerEnvironment(
compilerClasspath.files.toList(), messageCollector, outputItemCollector,
compilerClasspath.files.toList(), errorMessageCollector, outputItemCollector,
reportingSettings = ReportingSettings(),
outputFiles = outputs
)
Expand All @@ -110,49 +117,32 @@ abstract class KotlinCompilerRunnerImpl @Inject constructor(
}
}

internal fun CommonCompilerArguments.copyFrom(args: KotlinCompilerArguments) {
freeArgs = args.freeArgs
verbose = args.verbose
allWarningsAsErrors = args.allWarningsAsErrors
languageVersion = args.languageVersion
apiVersion = args.apiVersion
useK2 = args.useK2
incrementalCompilation = args.incrementalCompilation
pluginOptions = args.pluginOptions.toTypedArray()
pluginClasspaths = args.pluginClasspaths.map { it.absolutePath }.toTypedArray()
expectActualLinker = args.expectActualLinker
multiPlatform = args.multiPlatform
}

abstract class KotlinJvmCompilerRunnerImpl @Inject constructor(
task: Task,
objectFactory: ObjectFactory,
workerExecutor: WorkerExecutor
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinJvmCompilerRunner {

override fun runJvmCompilerAsync(
args: KotlinJvmCompilerArguments,
options: CompilerJvmOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
val compilerRunner = prepareCompilerRunner()
val compilerArgs = K2JVMCompilerArguments().apply {
copyFrom(args)

friendPaths = args.friendPaths.map { it.absolutePath }.toTypedArray()
classpath = args.libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
destination = args.destination?.absolutePath

noJdk = args.noJdk
noStdlib = args.noStdlib
noReflect = args.noReflect
moduleName = args.moduleName
jvmTarget = args.jvmTarget
jdkRelease = args.jdkRelease
allowNoSourceFiles = args.allowNoSourceFiles
javaSourceRoots = args.javaSourceRoots.map { it.absolutePath }.toTypedArray()
options as CompilerJvmOptionsDefault
options.fillCompilerArguments(this)

this@apply.friendPaths = friendPaths.map { it.absolutePath }.toTypedArray()
this@apply.classpath = libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
this@apply.destination = destination.absolutePath
}

compilerRunner.runJvmCompilerAsync(
Expand Down Expand Up @@ -191,30 +181,34 @@ abstract class KotlinJsCompilerRunnerImpl @Inject constructor(
}

override fun runJsCompilerAsync(
args: KotlinJsCompilerArguments,
options: CompilerJsOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
val compilerRunner = prepareCompilerRunner()
val compilerArgs = K2JSCompilerArguments().apply {
copyFrom(args)
options as CompilerJsOptionsDefault
options.fillCompilerArguments(this)

outputFile = args.destination?.absolutePath
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
this@apply.outputFile = destination.absolutePath

noStdlib = args.noStdlib
irOnly = args.irOnly
irProduceJs = args.irProduceJs
irProduceKlibDir = args.irProduceKlibDir
irProduceKlibFile = args.irProduceKlibFile
irBuildCache = args.irBuildCache
wasm = args.wasm
target = args.target
irOnly = this@apply.freeArgs.contains("-Xir-only")
irProduceJs = this@apply.freeArgs.contains("-Xir-produce-js")
irProduceKlibDir = this@apply.freeArgs.contains("-Xir-produce-klib-dir")
irProduceKlibFile = this@apply.freeArgs.contains("-Xir-produce-klib-file")
irBuildCache = this@apply.freeArgs.contains("-Xir-build-cache")
wasm = this@apply.freeArgs.contains("-Xwasm")

friendModules = args.friendPaths.filter { libFilter(this, it) }
this@apply.friendModules = friendPaths.filter { libFilter(this, it) }
.map { it.absolutePath }.joinToString(File.pathSeparator)
libraries = args.libraries.filter { libFilter(this, it) }
this@apply.libraries = libraries.filter { libFilter(this, it) }
.map { it.absolutePath }.joinToString(File.pathSeparator)
}

Expand All @@ -229,19 +223,25 @@ abstract class KotlinMetadataCompilerRunnerImpl @Inject constructor(
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinMetadataCompilerRunner {

override fun runMetadataCompilerAsync(
args: KotlinMetadataCompilerArguments,
options: CompilerCommonOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
val compilerRunner = prepareCompilerRunner()
val compilerArgs = K2MetadataCompilerArguments().apply {
copyFrom(args)
options as CompilerCommonOptionsDefault
options.fillCompilerArguments(this)

friendPaths = args.friendPaths.map { it.absolutePath }.toTypedArray()
classpath = args.libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
destination = args.destination?.absolutePath
this@apply.friendPaths = friendPaths.map { it.absolutePath }.toTypedArray()
this@apply.classpath = libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
this@apply.destination = destination.absolutePath
}

compilerRunner.runMetadataCompilerAsync(sources, commonSources, compilerArgs, environment)
Expand All @@ -259,45 +259,47 @@ abstract class KotlinNativeCompilerRunnerImpl @Inject constructor(
.KotlinNativeCompilerRunner.Settings.fromProject(task.project)

override fun runNativeCompilerAsync(
args: KotlinNativeCompilerArguments,
options: CompilerCommonOptions,
freeArgs: List<String>,
sources: List<File>,
commonSources: List<File>,
friendPaths: List<File>,
libraries: List<File>,
outputs: List<File>,
destination: File,
target: String
) {
val output = File(outputs.first(), "dummy.out")

val target = KonanTarget.predefinedTargets.get(args.target!!)!!
val target = KonanTarget.predefinedTargets.get(target)!!
val buildArgs: MutableList<String> = mutableListOf(
"-o", output.path,
"-target", target.name,
"-p", "library",
"-Xmulti-platform"
)
args.libraries.flatMap { listOf("-l", it.absolutePath) }.let { buildArgs.addAll(it) }
args.friendPaths.ifNotEmpty {
libraries.flatMap { listOf("-l", it.absolutePath) }.let { buildArgs.addAll(it) }
friendPaths.ifNotEmpty {
buildArgs.add("-friend-modules")
buildArgs.add(joinToString(File.pathSeparator))
}

if (args.verbose)
if (options.verbose.get())
buildArgs.add("-verbose")
if (args.allWarningsAsErrors)
if (options.allWarningsAsErrors.get())
buildArgs.add("-Werror")

args.pluginClasspaths.map { "-Xplugin=${it.absolutePath}" }.let { buildArgs.addAll(it) }
args.pluginOptions.flatMap { listOf("-P", it) }.let { buildArgs.addAll(it) }

args.languageVersion?.let {
options.languageVersion.getOrNull()?.let {
buildArgs.add("-language-version")
buildArgs.add(it)
buildArgs.add(it.version)
}
args.apiVersion?.let {
options.apiVersion.getOrNull()?.let {
buildArgs.add("-api-version")
buildArgs.add(it)
buildArgs.add(it.version)
}

buildArgs.addAll(sources.map { it.absolutePath })
buildArgs.addAll(args.freeArgs)
buildArgs.addAll(freeArgs)
buildArgs.addAll(commonSources.map { it.absolutePath })

org.jetbrains.kotlin.compilerRunner.KotlinNativeCompilerRunner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ internal inline fun <reified T : Task> Project.locateTask(name: String): TaskPro
internal fun findJavaTaskForKotlinCompilation(compilation: KotlinCompilation<*>): TaskProvider<out JavaCompile>? =
when (compilation) {
is KotlinJvmAndroidCompilation -> compilation.compileJavaTaskProvider
is KotlinWithJavaCompilation -> compilation.compileJavaTaskProvider
is KotlinWithJavaCompilation<*, *> -> compilation.compileJavaTaskProvider
is KotlinJvmCompilation -> compilation.compileJavaTaskProvider // may be null for Kotlin-only JVM target in MPP
else -> null
}
Expand Down
Loading

0 comments on commit 0bf44d1

Please sign in to comment.