|
| 1 | +package com.compiler.server.common.components |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.nio.file.Path |
| 5 | +import java.util.* |
| 6 | +import kotlin.io.path.* |
| 7 | + |
| 8 | +@OptIn(ExperimentalPathApi::class) |
| 9 | +fun <T> usingTempDirectory(action: (path: Path) -> T): T { |
| 10 | + val path = getTempDirectory() |
| 11 | + path.createDirectories() |
| 12 | + return try { |
| 13 | + action(path) |
| 14 | + } finally { |
| 15 | + path.deleteRecursively() |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +private fun getTempDirectory(): Path { |
| 20 | + val dir = System.getProperty("java.io.tmpdir") |
| 21 | + val sessionId = UUID.randomUUID().toString().replace("-", "") |
| 22 | + return File(dir).canonicalFile.resolve(sessionId).toPath() |
| 23 | +} |
| 24 | + |
| 25 | +fun compileWasmArgs( |
| 26 | + moduleName: String, |
| 27 | + filePaths: List<String>, |
| 28 | + klibPath: String, |
| 29 | + compilerPlugins: List<String>, |
| 30 | + compilerPluginOptions: List<String>, |
| 31 | + dependencies: List<String>, |
| 32 | +): List<String> { |
| 33 | + val compilerPluginsArgs: List<String> = compilerPlugins |
| 34 | + .takeIf { it.isNotEmpty() } |
| 35 | + ?.let { plugins -> |
| 36 | + plugins.map { |
| 37 | + "-Xplugin=$it" |
| 38 | + } + compilerPluginOptions.map { |
| 39 | + "-P=$it" |
| 40 | + } |
| 41 | + } ?: emptyList() |
| 42 | + val additionalCompilerArgumentsForKLib: List<String> = listOf( |
| 43 | + "-Xreport-all-warnings", |
| 44 | + "-Xuse-fir-extended-checkers", |
| 45 | + "-Xwasm", |
| 46 | + "-Xir-produce-klib-dir", |
| 47 | + "-libraries=${dependencies.joinToString(PATH_SEPARATOR)}", |
| 48 | + "-ir-output-dir=$klibPath", |
| 49 | + "-ir-output-name=$moduleName", |
| 50 | + ) + compilerPluginsArgs |
| 51 | + |
| 52 | + return filePaths + additionalCompilerArgumentsForKLib |
| 53 | +} |
| 54 | + |
| 55 | +fun linkWasmArgs( |
| 56 | + moduleName: String, |
| 57 | + klibPath: String, |
| 58 | + dependencies: List<String>, |
| 59 | + icDir: Path?, |
| 60 | + outputDir: Path, |
| 61 | + debugInfo: Boolean, |
| 62 | +): List<String> { |
| 63 | + return mutableListOf( |
| 64 | + "-Xreport-all-warnings", |
| 65 | + "-Xuse-fir-extended-checkers", |
| 66 | + "-Xwasm", |
| 67 | + "-Xir-produce-js", |
| 68 | + "-Xinclude=$klibPath", |
| 69 | + "-libraries=${dependencies.joinToString(PATH_SEPARATOR)}", |
| 70 | + "-ir-output-dir=${(outputDir / "wasm").toFile().canonicalPath}", |
| 71 | + "-ir-output-name=$moduleName",).also { |
| 72 | + if (debugInfo) it.add("-Xwasm-generate-wat") |
| 73 | + |
| 74 | + if (icDir != null) { |
| 75 | + it.add("-Xcache-directory=${icDir.normalize().absolutePathString()}") |
| 76 | + } else { |
| 77 | + it.add("-Xir-dce") |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +val PATH_SEPARATOR: String = File.pathSeparator |
0 commit comments