Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM openjdk:11.0.16-jdk as build

ENV KOTLIN_LIB=1.8.20-Beta
ENV KOTLIN_LIB_JS=1.8.20-Beta-js
ENV KOTLIN_CACHES_JS=1.8.20-Beta-js-caches

RUN mkdir -p /kotlin-compiler-server
WORKDIR /kotlin-compiler-server
Expand All @@ -20,6 +21,7 @@ COPY --from=build /build/libs/META-INF /kotlin-compiler-server/META-INF
COPY --from=build /build/libs/BOOT-INF/classes /kotlin-compiler-server
COPY --from=build /kotlin-compiler-server/${KOTLIN_LIB} /kotlin-compiler-server/${KOTLIN_LIB}
COPY --from=build /kotlin-compiler-server/${KOTLIN_LIB_JS} /kotlin-compiler-server/${KOTLIN_LIB_JS}
COPY --from=build /kotlin-compiler-server/${KOTLIN_CACHES_JS} /kotlin-compiler-server/${KOTLIN_CACHES_JS}
COPY --from=build /kotlin-compiler-server/executor.policy /kotlin-compiler-server/
COPY --from=build /kotlin-compiler-server/indexes.json /kotlin-compiler-server/

Expand Down
33 changes: 32 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ val kotlinJsDependency: Configuration by configurations.creating {
)
attribute(
KotlinJsCompilerAttribute.jsCompilerAttribute,
KotlinJsCompilerAttribute.legacy
KotlinJsCompilerAttribute.ir
)
}
}

val kotlinJsIcCache: Configuration by configurations.creating {
isTransitive = false
attributes {
attribute(
KotlinPlatformType.attribute,
KotlinPlatformType.js
)
attribute(
KotlinJsCompilerAttribute.jsCompilerAttribute,
KotlinJsCompilerAttribute.ir
)
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements::class.java, "js-ir-cache")
)
}
}

val libJSFolder = "$kotlinVersion-js"
val libJSCachesFolder = "$kotlinVersion-js-caches"
val libJVMFolder = kotlinVersion
val propertyFile = "application.properties"
val jacksonVersionKotlinDependencyJar = "2.14.0" // don't forget to update version in `executor.policy` file.
Expand All @@ -43,6 +63,11 @@ val copyJSDependencies by tasks.creating(Copy::class) {
into(libJSFolder)
}

val copyJSCaches by tasks.creating(Copy::class) {
from(kotlinJsIcCache)
into(libJSCachesFolder)
}

plugins {
id("org.springframework.boot") version "2.7.8"
id("io.spring.dependency-management") version "1.1.0"
Expand Down Expand Up @@ -87,6 +112,8 @@ dependencies {
kotlinDependency("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.4")
kotlinJsDependency("org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion")

kotlinJsIcCache(project(":cache-preparator"))

annotationProcessor("org.springframework:spring-context-indexer")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.amazonaws.serverless:aws-serverless-java-container-springboot2:1.9.1")
Expand Down Expand Up @@ -131,7 +158,10 @@ fun generateProperties(prefix: String = "") = """
indexesJs.file=${prefix + indexesJs}
libraries.folder.jvm=${prefix + libJVMFolder}
libraries.folder.js=${prefix + libJSFolder}
caches.folder.js=${prefix + libJSCachesFolder}
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
server.compression.enabled=true
server.compression.mime-types=application/json
""".trimIndent()

tasks.withType<KotlinCompile> {
Expand All @@ -141,6 +171,7 @@ tasks.withType<KotlinCompile> {
}
dependsOn(copyDependencies)
dependsOn(copyJSDependencies)
dependsOn(copyJSCaches)
dependsOn(":executors:jar")
dependsOn(":indexation:run")
buildPropertyFile()
Expand Down
55 changes: 55 additions & 0 deletions cache-preparator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsCompilerAttribute
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBinaryMode
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary

plugins {
kotlin("multiplatform")
}

val kotlinJsDependency by rootProject.configurations

kotlin {
js(IR) {
nodejs()
val executables = binaries.executable()
val main by compilations.getting
main.configurations.apiConfiguration.extendsFrom(kotlinJsDependency)

val jsCaches by configurations.creating {
isVisible = false
isCanBeResolved = false
isCanBeConsumed = true

attributes {
attribute(
KotlinPlatformType.attribute,
KotlinPlatformType.js
)
attribute(
KotlinJsCompilerAttribute.jsCompilerAttribute,
KotlinJsCompilerAttribute.ir
)
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements::class.java, "js-ir-cache")
)
}
}

val linkTask = executables
.filterIsInstance<JsIrBinary>()
.single { it.mode == KotlinJsBinaryMode.DEVELOPMENT }
.linkTask

val rootCacheDir = linkTask
.map { it.rootCacheDirectory }

artifacts.add(jsCaches.name, rootCacheDir) {
builtBy(linkTask)
}
}
}

// just to calm down root project's allprojects block
configurations.register("implementation")
3 changes: 3 additions & 0 deletions cache-preparator/src/jsMain/kotlin/File.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
println("Hello, world")
}
3 changes: 2 additions & 1 deletion common/src/main/kotlin/component/KotlinEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import java.io.File

class KotlinEnvironment(
val classpath: List<File>,
additionalJsClasspath: List<File>
additionalJsClasspath: List<File>,
val cachesJsDir: File
) {
companion object {
/**
Expand Down
3 changes: 2 additions & 1 deletion indexation/src/main/kotlin/KotlinEnvironmentConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class KotlinEnvironmentConfiguration(fileName: String) {
val kotlinEnvironment = run {
val jvmFile = File(fileName)
val jsFile = File("$fileName-js")
val cachesJsDir = File("$fileName-js-caches")
val classPath =
listOfNotNull(jvmFile)
.flatMap {
Expand All @@ -15,6 +16,6 @@ class KotlinEnvironmentConfiguration(fileName: String) {
}

val additionalJsClasspath = listOfNotNull(jsFile)
KotlinEnvironment(classPath, additionalJsClasspath)
KotlinEnvironment(classPath, additionalJsClasspath, cachesJsDir)
}
}
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rootProject.name = "kotlin-compiler-server"
include(":executors")
include(":indexation")
include(":common")
include(":common")
include(":cache-preparator")
104 changes: 104 additions & 0 deletions src/main/kotlin/com/compiler/server/compiler/components/JsCompiler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.compiler.server.compiler.components

import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.js.klib.generateIrForKlibSerialization
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.ir.backend.js.*
import org.jetbrains.kotlin.ir.backend.js.codegen.JsGenerationGranularity
import org.jetbrains.kotlin.ir.backend.js.ic.CacheUpdater
import org.jetbrains.kotlin.ir.backend.js.ic.DirtyFileState
import org.jetbrains.kotlin.ir.backend.js.ic.ModuleArtifact
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImplForJsIC
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.psi.KtFile
import java.io.File

// copy from compiler private funs

fun processSourceModule(
project: Project,
files: List<KtFile>,
libraries: List<String>,
friendLibraries: List<String>,
configuration: CompilerConfiguration,
outputKlibPath: String
): ModulesStructure {
val sourceModule: ModulesStructure = prepareAnalyzedSourceModule(
project,
files,
configuration,
libraries,
friendLibraries,
AnalyzerWithCompilerReport(configuration)
)

val moduleSourceFiles = (sourceModule.mainModule as MainModule.SourceFiles).files
val expectDescriptorToSymbol = mutableMapOf<DeclarationDescriptor, IrSymbol>()

val (moduleFragment, _) = generateIrForKlibSerialization(
project,
moduleSourceFiles,
configuration,
sourceModule.jsFrontEndResult.jsAnalysisResult,
sourceModule.allDependencies.map { it.library },
emptyList(),
expectDescriptorToSymbol,
IrFactoryImpl,
verifySignatures = true
) {
sourceModule.getModuleDescriptor(it)
}

val metadataSerializer =
KlibMetadataIncrementalSerializer(
configuration,
sourceModule.project,
sourceModule.jsFrontEndResult.hasErrors
)

generateKLib(
sourceModule,
outputKlibPath,
nopack = true,
jsOutputName = null,
icData = emptyList(),
expectDescriptorToSymbol = expectDescriptorToSymbol,
moduleFragment = moduleFragment
) { file ->
metadataSerializer.serializeScope(file, sourceModule.jsFrontEndResult.bindingContext, moduleFragment.descriptor)
}
return sourceModule
}

fun prepareIcCaches(
includes: String,
cacheDirectory: String,
libraries: List<String>,
friendLibraries: List<String>,
configurationJs: CompilerConfiguration,
): List<ModuleArtifact> {
val cacheUpdater = CacheUpdater(
mainModule = includes,
allModules = libraries,
mainModuleFriends = friendLibraries,
cacheDir = cacheDirectory,
compilerConfiguration = configurationJs,
irFactory = { IrFactoryImplForJsIC(WholeWorldStageController()) },
mainArguments = emptyList(),
compilerInterfaceFactory = { mainModule, cfg ->
JsIrCompilerWithIC(
mainModule,
cfg,
JsGenerationGranularity.WHOLE_PROGRAM,
es6mode = false
)
}
)

return cacheUpdater.actualizeCaches()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.compiler.server.compiler.components

import com.compiler.server.model.bean.CachesFile
import com.compiler.server.model.bean.LibrariesFile
import component.KotlinEnvironment
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class KotlinEnvironmentConfiguration(val librariesFile: LibrariesFile) {
class KotlinEnvironmentConfiguration(
val librariesFile: LibrariesFile,
val cachesFiles: CachesFile
) {
@Bean
fun kotlinEnvironment(): KotlinEnvironment {
val classPath =
Expand All @@ -17,6 +21,6 @@ class KotlinEnvironmentConfiguration(val librariesFile: LibrariesFile) {
}

val additionalJsClasspath = listOfNotNull(librariesFile.js)
return KotlinEnvironment(classPath, additionalJsClasspath)
return KotlinEnvironment(classPath, additionalJsClasspath, cachesFiles.js)
}
}
Loading