From 8a0f2cd0b398ee0b7740e44a0e5fb2f75d090ccb Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 30 May 2022 23:42:02 -0700 Subject: Changeset: 59 Integrate enum codegen into the actual project --- source/20-codegen-compiler/CodegenUtils.inl | 48 ++++++++--------------------- 1 file changed, 13 insertions(+), 35 deletions(-) (limited to 'source/20-codegen-compiler/CodegenUtils.inl') 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 #include -)"""[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 -#include + 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 #include using namespace std::literals; - )"""[1], - }); +)"""[1]; + + header.AddOutputThing(std::move(headerOut)); + source.AddOutputThing(std::move(sourceOut)); } } // namespace Utils -- cgit v1.2.3-70-g09d2