# embed_spv.cmake — convert a SPIR-V binary to a C header
#
# Usage (invoked by CMakeLists.txt add_custom_command):
#   cmake -DINPUT=<path/to/foo.spv> -DOUTPUT=<path/to/foo_spv.h> -P embed_spv.cmake
#
# Output format (matches xxd -i naming convention used in repro.cpp):
#   alignas(4) static const unsigned char <varname>[] = { 0x03, 0x02, ... };
#   static const unsigned int <varname>_len = <N>;
#
# <varname> is derived from the INPUT filename (basename without extension,
# with non-alphanumeric characters replaced by underscores).

cmake_minimum_required(VERSION 3.0)

get_filename_component(_base "${INPUT}" NAME_WE)
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" _var "${_base}")

file(READ "${INPUT}" _raw HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " _bytes "${_raw}")
string(LENGTH "${_raw}" _hexlen)
math(EXPR _bytelen "${_hexlen} / 2")

file(WRITE "${OUTPUT}"
    "/* auto-generated by embed_spv.cmake — do not edit */\n"
    "/* source: ${INPUT} */\n"
    "alignas(4) static const unsigned char ${_var}[] = { ${_bytes}};\n"
    "static const unsigned int ${_var}_len = ${_bytelen};\n")
