Skip to content

Commit

Permalink
Add the ability to set kotlinc compiler flags
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed Sep 5, 2024
1 parent 5095dcb commit aa002eb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private val oatDumpParser = OatDumpParser()

suspend fun buildAndRun(
toolPaths: ToolPaths,
compilerFlags: String,
source: String,
onLogs: (AnnotatedString) -> Unit,
onStatusUpdate: (String, Float) -> Unit
Expand All @@ -62,7 +63,7 @@ suspend fun buildAndRun(
Files.writeString(path, source)
writeSupportFiles(directory)

val kotlinc = KotlinCompiler(toolPaths, directory).compile(path)
val kotlinc = KotlinCompiler(toolPaths, directory).compile(compilerFlags, path)

if (kotlinc.exitCode != 0) {
withContext(ui) {
Expand Down Expand Up @@ -95,6 +96,7 @@ suspend fun buildAndRun(
suspend fun buildAndDisassemble(
toolPaths: ToolPaths,
source: String,
compilerFlags: String,
r8rules: String,
minApi: Int,
instructionSets: Map<ISA, Boolean>,
Expand Down Expand Up @@ -122,7 +124,7 @@ suspend fun buildAndDisassemble(
Files.writeString(path, source)
writeSupportFiles(directory)

val kotlinc = KotlinCompiler(toolPaths, directory).compile(path)
val kotlinc = KotlinCompiler(toolPaths, directory).compile(compilerFlags, path)

if (kotlinc.exitCode != 0) {
updater.addJob(launch(ui) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ private fun FrameWindowScope.MainMenu(
buildAndDisassemble(
explorerState.toolPaths,
sourceTextArea.text,
explorerState.compilerFlags,
explorerState.r8Rules,
explorerState.minApi,
instructionSets,
Expand All @@ -543,6 +544,7 @@ private fun FrameWindowScope.MainMenu(
scope.launch {
buildAndRun(
explorerState.toolPaths,
explorerState.compilerFlags,
sourceTextArea.text,
onLogsUpdate,
onStatusUpdate
Expand Down
9 changes: 7 additions & 2 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fun Settings(
) {
val androidHome = rememberTextFieldState(state.androidHome)
val kotlinHome = rememberTextFieldState(state.kotlinHome)
val compilerFlags = rememberTextFieldState(state.compilerFlags)
val r8rules = rememberTextFieldState(state.r8Rules)
val minApi = rememberTextFieldState(state.minApi.toString())
val indent = rememberTextFieldState(state.indent.toString())
Expand All @@ -48,6 +49,7 @@ fun Settings(
state.saveState(
androidHome.text.toString(),
kotlinHome.text.toString(),
compilerFlags.text.toString(),
r8rules.text.toString(),
minApi.text.toString(),
indent.text.toString(),
Expand All @@ -63,6 +65,7 @@ fun Settings(
StringSetting("Kotlin home directory: ", kotlinHome) { toolPaths.isKotlinHomeValid }
IntSetting("Decompiled code indent: ", indent, minValue = 2)
IntSetting("Line number column width: ", lineNumberWidth, minValue = 1)
StringSetting("Kotlin compiler flags: ", compilerFlags)
MultiLineStringSetting("R8 rules: ", r8rules)
IntSetting("Min API: ", minApi, minValue = 1)
BooleanSetting("Decompile hidden instruction sets", decompileHiddenIsa)
Expand Down Expand Up @@ -90,6 +93,7 @@ private fun ColumnScope.Buttons(
private fun ExplorerState.saveState(
androidHome: String,
kotlinHome: String,
compilerFlags: String,
r8Rules: String,
minApi: String,
indent: String,
Expand All @@ -98,6 +102,7 @@ private fun ExplorerState.saveState(
) {
this.androidHome = androidHome
this.kotlinHome = kotlinHome
this.compilerFlags = compilerFlags
this.r8Rules = r8Rules
this.minApi = minApi.toIntOrNull() ?: 21
this.indent = indent.toIntOrNull() ?: 4
Expand All @@ -107,8 +112,8 @@ private fun ExplorerState.saveState(
}

@Composable
private fun StringSetting(title: String, state: TextFieldState, isValid: () -> Boolean) {
SettingRow(title, state, isValid,)
private fun StringSetting(title: String, state: TextFieldState, isValid: () -> Boolean = { true }) {
SettingRow(title, state, isValid)
}

@Composable
Expand Down
2 changes: 2 additions & 0 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private const val AndroidHome = "ANDROID_HOME"
private const val KotlinHome = "KOTLIN_HOME"
private const val Optimize = "OPTIMIZE"
private const val KeepEverything = "KEEP_EVERYTHING"
private const val CompilerFlags = "COMPILER_FLAGS"
private const val R8Rules = "R8_RULES"
private const val MinApi = "MIN_API"
private const val AutoBuildOnStartup = "AUTO_BUILD_ON_STARTUP"
Expand Down Expand Up @@ -58,6 +59,7 @@ class ExplorerState {
var toolPaths by mutableStateOf(createToolPaths())
var optimize by BooleanState(Optimize, true)
var keepEverything by BooleanState(KeepEverything, true)
var compilerFlags by StringState(CompilerFlags, "")
var r8Rules by StringState(R8Rules, "")
var minApi by IntState(MinApi, 21)
var autoBuildOnStartup by BooleanState(AutoBuildOnStartup, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import dev.romainguy.kotlin.explorer.process
import java.nio.file.Path

class KotlinCompiler(private val toolPaths: ToolPaths, private val outputDirectory: Path) {
suspend fun compile(compilerFlags: String, source: Path) =
process(*buildCompileCommand(compilerFlags, source), directory = outputDirectory)

suspend fun compile(source: Path) = process(*buildCompileCommand(source), directory = outputDirectory)

private fun buildCompileCommand(file: Path): Array<String> {
private fun buildCompileCommand(compilerFlags: String, file: Path): Array<String> {
val command = mutableListOf(
toolPaths.kotlinc.toString(),
"-Xmulti-platform",
Expand All @@ -35,7 +35,10 @@ class KotlinCompiler(private val toolPaths: ToolPaths, private val outputDirecto
(toolPaths.kotlinLibs + listOf(toolPaths.platform)).joinToString(":") { jar -> jar.toString() },
file.toString(),
file.parent.resolve("Keep.kt").toString()
)
).apply {
// TODO: Do something smarter in case a flag looks like -foo="something with space"
addAll(compilerFlags.split(' '))
}

return command.toTypedArray()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LocalBuilder(private val outputDirectory: Path) : Builder {
}

private suspend fun kotlinCompile(path: Path) {
val result = kotlinCompiler.compile(path)
val result = kotlinCompiler.compile(compilerFlags, path)
if (result.exitCode != 0) {
System.err.println(result.output)
fail("kotlinc error")
Expand Down

0 comments on commit aa002eb

Please sign in to comment.