diff --git a/korge-kotlin-compiler/.gitignore b/korge-kotlin-compiler/.gitignore deleted file mode 100644 index 616e7c1ef1..0000000000 --- a/korge-kotlin-compiler/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/build -/.gradle -/.idea -/out -/bin diff --git a/korge-kotlin-compiler/build.gradle.kts b/korge-kotlin-compiler/build.gradle.kts deleted file mode 100644 index a1ac70dfb4..0000000000 --- a/korge-kotlin-compiler/build.gradle.kts +++ /dev/null @@ -1,71 +0,0 @@ -import korlibs.korge.gradle.targets.android.* -import korlibs.root.* - -plugins { - //id "kotlin" version "1.6.21" - id("kotlin") - //id "org.jetbrains.kotlin.jvm" - id("maven-publish") - //alias(libs.plugins.conventions.jvm) - //alias(libs.plugins.compiler.specific.module) -} - -//name = "korge-kotlin-plugin" -description = "Multiplatform Game Engine written in Kotlin" -group = RootKorlibsPlugin.KORGE_GROUP - -val jversion = GRADLE_JAVA_VERSION_STR - -java { - setSourceCompatibility(jversion) - setTargetCompatibility(jversion) -} - -tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class).all { - kotlinOptions { - jvmTarget = jversion - apiVersion = "1.8" - languageVersion = "1.8" - suppressWarnings = true - } -} - -publishing { - publications { - val maven by creating(MavenPublication::class) { - groupId = group.toString() - artifactId = project.name - version = version - from(components["kotlin"]) - } - } -} - -val publishJvmPublicationToMavenLocal = tasks.register("publishJvmPublicationToMavenLocal", Task::class) { - group = "publishing" - dependsOn("publishMavenPublicationToMavenLocal") -} - -afterEvaluate { - if (tasks.findByName("publishMavenPublicationToMavenRepository") != null) { - tasks.register("publishJvmPublicationToMavenRepository", Task::class) { - group = "publishing" - dependsOn("publishMavenPublicationToMavenRepository") - } - } -} - -korlibs.NativeTools.groovyConfigurePublishing(project, false) -korlibs.NativeTools.groovyConfigureSigning(project) - -dependencies { - implementation("org.jetbrains.kotlin:kotlin-build-tools-impl") - compileOnly("org.jetbrains.kotlin:kotlin-build-tools-api") - //api("org.jetbrains.kotlin:kotlin-compiler-embeddable") - //api("org.jetbrains.kotlin:kotlin-compiler-client-embeddable") - //api("org.jetbrains.kotlin:kotlin-daemon-embeddable") - //api("org.jetbrains.kotlin:kotlin-gradle-plugin") - testImplementation(libs.bundles.kotlin.test) -} - -tasks { val jvmTest by creating { dependsOn("test") } } diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompiler.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompiler.kt deleted file mode 100644 index 6857514fe1..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompiler.kt +++ /dev/null @@ -1,301 +0,0 @@ -@file:OptIn(ExperimentalBuildToolsApi::class) - -package korlibs.korge.kotlincompiler - -import korlibs.korge.kotlincompiler.maven.* -import org.jetbrains.kotlin.buildtools.api.* -import org.jetbrains.kotlin.buildtools.api.jvm.* -import java.io.* -import java.security.* -import java.util.* -import kotlin.system.* - -// https://github.com/JetBrains/kotlin/tree/master/compiler/build-tools/kotlin-build-tools-api -// https://github.com/JetBrains/kotlin/blob/bc1ddd8205f6107c7aec87a9fb3bd7713e68902d/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/model/JvmModule.kt -class KorgeKotlinCompiler { - - companion object { - - @JvmStatic - fun main(args: Array) { - println(javaExecutablePath) - - val libs = filesForMaven( - MavenArtifact("org.jetbrains.kotlin", "kotlin-stdlib", "2.0.0"), - MavenArtifact("com.soywiz.korge", "korge-jvm", "999.0.0.999") - ) - - val mod1 = Module( - path = File("C:\\Users\\soywiz\\projects\\korge-snake\\modules\\korma-tile-matching"), - libs = libs - ) - val snakeModule = Module( - path = File("C:\\Users\\soywiz\\projects\\korge-snake"), - moduleDeps = setOf(mod1), - libs = libs - ) - - compileAndRun(snakeModule, mapOf("KORGE_HEADLESS" to "true", "KORGE_IPC" to "C:\\Users\\soywiz\\AppData\\Local\\Temp\\/KORGE_IPC-304208")) - } - - fun compileAndRun(module: Module, envs: Map = mapOf()) { - compileAllModules(module) - runModule(module, envs) - } - - fun compileAllModules(module: Module) { - module.allModuleDeps.forEach { - compileModule(it) - } - } - - val javaExecutablePath by lazy { - ProcessHandle.current() - .info() - .command() - .orElseThrow() - } - - fun compileModule( - module: Module - ) { - val srcDirs = module.srcDirs - val resourcesDirs = module.resourceDirs - val libFiles = arrayListOf() - val root = module.path - - for (dep in module.allModuleDeps.map { it.classesDir }) { - libFiles += dep - } - - val compiler = KorgeKotlinCompiler() - compiler.rootDir = root - compiler.sourceDirs = srcDirs.toSet() - compiler.libs = module.libs.toSet() + libFiles.toSet() - - lateinit var result: CompilationResult - val time = measureTimeMillis { - result = compiler.compileJvm() - //println(compiler.compileJvm(forceRecompilation = true)) - } - if (result != CompilationResult.COMPILATION_SUCCESS) { - //compiler.filesTxtFile.delete() // Deletes just in case - } - println("$result: $time ms") - //compiler.runJvm() - } - - fun runModule(module: Module, envs: Map = mapOf()) { - val allClasspaths: Set = module.allModuleDeps.flatMap { setOf(it.classesDir) + it.resourceDirs + it.libs }.toSet() - runJvm(module.main, allClasspaths, envs) - } - - fun runJvm(main: String, classPaths: Collection, envs: Map = mapOf()): Int { - val allArgs = listOf(javaExecutablePath, "-cp", classPaths.joinToString(File.pathSeparator), main) - println(allArgs.joinToString(" ")) - return ProcessBuilder(allArgs) - .inheritIO() - .also { it.environment().putAll(envs) } - .start() - .also { proc -> Runtime.getRuntime().addShutdownHook(Thread { proc.destroy(); proc.destroyForcibly() }) } - .waitFor() - } - - fun filesForMaven(vararg artifacts: MavenArtifact): Set = artifacts.flatMap { filesForMaven(it) }.toSet() - fun filesForMaven(artifacts: List): Set = artifacts.flatMap { filesForMaven(it) }.toSet() - fun filesForMaven(artifact: MavenArtifact): Set = MavenTools.getMavenArtifacts(artifact) - } - - data class Module( - val path: File, - val moduleDeps: Set = setOf(), - val libs: Set = setOf(), - val main: String = "MainKt", - ) { - val allModuleDeps: Set by lazy { - (moduleDeps.flatMap { it.allModuleDeps + it } + this).toSet() - } - val allTransitiveLibs by lazy { allModuleDeps.flatMap { it.libs }.toSet() } - - val buildDir = File(path, ".korge") - val classesDir = File(buildDir, "classes") - val srcDirs by lazy { - val dirs = arrayListOf() - if (File(path, "src/commonMain/kotlin").isDirectory) { - dirs += File(path, "src/commonMain/kotlin") - dirs += File(path, "src/jvmMain/kotlin") - } else { - dirs += File(path, "src") - dirs += File(path, "src@jvm") - } - dirs - } - val resourceDirs by lazy { - val dirs = arrayListOf() - if (File(path, "src/commonMain/kotlin").isDirectory) { - dirs += File(path, "src/commonMain/resources") - dirs += File(path, "src/jvmMain/resources") - } else { - dirs += File(path, "resources") - dirs += File(path, "resources@jvm") - } - dirs - } - } - - var rootDir: File = File("/temp") - val buildDirectory get() = File(rootDir, ".korge").absoluteFile - val filesTxtFile get() = File(buildDirectory, "files.txt") - val classesDir get() = File(buildDirectory, "classes").absolutePath - var sourceDirs: Set = emptySet() - var libs: Set = emptySet() - set(value) { - if (field != value) { - field = value - snapshot = null - } - } - - private var snapshot: ClasspathSnapshotBasedIncrementalCompilationApproachParameters? = null - //private val service = CompilationService.loadImplementation(ClassLoader.getSystemClassLoader()) - //private val service = CompilationService.loadImplementation(KorgeKotlinCompiler::class.java.classLoader) - private val service = CompilationService.loadImplementation(ClassLoader.getPlatformClassLoader()) - private val executionConfig = service.makeCompilerExecutionStrategyConfiguration() - .useInProcessStrategy() - - private val icWorkingDir by lazy { File(buildDirectory, "ic").also { it.mkdirs() } } - private val icCachesDir by lazy { File(icWorkingDir, "caches").also { it.mkdirs() } } - - private fun createSnapshots(): ClasspathSnapshotBasedIncrementalCompilationApproachParameters { - val snapshots = mutableListOf() - - for (lib in libs) { - if (lib.isFile) { - val hexDigest = HexFormat.of().formatHex(MessageDigest.getInstance("SHA1").digest(lib.readBytes())) - val file = File(icWorkingDir, "dep-" + lib.name + "-$hexDigest.snapshot").absoluteFile - if (!file.exists()) { - val snapshot = service.calculateClasspathSnapshot(lib, ClassSnapshotGranularity.CLASS_MEMBER_LEVEL) - println("Saving... $file") - file.parentFile.mkdirs() - //println(snapshot.classSnapshots) - snapshot.saveSnapshot(file) - } else { - //println("Loading... $file") - } - snapshots += file - } else { - val snapshot = service.calculateClasspathSnapshot(lib, ClassSnapshotGranularity.CLASS_MEMBER_LEVEL) - val hash = snapshot.classSnapshots.values - .filterIsInstance() - .withIndex() - .sumOf { (index, snapshot) -> index * 31 + snapshot.classAbiHash } - val file = File(icWorkingDir, "dep-$hash.snapshot") - file.parentFile.mkdirs() - snapshot.saveSnapshot(file) - snapshots += file - } - } - - val shrunkClasspathSnapshotFile = File(icWorkingDir, "shrunk-classpath-snapshot.bin") - return ClasspathSnapshotBasedIncrementalCompilationApproachParameters( - snapshots, - //emptyList(), - shrunkClasspathSnapshotFile - ) - } - - fun getAllFiles(): List { - return sourceDirs.flatMap { it.walkBottomUp() }.filter { it.extension == "kt" }.map { it.absoluteFile } - } - - fun getAllFilesToModificationTime(): Map { - return getAllFiles().associateWith { it.lastModified() } - } - - private fun saveFileToTime(files: Map): String { - return files.entries.joinToString("\n") { "${it.key}:::${it.value}" } - } - - private fun loadFileToTime(text: String): Map { - return text.split("\n").filter { it.contains(":::") }.map { val (file, time) = it.split(":::"); File(file) to time.toLong() }.toMap() - } - - fun compileJvm(forceRecompilation: Boolean = false): CompilationResult { - buildDirectory.mkdirs() - if (forceRecompilation) { - filesTxtFile.delete() - } - val oldFiles = loadFileToTime(filesTxtFile.takeIf { it.exists() }?.readText() ?: "") - val allFiles = getAllFilesToModificationTime() - filesTxtFile.writeText(saveFileToTime(allFiles)) - val sourcesChanges = getModifiedFiles(oldFiles, allFiles) - - if (snapshot == null) { - snapshot = createSnapshots() - } - - return service.compileJvm( - projectId = ProjectId.ProjectUUID(UUID.randomUUID()), - strategyConfig = executionConfig, - compilationConfig = service.makeJvmCompilationConfiguration().also { compilationConfig -> - compilationConfig.useIncrementalCompilation( - icCachesDir, - sourcesChanges, - snapshot!!, - compilationConfig.makeClasspathSnapshotBasedIncrementalCompilationConfiguration().also { - it.setBuildDir(buildDirectory) - it.setRootProjectDir(rootDir) - //it.forceNonIncrementalMode(true) - } - ) - }, - //listOf(File("/temp/1")), - sources = listOf( - //File("/temp/1"), - //File("/temp/1-common") - //File("C:\\Users\\soywiz\\projects\\korge-snake\\src"), - //File("C:\\Users\\soywiz\\projects\\korge-snake\\modules\\korma-tile-matching\\src\\commonMain\\kotlin"), - ) + allFiles.map { it.key }, - //listOf(File("/temp/1-common")), - arguments = listOf( - "-module-name=${rootDir.name}", - "-Xjdk-release=17", - //"-Xuse-fast-jar-file-system", - "-jvm-target=17", - "-Xmulti-platform", - //"-progressive", - "-language-version=1.9", - "-api-version=1.9", - "-no-stdlib", - "-no-reflect", - "-Xexpect-actual-classes", - "-Xenable-incremental-compilation", - "-classpath=${libs.joinToString(File.pathSeparator) { it.absolutePath }}", - "-d", - classesDir, - //add("-Xfriend-paths=${friendPaths.joinToString(",")}") - //"C:\\Users\\soywiz\\projects\\korge-snake\\src", - //"C:\\Users\\soywiz\\projects\\korge-snake\\modules\\korma-tile-matching\\src\\commonMain\\kotlin", - ) - ) - } - - fun getJavaCommandLine() { - - } - - fun getModifiedFiles(old: Map, new: Map): SourcesChanges.Known { - val modified = arrayListOf() - val removed = arrayListOf() - for ((file, newTime) in new) { - if (file !in old) { - removed += file - } else if (old[file] != newTime) { - modified += file - } - } - return SourcesChanges.Known(modified, removed) - } -} - - diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompilerCLI.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompilerCLI.kt deleted file mode 100644 index b4ec3aeb80..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/KorgeKotlinCompilerCLI.kt +++ /dev/null @@ -1,42 +0,0 @@ -package korlibs.korge.kotlincompiler - -import java.io.File - -object KorgeKotlinCompilerCLI { - @JvmStatic - fun main(args: Array) { - for (line in System.`in`.bufferedReader().lineSequence()) { - val command = line.substringBefore(' ') - val params = line.substringAfter(' ', "") - when (command) { - "listen" -> { - val socketFile = File("$params.socket") - val pidFile = File("$params.pid") - val currentPid = ProcessHandle.current().pid() - pidFile.writeText("$currentPid") - //println("Listening on $currentPid") - TODO() - } - "exit" -> { - System.exit(0) - } - "compile" -> { - } - "run" -> { - } - "stop" -> { - } - "package:jvm" -> { - } - //"package:js" -> { - //} - //"package:wasm" -> { - //} - //"package:ios" -> { - //} - //"package:android" -> { - //} - } - } - } -} diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenArtifact.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenArtifact.kt deleted file mode 100644 index ef469acd88..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenArtifact.kt +++ /dev/null @@ -1,6 +0,0 @@ -package korlibs.korge.kotlincompiler.maven - -data class MavenArtifact(val group: String, val name: String, val version: String, val classifier: String? = null, val extension: String = "jar") { - val groupSeparator by lazy { group.replace(".", "/") } - val localPath by lazy { "$groupSeparator/$name/$version/$name-$version.$extension" } -} diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenDependency.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenDependency.kt deleted file mode 100644 index 0f38cbafcb..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenDependency.kt +++ /dev/null @@ -1,3 +0,0 @@ -package korlibs.korge.kotlincompiler.maven - -data class MavenDependency(val artifact: MavenArtifact, val scope: String) diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenTools.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenTools.kt deleted file mode 100644 index d3d803e665..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/MavenTools.kt +++ /dev/null @@ -1,37 +0,0 @@ -package korlibs.korge.kotlincompiler.maven - -import java.io.* -import java.net.* - -object MavenTools { - fun getMavenArtifacts(artifact: MavenArtifact, explored: MutableSet = mutableSetOf()): Set { - val explore = ArrayDeque() - explore += artifact - val out = mutableSetOf() - while (explore.isNotEmpty()) { - val artifact = explore.removeFirst() - if (artifact in explored) continue - explored += artifact - val pom = Pom.parse(getSingleMavenArtifact(artifact.copy(extension = "pom"))) - if (pom.packaging == null || pom.packaging == "jar") { - out += artifact - } - for (dep in pom.deps) { - explore += dep.artifact - } - } - return out.map { getSingleMavenArtifact(it) }.toSet() - } - - fun getSingleMavenArtifact(artifact: MavenArtifact): File { - val file = File(System.getProperty("user.home"), ".m2/repository/${artifact.localPath}") - if (!file.exists()) { - file.parentFile.mkdirs() - val url = URL("https://repo1.maven.org/maven2/${artifact.localPath}") - println("Downloading $url") - file.writeBytes(url.readBytes()) - } - return file - } - -} diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/Pom.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/Pom.kt deleted file mode 100644 index d1ccc45ef8..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/maven/Pom.kt +++ /dev/null @@ -1,35 +0,0 @@ -package korlibs.korge.kotlincompiler.maven - -import org.w3c.dom.* -import java.io.* -import javax.xml.* -import javax.xml.parsers.* - -class Pom( - val packaging: String? = null, - val deps: List = emptyList(), -) { - companion object { - fun parse(file: File): Pom = parse(file.readText()) - fun parse(text: String): Pom { - val db = DocumentBuilderFactory.newInstance().also { it.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true) }.newDocumentBuilder() - val doc = db.parse(text.byteInputStream()) - val out = arrayListOf() - val node = doc.getElementsByTagName("packaging").toList().firstOrNull() - for (e in doc.getElementsByTagName("dependency").toList()) { - val groupId = e.findChildByTagName("groupId").firstOrNull()?.textContent?.trim() ?: error("Missing groupId") - val artifactId = e.findChildByTagName("artifactId").firstOrNull()?.textContent?.trim() ?: error("Missing artifactId") - val scope = e.findChildByTagName("scope").firstOrNull()?.textContent?.trim() - if (scope == "test" || scope == null) continue - val version = e.findChildByTagName("version").firstOrNull()?.textContent?.trim() ?: error("Missing version for $groupId:$artifactId in $text") - if (version.contains("\$")) continue - out += MavenDependency(MavenArtifact(groupId, artifactId, version), scope ?: "compile") - //println("DEP: $groupId:$artifactId:$version :: $scope") - } - return Pom(packaging = node?.textContent, deps = out) - } - - private fun NodeList.toList(): List = (0 until length).map { item(it) } - private fun Node.findChildByTagName(tagName: String): List = childNodes.toList().filter { it.nodeName.equals(tagName, ignoreCase = true) } - } -} diff --git a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/module/ModuleParser.kt b/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/module/ModuleParser.kt deleted file mode 100644 index b30b808404..0000000000 --- a/korge-kotlin-compiler/src/main/kotlin/korlibs/korge/kotlincompiler/module/ModuleParser.kt +++ /dev/null @@ -1,4 +0,0 @@ -package korlibs.korge.kotlincompiler.module - -class ModuleParser { -} diff --git a/settings.gradle.kts b/settings.gradle.kts index a99cc00d42..135f9ddba9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,7 +28,6 @@ val disabledExtraKorgeLibs = isPropertyTrue("DISABLED_EXTRA_KORGE_LIBS") include(":korge") include(":korge-core") include(":korge-kotlin-plugin") -include(":korge-kotlin-compiler") include(":korge-gradle-plugin") include(":korge-gradle-plugin-common") include(":korge-gradle-plugin-settings")