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

Proposal: Enable independent code generation for every source set, decoupling KSP invocations from Kotlin compilations #1037

Open
OliverO2 opened this issue Jul 9, 2022 · 0 comments
Labels
feature request Feature request for discussion

Comments

@OliverO2
Copy link

OliverO2 commented Jul 9, 2022

Related

To Consider

Prerequisites

Background

Kotlin Multiplatform compilation

This is how Kotlin multiplatform compilation currently works (based on input from the JB Kotlin Multiplatform team):

Given a hierarchy of source sets and calling the leaves "target source sets", the Kotlin Gradle multiplatform plugin will create Kotlin compilations for these source sets:

  1. commonMain
  2. each target source set (e.g. jvmMain, jvmTest, jsMain, jsTest)
  3. each intermediate source set if it is shared across two or more target source sets

NOTE: Shared test source sets never receive their own compilation.

Examples
Example A

Given the following source set hierarchy:

  • commonMain
    • clientMain
      • jvmMain

There will be one Kotlin compilation for commonMain, and one for jvmMain. clientMain will not have its own compilation as it is not shared across multiple targets.

Example B

Now change the source set hierarchy like so:

  • commonMain
    • clientMain
      • jvmMain
      • jsMain

The will be one Kotlin compilation for commonMain, one for clientMain, one for jvmMain, one for jsMain.

KSP Multiplatform operations

With #1021 merged, KSP can be configured per source set. A companion KSP task will be created for each Kotlin compilation, given its source set has an active KSP configuration.

If KSP is configured for commonMain and that configuration is inherited, there will be one KSP invocation per Kotlin compilation:

Example A will not have a KSP invocation for clientMain. That means

  • Whatever KSP should process for clientMain must be incorporated in its invocation for jvmMain.
  • KSP cannot output clientMain-specific code in its own output directory (it would output such code in the jvmMain output directory).

Example B will have a separate KSP invocation for clientMain, along with its own output directory for clientMain.

Current Limitations Within The IDE

If KSP generates code from annotations in parent source sets, and that code is output in a target source set directory, there is currently no way to correctly tell the IDE where to find such code.

Example

As shared test source sets never have their own KSP invocation, a KSP processor will typically generate identical code for each target source set depending on the same parent source set.

E.g., code generated for commonTest will appear in jvmTest and jsTest.

Proposed Solution

Decouple KSP invocations from Kotlin compilations, allowing separate KSP invocations and separate KSP output directories for

  • parent source sets which are not shared across two or more targets,
  • parent test source sets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request for discussion
Projects
None yet
Development

No branches or pull requests

2 participants