From 4a121bf9db90d18fd85a1cb428de35b885f3638d Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 13 Sep 2023 23:57:08 +0100 Subject: [PATCH] cmake: replace python with a c++ target --- CMakeLists.txt | 9 ++++---- icon/generator.cxx | 54 ++++++++++++++++++++++++++++++++++++++++++++++ icon/generator.py | 22 ------------------- 3 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 icon/generator.cxx delete mode 100755 icon/generator.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a20abd..cec2548 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/icon/generator.cxx b/icon/generator.cxx new file mode 100644 index 0000000..dde42c2 --- /dev/null +++ b/icon/generator.cxx @@ -0,0 +1,54 @@ +#define LODEPNG_NO_COMPILE_ENCODER +#define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS + +#include "../modules/lodepng/lodepng.h" + +#include + +unsigned int output(const char* filename, const char* function_name, unsigned int size) { + std::vector 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(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; +} diff --git a/icon/generator.py b/icon/generator.py deleted file mode 100755 index 3cf38c2..0000000 --- a/icon/generator.py +++ /dev/null @@ -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 + "; }")