Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running KSP via KotlinCompilerRunner #1119

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/standalone.yml
@@ -0,0 +1,74 @@
# Workflow to run tests

name: standalone compiler runner

on:
# runs on 2:00 AM PST or 3:00 AM PDT
schedule:
- cron: '0 10 * * *'

jobs:
build-and-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
branch: [ main, 1.0.7-release ]

# The type of runner that the job will run on
runs-on: ${{ matrix.os }}

steps:
- name: Setup Java 9
uses: actions/setup-java@v1.4.3
with:
java-version: '9'
java-package: jdk
architecture: x64
- name: set JDK_9 environment variable for kotlin compiler
shell: bash
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
run: echo ::set-env name=JDK_9::$(echo $JAVA_HOME)
- name: Setup Java 11
uses: actions/setup-java@v1.4.3
with:
java-version: '11'
java-package: jdk
architecture: x64

# Checkout
- uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ matrix.branch }}

# Build cache
- name: Cache Gradle Cache
uses: actions/cache@v2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}-${{ hashFiles('**/gradle.properties') }}
# An ordered list of keys to use for restoring the cache if no cache hit occurred for key
restore-keys: |
${{ runner.os }}-gradle-
- name: Cache gradle wrapper
uses: actions/cache@v2
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}

# Run tests
- name: test
shell: bash
run: ./gradlew -Pksp.compiler.runner=standalone --stacktrace --info :integration-tests:test :gradle-plugin:test
- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: test-reports-standalone-${{ matrix.os }}-${{ matrix.branch }}
path: |
compiler-plugin/build/reports
integration-tests/build/reports
gradle-plugin/build/reports
common-util/build/reports
2 changes: 2 additions & 0 deletions gradle-plugin/build.gradle.kts
Expand Up @@ -24,6 +24,7 @@ plugins {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin-api:$kotlinBaseVersion")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinBaseVersion")
implementation("org.jetbrains.kotlin:kotlin-compiler-runner:$kotlinBaseVersion")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KGP has compileOnly dependency on this library as all classes should come from kotlin-compiler-embeddable on runtime. Probably you should do the same

compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinBaseVersion")
// replace AGP dependency w/ gradle-api when we have source registering API available.
compileOnly("com.android.tools.build:gradle:$agpBaseVersion")
Expand Down Expand Up @@ -105,6 +106,7 @@ val writeTestPropsTask = tasks.register<WriteProperties>("prepareTestConfigurati
property("mavenRepoDir", File(rootProject.buildDir, "repos/test").absolutePath)
property("kspProjectRootDir", rootProject.projectDir.absolutePath)
property("processorClasspath", project.tasks["compileTestKotlin"].outputs.files.asPath)
property("kspCompilerRunner", project.properties.getOrDefault("ksp.compiler.runner", "inherited") as String)
}

java {
Expand Down
Expand Up @@ -19,6 +19,7 @@ package com.google.devtools.ksp.gradle
import com.android.build.api.dsl.CommonExtension
import com.android.build.gradle.BaseExtension
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
Expand Down Expand Up @@ -65,7 +66,7 @@ object AndroidPluginIntegration {
fun registerGeneratedJavaSources(
project: Project,
kotlinCompilation: KotlinJvmAndroidCompilation,
kspTaskProvider: TaskProvider<KspTaskJvm>,
kspTaskProvider: TaskProvider<out Task>,
javaOutputDir: File,
classOutputDir: File,
resourcesOutputDir: FileCollection,
Expand Down
@@ -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 =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are targeting Kotlin 1.8.0 - you could already use KotlinJvmFactory to create CompilerJvmOptions

objectFactory.newInstance<CompilerJvmOptionsDefault>()

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

fun createCompilerCommonOptions(objectFactory: ObjectFactory): CompilerCommonOptions =
objectFactory.newInstance<CompilerCommonOptionsDefault>()
Comment on lines +37 to +41

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For KotlinJsFactory and KotlinCommonFactory please open new issues, possibly it is still ok to add them into 1.8.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need same way to create for soon-to-added CompilerNativeOptionsinterface - pleases create an issue as well

}
}
@@ -0,0 +1,134 @@
/*
* 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.Task
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
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

interface KotlinCompilerRunner {
ting-yuan marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Remove those properties when getting into KGP.
// They should be configured by KGP. For now, they allow KSP to copy the settings from compilation task.

@get:Internal
val compilerExecutionStrategy: Property<KotlinCompilerExecutionStrategy>

@get:Internal
val useDaemonFallbackStrategy: Property<Boolean>

@get:Internal
val kotlinDaemonJvmArguments: ListProperty<String>

@get:Classpath
val compilerClasspath: ConfigurableFileCollection
}

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

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

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

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

// TODO: Maybe move those functions into proper KGP class when getting into KGP.

// TODO: Remove objectFactory when getting into KGP.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think KGP or some Kotlin library should provide method to create compiler runners for specific platforms. It could be useful for other build tools as well. Would be nice to open an issue.

fun createKotlinJvmCompilerRunner(
task: Task,
objectFactory: ObjectFactory,
): KotlinJvmCompilerRunner {
return objectFactory.newInstance<KotlinJvmCompilerRunnerImpl>(task)
}

// TODO: Remove objectFactory when getting into KGP.
fun createKotlinJsCompilerRunner(
task: Task,
objectFactory: ObjectFactory,
ting-yuan marked this conversation as resolved.
Show resolved Hide resolved
): KotlinJsCompilerRunner {
return objectFactory.newInstance<KotlinJsCompilerRunnerImpl>(task)
}

// TODO: Remove objectFactory when getting into KGP.
fun createKotlinMetadataCompilerRunner(
task: Task,
objectFactory: ObjectFactory,
): KotlinMetadataCompilerRunner {
return objectFactory.newInstance<KotlinMetadataCompilerRunnerImpl>(task)
}

// TODO: Remove objectFactory when getting into KGP.
fun createKotlinNativeCompilerRunner(
task: Task,
objectFactory: ObjectFactory,
): KotlinNativeCompilerRunner {
return objectFactory.newInstance<KotlinNativeCompilerRunnerImpl>(task)
}