Skip to content

Commit

Permalink
Support K2. (#1212)
Browse files Browse the repository at this point in the history
* Support K2.

* Revert.

* Revert.

* Update API.

* Use deprecated API.

* Add comments.

* Move dependencies.

* Temporarily ignore tests so we don't spin on CI.

* Deprecations.

* Fix assembleAndroidTest.

* Unused dependency.

* Run tests against K2.

* Remove.

* Revert API.

* Revert library updates.

* Remove endpoint companion object.

* Revert.

* Pull into separate PR.

* Replace deprecated property.

* Unused import.

* Update build.gradle.kts

Co-authored-by: Jake Wharton <[email protected]>

* Update other usage.

---------

Co-authored-by: Jake Wharton <[email protected]>
  • Loading branch information
colinrtwhite and JakeWharton authored Jan 16, 2024
1 parent 41c4252 commit cdf5dfc
Show file tree
Hide file tree
Showing 31 changed files with 137 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ reports
jacoco.exec
.externalNativeBuild
.cxx
.kotlin

# iOS
*.pbxuser
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ allprojects {
repositories {
maven {
name = "testMaven"
url = file("${rootProject.buildDir}/testMaven").toURI()
url = rootProject.layout.buildDirectory.dir("testMaven").get().asFile.toURI()
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ApplySourceMapToBytecodeTest {
)

@Before
@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
fun setUp() {
// Configure QuickJS to support module loading.
app.cash.zipline.internal.initModuleLoader(quickJs)
Expand Down Expand Up @@ -222,7 +222,7 @@ class ApplySourceMapToBytecodeTest {
assertThat(quickJs.evaluate("doubleToDisplayString('1000.0')")).isEqualTo("1000.0")
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
private fun loadJsModule(id: String, bytecode: ByteArray) {
app.cash.zipline.internal.loadJsModule(quickJs, id, bytecode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GenerateKeyPair(
.default(Ed25519)
.help("Signing algorithm to use.")

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
override fun run() {
val keyPair = app.cash.zipline.loader.internal.generateKeyPair(algorithm)
out.println(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DownloadTest {
assertTrue(fileSystem.exists(tmpDirPath / testFixtures.alphaSha256Hex))
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
private fun getApplicationManifestFileName(applicationName: String) =
app.cash.zipline.loader.internal.getApplicationManifestFileName(applicationName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ internal class ZiplineCompiler(

private fun getJsFiles(files: List<File>) = files.filter { it.path.endsWith(".js") }

@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
private val manifestFileName = app.cash.zipline.loader.internal.MANIFEST_FILE_NAME

@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
private fun collectDependencies(quickJs: QuickJs, bytecode: ByteArray): List<String> {
app.cash.zipline.internal.collectModuleDependencies(quickJs)
quickJs.execute(bytecode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class ZiplinePlugin : KotlinCompilerPluginSupportPlugin {
}
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
private fun generateKeyPair(algorithm: SignatureAlgorithmId) {
val logger = LoggerFactory.getLogger(ZiplinePlugin::class.java)
val keyPair = app.cash.zipline.loader.internal.generateKeyPair(algorithm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ZiplineCompilerTest {
private val quickJs = QuickJs.create()

@Before
@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
fun setUp() {
// Configure QuickJS to support module loading.
app.cash.zipline.internal.initModuleLoader(quickJs)
Expand Down Expand Up @@ -224,7 +224,7 @@ class ZiplineCompilerTest {
return result
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
private fun loadJsModule(quickJs: QuickJs, id: String, bytecode: ByteArray) {
return app.cash.zipline.internal.loadJsModule(quickJs, id, bytecode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ZiplineGradleDownloaderTest {
)
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
private fun getApplicationManifestFileName(applicationName: String) =
app.cash.zipline.loader.internal.getApplicationManifestFileName(applicationName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ import java.io.File
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Test

class ZiplineGradlePluginTest {
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

@RunWith(Parameterized::class)
class ZiplineGradlePluginTest(
private val enableK2: Boolean,
) {
@Test
fun productionBuilds() {
val projectDir = File("src/test/projects/basic")
Expand Down Expand Up @@ -501,13 +507,17 @@ class ZiplineGradlePluginTest {
}
}

private fun createRunner(projectDir: File, vararg taskNames: String): GradleRunner {
private fun createRunner(
projectDir: File,
vararg taskNames: String,
): GradleRunner {
val gradleRoot = projectDir.resolve("gradle").also { it.mkdir() }
File("../gradle/wrapper").copyRecursively(gradleRoot.resolve("wrapper"), true)
val arguments = arrayOf("--info", "--stacktrace", "--continue", "-PenableK2=$enableK2")
return GradleRunner.create()
.withProjectDir(projectDir)
.withDebug(true) // Run in-process.
.withArguments("--info", "--stacktrace", "--continue", *taskNames, versionProperty)
.withArguments(*arguments, *taskNames, versionProperty)
.forwardOutput()
}

Expand Down Expand Up @@ -535,7 +545,14 @@ class ZiplineGradlePluginTest {
val SUCCESS_OUTCOMES = listOf(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE)
val versionProperty = "-PziplineVersion=${System.getProperty("ziplineVersion")}"

@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
private val manifestFileName = app.cash.zipline.loader.internal.MANIFEST_FILE_NAME

@JvmStatic
@Parameters
fun data() = listOf(
arrayOf(true),
arrayOf(false),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ plugins {

kotlin {
androidTarget()

js {
browser()
binaries.executable()
}

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
commonMain {
dependencies {
Expand Down
10 changes: 10 additions & 0 deletions zipline-gradle-plugin/src/test/projects/basic/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ kotlin {
binaries.executable()
}

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
commonMain {
dependencies {
Expand Down
10 changes: 10 additions & 0 deletions zipline-gradle-plugin/src/test/projects/crash/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ kotlin {
binaries.executable()
}

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
commonMain {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ plugins {
kotlin {
jvm()

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
val jvmMain by getting {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ kotlin {
}
}

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
commonMain {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ kotlin {
binaries.executable()
}

if (project.property("enableK2").toString().toBooleanStrict()) {
targets.configureEach {
compilations.configureEach {
compilerOptions.options.languageVersion.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0,
)
}
}
}

sourceSets {
commonMain {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.config.CompilerConfiguration
@OptIn(ExperimentalCompilerApi::class)
@AutoService(CompilerPluginRegistrar::class)
class ZiplineCompilerPluginRegistrar : CompilerPluginRegistrar() {
override val supportsK2 get() = false
override val supportsK2 get() = true

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
val messageCollector = configuration.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ManifestVerifier private constructor(
* @throws IllegalStateException if no trusted signature is found, or if a signature doesn't
* verify.
*/
@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
fun verify(manifestBytes: ByteString, manifest: ZiplineManifest): String? {
if (!doSignatureChecks) return null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class ZiplineLoader internal constructor(
* After identifying a manifest to load this fetches all the code, loads it into a JS runtime,
* and runs both the user's initializer and the manifest's specified main function.
*/
@Suppress("INVISIBLE_MEMBER") // Access :zipline internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline internals.
internal suspend fun loadFromManifest(
applicationName: String,
eventListener: EventListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ProductionFetcherReceiverTest {
assertEquals(testFixtures.alphaByteString, ziplineFileFromCache)
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
private fun getLog() = app.cash.zipline.internal.getLog(zipline.quickJs)

private suspend fun ZiplineLoader.loadOrFail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class ZiplineLoaderTest {
}
}

@Suppress("INVISIBLE_MEMBER") // Access :zipline-loader internals.
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // Access :zipline-loader internals.
private fun Zipline.getLog(): String? = app.cash.zipline.internal.getLog(quickJs)

private suspend fun ZiplineLoader.loadOrFail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ internal interface ProfilerClock {
val nanoTime: Long
}

internal expect object DefaultProfilerClock : ProfilerClock
internal expect object DefaultProfilerClock : ProfilerClock {
override val nanoTime: Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
package app.cash.zipline.profiler

internal actual object DefaultProfilerClock : ProfilerClock {
override val nanoTime: Long get() = System.nanoTime()
actual override val nanoTime: Long get() = System.nanoTime()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
package app.cash.zipline.profiler

internal actual object DefaultProfilerClock : ProfilerClock {
override val nanoTime: Long get() = kotlin.system.getTimeNanos()
actual override val nanoTime: Long get() = kotlin.system.getTimeNanos()
}
18 changes: 12 additions & 6 deletions zipline-testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.plugin.PLUGIN_CLASSPATH_CONFIGURATION_NAME
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile

Expand Down Expand Up @@ -69,19 +68,26 @@ kotlin {

tasks {
// https://kotlinlang.org/docs/whatsnew19.html#library-linkage-in-kotlin-native
withType<KotlinNativeCompile>().all {
kotlinOptions {
freeCompilerArgs += listOf("-Xpartial-linkage-loglevel=ERROR")
withType<KotlinNativeCompile>().configureEach {
compilerOptions {
freeCompilerArgs.addAll("-Xpartial-linkage-loglevel=ERROR")
}
}

// https://youtrack.jetbrains.com/issue/KT-56025
// https://youtrack.jetbrains.com/issue/KT-57203
named("jsBrowserProductionWebpack").configure {
// Required for K1.
findByName("jsBrowserProductionWebpack")?.apply {
dependsOn(named("jsProductionLibraryCompileSync"))
dependsOn(named("jsDevelopmentLibraryCompileSync"))
}
named("jsBrowserProductionLibraryPrepare").configure {
// Required for K1.
findByName("jsBrowserProductionLibraryPrepare")?.apply {
dependsOn(named("jsProductionExecutableCompileSync"))
dependsOn(named("jsDevelopmentLibraryCompileSync"))
}
// Required for K2.
findByName("jsBrowserProductionLibraryDistribution")?.apply {
dependsOn(named("jsProductionExecutableCompileSync"))
dependsOn(named("jsDevelopmentLibraryCompileSync"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalJsExport::class)

package app.cash.zipline.testing

import app.cash.zipline.Zipline
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -49,6 +52,7 @@ fun unblockSuspendingJs() {
mutex.unlock()
}

@OptIn(DelicateCoroutinesApi::class)
@JsExport
fun callSuspendingEchoService(message: String) {
val service = zipline.take<SuspendingEchoService>("jvmSuspendingEchoService")
Expand Down
Loading

0 comments on commit cdf5dfc

Please sign in to comment.