Skip to content

Commit

Permalink
Introduce KotlinCompilerArguments
Browse files Browse the repository at this point in the history
and target specific subclasses. This is a simplified version of the
CommonCompilerArguments family.
  • Loading branch information
ting-yuan committed Oct 1, 2022
1 parent d6cf0d3 commit 8782e89
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 171 deletions.
@@ -0,0 +1,93 @@
/*
* 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.
*/

package com.google.devtools.ksp.gradle

import java.io.File

/**
* A simplified version of CommonComilerArguments.
*/
open class KotlinCompilerArguments {
open var freeArgs: List<String> = emptyList()
open var verbose: Boolean = false
open var allWarningsAsErrors: Boolean = false
open var languageVersion: String? = null
open var apiVersion: String? = null
open var useK2: Boolean = false
open var incrementalCompilation: Boolean = false
open var pluginOptions: List<String> = emptyList()
open var pluginClasspaths: List<File> = emptyList()
open var multiPlatform: Boolean = false
open var expectActualLinker: Boolean = false

// A.K.A. friend modules
open var friendPaths: List<File> = emptyList()

// A.K.A. classpath
open var libraries: List<File> = emptyList()

// A.K.A. output, output file
open var destination: File? = null
}

/**
* A simplified version of K2JVMComilerArguments.
*/
class KotlinJvmCompilerArguments : KotlinCompilerArguments() {
var noJdk: Boolean = false
var noStdlib: Boolean = false
var noReflect: Boolean = false
var moduleName: String? = null
var jvmTarget: String? = null
var jdkRelease: String? = null
var allowNoSourceFiles: Boolean = false

var javaSourceRoots: List<File> = emptyList()
}

/**
* A simplified version of K2JSComilerArguments.
*/
class KotlinJsCompilerArguments : KotlinCompilerArguments() {
var noStdlib: Boolean = false
var irOnly: Boolean = false
var irProduceJs: Boolean = false
var irProduceKlibDir: Boolean = false
var irProduceKlibFile: Boolean = false
var irBuildCache: Boolean = false
var wasm: Boolean = false
var target: String = "v5"

override var multiPlatform = true
}

/**
* A simplified version of K2MetadataComilerArguments.
*/
class KotlinMetadataCompilerArguments : KotlinCompilerArguments() {
override var multiPlatform = true
override var expectActualLinker = true
}

/**
* A simplified version of K2NativeComilerArguments.
*/
class KotlinNativeCompilerArguments : KotlinCompilerArguments() {
var target: String? = null
override var multiPlatform = true
}
Expand Up @@ -26,10 +26,6 @@ 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.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2NativeCompilerArguments
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy
import org.jetbrains.kotlin.gradle.utils.newInstance
import java.io.File
Expand All @@ -53,7 +49,7 @@ interface KotlinCompilerRunner {

interface KotlinJvmCompilerRunner : KotlinCompilerRunner {
fun runJvmCompilerAsync(
args: K2JVMCompilerArguments,
args: KotlinJvmCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
Expand All @@ -62,7 +58,7 @@ interface KotlinJvmCompilerRunner : KotlinCompilerRunner {

interface KotlinJsCompilerRunner : KotlinCompilerRunner {
fun runJsCompilerAsync(
args: K2JSCompilerArguments,
args: KotlinJsCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
Expand All @@ -71,7 +67,7 @@ interface KotlinJsCompilerRunner : KotlinCompilerRunner {

interface KotlinMetadataCompilerRunner : KotlinCompilerRunner {
fun runMetadataCompilerAsync(
args: K2MetadataCompilerArguments,
args: KotlinMetadataCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
Expand All @@ -80,7 +76,7 @@ interface KotlinMetadataCompilerRunner : KotlinCompilerRunner {

interface KotlinNativeCompilerRunner : KotlinCompilerRunner {
fun runNativeCompilerAsync(
args: K2NativeCompilerArguments,
args: KotlinNativeCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>,
Expand Down
Expand Up @@ -28,10 +28,12 @@ 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
import org.jetbrains.kotlin.cli.common.arguments.K2NativeCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.isIrBackendEnabled
import org.jetbrains.kotlin.cli.common.arguments.isPreIrBackendDisabled
import org.jetbrains.kotlin.compilerRunner.CompilerExecutionSettings
import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers
Expand All @@ -45,6 +47,9 @@ import org.jetbrains.kotlin.gradle.tasks.GradleCompileTaskProvider
import org.jetbrains.kotlin.gradle.utils.newInstance
import org.jetbrains.kotlin.gradle.utils.propertyWithNewInstance
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.library.impl.isKotlinLibrary
import org.jetbrains.kotlin.utils.JsLibraryUtils
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.io.File
import javax.inject.Inject

Expand Down Expand Up @@ -105,26 +110,56 @@ 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: K2JVMCompilerArguments,
args: KotlinJvmCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, 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()
}

compilerRunner.runJvmCompilerAsync(
sourcesToCompile = sources,
commonSources = commonSources,
javaPackagePrefix = null,
args = args,
args = compilerArgs,
environment = environment,
jdkHome = defaultKotlinJavaToolchain.get().providedJvm.get().javaHome,
null
Expand All @@ -138,16 +173,52 @@ abstract class KotlinJsCompilerRunnerImpl @Inject constructor(
workerExecutor: WorkerExecutor
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinJsCompilerRunner {

private fun libFilter(args: K2JSCompilerArguments, file: File): Boolean =
file.exists() && when {
// JS_IR
args.isIrBackendEnabled() && args.isPreIrBackendDisabled() ->
isKotlinLibrary(file)

// JS_LEGACY
!args.isIrBackendEnabled() && !args.isPreIrBackendDisabled() ->
JsLibraryUtils.isKotlinJavascriptLibrary(file)

// JS_BOTH
args.isIrBackendEnabled() && !args.isPreIrBackendDisabled() ->
isKotlinLibrary(file) && JsLibraryUtils.isKotlinJavascriptLibrary(file)

else -> throw IllegalArgumentException("Cannot determine JS backend.")
}

override fun runJsCompilerAsync(
args: K2JSCompilerArguments,
args: KotlinJsCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
val compilerRunner = prepareCompilerRunner()
val compilerArgs = K2JSCompilerArguments().apply {
copyFrom(args)

outputFile = args.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

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

compilerRunner.runJsCompilerAsync(sources, commonSources, args, environment, null)
compilerRunner.runJsCompilerAsync(sources, commonSources, compilerArgs, environment, null)
}
}

Expand All @@ -158,15 +229,22 @@ abstract class KotlinMetadataCompilerRunnerImpl @Inject constructor(
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinMetadataCompilerRunner {

override fun runMetadataCompilerAsync(
args: K2MetadataCompilerArguments,
args: KotlinMetadataCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>
) {
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
val compilerRunner = prepareCompilerRunner()
val compilerArgs = K2MetadataCompilerArguments().apply {
copyFrom(args)

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

compilerRunner.runMetadataCompilerAsync(sources, commonSources, args, environment)
compilerRunner.runMetadataCompilerAsync(sources, commonSources, compilerArgs, environment)
}
}

Expand All @@ -181,7 +259,7 @@ abstract class KotlinNativeCompilerRunnerImpl @Inject constructor(
.KotlinNativeCompilerRunner.Settings.fromProject(task.project)

override fun runNativeCompilerAsync(
args: K2NativeCompilerArguments,
args: KotlinNativeCompilerArguments,
sources: List<File>,
commonSources: List<File>,
outputs: List<File>,
Expand All @@ -195,19 +273,19 @@ abstract class KotlinNativeCompilerRunnerImpl @Inject constructor(
"-p", "library",
"-Xmulti-platform"
)
args.libraries?.flatMap { listOf("-l", it) }?.let { buildArgs.addAll(it) }
args.friendModules?.let {
args.libraries.flatMap { listOf("-l", it.absolutePath) }.let { buildArgs.addAll(it) }
args.friendPaths.ifNotEmpty {
buildArgs.add("-friend-modules")
buildArgs.add(it)
buildArgs.add(joinToString(File.pathSeparator))
}

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

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

args.languageVersion?.let {
buildArgs.add("-language-version")
Expand Down

0 comments on commit 8782e89

Please sign in to comment.