From 2cd41adf425baeb87cacb8fcf741e8c3e50f9428 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:25:04 +0300 Subject: [PATCH] Remove bin2c, load the PPC context header by path. --- CMakeLists.txt | 2 - PowerRecomp/CMakeLists.txt | 6 --- PowerRecomp/generated/.gitignore | 3 -- PowerRecomp/main.cpp | 10 +++- PowerRecomp/pch.h | 1 - PowerRecomp/recompiler.cpp | 11 ++++- PowerRecomp/recompiler.h | 2 +- cmake/bin2h.cmake | 84 -------------------------------- 8 files changed, 19 insertions(+), 100 deletions(-) delete mode 100644 PowerRecomp/generated/.gitignore delete mode 100644 cmake/bin2h.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 450a6dd..76b48ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,6 @@ endif() set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") -include("cmake/bin2h.cmake") - add_subdirectory(${THIRDPARTY_ROOT}) set(POWERANALYSE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/PowerAnalyse) set(POWERUTILS_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/PowerUtils) diff --git a/PowerRecomp/CMakeLists.txt b/PowerRecomp/CMakeLists.txt index 287e9e3..7df1149 100644 --- a/PowerRecomp/CMakeLists.txt +++ b/PowerRecomp/CMakeLists.txt @@ -2,12 +2,6 @@ cmake_minimum_required (VERSION 3.8) project("PowerRecomp") -BIN2H(SOURCE_FILE - ${POWERUTILS_ROOT}/ppc_context.h - HEADER_FILE "generated/ppc_context.gen.h" - ARRAY_TYPE "char" - VARIABLE_NAME "g_PPCContextText") - add_executable(PowerRecomp "main.cpp" "recompiler.cpp" diff --git a/PowerRecomp/generated/.gitignore b/PowerRecomp/generated/.gitignore deleted file mode 100644 index cec9082..0000000 --- a/PowerRecomp/generated/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -* - -!.gitignore diff --git a/PowerRecomp/main.cpp b/PowerRecomp/main.cpp index f172546..b0e5e2d 100644 --- a/PowerRecomp/main.cpp +++ b/PowerRecomp/main.cpp @@ -23,7 +23,15 @@ int main(int argc, char* argv[]) entry->name = "_xstart"; } - recompiler.Recompile(); + const char* headerFilePath = +#ifdef HEADER_FILE_PATH + HEADER_FILE_PATH +#else + argv[2] +#endif + ; + + recompiler.Recompile(headerFilePath); } else { diff --git a/PowerRecomp/pch.h b/PowerRecomp/pch.h index a5a047e..02e53d9 100644 --- a/PowerRecomp/pch.h +++ b/PowerRecomp/pch.h @@ -17,4 +17,3 @@ #include #include #include -#include "generated/ppc_context.gen.h" diff --git a/PowerRecomp/recompiler.cpp b/PowerRecomp/recompiler.cpp index c499071..78443e8 100644 --- a/PowerRecomp/recompiler.cpp +++ b/PowerRecomp/recompiler.cpp @@ -2361,7 +2361,7 @@ bool Recompiler::Recompile(const Function& fn) return allRecompiled; } -void Recompiler::Recompile() +void Recompiler::Recompile(const std::filesystem::path& headerFilePath) { out.reserve(10 * 1024 * 1024); @@ -2403,7 +2403,14 @@ void Recompiler::Recompile() println("#pragma once"); println("#include \"ppc_config.h\"\n"); - println("{}", std::string_view{g_PPCContextText, g_PPCContextText_size}); + + std::ifstream stream(headerFilePath); + if (stream.good()) + { + std::stringstream ss; + ss << stream.rdbuf(); + out += ss.str(); + } SaveCurrentOutData("ppc_context.h"); } diff --git a/PowerRecomp/recompiler.h b/PowerRecomp/recompiler.h index f014d96..abb5498 100644 --- a/PowerRecomp/recompiler.h +++ b/PowerRecomp/recompiler.h @@ -64,7 +64,7 @@ struct Recompiler bool Recompile(const Function& fn); - void Recompile(); + void Recompile(const std::filesystem::path& headerFilePath); void SaveCurrentOutData(const std::string_view& name = std::string_view()); }; diff --git a/cmake/bin2h.cmake b/cmake/bin2h.cmake deleted file mode 100644 index 5324081..0000000 --- a/cmake/bin2h.cmake +++ /dev/null @@ -1,84 +0,0 @@ -# https://github.com/sivachandran/cmake-bin2h -include(CMakeParseArguments) - -# Function to wrap a given string into multiple lines at the given column position. -# Parameters: -# VARIABLE - The name of the CMake variable holding the string. -# AT_COLUMN - The column position at which string will be wrapped. -function(WRAP_STRING) - set(oneValueArgs VARIABLE AT_COLUMN) - cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN}) - - string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength) - math(EXPR offset "0") - - while(stringLength GREATER 0) - - if(stringLength GREATER ${WRAP_STRING_AT_COLUMN}) - math(EXPR length "${WRAP_STRING_AT_COLUMN}") - else() - math(EXPR length "${stringLength}") - endif() - - string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line) - set(lines "${lines}\n${line}") - - math(EXPR stringLength "${stringLength} - ${length}") - math(EXPR offset "${offset} + ${length}") - endwhile() - - set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE) -endfunction() - -# Function to embed contents of a file as byte array in C/C++ header file(.h). The header file -# will contain a byte array and integer variable holding the size of the array. -# Parameters -# SOURCE_FILE - The path of source file whose contents will be embedded in the header file. -# VARIABLE_NAME - The name of the variable for the byte array. The string "_SIZE" will be append -# to this name and will be used a variable name for size variable. -# HEADER_FILE - The path of header file. -# ARRAY_TYPE - The type of each element of the array in the header file. -# APPEND - If specified appends to the header file instead of overwriting it -# NULL_TERMINATE - If specified a null byte(zero) will be append to the byte array. This will be -# useful if the source file is a text file and we want to use the file contents -# as string. But the size variable holds size of the byte array without this -# null byte. -# Usage: -# bin2h(SOURCE_FILE "Logo.png" HEADER_FILE "Logo.h" ARRAY_TYPE "char" VARIABLE_NAME "LOGO_PNG") -function(BIN2H) - set(options APPEND NULL_TERMINATE) - set(oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE ARRAY_TYPE) - cmake_parse_arguments(BIN2H "${options}" "${oneValueArgs}" "" ${ARGN}) - - # reads source file contents as hex string - file(READ ${BIN2H_SOURCE_FILE} hexString HEX) - string(LENGTH ${hexString} hexStringLength) - - # appends null byte if asked - if(BIN2H_NULL_TERMINATE) - set(hexString "${hexString}00") - endif() - - # wraps the hex string into multiple lines at column 32(i.e. 16 bytes per line) - wrap_string(VARIABLE hexString AT_COLUMN 32) - math(EXPR arraySize "${hexStringLength} / 2") - - # adds '0x' prefix and comma suffix before and after every byte respectively - string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${hexString}) - # removes trailing comma - string(REGEX REPLACE ", $" "" arrayValues ${arrayValues}) - - # converts the variable name into proper C identifier - string(MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME) - - # declares byte array and the length variables - set(arrayDefinition "const ${BIN2H_ARRAY_TYPE} ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };") - set(arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_size = ${arraySize};") - - set(declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n") - if(BIN2H_APPEND) - file(APPEND ${BIN2H_HEADER_FILE} "${declarations}") - else() - file(WRITE ${BIN2H_HEADER_FILE} "${declarations}") - endif() -endfunction()