google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.87k stars 269 forks source link

How to generate code for common Ios target instead of individual (X64, Arm64) source sets #929

Open Legion2 opened 2 years ago

Legion2 commented 2 years ago

We use the shorthand ios() function to configure common ios source sets and we only have code in the iosMain source set. However ksp generates code into the IosX64 and IosArm64 source sets. For both source sets, identical code is generated, but we cannot access the definitions from the iosMain source set.

dependencies {
    add("kspAndroid", project(":processor"))
    add("kspIosX64", project(":processor"))
    add("kspIosArm64", project(":processor"))
}

If we try to configure add("kspIos", project(":processor")) we get the following error: Configuration with name 'kspIos' not found.

Either the commonization task should run on the generated sources and generate the common definitions or ksp should generate code for the iosMain source set.

nrobi144 commented 2 years ago

Tried add("kspIos", myKspPlugin) on v1.6.10-1.0.4 and it works

Legion2 commented 2 years ago

With 1.6.20-1.0.4 it does not work. We use ios() to add the ios targets in the kotlin dsl.

bennyhuo commented 1 year ago
    add("kspCommonMainMetadata", project(":klue-compiler"))
    add("kspAndroid", project(":klue-compiler"))
    add("kspIosArm64", project(":klue-compiler"))
    add("kspIosSimulatorArm64", project(":klue-compiler"))
    add("kspIosX64", project(":klue-compiler"))
    add("kspJs", project(":klue-compiler"))

It works in Kotlin 1.8.20.

Legion2 commented 1 year ago
    add("kspCommonMainMetadata", project(":klue-compiler"))
    add("kspAndroid", project(":klue-compiler"))
    add("kspIosArm64", project(":klue-compiler"))
    add("kspIosSimulatorArm64", project(":klue-compiler"))
    add("kspIosX64", project(":klue-compiler"))
    add("kspJs", project(":klue-compiler"))

It works in Kotlin 1.8.20.

This still only generates code for code for individual targets and commonMain but not iosMain.

burnoo commented 11 months ago

Found workaround, but it works only for extensions.

Koin example: You can add following code to your iosMain sourceSet:

package org.koin.ksp.generated // package should be same as your generated code

import org.koin.core.module.Module

@Suppress("UnusedReceiverParameter")
val Any.module: Module
    get() = throw RuntimeException("Koin module was not generated. Add ksp for all your targets")

On iosMain IDE will recognize the above extension, but on more specific target (e.g. IosArm64) it will use generated code instead, as val Module.module: Module is a better match than val Any.module: Module.

ltttttttttttt commented 6 months ago

Is there a formal solution now?

H4kt commented 5 months ago

+1 Facing the same issue

Ribesg commented 4 months ago

This seemingly unrelated comment is another (imho, less hacky) workaround for the iosMain target https://github.com/JetBrains/compose-multiplatform/issues/4928#issuecomment-2151021584