aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-19 20:14:17 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-19 20:14:17 +0100
commit2d0ea7d1e81f708aaf9066fc92784d7b44ff3e74 (patch)
treec7297d8b24ffc0ccb3e204727340ae14fdce6b7b /src/main/java
parentd7efb0a84f80452ab87978e845593a504e06f5d4 (diff)
Tidy up functions for running processes
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/Backend.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/ProcessRunner.java26
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java31
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java27
-rw-r--r--src/main/java/org/shedlang/compiler/cli/ShedCli.java4
6 files changed, 41 insertions, 51 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java
index 479a9a4f..9b030916 100644
--- a/src/main/java/org/shedlang/compiler/ShedCompiler.java
+++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java
@@ -26,7 +26,7 @@ import java.util.stream.StreamSupport;
import static org.shedlang.compiler.parser.Tokeniser.tokenise;
public class ShedCompiler {
- public void compile(Path projectRoot, Backend backend) throws IOException {
+ public void compile(Path projectRoot, Backend backend) throws IOException, InterruptedException {
var config = ProjectConfig.read(projectRoot);
var sourceRoot = projectRoot.resolve("src");
diff --git a/src/main/java/org/shedlang/compiler/backends/Backend.java b/src/main/java/org/shedlang/compiler/backends/Backend.java
index 54abde81..dd993cbd 100644
--- a/src/main/java/org/shedlang/compiler/backends/Backend.java
+++ b/src/main/java/org/shedlang/compiler/backends/Backend.java
@@ -16,7 +16,7 @@ public interface Backend {
Map<NamespaceName, List<Instruction>> namespaces,
TypeInfo typeInfo,
Path targetDir
- ) throws IOException;
+ ) throws IOException, InterruptedException;
String name();
}
diff --git a/src/main/java/org/shedlang/compiler/backends/ProcessRunner.java b/src/main/java/org/shedlang/compiler/backends/ProcessRunner.java
new file mode 100644
index 00000000..d71bd25b
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/ProcessRunner.java
@@ -0,0 +1,26 @@
+package org.shedlang.compiler.backends;
+
+import java.io.IOException;
+import java.util.List;
+
+public class ProcessRunner {
+ private ProcessRunner() {
+ }
+
+ /**
+ * Run a process until it exits, inheriting standard IO from its parent
+ * process.
+ */
+ public static void runInheritIo(List<String> args) throws IOException, InterruptedException {
+ var process = new ProcessBuilder(args)
+ .inheritIO()
+ .start();
+
+ var exitCode = process.waitFor();
+ if (exitCode != 0) {
+ // TODO: better error code
+ throw new RuntimeException("exit code: " + exitCode);
+ }
+ }
+
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
index f5c43efc..9bb113dd 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
@@ -19,6 +19,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo;
+
public class LlvmBackend implements Backend {
@Override
public void compile(
@@ -26,7 +28,7 @@ public class LlvmBackend implements Backend {
Map<NamespaceName, List<Instruction>> namespaces,
TypeInfo typeInfo,
Path targetDir
- ) throws IOException {
+ ) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
for (var namespace : namespaces.entrySet()) {
var namespaceName = namespace.getKey();
@@ -57,7 +59,7 @@ public class LlvmBackend implements Backend {
gccArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/build/libshed.a").toString());
- run(gccArgs);
+ runInheritIo(gccArgs);
}
}
@@ -70,7 +72,7 @@ public class LlvmBackend implements Backend {
LlvmModule llvmModule,
Path workingDirectory,
String name
- ) throws IOException {
+ ) throws IOException, InterruptedException {
var llvmModulePath = workingDirectory.resolve(name + ".ll");
Files.createDirectories(llvmModulePath.getParent());
var fileWriter = new FileWriter(llvmModulePath.toFile(), StandardCharsets.UTF_8);
@@ -84,8 +86,8 @@ public class LlvmBackend implements Backend {
return objectPath;
}
- private static void runLlvmCompiler(Path llvmModulePath, Path objectPath) throws IOException {
- run(List.of(
+ private static void runLlvmCompiler(Path llvmModulePath, Path objectPath) throws IOException, InterruptedException {
+ runInheritIo(List.of(
"llc-16", llvmModulePath.toString(),
"-filetype", "obj",
"-relocation-model", "pic",
@@ -93,23 +95,4 @@ public class LlvmBackend implements Backend {
));
}
- private static void run(List<String> args) throws IOException {
- var process = new ProcessBuilder(args)
- .inheritIO()
- .start();
-
- while (true) {
- try {
- var exitCode = process.waitFor();
- if (exitCode != 0) {
- // TODO: better error code
- throw new RuntimeException("exit code: " + exitCode);
- } else {
- return;
- }
- } catch (InterruptedException exception) {
-
- }
- }
- }
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
index dc6fd468..1526671d 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
@@ -17,6 +17,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo;
+
public class WasmBackend implements Backend {
@Override
public void compile(
@@ -24,7 +26,7 @@ public class WasmBackend implements Backend {
Map<NamespaceName, List<Instruction>> namespaces,
TypeInfo typeInfo,
Path targetDir
- ) throws IOException {
+ ) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
for (var namespace : namespaces.entrySet()) {
var namespaceName = namespace.getKey();
@@ -59,7 +61,7 @@ public class WasmBackend implements Backend {
ldArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/build-wasm/libshed.o").toString());
- run(ldArgs);
+ runInheritIo(ldArgs);
}
}
@@ -76,25 +78,4 @@ public class WasmBackend implements Backend {
}
return objectPath;
}
-
- // TODO: extract common run() code
- private static void run(List<String> args) throws IOException {
- var process = new ProcessBuilder(args)
- .inheritIO()
- .start();
-
- while (true) {
- try {
- var exitCode = process.waitFor();
- if (exitCode != 0) {
- // TODO: better error code
- throw new RuntimeException("exit code: " + exitCode);
- } else {
- return;
- }
- } catch (InterruptedException exception) {
-
- }
- }
- }
}
diff --git a/src/main/java/org/shedlang/compiler/cli/ShedCli.java b/src/main/java/org/shedlang/compiler/cli/ShedCli.java
index f8ab153a..95dfcd95 100644
--- a/src/main/java/org/shedlang/compiler/cli/ShedCli.java
+++ b/src/main/java/org/shedlang/compiler/cli/ShedCli.java
@@ -12,7 +12,7 @@ import java.io.IOException;
import java.nio.file.Paths;
public class ShedCli {
- public static void main(String[] rawArgs) throws IOException {
+ public static void main(String[] rawArgs) throws IOException, InterruptedException {
var args = parseArgs(rawArgs);
try {
compile(args);
@@ -23,7 +23,7 @@ public class ShedCli {
}
}
- private static void compile(Namespace args) throws IOException {
+ private static void compile(Namespace args) throws IOException, InterruptedException {
var sourcePath = Paths.get(args.getString("source"));
var compiler = new ShedCompiler();