aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-05 19:21:39 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-05 19:21:39 +0000
commit208f9539c41657e3b2b877d4c5985fade7ff7689 (patch)
treea56da526090ed0667632360efef91724385cf040 /src/main/java
parent99f491698ed4954b38ec4226920ca767121fc14a (diff)
Introduce classes for consistent target directory layout
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java3
-rw-r--r--src/main/java/org/shedlang/compiler/backends/Backend.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java8
-rw-r--r--src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java10
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java21
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java14
-rw-r--r--src/main/java/org/shedlang/compiler/backends/paths/PartialTargetPath.java15
-rw-r--r--src/main/java/org/shedlang/compiler/backends/paths/TargetDir.java30
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java15
9 files changed, 79 insertions, 41 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java
index 168db252..8c2407d6 100644
--- a/src/main/java/org/shedlang/compiler/ShedCompiler.java
+++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler;
import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode;
import org.shedlang.compiler.backends.Backend;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ConfigChecker;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.parser.Parser;
@@ -71,7 +72,7 @@ public class ShedCompiler {
config,
namespaces,
typeInfos,
- config.targetDir().resolve(backend.targetName())
+ new TargetDir(config.targetDir().resolve(backend.targetName()))
);
}
diff --git a/src/main/java/org/shedlang/compiler/backends/Backend.java b/src/main/java/org/shedlang/compiler/backends/Backend.java
index 370e73ba..9b610dce 100644
--- a/src/main/java/org/shedlang/compiler/backends/Backend.java
+++ b/src/main/java/org/shedlang/compiler/backends/Backend.java
@@ -1,11 +1,11 @@
package org.shedlang.compiler.backends;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.typechecker.TypeInfos;
import java.io.IOException;
-import java.nio.file.Path;
import java.util.List;
public interface Backend {
@@ -15,6 +15,6 @@ public interface Backend {
ProjectConfig config,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
- Path targetDir
+ TargetDir targetDir
) throws IOException, InterruptedException;
}
diff --git a/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java
index 8a52eae7..7bc2e925 100644
--- a/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java
@@ -1,5 +1,6 @@
package org.shedlang.compiler.backends;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.patio.PatioCodeGen;
import org.shedlang.compiler.patio.PatioCodeGenContext;
@@ -9,7 +10,6 @@ import org.shedlang.compiler.typechecker.TypeInfos;
import java.io.IOException;
import java.nio.file.Files;
-import java.nio.file.Path;
import java.util.List;
public class PatioTextBackend implements Backend {
@@ -22,7 +22,7 @@ public class PatioTextBackend implements Backend {
ProjectConfig config,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
- Path targetDir
+ TargetDir targetDir
) throws IOException {
var context = PatioCodeGenContext.forTypeInfos(typeInfos);
@@ -36,8 +36,8 @@ public class PatioTextBackend implements Backend {
PatioSerializer.serializeCompilationUnit(compilationUnit, builder);
var compilationUnitString = builder.toString();
- var path = targetDir.resolve("namespaces")
- .resolve(String.join("_", namespace.namespaceName().parts()) + ".patio-text");
+ var path = targetDir.namespace(namespace.namespaceName())
+ .withExtension(".patio-text");
Files.createDirectories(path.getParent());
Files.writeString(path, compilationUnitString);
}
diff --git a/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java b/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java
index 3a6962b0..ceda128b 100644
--- a/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java
@@ -1,13 +1,13 @@
package org.shedlang.compiler.backends;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ProjectConfig;
-import org.shedlang.compiler.shid.ShidSerializer;
import org.shedlang.compiler.shid.ShidNamespace;
+import org.shedlang.compiler.shid.ShidSerializer;
import org.shedlang.compiler.typechecker.TypeInfos;
import java.io.IOException;
import java.nio.file.Files;
-import java.nio.file.Path;
import java.util.List;
public class ShidTextBackend implements Backend {
@@ -20,7 +20,7 @@ public class ShidTextBackend implements Backend {
ProjectConfig config,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
- Path targetDir
+ TargetDir targetDir
) throws IOException {
for (var namespace : namespaces) {
var instructions = namespace.instructions();
@@ -29,8 +29,8 @@ public class ShidTextBackend implements Backend {
ShidSerializer.serializeInstructions(instructions, builder);
var instructionsString = builder.toString();
- var path = targetDir.resolve("namespaces")
- .resolve(String.join("_", namespace.namespaceName().parts()) + ".shid-text");
+ var path = targetDir.namespace(namespace.namespaceName())
+ .withExtension(".shid-text");
Files.createDirectories(path.getParent());
Files.writeString(path, instructionsString);
}
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 be6d115b..b40c6455 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java
@@ -10,12 +10,13 @@ import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmTrunc;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableGlobal;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableLocal;
import org.shedlang.compiler.backends.llvm.ir.ast.types.LlvmTypes;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.filesystem.ShedCompilerFiles;
import org.shedlang.compiler.patio.PatioCodeGen;
import org.shedlang.compiler.patio.PatioCodeGenContext;
-import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.shid.ShidNamespace;
+import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.typechecker.TypeInfos;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.util.processes.ProcessError;
@@ -26,6 +27,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
+import static org.shedlang.compiler.backends.llvm.LlvmCompiler.compileLlvmModule;
import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo;
public class LlvmBackend implements Backend {
@@ -44,7 +46,7 @@ public class LlvmBackend implements Backend {
ProjectConfig config,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
- Path targetDir
+ TargetDir targetDir
) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
for (var namespace : namespaces) {
@@ -57,8 +59,7 @@ public class LlvmBackend implements Backend {
var objectPath = compileLlvmModule(
llvmModule,
- targetDir.resolve("namespaces"),
- String.join("_", namespace.namespaceName().parts())
+ targetDir.namespace(namespace.namespaceName())
);
objectFilePaths.add(objectPath);
@@ -68,10 +69,10 @@ public class LlvmBackend implements Backend {
var linkInputs = new ArrayList<>(objectFilePaths);
var mainModule = this.generateMain(bin.mainNamespace(), typeInfos);
- var mainObjectPath = compileLlvmModule(mainModule, targetDir.resolve("bins"), bin.name());
+ var mainObjectPath = compileLlvmModule(mainModule, targetDir.bin(bin.name()));
linkInputs.add(mainObjectPath);
- link(linkInputs, targetDir.resolve(bin.name()));
+ link(linkInputs, targetDir.topLevel(bin.name()));
}
}
@@ -133,12 +134,4 @@ public class LlvmBackend implements Backend {
return llvmModule;
}
-
- private static Path compileLlvmModule(
- LlvmModule llvmModule,
- Path workingDirectory,
- String name
- ) throws IOException, InterruptedException {
- return new LlvmCompiler(workingDirectory).compileLlvmModule(llvmModule, name);
- }
}
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
index b9d82386..0d8ab3cc 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.backends.llvm;
import org.shedlang.compiler.backends.llvm.ir.LlvmIrSerializer;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmModule;
+import org.shedlang.compiler.backends.paths.PartialTargetPath;
import java.io.BufferedWriter;
import java.io.FileWriter;
@@ -14,24 +15,21 @@ import java.util.List;
import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo;
public class LlvmCompiler {
- private final Path workingDirectory;
-
- public LlvmCompiler(Path workingDirectory) {
- this.workingDirectory = workingDirectory;
+ private LlvmCompiler() {
}
- public Path compileLlvmModule(
+ public static Path compileLlvmModule(
LlvmModule llvmModule,
- String name
+ PartialTargetPath targetPath
) throws IOException, InterruptedException {
- var llvmModulePath = workingDirectory.resolve(name + ".ll");
+ var llvmModulePath = targetPath.withExtension(".ll");
Files.createDirectories(llvmModulePath.getParent());
var fileWriter = new FileWriter(llvmModulePath.toFile(), StandardCharsets.UTF_8);
try (var writer = new BufferedWriter(fileWriter)) {
LlvmIrSerializer.serializeModule(llvmModule, writer);
}
- var objectPath = workingDirectory.resolve(name + ".o");
+ var objectPath = targetPath.withExtension(".o");
runLlvmCompiler(llvmModulePath, objectPath);
return objectPath;
diff --git a/src/main/java/org/shedlang/compiler/backends/paths/PartialTargetPath.java b/src/main/java/org/shedlang/compiler/backends/paths/PartialTargetPath.java
new file mode 100644
index 00000000..cd40e00f
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/paths/PartialTargetPath.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.backends.paths;
+
+import java.nio.file.Path;
+
+public class PartialTargetPath {
+ private final Path path;
+
+ public PartialTargetPath(Path path) {
+ this.path = path;
+ }
+
+ public Path withExtension(String extension) {
+ return this.path.getParent().resolve(this.path.getFileName() + extension);
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/paths/TargetDir.java b/src/main/java/org/shedlang/compiler/backends/paths/TargetDir.java
new file mode 100644
index 00000000..cb6a14af
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/paths/TargetDir.java
@@ -0,0 +1,30 @@
+package org.shedlang.compiler.backends.paths;
+
+import org.shedlang.compiler.types.NamespaceName;
+
+import java.nio.file.Path;
+
+public class TargetDir {
+ private final Path targetDir;
+
+ public TargetDir(Path targetDir) {
+ this.targetDir = targetDir;
+ }
+
+ public PartialTargetPath bin(String name) {
+ return new PartialTargetPath(
+ targetDir.resolve("bin").resolve(name)
+ );
+ }
+
+ public PartialTargetPath namespace(NamespaceName namespaceName) {
+ return new PartialTargetPath(
+ targetDir.resolve("namespaces")
+ .resolve(String.join("_", namespaceName.parts()))
+ );
+ }
+
+ public Path topLevel(String name) {
+ return targetDir.resolve(name);
+ }
+}
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 2b0d31f0..ad311877 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
@@ -1,6 +1,8 @@
package org.shedlang.compiler.backends.wasm;
import org.shedlang.compiler.backends.Backend;
+import org.shedlang.compiler.backends.paths.PartialTargetPath;
+import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmDrop;
@@ -40,7 +42,7 @@ public class WasmBackend implements Backend {
ProjectConfig config,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
- Path targetDir
+ TargetDir targetDir
) throws IOException, InterruptedException {
var objectFilePaths = new ArrayList<Path>();
for (var namespace : namespaces) {
@@ -53,8 +55,7 @@ public class WasmBackend implements Backend {
var objectPath = writeObjectFile(
wasmModule,
- targetDir.resolve("namespaces"),
- String.join("_", namespace.namespaceName().parts())
+ targetDir.namespace(namespace.namespaceName())
);
objectFilePaths.add(objectPath);
}
@@ -63,10 +64,10 @@ public class WasmBackend implements Backend {
var linkInputs = new ArrayList<>(objectFilePaths);
var mainModule = this.generateMain(bin.mainNamespace(), typeInfos);
- var mainObjectPath = writeObjectFile(mainModule, targetDir.resolve("bins"), bin.name());
+ var mainObjectPath = writeObjectFile(mainModule, targetDir.bin(bin.name()));
linkInputs.add(mainObjectPath);
- link(linkInputs, targetDir.resolve(bin.name() + ".wasm"));
+ link(linkInputs, targetDir.topLevel(bin.name() + ".wasm"));
}
}
@@ -124,8 +125,8 @@ public class WasmBackend implements Backend {
return wasmModule;
}
- private static Path writeObjectFile(WasmModule module, Path workingDirectory, String name) throws IOException {
- var objectPath = workingDirectory.resolve(name + ".o");
+ private static Path writeObjectFile(WasmModule module, PartialTargetPath targetPath) throws IOException {
+ var objectPath = targetPath.withExtension(".o");
Files.createDirectories(objectPath.getParent());
try (var outputStream = new FileOutputStream(objectPath.toFile())) {
WasmBinaryFormat.serializeModule(module, outputStream);