|
| 1 | +# cmake/WasmHtml.cmake |
| 2 | +# Post-processes the Qt-generated Wasm HTML file: |
| 3 | +# - Injects a favicon link tag |
| 4 | +# - Replaces the Qt logo reference with the MMapper logo |
| 5 | +# - Injects the COI service worker script tag |
| 6 | +# - Copies favicon.ico and logo.png alongside the HTML |
| 7 | +# - Downloads coi-serviceworker.js alongside the HTML |
| 8 | +# |
| 9 | +# Usage (as a post-build cmake -P script): |
| 10 | +# cmake -DHTML_FILE=<path/mmapper.html> |
| 11 | +# -DFAVICON_SRC=<path/m-release.ico> |
| 12 | +# -DLOGO_SRC=<path/m-release.png> |
| 13 | +# -P WasmHtml.cmake |
| 14 | + |
| 15 | +foreach(var HTML_FILE FAVICON_SRC LOGO_SRC) |
| 16 | + if(NOT ${var}) |
| 17 | + message(FATAL_ERROR "${var} not specified for WasmHtml.cmake") |
| 18 | + endif() |
| 19 | +endforeach() |
| 20 | + |
| 21 | +file(READ "${HTML_FILE}" HTML_CONTENT) |
| 22 | + |
| 23 | +# Inject favicon link and COI service worker script after <head> |
| 24 | +string(REPLACE |
| 25 | + "<head>" |
| 26 | + "<head>\n <link rel=\"icon\" type=\"image/x-icon\" href=\"favicon.ico\">\n <script src=\"./coi-serviceworker.js\"></script>" |
| 27 | + HTML_CONTENT "${HTML_CONTENT}" |
| 28 | +) |
| 29 | + |
| 30 | +# Replace Qt logo with MMapper logo |
| 31 | +string(REPLACE |
| 32 | + "src=\"qtlogo.svg\" width=\"320\" height=\"200\"" |
| 33 | + "src=\"logo.png\"" |
| 34 | + HTML_CONTENT "${HTML_CONTENT}" |
| 35 | +) |
| 36 | + |
| 37 | +file(WRITE "${HTML_FILE}" "${HTML_CONTENT}") |
| 38 | + |
| 39 | +# Copy assets alongside the HTML (use configure_file for portable rename support) |
| 40 | +get_filename_component(OUT_DIR "${HTML_FILE}" DIRECTORY) |
| 41 | +configure_file("${FAVICON_SRC}" "${OUT_DIR}/favicon.ico" COPYONLY) |
| 42 | +configure_file("${LOGO_SRC}" "${OUT_DIR}/logo.png" COPYONLY) |
| 43 | + |
| 44 | +# Download coi-serviceworker.js if not already present |
| 45 | +set(COI_JS "${OUT_DIR}/coi-serviceworker.js") |
| 46 | +if(NOT EXISTS "${COI_JS}") |
| 47 | + message(STATUS "Downloading coi-serviceworker.js...") |
| 48 | + file(DOWNLOAD |
| 49 | + "https://raw.githubusercontent.com/gzuidhof/coi-serviceworker/refs/heads/master/coi-serviceworker.js" |
| 50 | + "${COI_JS}" |
| 51 | + STATUS DOWNLOAD_STATUS |
| 52 | + ) |
| 53 | + list(GET DOWNLOAD_STATUS 0 DOWNLOAD_CODE) |
| 54 | + if(NOT DOWNLOAD_CODE EQUAL 0) |
| 55 | + list(GET DOWNLOAD_STATUS 1 DOWNLOAD_ERROR) |
| 56 | + message(WARNING "Failed to download coi-serviceworker.js: ${DOWNLOAD_ERROR}. " |
| 57 | + "The Wasm build may not function correctly in cross-origin contexts.") |
| 58 | + endif() |
| 59 | +endif() |
| 60 | + |
| 61 | +message(STATUS "Patched Wasm HTML: ${HTML_FILE}") |
0 commit comments