Skip to content

Commit 39d2382

Browse files
committed
Only configure runtimes that will actually be used
Closes #95.
1 parent 64b08a0 commit 39d2382

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎src/commonMain/kotlin/jvm.kt‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,22 @@ class JvmRuntimeConfig(recognizedArgs: Array<String>) :
127127
mainArgs += vars.calculate(config.jvmMainArgs, hints)
128128
debugList("Main arguments calculated:", mainArgs)
129129

130-
this.java = java
131-
132130
// If -XstartOnFirstThread is given, we'd like to issue a RUNLOOP:main directive.
133131
// But we should only do this if --jaunch-runloop wasn't also given, since that
134132
// is the more direct and runtime-agnostic way of setting the RUNLOOP mode.
135133
skipRunLoop = "runloop" in config.internalFlags
134+
135+
this.java = java
136+
configured = true
137+
}
138+
139+
override fun rawConfigValues(config: JaunchConfig): List<Array<String>> {
140+
return listOf(
141+
config.jvmClasspath,
142+
config.jvmRuntimeArgs,
143+
config.jvmMainClass,
144+
config.jvmMainArgs
145+
)
136146
}
137147

138148
override fun injectInto(vars: Vars) {

‎src/commonMain/kotlin/main.kt‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fun main(args: Array<String>) {
101101
val nonGlobalDirectives = executeGlobalDirectives(globalDirectiveFunctions,
102102
configDirectives, userArgs)
103103

104-
val runtimes = configureRuntimes(config, configDir, hints, vars)
104+
val runtimes = configureRuntimes(config, configDir, configDirectives, launchDirectives, hints, vars)
105105

106106
debugBanner("BUILDING ARGUMENT LISTS")
107107

@@ -377,6 +377,8 @@ private fun applyModeHints(
377377
private fun configureRuntimes(
378378
config: JaunchConfig,
379379
configDir: File,
380+
configDirectives: List<String>,
381+
launchDirectives: List<String>,
380382
hints: MutableSet<String>,
381383
vars: Vars
382384
): List<RuntimeConfig> {
@@ -385,10 +387,34 @@ private fun configureRuntimes(
385387
if (config.jvmEnabled == true) runtimes += JvmRuntimeConfig(config.jvmRecognizedArgs)
386388
if (config.pythonEnabled == true) runtimes += PythonRuntimeConfig(config.pythonRecognizedArgs)
387389

388-
// Discover the runtime installations.
390+
// Discover the runtime installations - but only for runtimes that are actually needed.
391+
for (r in runtimes) {
392+
// Check if this runtime will be used for any launch or config directives.
393+
if (
394+
r.directive in launchDirectives ||
395+
configDirectives.any { r.supportedDirectives.containsKey(it) }
396+
) {
397+
debugBanner("CONFIGURING RUNTIME: ${r.directive}")
398+
r.configure(configDir, config, hints, vars)
399+
}
400+
else {
401+
debugBanner("SKIPPING DORMANT RUNTIME: ${r.directive}")
402+
}
403+
}
404+
405+
// Now configure any runtimes that are DEPENDENCIES of the now-configured ones.
389406
for (r in runtimes) {
390-
debugBanner("CONFIGURING RUNTIME: ${r.directive}")
391-
r.configure(configDir, config, hints, vars)
407+
if (r.configured) continue
408+
val needed = runtimes.any { active ->
409+
active.configured && active.dependsOn(r, config)
410+
}
411+
if (needed) {
412+
debugBanner("CONFIGURING RUNTIME DEPENDENCY: ${r.directive}")
413+
r.configure(configDir, config, hints, vars)
414+
}
415+
else {
416+
debugBanner("SKIPPING UNNEEDED RUNTIME: ${r.directive}")
417+
}
392418
}
393419

394420
return runtimes

‎src/commonMain/kotlin/python.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class PythonRuntimeConfig(recognizedArgs: Array<String>) :
112112
debugList("Main arguments calculated:", mainArgs)
113113

114114
this.python = python
115+
configured = true
116+
}
117+
118+
override fun rawConfigValues(config: JaunchConfig): List<Array<String>> {
119+
return listOf(
120+
config.pythonRuntimeArgs,
121+
config.pythonScriptPath,
122+
config.pythonMainArgs
123+
)
115124
}
116125

117126
override fun injectInto(vars: Vars) {

‎src/commonMain/kotlin/runtime.kt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class RuntimeConfig(
1616
val runtimeArgs = mutableListOf<String>()
1717
val mainArgs = mutableListOf<String>()
1818
var mainProgram: String? = null
19+
var configured = false
1920

2021
/** Dictionary of supported directives and their associated implementations. */
2122
abstract val supportedDirectives: DirectivesMap
@@ -37,6 +38,12 @@ abstract class RuntimeConfig(
3738
vars: Vars
3839
)
3940

41+
/**
42+
* Returns the list of raw config value arrays that this runtime uses.
43+
* These will be checked for variable references to other runtimes.
44+
*/
45+
protected abstract fun rawConfigValues(config: JaunchConfig): List<Array<String>>
46+
4047
/** Populate variables with information about this runtime. */
4148
abstract fun injectInto(vars: Vars)
4249

@@ -65,13 +72,26 @@ abstract class RuntimeConfig(
6572
}.firstOrNull { it > 0 } ?: 0
6673
}
6774

75+
/**
76+
* Check if this runtime depends on the given dependency runtime.
77+
* A runtime depends on another if any of its raw config values reference
78+
* variables from that runtime's namespace (e.g., `${jvm.libjvmPath}` or `@{jvm.runtimeArgs}`).
79+
*/
80+
fun dependsOn(dependency: RuntimeConfig, config: JaunchConfig): Boolean {
81+
val pattern = Regex("""[$@]\{${dependency.prefix}\.[^}]+}""")
82+
return rawConfigValues(config).any { configArray ->
83+
configArray.any { value -> pattern.containsMatchIn(value) }
84+
}
85+
}
86+
6887
/**
6988
* Attempt to execute the given directive.
7089
* @param directive The directive to maybe execute.
7190
* @param args The arguments passed by the user, which the directive might wish to examine.
7291
* @return true iff the directive was successfully executed.
7392
*/
7493
fun tryDirective(directive: String, args: ProgramArgs): Boolean {
94+
if (!configured) return false
7595
val doDirective = supportedDirectives[directive] ?: return false
7696
debug("$prefix: executing directive: $directive")
7797
doDirective(args)

0 commit comments

Comments
 (0)