diff --git a/prebuild.cpp b/prebuild.cpp new file mode 100644 index 00000000..fb04720c --- /dev/null +++ b/prebuild.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include + +std::string to_hex_string(const std::string& input) { + std::string result; + result.reserve(input.length() * 4); + for (const auto& c : input) { + char buf[5] = {0}; + sprintf(buf, "%02x", static_cast(c)); + result += "\\x"; + result += buf; + } + return result; +} + +std::map generate_python_sources() { + std::map sources; + + for (const auto& file : std::filesystem::directory_iterator("python")) { + if (file.path().extension() == ".py") { + std::string key = file.path().stem().string(); + std::ifstream f(file.path()); + std::string content((std::istreambuf_iterator(f)), std::istreambuf_iterator()); + sources[key] = to_hex_string(content); + } + } + + return sources; +} + +std::string generate_header(const std::map& sources) { + std::time_t timestamp = std::time(nullptr); + std::tm* now = std::localtime(×tamp); + char timestamp_str[20]; + std::strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S", now); + + std::string header; + header += "#pragma once\n"; + header += "// generated on "; + header += timestamp_str; + header += "\n#include \n#include \n\nnamespace pkpy{\n"; + header += " inline static std::map kPythonLibs = {\n"; + + for (const auto& [key, value] : sources) { + header += " {\"" + key + "\", \"" + value + "\"},\n"; + } + + header += " };\n"; + header += "} // namespace pkpy\n"; + + return header; +} + +int main() { + auto sources = generate_python_sources(); + std::string header = generate_header(sources); + std::ofstream header_file("src/_generated.h"); + header_file << header; + return 0; +} \ No newline at end of file diff --git a/prebuild.sh b/prebuild.sh new file mode 100644 index 00000000..327997bc --- /dev/null +++ b/prebuild.sh @@ -0,0 +1,3 @@ +g++ -o prebuild --std=c++17 prebuild.cpp +./prebuild +rm prebuild \ No newline at end of file