package org.shedlang.compiler.runtime; import org.shedlang.compiler.backends.paths.TargetDir; import org.shedlang.compiler.config.GcConfig; import org.shedlang.compiler.config.GcMarkAndSweepConfig; import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.util.processes.ProcessError; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo; public class ShedRuntime { private static final Map STANDARD_RUNTIMES = Map.ofEntries( Map.entry(GcConfig.BUMP, "bump"), Map.entry(GcMarkAndSweepConfig.DEFAULT, "mark-and-sweep") ); public static ShedRuntime defaultRuntime(String targetName) { return new ShedRuntime( ShedCompilerFiles.standardRuntimeTargetDir( targetName, "mark-and-sweep" ) ); } public static ShedRuntime runtime( String targetName, Optional gcConfig, TargetDir targetDir ) throws ProcessError, InterruptedException { var gc = gcConfig.orElse(GcConfig.DEFAULT); var standardRuntimeName = STANDARD_RUNTIMES.get(gc); if (standardRuntimeName != null) { return new ShedRuntime( ShedCompilerFiles.standardRuntimeTargetDir( targetName, standardRuntimeName ) ); } // TODO: deal with stale compiled runtimes (in build.py?) var runtimeDir = targetDir.topLevel("runtime"); var buildRuntimeCommand = new ArrayList<>(List.of( "python3", ShedCompilerFiles.getRoot().resolve("runtime/build.py").toString(), "custom", "--target", targetName, "--gc", gc.name(), "--output-dir", runtimeDir.toString() )); for (var option : gc.runtimeBuildOptions()) { buildRuntimeCommand.add("-D" + option); } runInheritIo(buildRuntimeCommand); return new ShedRuntime(runtimeDir); } private final Path dir; private ShedRuntime(Path dir) { this.dir = dir; } public Path dir() { return this.dir; } }