-
Notifications
You must be signed in to change notification settings - Fork 283
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
base: main
Are you sure you want to change the base?
Conversation
@gavra0 here is a bit more explanation. KotlinCompilerRunner.kt is what I hope to contribute to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, it seems ok. The API surface is not too big:
- given a task you'll get a compiler runner
- compiler runner should allow you to specify compiler args and input/output/common sources
gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinCompilerRunner.kt
Show resolved
Hide resolved
gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinCompilerRunner.kt
Show resolved
Hide resolved
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires moving arguments to -api
module, we need to check if that's possible and if they match the API guarantees.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'll create a simplified / public version of those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed with a simplified version of the arguments in 8782e89.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is the right way to solve this. Forking of args has some downsides, as they are generated directly from compiler flags.
@Tapchicoma what do you think? Is compiler args something you'd like to expose in the API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these particular flags are the part of compiler and will never be in KGP-api artifact. On the other side - we are exposing currently kotlinOptions
and from 1.8.0
- compilerOptions
generated DSL. And we have plans to add more missing options to compilerOptions
in followup releases.
Probably we should think about another approach that will not require to use Kotlin compiler arguments from KSP side. Also this arguments are not an official compiler api and could have breaking changes between minor releases.
@get:Internal val workerExecutor: WorkerExecutor | ||
) : KotlinCompilerRunner { | ||
@get:Internal | ||
internal val taskProvider: Provider<GradleCompileTaskProvider> = objectFactory.property( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm positively surprised that GradleCompileTaskProvider
does not require Kotlin-specific task type, only Gradle Task is enough :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is declared on AbstractKotlinCompile
and is used to provide generic compilation configuration that is same for all targets 🙂
gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinCompilerRunnerImpls.kt
Show resolved
Hide resolved
which is based on KGP's GradleCompilerRunnerWithWorker. Also refactored the code to allow selection between the old and new ways to create KSP tasks and call the compiler.
and target specific subclasses. This is a simplified version of the CommonCompilerArguments family.
The proposed
With a few exceptions. They are unlikely to change and may be stable enough to get into the KGP-api artifact.
Proposal
Admittedly, there are still a few options that may be subject to change in the future. It might be better to handle those in
To summarize, with the newly proposed
@Tapchicoma what do you think? The only new interface introduced is edit: A factory / builder of the |
0bf44d1
to
e959ad9
Compare
@Tapchicoma I've uploaded an implementation that makes use of
Currently I have to copy some free args from JS compilation task to select the backend and filter the libraries. Because
Paths are moved into the parameters of
|
@@ -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") |
There was a problem hiding this comment.
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
// TODO: to be replaced by KotlinJvmFactory, etc. | ||
class CompilerOptionsFactory { | ||
companion object { | ||
fun createCompilerJvmOptions(objectFactory: ObjectFactory): CompilerJvmOptions = |
There was a problem hiding this comment.
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
fun createCompilerJsOptions(objectFactory: ObjectFactory): CompilerJsOptions = | ||
objectFactory.newInstance<CompilerJsOptionsDefault>() | ||
|
||
fun createCompilerCommonOptions(objectFactory: ObjectFactory): CompilerCommonOptions = | ||
objectFactory.newInstance<CompilerCommonOptionsDefault>() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 CompilerNativeOptions
interface - pleases create an issue as well
FYI: KGP adds these |
|
||
// TODO: Maybe move those functions into proper KGP class when getting into KGP. | ||
|
||
// TODO: Remove objectFactory when getting into KGP. |
There was a problem hiding this comment.
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.
@get:Internal val workerExecutor: WorkerExecutor | ||
) : KotlinCompilerRunner { | ||
@get:Internal | ||
internal val taskProvider: Provider<GradleCompileTaskProvider> = objectFactory.property( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is declared on AbstractKotlinCompile
and is used to provide generic compilation configuration that is same for all targets 🙂
Generally KGP will try to move away from |
FYI: KGP will go away from |
which is based on KGP's GradleCompilerRunnerWithWorker.
Also refactored the code to allow selection between the old and new
ways to create KSP tasks and call the compiler.