From 85f9578fb680ab4ee959469133714acd908a7c10 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 24 Mar 2024 14:52:59 +0000 Subject: Support custom runtimes when compiling --- .../org/shedlang/compiler/runtime/ShedRuntime.java | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java (limited to 'src/main/java/org/shedlang/compiler/runtime') diff --git a/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java b/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java new file mode 100644 index 00000000..25a81345 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java @@ -0,0 +1,73 @@ +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.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?) + // TODO: handle GC config + var runtimeDir = targetDir.topLevel("runtime"); + var buildRuntimeCommand = List.of( + "python3", + ShedCompilerFiles.getRoot().resolve("runtime/build.py").toString(), + "custom", + "--target", targetName, + "--gc", gc.name(), + "--output-dir", runtimeDir.toString() + ); + runInheritIo(buildRuntimeCommand); + + return new ShedRuntime(runtimeDir); + } + + private final Path dir; + + private ShedRuntime(Path dir) { + this.dir = dir; + } + + public Path dir() { + return this.dir; + } +} -- cgit v1.2.3