From d18a28a9659092952aef70a30a47726e7c16d31a Mon Sep 17 00:00:00 2001 From: rtk0c Date: Tue, 24 May 2022 21:47:55 -0700 Subject: Changeset: 38 Branch comment: [] Add infrastructure for codegen --- CMakeLists.txt | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index ad2bf16..3d349c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,50 @@ add_subdirectory(3rdparty/imgui) add_subdirectory(3rdparty/imguicolortextedit) add_subdirectory(3rdparty/tracy) +# ============================================================================== + +file(GLOB_RECURSE commonthings_SOURCES source-common/*.c source-common/*.cpp) +add_library(commonthings OBJECT ${commonthings_SOURCES}) + +set_target_properties(commonthings PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF + + # Many files include platform headers, we don't want to leak them - it's just simpler to disable unity build for everything + UNITY_BUILD OFF +) + +target_include_directories(commonthings PUBLIC source-common) +target_link_libraries(commonthings PUBLIC + # External dependencies + ${CONAN_LIBS} + glm::glm +) + +# ============================================================================== + +# NOTE: delibrately not recursive, see README.md in the folder for details +file(GLOB meta_codegen_SOURCES buildtools/codegen/*.c buildtools/codegen/*.cpp) +add_executable(meta_codegen ${meta_codegen_SOURCES}) + +set_target_properties(meta_codegen PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF + UNITY_BUILD OFF +) + +target_link_libraries(meta_codegen PRIVATE + # External dependencies + ${CONAN_LIBS} + + # Project internal components + commonthings +) + +# ============================================================================== + # add_executable requires at least one source file add_executable(${PROJECT_NAME} dummy.c) add_subdirectory(source) @@ -20,8 +64,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES UNITY_BUILD_UNIQUE_ID "${PROJECT_NAME}_UNITY_ID" ) -target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) set_target_properties(${PROJECT_NAME} PROPERTIES + CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF ) @@ -37,6 +81,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE ) target_link_libraries(${PROJECT_NAME} PRIVATE + # External dependencies ${CONAN_LIBS} OpenGL::GL glfw @@ -44,6 +89,9 @@ target_link_libraries(${PROJECT_NAME} PRIVATE imgui ImGuiColorTextEdit tracy + + # Project internal components + commonthings ) option(BRUSSEL_ENABLE_PROFILING "Whether profiling support is enabled or not." OFF) -- cgit v1.2.3-70-g09d2 From 6c3007295a6a8c6803933392834c974ec5f56aa0 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 27 May 2022 17:31:04 -0700 Subject: Changeset: 41 Add logic to parse enums --- CMakeLists.txt | 4 + buildtools/cmake/RTTI.cmake | 31 +++ buildtools/codegen/CodegenDefinition.hpp | 1 + buildtools/codegen/CodegenOutput.cpp | 7 + buildtools/codegen/CodegenOutput.hpp | 17 ++ buildtools/codegen/main.cpp | 257 +++++++++++-------------- buildtools/codegen/tests/examples/TestEnum.hpp | 8 + 7 files changed, 178 insertions(+), 147 deletions(-) create mode 100644 buildtools/cmake/RTTI.cmake create mode 100644 buildtools/codegen/CodegenDefinition.hpp create mode 100644 buildtools/codegen/CodegenOutput.cpp create mode 100644 buildtools/codegen/CodegenOutput.hpp create mode 100644 buildtools/codegen/tests/examples/TestEnum.hpp (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d349c5..19518bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ project(ProjectBrussel LANGUAGES C CXX) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() +include(buildtools/cmake/RTTI.cmake) + find_package(OpenGL REQUIRED) add_subdirectory(3rdparty/glfw) add_subdirectory(3rdparty/glm) @@ -53,6 +55,8 @@ target_link_libraries(meta_codegen PRIVATE commonthings ) +target_flag_rtti(meta_codegen OFF) + # ============================================================================== # add_executable requires at least one source file diff --git a/buildtools/cmake/RTTI.cmake b/buildtools/cmake/RTTI.cmake new file mode 100644 index 0000000..b948497 --- /dev/null +++ b/buildtools/cmake/RTTI.cmake @@ -0,0 +1,31 @@ +function(target_flag_rtti_msvc TARGET_NAME ENABLED) + if(ENABLED) + target_compile_options(${TARGET_NAME} PRIVATE /GR) + else() + target_compile_options(${TARGET_NAME} PRIVATE /GR-) + endif() +endfunction() + +function(target_flag_rtti_gcc TARGET_NAME ENABLED) + if(ENABLED) + target_compile_options(${TARGET_NAME} PRIVATE -frtti) + else() + target_compile_options(${TARGET_NAME} PRIVATE -fno-rtti) + endif() +endfunction() + +function(target_flag_rtti TARGET_NAME ENABLED) + if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + target_flag_rtti_msvc(${TARGET_NAME} ${ENABLED}) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC") + target_flag_rtti_msvc(${TARGET_NAME} ${ENABLED}) + elseif(CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "GNU") + target_flag_rtti_gcc(${TARGET_NAME} ${ENABLED}) + endif() + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + target_flag_rtti_gcc(${TARGET_NAME} ${ENABLED}) + else() + message(FATAL "target_flag_rtti(): Unknown compiler ${CMAKE_CXX_COMPILER_ID}") + endif() +endfunction() diff --git a/buildtools/codegen/CodegenDefinition.hpp b/buildtools/codegen/CodegenDefinition.hpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/buildtools/codegen/CodegenDefinition.hpp @@ -0,0 +1 @@ +#pragma once diff --git a/buildtools/codegen/CodegenOutput.cpp b/buildtools/codegen/CodegenOutput.cpp new file mode 100644 index 0000000..214ded6 --- /dev/null +++ b/buildtools/codegen/CodegenOutput.cpp @@ -0,0 +1,7 @@ +#include "CodegenOutput.hpp" + +#include + +void CodegenOutput::AddOutputThing(CodegenOutputThing thing) { + mOutThings.push_back(std::move(thing)); +} diff --git a/buildtools/codegen/CodegenOutput.hpp b/buildtools/codegen/CodegenOutput.hpp new file mode 100644 index 0000000..660feb1 --- /dev/null +++ b/buildtools/codegen/CodegenOutput.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +// A generic "thing" (could be anything, comments, string-concated functionsm, etc.) to spit into the output file +struct CodegenOutputThing { + std::string text; +}; + +class CodegenOutput { +private: + std::vector mOutThings; + +public: + void AddOutputThing(CodegenOutputThing thing); +}; diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index cf31bd8..9f89191 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -1,4 +1,5 @@ #include "CodegenLookupTable.h" +#include "CodegenOutput.hpp" #include "Macros.hpp" #include "ScopeGuard.hpp" #include "Utils.hpp" @@ -65,124 +66,10 @@ struct InputDefinitionEnum { EnumUnderlyingType underlyingType; }; -enum LexedTokenType { - // stb_c_lexer token types, ported over - LTT_Identifier, - LTT_IntLiteral, - LTT_FloatLiteral, - LTT_DqString, - LTT_SqString, - LTT_CharLiteral, - LTT_OperEquals, - LTT_OperNotEquals, - LTT_OperLessOrEqual, - LTT_OperGreaterOrEqual, - LTT_OperAndAnd, - LTT_OperOrOr, - LTT_OperShiftLeft, - LTT_OperShiftRight, - LTT_OperIncrement, - LTT_OperDecrement, - LTT_OperAddAssign, - LTT_OperSubAssign, - LTT_OperMulAssign, - LTT_OperDivAssign, - LTT_OperModAssign, - LTT_OperAndAssign, - LTT_OperOrAssign, - LTT_OperXorAssign, - LTT_OperArrow, - LTT_OperEqualArrow, - LTT_OperShiftLeflAssign, - LTT_OperShiftRightAssign, - - // Custom token types - LTT_OperAdd, - LTT_OperSub, - LTT_OperMul, - LTT_OperDiv, - LTT_OperMod, - LTT_ParenOpen, - LTT_ParenClose, - LTT_BracketOpen, - LTT_BracketClose, - LTT_BraceOpen, - LTT_BraceClose, - - LTT_COUNT, -}; - -// NOTE: maintain with CLEX_* defined in stb_c_lexer.h -LUT_DECL_VAR(gClexTokens, int, CLEX_first_unused_token, LexedTokenType, LTT_COUNT) { - LUT_MAP_FOR(gClexTokens); - LUT_MAP(CLEX_id, LTT_Identifier); - LUT_MAP(CLEX_intlit, LTT_IntLiteral); - LUT_MAP(CLEX_floatlit, LTT_FloatLiteral); - LUT_MAP(CLEX_dqstring, LTT_DqString); - LUT_MAP(CLEX_sqstring, LTT_SqString); - LUT_MAP(CLEX_charlit, LTT_CharLiteral); - LUT_MAP(CLEX_eq, LTT_OperEquals); - LUT_MAP(CLEX_noteq, LTT_OperNotEquals); - LUT_MAP(CLEX_lesseq, LTT_OperLessOrEqual); - LUT_MAP(CLEX_greatereq, LTT_OperGreaterOrEqual); - LUT_MAP(CLEX_andand, LTT_OperAndAnd); - LUT_MAP(CLEX_oror, LTT_OperOrOr); - LUT_MAP(CLEX_shl, LTT_OperShiftLeft); - LUT_MAP(CLEX_shr, LTT_OperShiftRight); - LUT_MAP(CLEX_plusplus, LTT_OperIncrement); - LUT_MAP(CLEX_minusminus, LTT_OperDecrement); - LUT_MAP(CLEX_pluseq, LTT_OperAddAssign); - LUT_MAP(CLEX_minuseq, LTT_OperSubAssign); - LUT_MAP(CLEX_muleq, LTT_OperMulAssign); - LUT_MAP(CLEX_diveq, LTT_OperDivAssign); - LUT_MAP(CLEX_modeq, LTT_OperModAssign); - LUT_MAP(CLEX_andeq, LTT_OperAndAssign); - LUT_MAP(CLEX_oreq, LTT_OperOrAssign); - LUT_MAP(CLEX_xoreq, LTT_OperXorAssign); - LUT_MAP(CLEX_arrow, LTT_OperArrow); - LUT_MAP(CLEX_eqarrow, LTT_OperEqualArrow); - LUT_MAP(CLEX_shleq, LTT_OperShiftLeflAssign); - LUT_MAP(CLEX_shreq, LTT_OperShiftRightAssign); -} - -LUT_DECL_VAR(gSingleCharTokens, char, std::numeric_limits::max() + 1, LexedTokenType, LTT_COUNT) { - LUT_MAP_FOR(gSingleCharTokens); - LUT_MAP('+', LTT_OperAdd); - LUT_MAP('-', LTT_OperSub); - LUT_MAP('*', LTT_OperMul); - LUT_MAP('/', LTT_OperDiv); - LUT_MAP('%', LTT_OperMod); - LUT_MAP('(', LTT_ParenOpen); - LUT_MAP(')', LTT_ParenClose); - LUT_MAP('[', LTT_BracketOpen); - LUT_MAP(']', LTT_BracketClose); - LUT_MAP('{', LTT_BraceOpen); - LUT_MAP('}', LTT_BraceClose); -} - -// See stb_c_lexer.h's comments, here are a few additinos that aren't made clear in the file: -// - `lexer->token` (noted as "token" below) after calling stb_c_lexer_get_token() contains either: -// 1. 0 <= token < 256: an ASCII character (more precisely a single char that the lexer ate; technically can be an incomplete code unit) -// 2. token < 0: an unknown token -// 3. One of the `CLEX_*` enums: a special, recognized token such as an operator -LexedTokenType MapFromStb(const stb_lexer& lexer) { - if (lexer.token >= 0 && lexer.token < 256) { - // Single char token - char c = lexer.token; - return LUT_LOOKUP(gSingleCharTokens, lexer.token); - } - - return LUT_LOOKUP(gClexTokens, lexer.token); -} -int MapToStb(LexedTokenType token) { - // TODO - - return LUT_REV_LOOKUP(gClexTokens, token); -} - struct StbLexerToken { std::string text; - LexedTokenType type; + // Can either be CLEX_* values, or just chars for single character tokens + int type; }; void CheckBraceDepth(int braceDpeth) { @@ -200,17 +87,37 @@ void HandleInputFile(std::string_view source) { std::vector tokens; std::vector foundStructs; + InputDefinitionStruct currStruct; std::vector foundEnums; + InputDefinitionEnum currEnum; + + auto PushFoundStruct = [&]() { + foundStructs.push_back(std::move(currStruct)); + currStruct = {}; + }; + auto PushFoundEnum = [&]() { + foundEnums.push_back(std::move(currEnum)); + currEnum = {}; + }; enum NextMatchingConstruct { NMC_None, NMC_Enum, NMC_StructClass, } matchingConstruct = NMC_None; + bool matchingConstructInBody = false; + + bool matchingDirectiveParams = false; int bracePairDepth = 0; while (true) { + // See stb_c_lexer.h's comments, here are a few additinos that aren't made clear in the file: + // - `lexer->token` (noted as "token" below) after calling stb_c_lexer_get_token() contains either: + // 1. 0 <= token < 256: an ASCII character (more precisely a single char that the lexer ate; technically can be an incomplete code unit) + // 2. token < 0: an unknown token + // 3. One of the `CLEX_*` enums: a special, recognized token such as an operator + int stbToken = stb_c_lexer_get_token(&lexer); if (stbToken == 0) { // EOF @@ -225,13 +132,53 @@ void HandleInputFile(std::string_view source) { switch (lexer.token) { case CLEX_id: { - std::string_view idenText(lexer.string, lexer.string_len); - if (idenText == "struct"sv || idenText == "class"sv) { - // TODO - matchingConstruct = NMC_StructClass; - } else if (idenText == "enum"sv) { - // TODO - matchingConstruct = NMC_Enum; + // WORKAROUND: stb_c_lexer doens't set string_len properly when parsing identifiers + std::string_view idenText(lexer.string); + // std::string_view idenText(lexer.string, lexer.string_len); + switch (matchingConstruct) { + case NMC_StructClass: { + if (matchingConstructInBody) { + // TODO + } + } break; + + case NMC_Enum: { + if (matchingConstructInBody) { + printf("[DEBUG] found enum element '%s'\n", lexer.string); + currEnum.elements.push_back(InputDefinitionEnumElement{ + .name = std::string(idenText), + .value = 0, // TODO parse + }); + } else { + currEnum.name = std::string(idenText); + } + } break; + + default: { + if (idenText == "struct"sv || idenText == "class"sv) { + printf("[DEBUG] found struct named\n"); + matchingConstruct = NMC_StructClass; + } else if (idenText == "enum"sv) { + printf("[DEBUG] found enum\n"); + matchingConstruct = NMC_Enum; + } else if (idenText == "BRUSSEL_CLASS"sv) { + // TODO + printf("[DEBUG] found BRUSSEL_CLASS\n"); + } else if (idenText == "BRUSSEL_ENUM"sv) { + matchingDirectiveParams = true; + printf("[DEBUG] found BRUSSEL_ENUM\n"); + } + + if (matchingDirectiveParams) { + for (auto& foundEnum : foundEnums) { + if (foundEnum.name == idenText) { + // TODO generate data + break; + } + } + matchingDirectiveParams = false; + } + } break; } } break; @@ -265,30 +212,49 @@ void HandleInputFile(std::string_view source) { } break; - case CLEX_parse_error: { - fprintf(stderr, "[ERROR] stb_c_lexer countered a parse error."); - // TODO how to handle? + case '{': { + bracePairDepth++; + CheckBraceDepth(bracePairDepth); + + switch (matchingConstruct) { + case NMC_StructClass: + case NMC_Enum: { + matchingConstructInBody = true; + } break; + + default: break; + } } break; - default: { - if (lexer.token >= 0 && lexer.token < 256) { - // Single char token - char c = lexer.token; - switch (c) { - case '{': { - bracePairDepth++; - CheckBraceDepth(bracePairDepth); - } break; - - case '}': { - bracePairDepth--; - CheckBraceDepth(bracePairDepth); - } break; - } - } else { - fprintf(stderr, "[ERROR] Encountered unknown token %ld.", lexer.token); + case '}': { + bracePairDepth--; + CheckBraceDepth(bracePairDepth); + + switch (matchingConstruct) { + case NMC_StructClass: { + matchingConstruct = NMC_None; + matchingConstructInBody = false; + } break; + + case NMC_Enum: { + printf("[DEBUG] committed enum '%s'\n", currEnum.name.c_str()); + for (auto& elm : currEnum.elements) { + printf(" - element %s = %" PRId64 "\n", elm.name.c_str(), elm.value); + } + + matchingConstruct = NMC_None; + matchingConstructInBody = false; + PushFoundEnum(); + } break; + + default: break; } } break; + + case CLEX_parse_error: { + fprintf(stderr, "[ERROR] stb_c_lexer countered a parse error."); + // TODO how to handle? + } break; } } @@ -345,9 +311,6 @@ void HandleArgument(InputOpcode opcode, std::string_view operand) { } int main(int argc, char* argv[]) { - LUT_INIT(gClexTokens); - LUT_INIT(gSingleCharTokens); - // TODO better arg parser // option 1: use cxxopts and positional arguments // option 1: take one argument only, being a json objecet @@ -359,7 +322,7 @@ int main(int argc, char* argv[]) { auto separatorLoc = arg.find(':'); if (separatorLoc != std::string_view::npos) { auto opcode = ParseInputOpcode(arg.substr(0, separatorLoc)); - auto operand = arg.substr(separatorLoc); + auto operand = arg.substr(separatorLoc + 1); HandleArgument(opcode, operand); } } diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp b/buildtools/codegen/tests/examples/TestEnum.hpp new file mode 100644 index 0000000..aaa3d74 --- /dev/null +++ b/buildtools/codegen/tests/examples/TestEnum.hpp @@ -0,0 +1,8 @@ +#include "MacrosCodegen.hpp" + +enum MyEnum { + EnumElement1, + EnumElement2, + EnumElement3, +}; +BRUSSEL_ENUM(MyEnum, GenInfo); -- cgit v1.2.3-70-g09d2 From 6f29abe5571eb68207986bdadb97b207264ac958 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 29 May 2022 21:57:53 -0700 Subject: Changeset: 49 Convert codegen output to use template specialization --- CMakeLists.txt | 45 +++++-- README.md | 9 ++ buildtools/codegen/CodegenOutput.inl | 3 +- buildtools/codegen/main.cpp | 148 ++++++++++++++------- buildtools/codegen/tests/examples/TestEnum.hpp | 23 ---- buildtools/codegen/tests/examples/TestEnum.hpp.txt | 21 +++ source-codegen-base/MetadataBase.hpp | 14 ++ 7 files changed, 180 insertions(+), 83 deletions(-) delete mode 100644 buildtools/codegen/tests/examples/TestEnum.hpp create mode 100644 buildtools/codegen/tests/examples/TestEnum.hpp.txt create mode 100644 source-codegen-base/MetadataBase.hpp (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 19518bf..0e8dc26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,10 +15,10 @@ add_subdirectory(3rdparty/tracy) # ============================================================================== -file(GLOB_RECURSE commonthings_SOURCES source-common/*.c source-common/*.cpp) -add_library(commonthings OBJECT ${commonthings_SOURCES}) +file(GLOB_RECURSE things_common_SOURCES source-common/*.c source-common/*.cpp) +add_library(things_common OBJECT ${things_common_SOURCES}) -set_target_properties(commonthings PROPERTIES +set_target_properties(things_common PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF @@ -27,8 +27,8 @@ set_target_properties(commonthings PROPERTIES UNITY_BUILD OFF ) -target_include_directories(commonthings PUBLIC source-common) -target_link_libraries(commonthings PUBLIC +target_include_directories(things_common PUBLIC source-common) +target_link_libraries(things_common PUBLIC # External dependencies ${CONAN_LIBS} glm::glm @@ -37,25 +37,45 @@ target_link_libraries(commonthings PUBLIC # ============================================================================== # NOTE: delibrately not recursive, see README.md in the folder for details -file(GLOB meta_codegen_SOURCES buildtools/codegen/*.c buildtools/codegen/*.cpp) -add_executable(meta_codegen ${meta_codegen_SOURCES}) +file(GLOB codegen_SOURCES buildtools/codegen/*.c buildtools/codegen/*.cpp) +add_executable(codegen ${codegen_SOURCES}) -set_target_properties(meta_codegen PROPERTIES +set_target_properties(codegen PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF UNITY_BUILD OFF ) -target_link_libraries(meta_codegen PRIVATE +target_link_libraries(codegen PRIVATE # External dependencies ${CONAN_LIBS} # Project internal components - commonthings + things_common ) -target_flag_rtti(meta_codegen OFF) +target_flag_rtti(codegen OFF) + +# ============================================================================== + +file(GLOB_RECURSE things_codegen_base_SOURCES source-common/*.c source-common/*.cpp) +add_library(things_codegen_base OBJECT ${things_codegen_base_SOURCES}) + +set_target_properties(things_common PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF +) + +target_include_directories(things_codegen_base PUBLIC source-codegen-base) +target_link_libraries(things_codegen_base PUBLIC + # External dependencies + ${CONAN_LIBS} + + # Project internal components + things_common +) # ============================================================================== @@ -95,7 +115,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE tracy # Project internal components - commonthings + things_common + things_metadata ) option(BRUSSEL_ENABLE_PROFILING "Whether profiling support is enabled or not." OFF) diff --git a/README.md b/README.md index dc0b4c1..5caf9f7 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,12 @@ + [Urho3D](https://urho3d.io/) + [godot](https://godotengine.org/) + Everything else in `3rdparty/` + +## Project Structure +- `buildtools` + - `cmake`: CMake scripts consumed by the root `CMakeLists.txt`. + - `codegen`: Code generator similar to Qt MOC. + Currently runs on: `source`. +- `source-common`: Code that's compiled as a part of all targets. Check each target's build script for details. +- `source-codegen-base`: Code that's consumed along with output of `buildtools/codegen`. +- `source`: The main game. diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index d1f2e50..edd9abc 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -38,9 +38,8 @@ public: std::move(other.mOutFunctions.begin(), other.mOutFunctions.end(), std::back_inserter(this->mOutFunctions)); } - void Write(FILE* file) { + void Write(FILE* file) const { for (auto& thing : mOutThings) { - WRITE_LIT(file, "// Output thing\n"); fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); WRITE_LIT(file, "\n"); } diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index df52b66..e759c31 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -273,7 +273,7 @@ std::string GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, bool return mapName; } -void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags options) { +void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const DeclEnum& decl, EnumFlags options) { bool useExcludeHeuristics = options.IsSet(EMGO_ExcludeUseHeuristics); if (options.IsSet(EMGO_ToString)) { @@ -281,7 +281,7 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags= %d) return {};", maxVal); - APPEND_FMT_LN(o, " return %s[value - %d];", arrayName.c_str(), minVal); - APPEND_LIT_LN(o, "}"); - - out.AddOutputThing(std::move(lookupFunction)); + CodegenOutputThing lookupFunctionDecl; + auto& o1 = lookupFunctionDecl.text; + APPEND_LIT_LN(o1, "template <>"); + APPEND_FMT_LN(o1, "std::string_view Metadata::EnumToString<%s>(%s);", decl.name.c_str(), decl.name.c_str()); + + CodegenOutputThing lookupFunctionDef; + auto& o2 = lookupFunctionDef.text; + APPEND_LIT_LN(o2, "template <>"); + APPEND_FMT_LN(o2, "std::string_view Metadata::EnumToString<%s>(%s value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o2, " if (value < 0 || value >= %d) return {};", maxVal); + APPEND_FMT_LN(o2, " return %s[value - %d];", arrayName.c_str(), minVal); + APPEND_LIT_LN(o2, "}"); + + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); + sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } break; case EVP_Bits: { - auto arrayName = GenerateEnumStringArray(out, decl, useExcludeHeuristics); + auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics); // TODO } break; case EVP_Random: { - auto mapName = GenerateEnumStringMap(out, decl, useExcludeHeuristics); + auto mapName = GenerateEnumStringMap(sourceOut, decl, useExcludeHeuristics); // TODO } break; @@ -330,19 +337,26 @@ void GenerateForEnum(CodegenOutput& out, const DeclEnum& decl, EnumFlags EnumFromString_%s(std::string_view value) {", decl.name.c_str(), decl.name.c_str()); - APPEND_FMT_LN(o2, " auto iter = %s.find(value);", mapName); - APPEND_FMT_LN(o2, " if (iter != %s.end()) {", mapName); - APPEND_LIT_LN(o2, " return iter->second;"); - APPEND_LIT_LN(o2, " } else {"); - APPEND_LIT_LN(o2, " return {};"); - APPEND_LIT_LN(o2, " }"); - APPEND_LIT_LN(o2, "}"); - - out.AddOutputThing(std::move(lookupTable)); - out.AddOutputThing(std::move(lookupFunction)); + CodegenOutputThing lookupFunctionDecl; + auto& o2 = lookupFunctionDecl.text; + APPEND_LIT_LN(o2, "template <>"); + APPEND_FMT_LN(o2, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view);", decl.name.c_str(), decl.name.c_str()); + + CodegenOutputThing lookupFunctionDef; + auto& o3 = lookupFunctionDef.text; + APPEND_LIT_LN(o3, "template <>"); + APPEND_FMT_LN(o3, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value) {", decl.name.c_str(), decl.name.c_str()); + APPEND_FMT_LN(o3, " auto iter = %s.find(value);", mapName); + APPEND_FMT_LN(o3, " if (iter != %s.end()) {", mapName); + APPEND_LIT_LN(o3, " return iter->second;"); + APPEND_LIT_LN(o3, " } else {"); + APPEND_LIT_LN(o3, " return {};"); + APPEND_LIT_LN(o3, " }"); + APPEND_LIT_LN(o3, "}"); + + sourceOut.AddOutputThing(std::move(lookupTable)); + headerOut.AddOutputThing(std::move(lookupFunctionDecl)); + sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } } @@ -358,8 +372,9 @@ void HandleInputFile(AppState& state, std::string_view source) { printf("END tokens\n"); #endif - CodegenInput fileInput; - CodegenOutput fileOutput; + CodegenInput cgInput; + auto& cgHeaderOutput = state.headerOutput; + auto& cgSourceOutput = state.sourceOutput; int bracePairDepth = 0; while (idx < tokens.size()) { @@ -444,7 +459,7 @@ void HandleInputFile(AppState& state, std::string_view source) { ++idx; } - fileInput.AddEnum(std::move(enumDecl)); + cgInput.AddEnum(std::move(enumDecl)); goto endIdenCase; } @@ -480,7 +495,7 @@ void HandleInputFile(AppState& state, std::string_view source) { } auto& enumName = argList[0][0]->text; - auto enumDecl = fileInput.FindEnumByName(enumName); + auto enumDecl = cgInput.FindEnumByName(enumName); if (!enumDecl) { printf("[ERROR] BRUSSEL_ENUM: referring to non-existent enum '%s'\n", enumName.c_str()); break; @@ -497,7 +512,7 @@ void HandleInputFile(AppState& state, std::string_view source) { } } - GenerateForEnum(fileOutput, *enumDecl, options); + GenerateForEnum(cgHeaderOutput, cgSourceOutput, *enumDecl, options); idx = newIdx; incrementTokenIdx = false; @@ -530,8 +545,6 @@ void HandleInputFile(AppState& state, std::string_view source) { if (bracePairDepth != 0) { printf("[WARNING] unbalanced brace at end of file."); } - - state.sourceOutput.MergeContents(std::move(fileOutput)); } std::string ReadFileAtOnce(const fs::path& path) { @@ -596,6 +609,23 @@ InputOpcode ParseInputOpcode(std::string_view text) { return IOP_COUNT; } +bool WriteOutputFile(const CodegenOutput& output, const char* dirPath, const char* fileName) { + char path[2048]; + snprintf(path, sizeof(path), "%s/%s", dirPath, fileName); + + auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!outputFile) { + printf("[ERROR] unable to open output file %s", path); + return false; + } + DEFER { fclose(outputFile); }; + + DEBUG_PRINTF("Writing output %s", path); + output.Write(outputFile); + + return true; +} + int main(int argc, char* argv[]) { STR_LUT_INIT(ClexNames); BSTR_LUT_INIT(CppKeyword); @@ -609,14 +639,22 @@ int main(int argc, char* argv[]) { AppState state; + state.headerOutput.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#pragma once + +#include +)"""[1], + }); + state.sourceOutput.AddOutputThing(CodegenOutputThing{ .text = &R"""( // This file is generated. Any changes will be overidden when building. +#include "MetadataImpl.hpp" #include #include -#include -#include using namespace std::literals; )"""[1], }); @@ -627,7 +665,7 @@ using namespace std::literals; // NOTE: keep in sync with various enum options and parser code printf(&R"""( USAGE: codegen.exe [:]... -where : the _file_ to write generated contents to +where : the directory to write generated contents to. This will NOT automatically create the directory. is one of: "single" process this file only "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files @@ -635,8 +673,33 @@ where : the _file_ to write generated contents to return -1; } - const char* outputFilePath = argv[1]; - DEBUG_PRINTF("Outputting to file %s.\n", outputFilePath); + const char* outputDir = argv[1]; + DEBUG_PRINTF("Outputting to directory %s.\n", outputDirPath); + + { + char path[2048]; + snprintf(path, sizeof(path), "%s/%s", outputDir, "Metadata.hpp"); + + auto fileContent = R"""( +// This file is generated. Any changes will be overidden when building. +// This file is an umbrella header for all generated metadata. End users simply include this file to get access to everything. +#pragma once + +#include + +#include +)"""sv.substr(1); // Get rid of the \n at the beginning + + auto file = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!file) { + printf("[ERROR] unable to open output file %s", path); + return -1; + } + DEFER { fclose(file); }; + + DEBUG_PRINTF("Writing output file %s", path); + fwrite(fileContent.data(), sizeof(decltype(fileContent)::value_type), fileContent.size(), file); + } for (int i = 2; i < argc; ++i) { std::string_view arg(argv[i]); @@ -652,15 +715,8 @@ where : the _file_ to write generated contents to } } - { - auto outputFile = Utils::OpenCstdioFile(outputFilePath, Utils::WriteTruncate); - if (!outputFile) { - printf("[ERROR] unable to open output file %s", outputFilePath); - return -1; - } - DEFER { fclose(outputFile); }; - state.sourceOutput.Write(outputFile); - } + WriteOutputFile(state.headerOutput, outputDir, "MetadataImpl.hpp"); + WriteOutputFile(state.sourceOutput, outputDir, "MetadataImpl.cpp"); return 0; } diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp b/buildtools/codegen/tests/examples/TestEnum.hpp deleted file mode 100644 index 6b4ab33..0000000 --- a/buildtools/codegen/tests/examples/TestEnum.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "MacrosCodegen.hpp" - -enum MyEnum { - EnumElement1, - EnumElement2, - EnumElement3, -}; -BRUSSEL_ENUM(MyEnum, ToString FromString); - -enum CountedEnumAll { - CEA_Foo, - CEA_Bar, - CEA_COUNT, -}; -BRUSSEL_ENUM(CountedEnumAll, ToString FromString); - -enum CountedEnum { - CE_Foo, - CE_Bar, - CE_FooBar, - CE_COUNT, -}; -BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); diff --git a/buildtools/codegen/tests/examples/TestEnum.hpp.txt b/buildtools/codegen/tests/examples/TestEnum.hpp.txt new file mode 100644 index 0000000..e596c5e --- /dev/null +++ b/buildtools/codegen/tests/examples/TestEnum.hpp.txt @@ -0,0 +1,21 @@ +enum MyEnum { + EnumElement1, + EnumElement2, + EnumElement3, +}; +BRUSSEL_ENUM(MyEnum, ToString FromString); + +enum CountedEnumAll { + CEA_Foo, + CEA_Bar, + CEA_COUNT, +}; +BRUSSEL_ENUM(CountedEnumAll, ToString FromString); + +enum CountedEnum { + CE_Foo, + CE_Bar, + CE_FooBar, + CE_COUNT, +}; +BRUSSEL_ENUM(CountedEnum, ToString FromString ExcludeHeuristics); diff --git a/source-codegen-base/MetadataBase.hpp b/source-codegen-base/MetadataBase.hpp new file mode 100644 index 0000000..59c61da --- /dev/null +++ b/source-codegen-base/MetadataBase.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace Metadata { + +template +std::string_view EnumToString(TEnum value) = delete; + +template +std::optional EnumFromString(std::string_view str) = delete; + +} // namespace Metadata -- cgit v1.2.3-70-g09d2 From cdd84f25ab1d2a57ee5c7b4c954e35a8d7e2dca3 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 29 May 2022 22:04:13 -0700 Subject: Changeset: 50 Fix buildsystem mistakes in changeset 49, add debug print option --- CMakeLists.txt | 11 ++++++++--- buildtools/codegen/CodegenConfig.hpp | 4 +++- buildtools/codegen/main.cpp | 2 +- source-codegen-base/MetadataBase.cpp | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 source-codegen-base/MetadataBase.cpp (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e8dc26..55db14b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,12 +57,17 @@ target_link_libraries(codegen PRIVATE target_flag_rtti(codegen OFF) +option(BRUSSEL_CODEGEN_DEBUG_PRINT "Enable debug printing in the code generator or not." OFF) +if(BRUSSEL_CODEGEN_DEBUG_PRINT) + target_compile_definitions(codegen PRIVATE CODEGEN_DEBUG_PRINT=1) +endif() + # ============================================================================== -file(GLOB_RECURSE things_codegen_base_SOURCES source-common/*.c source-common/*.cpp) +file(GLOB_RECURSE things_codegen_base_SOURCES source-codegen-base/*.c source-codegen-base/*.cpp) add_library(things_codegen_base OBJECT ${things_codegen_base_SOURCES}) -set_target_properties(things_common PROPERTIES +set_target_properties(things_codegen_base PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF @@ -116,7 +121,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE # Project internal components things_common - things_metadata + things_codegen_base ) option(BRUSSEL_ENABLE_PROFILING "Whether profiling support is enabled or not." OFF) diff --git a/buildtools/codegen/CodegenConfig.hpp b/buildtools/codegen/CodegenConfig.hpp index 4ed576a..b9dc56c 100644 --- a/buildtools/codegen/CodegenConfig.hpp +++ b/buildtools/codegen/CodegenConfig.hpp @@ -1,6 +1,8 @@ #pragma once -#define CODEGEN_DEBUG_PRINT 1 +#ifndef CODEGEN_DEBUG_PRINT +# define CODEGEN_DEBUG_PRINT 0 +#endif #if CODEGEN_DEBUG_PRINT # define DEBUG_PRINTF(...) printf(__VA_ARGS__) diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index e759c31..298f19e 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -674,7 +674,7 @@ where : the directory to write generated contents to. This will N } const char* outputDir = argv[1]; - DEBUG_PRINTF("Outputting to directory %s.\n", outputDirPath); + DEBUG_PRINTF("Outputting to directory %s.\n", outputDir); { char path[2048]; diff --git a/source-codegen-base/MetadataBase.cpp b/source-codegen-base/MetadataBase.cpp new file mode 100644 index 0000000..3ccf870 --- /dev/null +++ b/source-codegen-base/MetadataBase.cpp @@ -0,0 +1 @@ +#include "MetadataBase.hpp" -- cgit v1.2.3-70-g09d2 From ce9559e8c2b69d46cff064241bd9a04c014af44f Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 30 May 2022 13:09:26 -0700 Subject: Changeset: 51 Add integration into the main game --- CMakeLists.txt | 24 +++++- buildtools/codegen/CodegenOutput.inl | 19 ++++- buildtools/codegen/CodegenUtils.inl | 73 ++++++++++++++++ buildtools/codegen/main.cpp | 154 ++++++++++------------------------ source-codegen-base/MacrosCodegen.hpp | 7 ++ source-codegen-base/Metadata.cpp | 1 + source-codegen-base/Metadata.hpp | 4 + source-codegen-base/MetadataBase.hpp | 4 +- source-common/Macros.hpp | 2 + source-common/MacrosCodegen.hpp | 7 -- source/GraphicsTags.cpp | 2 + source/GraphicsTags.hpp | 12 +++ source/main.cpp | 5 ++ 13 files changed, 191 insertions(+), 123 deletions(-) create mode 100644 buildtools/codegen/CodegenUtils.inl create mode 100644 source-codegen-base/MacrosCodegen.hpp create mode 100644 source-codegen-base/Metadata.cpp create mode 100644 source-codegen-base/Metadata.hpp delete mode 100644 source-common/MacrosCodegen.hpp (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 55db14b..4800935 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,6 @@ if(BRUSSEL_CODEGEN_DEBUG_PRINT) target_compile_definitions(codegen PRIVATE CODEGEN_DEBUG_PRINT=1) endif() -# ============================================================================== - file(GLOB_RECURSE things_codegen_base_SOURCES source-codegen-base/*.c source-codegen-base/*.cpp) add_library(things_codegen_base OBJECT ${things_codegen_base_SOURCES}) @@ -82,6 +80,25 @@ target_link_libraries(things_codegen_base PUBLIC things_common ) +# TODO support reading all files from the target, instead of manually supplying a search dir +function(target_gen_metadata TARGET_NAME SEARCH_DIR) + get_target_property(TARGET_SOURCES ${TARGET_NAME} SOURCES) + + set(OUTPUT_DIR ${CMAKE_BINARY_DIR}/generated/${TARGET_NAME}) + set(OUTPUT_FILES + ${OUTPUT_DIR}/generated/GeneratedCode.hpp + ${OUTPUT_DIR}/generated/GeneratedCode.cpp + ) + add_custom_command( + OUTPUT ${OUTPUT_FILES} + COMMAND codegen ${OUTPUT_DIR}/generated rec:${SEARCH_DIR} + DEPENDS ${TARGET_SOURCES} + ) + + target_include_directories(${TARGET_NAME} PRIVATE ${OUTPUT_DIR}) + target_sources(${TARGET_NAME} PRIVATE ${OUTPUT_FILES}) +endfunction() + # ============================================================================== # add_executable requires at least one source file @@ -153,3 +170,6 @@ if(BRUSSEL_ENABLE_ASAN) -fno-omit-frame-pointer ) endif() + +get_filename_component(METADATA_INP_DIR "source" ABSOLUTE) +target_gen_metadata(${PROJECT_NAME} ${METADATA_INP_DIR}) diff --git a/buildtools/codegen/CodegenOutput.inl b/buildtools/codegen/CodegenOutput.inl index edd9abc..ff7b912 100644 --- a/buildtools/codegen/CodegenOutput.inl +++ b/buildtools/codegen/CodegenOutput.inl @@ -3,6 +3,9 @@ #include "CodegenDecl.hpp" #include "CodegenMacros.hpp" +#include + +#include #include #include #include @@ -16,6 +19,7 @@ struct CodegenOutputThing { class CodegenOutput { private: + robin_hood::unordered_set mRequestIncludes; std::vector mOutThings; std::vector mOutStructs; std::vector mOutEnums; @@ -27,6 +31,12 @@ public: bool optionAutoAddPrefix : 1 = false; public: + void AddRequestInclude(std::string_view include) { + if (!mRequestIncludes.contains(include)) { + mRequestIncludes.insert(std::string(include)); + } + } + void AddOutputThing(CodegenOutputThing thing) { mOutThings.push_back(std::move(thing)); } @@ -39,15 +49,20 @@ public: } void Write(FILE* file) const { + for (auto& include : mRequestIncludes) { + // TODO how to resolve to the correct include paths? + WRITE_FMT_LN(file, "#include <%s>", include.c_str()); + } + for (auto& thing : mOutThings) { fwrite(thing.text.c_str(), sizeof(char), thing.text.size(), file); WRITE_LIT(file, "\n"); } for (auto& declStruct : mOutStructs) { - WRITE_FMT(file, "struct %s {\n", declStruct.name.c_str()); + WRITE_FMT_LN(file, "struct %s {", declStruct.name.c_str()); // TODO - WRITE_LIT(file, "}\n"); + WRITE_LIT_LN(file, "};"); } for (auto& declEnum : mOutEnums) { diff --git a/buildtools/codegen/CodegenUtils.inl b/buildtools/codegen/CodegenUtils.inl new file mode 100644 index 0000000..ea46ac1 --- /dev/null +++ b/buildtools/codegen/CodegenUtils.inl @@ -0,0 +1,73 @@ +#pragma once + +#include "CodegenConfig.hpp" +#include "CodegenMacros.hpp" + +#include "CodegenOutput.inl" + +#include +#include + +#include +#include +#include + +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)); + + auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); + if (!outputFile) { + printf("[ERROR] unable to open output file %s\n", path); + return false; + } + DEFER { fclose(outputFile); }; + + DEBUG_PRINTF("Writing output %s\n", path); + output.Write(outputFile); + + return true; +} + +void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { + output.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#pragma once + +#include +)"""[1], + }); +} + +void ProduceGeneratedSourceFileHeader(CodegenOutput& output) { + output.AddOutputThing(CodegenOutputThing{ + .text = &R"""( +// This file is generated. Any changes will be overidden when building. +#include "GeneratedCode.hpp" + +#include +#include +using namespace std::literals; + )"""[1], + }); +} + +} // namespace Utils diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp index 298f19e..3a3e443 100644 --- a/buildtools/codegen/main.cpp +++ b/buildtools/codegen/main.cpp @@ -4,6 +4,7 @@ #include "CodegenInput.inl" #include "CodegenOutput.inl" +#include "CodegenUtils.inl" #include #include @@ -27,10 +28,12 @@ using namespace std::literals; namespace fs = std::filesystem; // TODO handle namespace +// TODO support codegen target in .cpp files struct AppState { - CodegenOutput headerOutput; - CodegenOutput sourceOutput; + std::string_view outputDir; + CodegenOutput mainHeaderOutput; + CodegenOutput mainSourceOutput; }; enum { @@ -113,7 +116,7 @@ bool StbTokenIsMultiChar(int lexerToken) { void CheckBraceDepth(int braceDpeth) { if (braceDpeth < 0) { - printf("[WARNING] unbalanced brace"); + printf("[WARNING] unbalanced brace\n"); } } @@ -197,7 +200,7 @@ std::vector RecordTokens(std::string_view source) { } if (lexer.token == CLEX_parse_error) { - printf("[ERROR] stb_c_lexer countered a parse error."); + printf("[ERROR] stb_c_lexer countered a parse error.\n"); // TODO how to handle? continue; } @@ -292,11 +295,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D maxVal = decl.elements[decl.elements.size() - 2].value; } - CodegenOutputThing lookupFunctionDecl; - auto& o1 = lookupFunctionDecl.text; - APPEND_LIT_LN(o1, "template <>"); - APPEND_FMT_LN(o1, "std::string_view Metadata::EnumToString<%s>(%s);", decl.name.c_str(), decl.name.c_str()); - CodegenOutputThing lookupFunctionDef; auto& o2 = lookupFunctionDef.text; APPEND_LIT_LN(o2, "template <>"); @@ -305,7 +303,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_FMT_LN(o2, " return %s[value - %d];", arrayName.c_str(), minVal); APPEND_LIT_LN(o2, "}"); - headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } break; @@ -337,11 +334,6 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_LIT_LN(o1, "};"); // Generate lookup function - CodegenOutputThing lookupFunctionDecl; - auto& o2 = lookupFunctionDecl.text; - APPEND_LIT_LN(o2, "template <>"); - APPEND_FMT_LN(o2, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view);", decl.name.c_str(), decl.name.c_str()); - CodegenOutputThing lookupFunctionDef; auto& o3 = lookupFunctionDef.text; APPEND_LIT_LN(o3, "template <>"); @@ -355,12 +347,11 @@ void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const D APPEND_LIT_LN(o3, "}"); sourceOut.AddOutputThing(std::move(lookupTable)); - headerOut.AddOutputThing(std::move(lookupFunctionDecl)); sourceOut.AddOutputThing(std::move(lookupFunctionDef)); } } -void HandleInputFile(AppState& state, std::string_view source) { +void HandleInputFile(AppState& state, std::string_view filenameStem, std::string_view source) { auto tokens = RecordTokens(source); size_t idx = 0; @@ -373,8 +364,10 @@ void HandleInputFile(AppState& state, std::string_view source) { #endif CodegenInput cgInput; - auto& cgHeaderOutput = state.headerOutput; - auto& cgSourceOutput = state.sourceOutput; + CodegenOutput cgHeaderOutput; + Utils::ProduceGeneratedHeaderFileHeader(cgHeaderOutput); + CodegenOutput cgSourceOutput; + Utils::ProduceGeneratedSourceFileHeader(cgSourceOutput); int bracePairDepth = 0; while (idx < tokens.size()) { @@ -545,21 +538,9 @@ void HandleInputFile(AppState& state, std::string_view source) { if (bracePairDepth != 0) { printf("[WARNING] unbalanced brace at end of file."); } -} - -std::string ReadFileAtOnce(const fs::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; + Utils::WriteOutputFile(cgHeaderOutput, state.outputDir, filenameStem, ".gh.inl"sv); + Utils::WriteOutputFile(cgSourceOutput, state.outputDir, filenameStem, ".gs.inl"sv); } enum InputOpcode { @@ -571,12 +552,17 @@ enum InputOpcode { void HandleArgument(AppState& state, InputOpcode opcode, std::string_view operand) { switch (opcode) { case IOP_ProcessSingleFile: { - fs::path filePath(operand); - auto source = ReadFileAtOnce(filePath); - HandleInputFile(state, source); + DEBUG_PRINTF("Processing single file %.*s\n", PRINTF_STRING_VIEW(operand)); + + fs::path path(operand); + auto filenameStem = path.stem().string(); + auto source = Utils::ReadFileAsString(path); + HandleInputFile(state, filenameStem, source); } break; case IOP_ProcessRecursively: { + DEBUG_PRINTF("Recursively processing folder %.*s\n", PRINTF_STRING_VIEW(operand)); + fs::path startPath(operand); for (auto& item : fs::recursive_directory_iterator(startPath)) { if (!item.is_regular_file()) { @@ -584,15 +570,19 @@ void HandleArgument(AppState& state, InputOpcode opcode, std::string_view operan } auto& path = item.path(); - auto filename = path.filename().string(); - if (filename != ".c" || - filename != ".cpp") + auto pathExt = path.extension(); + auto pathStem = path.stem(); + if (pathExt != ".h" && + pathExt != ".hpp") { continue; } - auto source = ReadFileAtOnce(path); - HandleInputFile(state, source); + DEBUG_PRINTF("Processing subfile %s\n", path.string().c_str()); + + auto filenameStem = pathStem.string(); + auto source = Utils::ReadFileAsString(path); + HandleInputFile(state, filenameStem, source); } } break; @@ -605,25 +595,10 @@ InputOpcode ParseInputOpcode(std::string_view text) { return IOP_ProcessSingleFile; } else if (text == "rec"sv) { return IOP_ProcessRecursively; + } else { + DEBUG_PRINTF("Unknown input opcode %s\n", text.data()); + throw std::runtime_error("Unknown input opcode"); } - return IOP_COUNT; -} - -bool WriteOutputFile(const CodegenOutput& output, const char* dirPath, const char* fileName) { - char path[2048]; - snprintf(path, sizeof(path), "%s/%s", dirPath, fileName); - - auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); - if (!outputFile) { - printf("[ERROR] unable to open output file %s", path); - return false; - } - DEFER { fclose(outputFile); }; - - DEBUG_PRINTF("Writing output %s", path); - output.Write(outputFile); - - return true; } int main(int argc, char* argv[]) { @@ -639,25 +614,8 @@ int main(int argc, char* argv[]) { AppState state; - state.headerOutput.AddOutputThing(CodegenOutputThing{ - .text = &R"""( -// This file is generated. Any changes will be overidden when building. -#pragma once - -#include -)"""[1], - }); - - state.sourceOutput.AddOutputThing(CodegenOutputThing{ - .text = &R"""( -// This file is generated. Any changes will be overidden when building. -#include "MetadataImpl.hpp" - -#include -#include -using namespace std::literals; - )"""[1], - }); + Utils::ProduceGeneratedHeaderFileHeader(state.mainHeaderOutput); + Utils::ProduceGeneratedSourceFileHeader(state.mainSourceOutput); // If no cli is provided (argv[0] conventionally but not mandatorily the cli), this will do thing // Otherwise, start with the 2nd element in the array, which is the 1st actual argument @@ -668,55 +626,31 @@ USAGE: codegen.exe [:]... where : the directory to write generated contents to. This will NOT automatically create the directory. is one of: "single" process this file only - "rec" starting at the given directory , recursively process all .h .c .hpp .cpp files + "rec" starting at the given directory , recursively process all .h .hpp files )"""[1]); return -1; } - const char* outputDir = argv[1]; + state.outputDir = std::string_view(argv[1]); DEBUG_PRINTF("Outputting to directory %s.\n", outputDir); - { - char path[2048]; - snprintf(path, sizeof(path), "%s/%s", outputDir, "Metadata.hpp"); - - auto fileContent = R"""( -// This file is generated. Any changes will be overidden when building. -// This file is an umbrella header for all generated metadata. End users simply include this file to get access to everything. -#pragma once - -#include - -#include -)"""sv.substr(1); // Get rid of the \n at the beginning - - auto file = Utils::OpenCstdioFile(path, Utils::WriteTruncate); - if (!file) { - printf("[ERROR] unable to open output file %s", path); - return -1; - } - DEFER { fclose(file); }; - - DEBUG_PRINTF("Writing output file %s", path); - fwrite(fileContent.data(), sizeof(decltype(fileContent)::value_type), fileContent.size(), file); - } - for (int i = 2; i < argc; ++i) { - std::string_view arg(argv[i]); + const char* argRaw = argv[i]; + std::string_view arg(argRaw); + DEBUG_PRINTF("Processing input command %s\n", argRaw); + auto separatorLoc = arg.find(':'); if (separatorLoc != std::string_view::npos) { auto opcodeString = arg.substr(0, separatorLoc); auto opcode = ParseInputOpcode(opcodeString); auto operand = arg.substr(separatorLoc + 1); - DEBUG_PRINTF("Processing input command %.*s at path %.*s\n", (int)opcodeString.size(), opcodeString.data(), (int)operand.size(), operand.data()); - HandleArgument(state, opcode, operand); } } - WriteOutputFile(state.headerOutput, outputDir, "MetadataImpl.hpp"); - WriteOutputFile(state.sourceOutput, outputDir, "MetadataImpl.cpp"); + Utils::WriteOutputFile(state.mainHeaderOutput, state.outputDir, "GeneratedCode.hpp"sv); + Utils::WriteOutputFile(state.mainSourceOutput, state.outputDir, "GeneratedCode.cpp"sv); return 0; } diff --git a/source-codegen-base/MacrosCodegen.hpp b/source-codegen-base/MacrosCodegen.hpp new file mode 100644 index 0000000..6803023 --- /dev/null +++ b/source-codegen-base/MacrosCodegen.hpp @@ -0,0 +1,7 @@ +// NOTE: contents of this file is coupled with buildtools/codegen/ +// when updating, change both sides at the same time + +#pragma once + +#define BRUSSEL_CLASS(name, options) +#define BRUSSEL_ENUM(name, options) diff --git a/source-codegen-base/Metadata.cpp b/source-codegen-base/Metadata.cpp new file mode 100644 index 0000000..ee32054 --- /dev/null +++ b/source-codegen-base/Metadata.cpp @@ -0,0 +1 @@ +#include "Metadata.hpp" diff --git a/source-codegen-base/Metadata.hpp b/source-codegen-base/Metadata.hpp new file mode 100644 index 0000000..a038c15 --- /dev/null +++ b/source-codegen-base/Metadata.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "MacrosCodegen.hpp" +#include "MetadataBase.hpp" diff --git a/source-codegen-base/MetadataBase.hpp b/source-codegen-base/MetadataBase.hpp index 59c61da..8be668d 100644 --- a/source-codegen-base/MetadataBase.hpp +++ b/source-codegen-base/MetadataBase.hpp @@ -6,9 +6,9 @@ namespace Metadata { template -std::string_view EnumToString(TEnum value) = delete; +std::string_view EnumToString(TEnum value); template -std::optional EnumFromString(std::string_view str) = delete; +std::optional EnumFromString(std::string_view str); } // namespace Metadata diff --git a/source-common/Macros.hpp b/source-common/Macros.hpp index b5d05fa..a255ada 100644 --- a/source-common/Macros.hpp +++ b/source-common/Macros.hpp @@ -14,6 +14,8 @@ #define UNUSED(x) (void)x; +#define PRINTF_STRING_VIEW(s) (int)s.size(), s.data() + #if defined(_MSC_VER) # define UNREACHABLE __assume(0) #elif defined(__GNUC__) || defined(__clang__) diff --git a/source-common/MacrosCodegen.hpp b/source-common/MacrosCodegen.hpp deleted file mode 100644 index 6803023..0000000 --- a/source-common/MacrosCodegen.hpp +++ /dev/null @@ -1,7 +0,0 @@ -// NOTE: contents of this file is coupled with buildtools/codegen/ -// when updating, change both sides at the same time - -#pragma once - -#define BRUSSEL_CLASS(name, options) -#define BRUSSEL_ENUM(name, options) diff --git a/source/GraphicsTags.cpp b/source/GraphicsTags.cpp index b389acf..522a58f 100644 --- a/source/GraphicsTags.cpp +++ b/source/GraphicsTags.cpp @@ -306,3 +306,5 @@ GLenum Tags::FindGLType(std::string_view name) { return GL_NONE; } } + +#include diff --git a/source/GraphicsTags.hpp b/source/GraphicsTags.hpp index 34c0885..09e62bf 100644 --- a/source/GraphicsTags.hpp +++ b/source/GraphicsTags.hpp @@ -5,6 +5,16 @@ #include #include +#include + +enum TestEnum { + TE_Position, + TE_Color, + TE_TexCoord, + TE_COUNT, +}; +BRUSSEL_ENUM(TestEnum, ToString FromString ExcludeHeuristics); + namespace Tags { /// Vertex element semantics, used to identify the meaning of vertex buffer contents enum VertexElementSemantic { @@ -96,3 +106,5 @@ GLenum FindGLType(std::string_view name); constexpr GLuint kInvalidLocation = std::numeric_limits::max(); } // namespace Tags + +#include diff --git a/source/main.cpp b/source/main.cpp index c49fc0b..353746b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,9 +3,11 @@ #include "AppConfig.hpp" #include "CommonVertexIndex.hpp" #include "EditorGuizmo.hpp" +#include "GraphicsTags.hpp" #include "Ires.hpp" #include "Level.hpp" #include "Material.hpp" +#include "Metadata.hpp" #include "Shader.hpp" #define GLFW_INCLUDE_NONE @@ -102,6 +104,9 @@ fs::path GetEnvVar(const char* name, const char* backup) { int main(int argc, char* argv[]) { using namespace Tags; + auto str = Metadata::EnumToString(TE_Color); + printf("%.*s", PRINTF_STRING_VIEW(str)); + constexpr auto kImGuiBackend = "imgui-backend"; constexpr auto kGameDataDir = "game-data-directory"; constexpr auto kGameAssetDir = "game-asset-directory"; -- cgit v1.2.3-70-g09d2