-
Notifications
You must be signed in to change notification settings - Fork 281
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 source set-specific code generation in Multiplatform projects #965
Comments
Some thoughts. Use cases for source set-aware KSP processing
Causes and consequences of KSP processors lacking source set information
Information required for the above use cases
Suggestion to solve the issue
Depending on the outcome of #1021, I'd be willing to come up with a PR to implement the above. What do you think? |
Sorry I am currently packed with other tasks, will reach back (also follow up on your PR) probably next week. |
@neetopia That's fine with me. As I'm also working on other tasks, I've just tried to wrap up everything I had in mind for KSP and discover related issues, so that once you are ready, work can continue without losing too much context. |
If the goal is to make the outputs observable to downstreams, one approach could be excluding (human written) parent sources from getNewFiles/getAllFiles/getSymbolsWithAnnotation/etc while keeping them in resolution scope, as if parents were already processed in some previous rounds. In that way
KSP still needs to find out the source sets for the inputs, but that can be kept in implementation details. Compared to the current model, an issue shared between the above approach and the proposed, explicit API is that, intermediate source sets may lose some context. For example, jvm specific functions won't be available when processing common source sets. Probably not a blocker though. |
Excluding (human written) parent sources from getNewFiles/getAllFiles/getSymbolsWithAnnotation/etc. would immediately solve use case 1 (generate code from symbols in its current (output) source set only). It would not cover use case 2 (generate If a processor is to generate some (non-trivial) source set-specific code, it must have some idea about the source set's targets and know which (library) APIs are available. Such information could be conferred via source set-specific configuration ( |
Posted a workaround in #963 (comment) in case all you need is to generate code for the |
As shown in #963, this repository's multiplatform example generates identical code for all source sets, resulting in "Redeclaration" compiler errors once dependencies are set up correctly. While this example can be fixed by generating code for the
commonMain
source set only, this will not work with projects requiring separate code sets generated for different source sets.OliverO2/kotlin-multiplatform-ksp contains an example project where this problem has been solved via source set detection:
https://github.com/OliverO2/kotlin-multiplatform-ksp/blob/214eef9e529ca3dae8a6e38123790b4795302969/symbol-processor/src/jvmMain/kotlin/QualifiableProcessor.kt#L37-L38
https://github.com/OliverO2/kotlin-multiplatform-ksp/blob/214eef9e529ca3dae8a6e38123790b4795302969/symbol-processor/src/jvmMain/kotlin/QualifiableProcessor.kt#L55-L56
As there seems to be no reliable API providing the required information on input and output source sets, the solution is unstable.
In addition, the output source set is only detected via the
codeGenerator
's generated file path, which makes it unsuitable for collecting information before deciding on code generation.Would it seem feasible for a future KSP release to provide information on input and output source sets via the
SymbolProcessorEnvironment
or theResolver
?The text was updated successfully, but these errors were encountered: