From e8998e3755abeb01811016b183c0e768950c72cf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 25 Jan 2025 17:32:58 +0000 Subject: Add macro guard to generated C header files --- .../compiler/backends/PatioNativeCHeadersBackend.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/main/java') 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; -- cgit v1.2.3