# Generate byte arrays without depending on external embedding tools.
set(EMBEDDED_SOURCE "#include \"embedded_assets.h\"\n\nnamespace\n{\n")
set(ASSET_ENTRIES "")
set(ASSET_INDEX 0)
foreach(ASSET IN LISTS STATIC_FILES)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${ASSET}")
file(READ "${ASSET}" ASSET_HEX HEX)
string(LENGTH "${ASSET_HEX}" ASSET_SIZE)
math(EXPR ASSET_SIZE "${ASSET_SIZE} / 2")
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1,"
ASSET_BYTES "${ASSET_HEX}")
string(APPEND EMBEDDED_SOURCE
"const unsigned char ASSET_${ASSET_INDEX}[] = {${ASSET_BYTES}0};\n")
file(RELATIVE_PATH ASSET_NAME "${CMAKE_CURRENT_SOURCE_DIR}/static"
"${ASSET}")
get_filename_component(ASSET_EXTENSION "${ASSET}" LAST_EXT)
if(ASSET_EXTENSION STREQUAL ".html")
set(ASSET_TYPE "text/html; charset=utf-8")
elseif(ASSET_EXTENSION STREQUAL ".css")
set(ASSET_TYPE "text/css; charset=utf-8")
elseif(ASSET_EXTENSION STREQUAL ".js")
set(ASSET_TYPE "text/javascript; charset=utf-8")
elseif(ASSET_EXTENSION STREQUAL ".svg")
set(ASSET_TYPE "image/svg+xml")
else()
set(ASSET_TYPE "application/octet-stream")
endif()
if(ASSET_NAME STREQUAL "index.html")
set(ASSET_PATH "/")
else()
set(ASSET_PATH "/static/${ASSET_NAME}")
endif()
string(APPEND ASSET_ENTRIES
" {\"${ASSET_PATH}\", \"${ASSET_TYPE}\", "
"{reinterpret_cast<const char*>(ASSET_${ASSET_INDEX}), "
"${ASSET_SIZE}}},\n")
math(EXPR ASSET_INDEX "${ASSET_INDEX} + 1")
endforeach()
string(APPEND EMBEDDED_SOURCE
"const EmbeddedAsset ASSETS[] = {\n${ASSET_ENTRIES}};\n}\n\n"
"std::span<const EmbeddedAsset> embeddedAssets()\n{\n"
" return ASSETS;\n}\n")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/generated/embedded_assets.cpp.tmp"
"${EMBEDDED_SOURCE}")
configure_file(
"${CMAKE_CURRENT_BINARY_DIR}/generated/embedded_assets.cpp.tmp"
"${CMAKE_CURRENT_BINARY_DIR}/generated/embedded_assets.cpp" COPYONLY)