diff options
author | rtk0c <[email protected]> | 2022-05-24 21:47:55 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-24 21:47:55 -0700 |
commit | d18a28a9659092952aef70a30a47726e7c16d31a (patch) | |
tree | b1d6304e631961a797225912ed1ef7ee55864f86 | |
parent | 123f741e3f5374b93ac39887b62bfa0d66762ae2 (diff) |
Changeset: 38 Branch comment: [] Add infrastructure for codegen
-rw-r--r-- | CMakeLists.txt | 50 | ||||
-rw-r--r-- | buildtools/codegen/README.md | 5 | ||||
-rw-r--r-- | buildtools/codegen/main.cpp | 102 | ||||
-rw-r--r-- | source-common/Macros.hpp (renamed from source/Macros.hpp) | 0 | ||||
-rw-r--r-- | source-common/MacrosCodegen.hpp | 4 | ||||
-rw-r--r-- | source-common/RapidJsonHelper.hpp (renamed from source/RapidJsonHelper.hpp) | 0 | ||||
-rw-r--r-- | source-common/ScopeGuard.hpp (renamed from source/ScopeGuard.hpp) | 0 | ||||
-rw-r--r-- | source-common/StbImplementations.c (renamed from source/stb_implementation.c) | 3 | ||||
-rw-r--r-- | source-common/Utils.cpp (renamed from source/Utils.cpp) | 0 | ||||
-rw-r--r-- | source-common/Utils.hpp (renamed from source/Utils.hpp) | 0 | ||||
-rw-r--r-- | source-common/YCombinator.hpp (renamed from source/YCombinator.hpp) | 0 | ||||
-rw-r--r-- | source/CMakeLists.txt | 2 |
12 files changed, 163 insertions, 3 deletions
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) diff --git a/buildtools/codegen/README.md b/buildtools/codegen/README.md new file mode 100644 index 0000000..7164132 --- /dev/null +++ b/buildtools/codegen/README.md @@ -0,0 +1,5 @@ +# Code Generator +The main code generator. + +## Folder structure +The main program's source files are all located in this folder directly. Text tempaltes are located in `templates/` and none of the files are compiled (even if they end with .c or .cpp). diff --git a/buildtools/codegen/main.cpp b/buildtools/codegen/main.cpp new file mode 100644 index 0000000..4a1d486 --- /dev/null +++ b/buildtools/codegen/main.cpp @@ -0,0 +1,102 @@ +#include "ScopeGuard.hpp" +#include "Utils.hpp" + +#include <stb_c_lexer.h> +#include <filesystem> +#include <string> +#include <string_view> + +using namespace std::literals; +namespace fs = std::filesystem; + +enum InputOpcode { + IOP_ProcessSingleFile, + IOP_ProcessRecursively, + IOP_COUNT, +}; + +InputOpcode ParseInputOpcode(std::string_view text) { + if (text == "single"sv) { + return IOP_ProcessSingleFile; + } else if (text == "rec"sv) { + return IOP_ProcessRecursively; + } + return IOP_COUNT; +} + +void HandleInputFile(std::string_view source) { + stb_lexer lexer; + char stringStorage[65536]; + const char* srcBegin = source.data(); + const char* srcEnd = srcBegin + source.length(); + stb_c_lexer_init(&lexer, srcBegin, srcEnd, stringStorage, sizeof(stringStorage)); + + // TODO +} + +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; +} + +void HandleArgument(InputOpcode opcode, std::string_view operand) { + switch (opcode) { + case IOP_ProcessSingleFile: { + fs::path filePath(operand); + auto source = ReadFileAtOnce(filePath); + HandleInputFile(source); + } break; + + case IOP_ProcessRecursively: { + fs::path startPath(operand); + for (auto& item : fs::directory_iterator(startPath)) { + if (!item.is_regular_file()) { + continue; + } + + auto& path = item.path(); + auto filename = path.filename().string(); + if (filename != ".c" || + filename != ".cpp") + { + continue; + } + + auto source = ReadFileAtOnce(path); + HandleInputFile(source); + } + } break; + + case IOP_COUNT: break; + } +} + +int main(int argc, char* argv[]) { + // TODO better arg parser + // option 1: use cxxopts and positional arguments + // option 1: take one argument only, being a json objecet + + // If no cli is provided (argv[0]), this loop will do nothing + // Otherwise, start with the 2nd element which is the 1st argument + for (int i = 1; i < argc; ++i) { + std::string_view arg(argv[i]); + auto separatorLoc = arg.find(':'); + if (separatorLoc != std::string_view::npos) { + auto opcode = ParseInputOpcode(arg.substr(0, separatorLoc)); + auto operand = arg.substr(separatorLoc); + HandleArgument(opcode, operand); + } + } + + return 0; +} diff --git a/source/Macros.hpp b/source-common/Macros.hpp index b5d05fa..b5d05fa 100644 --- a/source/Macros.hpp +++ b/source-common/Macros.hpp diff --git a/source-common/MacrosCodegen.hpp b/source-common/MacrosCodegen.hpp new file mode 100644 index 0000000..6c93d09 --- /dev/null +++ b/source-common/MacrosCodegen.hpp @@ -0,0 +1,4 @@ +#pragma once + +#define BRUSSEL_METACLASS +#define BRUSSEL_METAENUM(name) diff --git a/source/RapidJsonHelper.hpp b/source-common/RapidJsonHelper.hpp index 75cd93a..75cd93a 100644 --- a/source/RapidJsonHelper.hpp +++ b/source-common/RapidJsonHelper.hpp diff --git a/source/ScopeGuard.hpp b/source-common/ScopeGuard.hpp index 28f3385..28f3385 100644 --- a/source/ScopeGuard.hpp +++ b/source-common/ScopeGuard.hpp diff --git a/source/stb_implementation.c b/source-common/StbImplementations.c index 078ca5d..73bbc2a 100644 --- a/source/stb_implementation.c +++ b/source-common/StbImplementations.c @@ -9,3 +9,6 @@ #define STB_SPRINTF_IMPLEMENTATION #include <stb_sprintf.h> + +#define STB_C_LEXER_IMPLEMENTATION +#include <stb_c_lexer.h> diff --git a/source/Utils.cpp b/source-common/Utils.cpp index 53b3863..53b3863 100644 --- a/source/Utils.cpp +++ b/source-common/Utils.cpp diff --git a/source/Utils.hpp b/source-common/Utils.hpp index 9f28aad..9f28aad 100644 --- a/source/Utils.hpp +++ b/source-common/Utils.hpp diff --git a/source/YCombinator.hpp b/source-common/YCombinator.hpp index b1d2350..b1d2350 100644 --- a/source/YCombinator.hpp +++ b/source-common/YCombinator.hpp diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 6ca2cd5..b5de147 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -31,9 +31,7 @@ PRIVATE ) set(ProjectBrussel_SINGLE_UNIT_SRC - stb_implementation.c main.cpp - Utils.cpp # May include platform headers ) target_sources(${PROJECT_NAME} PRIVATE ${ProjectBrussel_SINGLE_UNIT_SRC}) set_source_files_properties(${ProjectBrussel_SINGLE_UNIT_SRC} |