aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-01-25 17:32:58 +0000
committerMichael Williamson <mike@zwobble.org>2025-01-25 17:32:58 +0000
commite8998e3755abeb01811016b183c0e768950c72cf (patch)
tree4fe2613bcead0903f02308917533738c4bbd6f3d /src/main/java
parent96c2c909ba7ec21a8eb349ef3902c68f9fa834a4 (diff)
Add macro guard to generated C header files
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java
index e483708b..caa84cfc 100644
--- a/src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java
@@ -54,8 +54,13 @@ public class PatioNativeCHeadersBackend implements Backend {
NamespaceName namespaceName,
PatioCompilationUnit patioCompilationUnit
) throws IOException, InterruptedException {
+ var macroName = String.format(
+ "SHED_NAMESPACES__%s__H",
+ String.join("__", namespaceName.parts())
+ );
serializeToDisk(
targetDir.namespace(namespaceName),
+ macroName,
patioCompilationUnit
);
@@ -69,21 +74,26 @@ public class PatioNativeCHeadersBackend implements Backend {
String mainFunctionName,
ProfileConfig profileConfig
) throws IOException, InterruptedException {
+ var macroName = String.format("SHED_BINS__%s__H", binName);
serializeToDisk(
targetDir.bin(binName),
+ macroName,
patioCompilationUnit
);
}
private void serializeToDisk(
PartialTargetPath partialTargetPath,
+ String macroName,
PatioCompilationUnit patioCompilationUnit
) throws IOException {
var empty = true;
var builder = new StringBuilder();
// TODO: path to header file? Use angle brackets?
- builder.append("#include \"shed.h\"\n");
+ builder.append(String.format("#ifndef %s\n", macroName));
+ builder.append(String.format("#define %s\n\n", macroName));
+ builder.append("#include \"shed.h\"\n\n");
for (var element : patioCompilationUnit.elements()) {
if (element instanceof PatioFunctionDeclaration patioFunctionDeclaration) {
// TODO: pass this information through rather than having to inspect the string?
@@ -101,6 +111,7 @@ public class PatioNativeCHeadersBackend implements Backend {
}
}
}
+ builder.append("\n#endif\n");
if (empty) {
return;