aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java
blob: 7c9dbdddc40f06bafe1045e5635fe9ee7e8bd004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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<GcConfig, String> 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> 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;
    }
}