mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
8da3b7f110
commit
ec7e0a6862
65
prebuild.cpp
Normal file
65
prebuild.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ctime>
|
||||
|
||||
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<unsigned char>(c));
|
||||
result += "\\x";
|
||||
result += buf;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> generate_python_sources() {
|
||||
std::map<std::string, std::string> 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<char>(f)), std::istreambuf_iterator<char>());
|
||||
sources[key] = to_hex_string(content);
|
||||
}
|
||||
}
|
||||
|
||||
return sources;
|
||||
}
|
||||
|
||||
std::string generate_header(const std::map<std::string, std::string>& 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 <map>\n#include <string>\n\nnamespace pkpy{\n";
|
||||
header += " inline static std::map<std::string, std::string> 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;
|
||||
}
|
3
prebuild.sh
Normal file
3
prebuild.sh
Normal file
@ -0,0 +1,3 @@
|
||||
g++ -o prebuild --std=c++17 prebuild.cpp
|
||||
./prebuild
|
||||
rm prebuild
|
Loading…
x
Reference in New Issue
Block a user