diff options
Diffstat (limited to 'source/20-codegen-compiler/CodegenUtils.inl')
-rw-r--r-- | source/20-codegen-compiler/CodegenUtils.inl | 48 |
1 files changed, 13 insertions, 35 deletions
diff --git a/source/20-codegen-compiler/CodegenUtils.inl b/source/20-codegen-compiler/CodegenUtils.inl index 6feb654..dddfe61 100644 --- a/source/20-codegen-compiler/CodegenUtils.inl +++ b/source/20-codegen-compiler/CodegenUtils.inl @@ -14,25 +14,7 @@ namespace Utils { -std::string ReadFileAsString(const std::filesystem::path& path) { - auto file = Utils::OpenCstdioFile(path, Utils::Read); - if (!file) throw std::runtime_error("Failed to open source file."); - DEFER { fclose(file); }; - - fseek(file, 0, SEEK_END); - auto fileSize = ftell(file); - rewind(file); - - std::string result(fileSize, '\0'); - fread(result.data(), fileSize, 1, file); - - return result; -} - -bool WriteOutputFile(const CodegenOutput& output, std::string_view dir, std::string_view filename, std::string_view additionalSuffix = {}) { - char path[2048]; - snprintf(path, sizeof(path), "%.*s/%.*s%.*s", PRINTF_STRING_VIEW(dir), PRINTF_STRING_VIEW(filename), PRINTF_STRING_VIEW(additionalSuffix)); - +bool WriteOutputFile(const CodegenOutput& output, const char* path) { auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); if (!outputFile) { printf("[ERROR] unable to open output file %s\n", path); @@ -74,9 +56,9 @@ std::string MakeFullName(std::string_view name, DeclNamespace* ns = nullptr) { return fullname; } -void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { - output.AddOutputThing(CodegenOutputThing{ - .text = &R"""( +void ProduceGeneratedHeader(const char* headerFilename, CodegenOutput& header, const char* sourceFilename, CodegenOutput& source) { + CodegenOutputThing headerOut; + headerOut.text += &R"""( // This file is generated. Any changes will be overidden when building. #pragma once @@ -84,23 +66,19 @@ void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { #include <cstddef> #include <cstdint> -)"""[1], - }); -} - -void ProduceGeneratedSourceFileHeader(CodegenOutput& output) { - output.AddOutputThing(CodegenOutputThing{ - // TODO we need to get the header name - .text = &R"""( -// This file is generated. Any changes will be overidden when building. +)"""[1]; -#include <cstddef> -#include <cstdint> + CodegenOutputThing sourceOut; + APPEND_LIT_LN(sourceOut.text, "// This file is generated. Any changes will be overidden when building."); + APPEND_FMT_LN(sourceOut.text, "#include \"%s\"", headerFilename); + sourceOut.text += &R"""( #include <frozen/string.h> #include <frozen/unordered_map.h> using namespace std::literals; - )"""[1], - }); +)"""[1]; + + header.AddOutputThing(std::move(headerOut)); + source.AddOutputThing(std::move(sourceOut)); } } // namespace Utils |