cmake: replace python with a c++ target

This commit is contained in:
Adam
2023-09-13 23:57:08 +01:00
parent 652a9980b7
commit 4a121bf9db
3 changed files with 59 additions and 26 deletions

View File

@@ -104,12 +104,13 @@ add_subdirectory(${CEF_LIBCEF_DLL_WRAPPER_PATH} libcef_dll_wrapper)
set(WINDOW_LAUNCHER_OS_SPECIFIC src/browser/window_launcher_linux.cxx)
# auto-generated C++ file
set(BOLT_ICON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/icon)
# compile an auto-generator, then use it to auto-generate a C++ file containing icon data
set(BOLT_ICON_DIR "${CMAKE_CURRENT_SOURCE_DIR}/icon")
add_executable(icon_gen icon/generator.cxx modules/lodepng/lodepng.cpp)
add_custom_command(
OUTPUT client_cmake_gen.cxx
DEPENDS ${BOLT_ICON_DIR}/generator.py ${BOLT_ICON_DIR}/16.png ${BOLT_ICON_DIR}/32.png ${BOLT_ICON_DIR}/64.png ${BOLT_ICON_DIR}/256.png
COMMAND python ${BOLT_ICON_DIR}/generator.py ${CMAKE_CURRENT_SOURCE_DIR} ${BOLT_ICON_DIR} ">client_cmake_gen.cxx"
DEPENDS icon_gen ${BOLT_ICON_DIR}/16.png ${BOLT_ICON_DIR}/32.png ${BOLT_ICON_DIR}/64.png ${BOLT_ICON_DIR}/256.png
COMMAND icon_gen ${CMAKE_CURRENT_SOURCE_DIR} ${BOLT_ICON_DIR} ">client_cmake_gen.cxx"
)
# This line needs to be updated manually with any new/deleted object files; cmake discourages GLOBbing source files

54
icon/generator.cxx Normal file
View File

@@ -0,0 +1,54 @@
#define LODEPNG_NO_COMPILE_ENCODER
#define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
#include "../modules/lodepng/lodepng.h"
#include <iostream>
unsigned int output(const char* filename, const char* function_name, unsigned int size) {
std::vector<unsigned char> image;
unsigned int width, height;
unsigned int error = lodepng::decode(image, width, height, filename);
if(error) return error;
if (width != size || height != size) return 1;
const char* var_name = function_name + 3;
std::cout << "constexpr unsigned char " << var_name << "[] = {";
bool first = true;
for (unsigned char& b: image) {
unsigned short num = static_cast<unsigned short>(b);
if (first) {
first = false;
std::cout << "0x" << num;
} else {
std::cout << ",0x" << num;
}
}
std::cout << "};" << std::endl << "const unsigned char* Browser::Client::" << function_name << "() const { return " << var_name << "; }" << std::endl;
return 0;
}
int main(int argc, const char** argv) {
if (argc < 3) return 1;
int err;
std::cout << std::hex << "#include \"" << argv[1] << "/src/browser/client.hxx\"" << std::endl;
size_t icon_path_len = strlen(argv[2]);
char* filename_buf = new char[icon_path_len + strlen("/tray.png") + 1]; // longest filename, including delimiter
memcpy(filename_buf, argv[2], icon_path_len);
strcpy(filename_buf + icon_path_len, "/16.png");
err = output(filename_buf, "GetIcon16", 16);
if (err) goto exit;
strcpy(filename_buf + icon_path_len, "/32.png");
err = output(filename_buf, "GetIcon32", 32);
if (err) goto exit;
strcpy(filename_buf + icon_path_len, "/64.png");
err = output(filename_buf, "GetIcon64", 64);
if (err) goto exit;
strcpy(filename_buf + icon_path_len, "/256.png");
err = output(filename_buf, "GetIcon256", 256);
if (err) goto exit;
strcpy(filename_buf + icon_path_len, "/tray.png");
err = output(filename_buf, "GetTrayIcon", 24);
exit:
delete[] filename_buf;
return err;
}

View File

@@ -1,22 +0,0 @@
#!/usr/bin/env python3
# python file invoked by cmake to output a .cxx file for the icons in this directory
from PIL import Image
import os
import sys
images = [("16.png", "GetIcon16"), ("32.png", "GetIcon32"), ("64.png", "GetIcon64"), ("256.png", "GetIcon256"), ("tray.png", "GetTrayIcon")]
srcdir = sys.argv[1] if len(sys.argv) > 1 else '.'
icondir = sys.argv[2] if len(sys.argv) > 2 else os.path.join(srcdir, 'icon')
print("#include \"" + srcdir + "/src/browser/client.hxx\"")
for (file_name, function_name) in images:
var_name = "icon_" + file_name.replace('.', '_')
out_px = []
f = Image.open(os.path.join(icondir, file_name), 'r')
pixels = f.load()
for y in range(f.height):
for x in range(f.width):
out_px += pixels[x, y]
print("constexpr unsigned char " + var_name + "[] = {" + ','.join(map(hex, out_px)) + "};")
print("const unsigned char* Browser::Client::" + function_name + "() const { return " + var_name + "; }")