Skip to content

Commit 2c6ebb2

Browse files
committed
Add IC for Compose Wasm
1 parent 64d493e commit 2c6ebb2

File tree

26 files changed

+552
-100
lines changed

26 files changed

+552
-100
lines changed

build.gradle.kts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ allprojects {
4141
maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
4242
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
4343
}
44-
afterEvaluate {
45-
dependencies {
46-
dependencies {
44+
}
45+
46+
setOf(
47+
rootProject,
48+
project(":common"),
49+
project(":executors"),
50+
project(":indexation"),
51+
).forEach { project ->
52+
project.afterEvaluate {
53+
project.dependencies {
54+
project.dependencies {
4755
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2")
4856
implementation(libs.kotlin.idea) {
4957
isTransitive = false
@@ -58,6 +66,31 @@ val resourceDependency: Configuration by configurations.creating {
5866
isCanBeConsumed = false
5967
}
6068

69+
70+
val kotlinComposeWasmIcLocalCache: Configuration by configurations.creating {
71+
isTransitive = false
72+
isCanBeResolved = true
73+
isCanBeConsumed = false
74+
attributes {
75+
attribute(
76+
CacheAttribute.cacheAttribute,
77+
CacheAttribute.LOCAL
78+
)
79+
}
80+
}
81+
82+
val kotlinComposeWasmIcLambdaCache: Configuration by configurations.creating {
83+
isTransitive = false
84+
isCanBeResolved = true
85+
isCanBeConsumed = false
86+
attributes {
87+
attribute(
88+
CacheAttribute.cacheAttribute,
89+
CacheAttribute.LAMBDA
90+
)
91+
}
92+
}
93+
6194
dependencies {
6295
annotationProcessor("org.springframework:spring-context-indexer")
6396
implementation("com.google.code.gson:gson")
@@ -85,6 +118,9 @@ dependencies {
85118
testImplementation(libs.kotlinx.coroutines.test)
86119

87120
resourceDependency(libs.skiko.js.wasm.runtime)
121+
122+
kotlinComposeWasmIcLocalCache(project(":cache-maker"))
123+
kotlinComposeWasmIcLambdaCache(project(":cache-maker"))
88124
}
89125

90126
fun buildPropertyFile() {
@@ -109,6 +145,7 @@ fun generateProperties(prefix: String = "") = """
109145
libraries.folder.compose-wasm=${prefix + libComposeWasm}
110146
libraries.folder.compose-wasm-compiler-plugins=${prefix + libComposeWasmCompilerPlugins}
111147
libraries.folder.compiler-plugins=${prefix + compilerPluginsForJVM}
148+
caches.folder.compose-wasm=${prefix + cachesComposeWasm}
112149
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
113150
server.compression.enabled=true
114151
server.compression.mime-types=application/json,text/javascript,application/wasm
@@ -120,6 +157,7 @@ tasks.withType<KotlinCompile> {
120157
}
121158
dependsOn(":executors:jar")
122159
dependsOn(":indexation:run")
160+
dependsOn(kotlinComposeWasmIcLocalCache)
123161
buildPropertyFile()
124162
}
125163
println("Using Kotlin compiler ${libs.versions.kotlin.get()}")
@@ -131,7 +169,7 @@ tasks.withType<BootJar> {
131169

132170
val buildLambda by tasks.creating(Zip::class) {
133171
val propertyFile = propertyFile
134-
val propertyFileContent = generateProperties("/var/task/")
172+
val propertyFileContent = generateProperties(lambdaPrefix)
135173

136174
from(tasks.compileKotlin)
137175
from(tasks.processResources) {
@@ -152,6 +190,7 @@ val buildLambda by tasks.creating(Zip::class) {
152190
from(libJVMFolder) { into(libJVM) }
153191
from(compilerPluginsForJVMFolder) {into(compilerPluginsForJVM)}
154192
from(libComposeWasmCompilerPluginsFolder) { into(libComposeWasmCompilerPlugins) }
193+
from(kotlinComposeWasmIcLambdaCache)
155194
into("lib") {
156195
from(configurations.compileClasspath) { exclude("tomcat-embed-*") }
157196
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.gradle.api.attributes.Attribute
2+
3+
enum class CacheAttribute {
4+
LOCAL,
5+
LAMBDA;
6+
7+
companion object {
8+
val cacheAttribute = Attribute.of("org.jetbrains.kotlin-compiler-server.cache", CacheAttribute::class.java)
9+
}
10+
}

buildSrc/src/main/kotlin/lambda.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val lambdaPrefix = "/var/task/"

buildSrc/src/main/kotlin/properties.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import org.gradle.accessors.dm.LibrariesForLibs
12
import org.gradle.api.Project
23
import org.gradle.kotlin.dsl.provideDelegate
3-
import org.gradle.accessors.dm.LibrariesForLibs
44
import org.gradle.kotlin.dsl.the
55

66
val indexes: String by System.getProperties()
@@ -25,6 +25,8 @@ val Project.libComposeWasm
2525
get() = "$kotlinVersion-compose-wasm"
2626
val Project.libComposeWasmCompilerPlugins
2727
get() = "$kotlinVersion-compose-wasm-compiler-plugins"
28+
val Project.cachesComposeWasm
29+
get() = "$kotlinVersion-caches-compose-wasm"
2830

2931
val Project.libJVMFolder
3032
get() = rootProject.layout.projectDirectory.dir(libJVM)
@@ -42,4 +44,7 @@ val Project.libComposeWasmFolder
4244
get() = rootProject.layout.projectDirectory.dir(libComposeWasm)
4345

4446
val Project.libComposeWasmCompilerPluginsFolder
45-
get() = rootProject.layout.projectDirectory.dir(libComposeWasmCompilerPlugins)
47+
get() = rootProject.layout.projectDirectory.dir(libComposeWasmCompilerPlugins)
48+
49+
val Project.cachesComposeWasmFolder
50+
get() = rootProject.layout.projectDirectory.dir(cachesComposeWasm)

cache-maker/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM amazoncorretto:17
2+
3+
ARG BASE_DIR
4+
5+
RUN mkdir -p $BASE_DIR
6+
WORKDIR $BASE_DIR
7+
ADD . $BASE_DIR
8+
9+
RUN ./gradlew :cache-maker:run

cache-maker/build.gradle.kts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
plugins {
2+
kotlin("jvm")
3+
application
4+
}
5+
6+
dependencies {
7+
implementation(project(":common", configuration = "default"))
8+
}
9+
10+
application {
11+
mainClass.set("cache.MainKt")
12+
}
13+
14+
tasks.withType<JavaExec> {
15+
dependsOn(":dependencies:copyDependencies")
16+
dependsOn(":dependencies:copyWasmDependencies")
17+
dependsOn(":dependencies:copyComposeWasmCompilerPlugins")
18+
dependsOn(":dependencies:copyComposeWasmDependencies")
19+
20+
val rootName = project.rootProject.projectDir.toString()
21+
22+
val kotlinVersion = libs.versions.kotlin.get()
23+
inputs.property("kotlinVersion", kotlinVersion)
24+
25+
// Adding classpath directories as task input for up-to-date checks
26+
inputs.dir(libWasmFolder)
27+
inputs.dir(libComposeWasmFolder)
28+
inputs.dir(libComposeWasmCompilerPluginsFolder)
29+
30+
// Adding resulting index files as output for up-to-date checks
31+
val composeCacheComposeWasm = "$rootName${File.separator}$cachesComposeWasm"
32+
outputs.dir(cachesComposeWasmFolder)
33+
34+
args = listOf(
35+
kotlinVersion,
36+
libJVMFolder.asFile.absolutePath,
37+
composeCacheComposeWasm,
38+
)
39+
}
40+
41+
val outputLocalCacheDir = rootDir.resolve(cachesComposeWasm)
42+
val buildCacheLocal by tasks.registering(Exec::class) {
43+
workingDir = rootDir
44+
executable = "${project.name}/docker-build-incremental-cache.sh"
45+
outputs.dir(outputLocalCacheDir)
46+
args = listOf(
47+
rootDir.normalize().absolutePath, // baseDir
48+
rootDir.normalize().absolutePath // targetDir
49+
)
50+
}
51+
52+
val outputLambdaCacheDir: Provider<Directory> = layout.buildDirectory.dir("incremental-cache")
53+
val buildCacheForLambda by tasks.registering(Exec::class) {
54+
workingDir = rootDir
55+
executable = "${project.name}/docker-build-incremental-cache.sh"
56+
57+
val outputDir = outputLambdaCacheDir
58+
59+
outputs.dir(outputDir.map { it.dir(cachesComposeWasm) })
60+
61+
doFirst {
62+
args = listOf(
63+
lambdaPrefix, // baseDir
64+
outputDir.get().asFile.normalize().absolutePath, // targetDir
65+
)
66+
}
67+
}
68+
69+
val kotlinComposeWasmIcLocalCache: Configuration by configurations.creating {
70+
isTransitive = false
71+
isCanBeResolved = false
72+
isCanBeConsumed = true
73+
attributes {
74+
attribute(
75+
CacheAttribute.cacheAttribute,
76+
CacheAttribute.LOCAL
77+
)
78+
}
79+
}
80+
81+
val kotlinComposeWasmIcLambdaCache: Configuration by configurations.creating {
82+
isTransitive = false
83+
isCanBeResolved = false
84+
isCanBeConsumed = true
85+
attributes {
86+
attribute(
87+
CacheAttribute.cacheAttribute,
88+
CacheAttribute.LAMBDA
89+
)
90+
}
91+
}
92+
93+
artifacts.add(kotlinComposeWasmIcLocalCache.name, outputLocalCacheDir) {
94+
builtBy(buildCacheLocal)
95+
}
96+
97+
artifacts.add(kotlinComposeWasmIcLambdaCache.name, outputLambdaCacheDir) {
98+
builtBy(buildCacheForLambda)
99+
}
100+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
kotlinVersion=$(awk '{ if ($1 == "kotlin") { gsub(/"/, "", $2); print $2; } }' FS=' = ' ./gradle/libs.versions.toml)
4+
5+
baseDir=$1
6+
targetDir=$2
7+
8+
echo "Kotlin Version for the docker: $kotlinVersion"
9+
echo "Base directory: $baseDir"
10+
echo "Target directory: $targetDir"
11+
12+
image_tag=my-image-name:$(date +%s)
13+
14+
docker build . --file cache-maker/Dockerfile --tag $image_tag --build-arg BASE_DIR=$baseDir
15+
16+
container=$(docker create $image_tag)
17+
18+
docker cp $container:$baseDir/$kotlinVersion-caches-compose-wasm $targetDir
19+
20+
docker start $container
21+
docker stop $container
22+
docker remove $container
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import com.compiler.server.common.components.KotlinEnvironment
2+
import com.compiler.server.common.components.compileWasmArgs
3+
import com.compiler.server.common.components.linkWasmArgs
4+
import com.compiler.server.common.components.usingTempDirectory
5+
import org.jetbrains.kotlin.cli.common.CLICompiler.Companion.doMainNoExit
6+
import org.jetbrains.kotlin.cli.js.K2JSCompiler
7+
import java.io.File
8+
import kotlin.io.path.div
9+
10+
11+
class CacheBuilder(
12+
private val kotlinEnvironment: KotlinEnvironment,
13+
) {
14+
fun compileCache(outputForCache: String) {
15+
val moduleName = "playground"
16+
usingTempDirectory { outputDir ->
17+
val resource = this::class.java.classLoader.getResource("File.kt")!!.path
18+
19+
val klibPath = (outputDir / "klib").toFile().normalize().absolutePath
20+
21+
val k2JSCompiler = K2JSCompiler()
22+
23+
doMainNoExit(
24+
k2JSCompiler,
25+
compileWasmArgs(
26+
moduleName,
27+
listOf(resource),
28+
klibPath,
29+
kotlinEnvironment.COMPOSE_WASM_COMPILER_PLUGINS,
30+
kotlinEnvironment.composeWasmCompilerPluginOptions,
31+
kotlinEnvironment.COMPOSE_WASM_LIBRARIES
32+
).toTypedArray()
33+
)
34+
35+
usingTempDirectory { tmpDir ->
36+
val cachesDir = tmpDir.resolve("caches").normalize()
37+
doMainNoExit(
38+
k2JSCompiler,
39+
linkWasmArgs(
40+
moduleName,
41+
klibPath,
42+
kotlinEnvironment.COMPOSE_WASM_LIBRARIES,
43+
cachesDir.normalize(),
44+
outputDir,
45+
false
46+
).toTypedArray()
47+
)
48+
49+
cachesDir.toFile().copyRecursively(File(outputForCache), overwrite = true)
50+
}
51+
}
52+
}
53+
}

cache-maker/src/main/kotlin/Main.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cache
2+
3+
import CacheBuilder
4+
import com.compiler.server.common.components.KotlinEnvironmentConfiguration
5+
6+
fun main(args: Array<String>) {
7+
val version = args[0]
8+
val directory = args[1]
9+
val outputPathCacheComposeWasm = args[2]
10+
val kotlinEnvironment = KotlinEnvironmentConfiguration(version, directory).kotlinEnvironment
11+
12+
CacheBuilder(
13+
kotlinEnvironment = kotlinEnvironment
14+
).compileCache(outputPathCacheComposeWasm)
15+
}

0 commit comments

Comments
 (0)