Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | # Parameters: input output prefix namespace |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 2 | file(READ ${input} fileHex HEX) |
| 3 | string(LENGTH "${fileHex}" fileHexSize) |
| 4 | math(EXPR fileSize "${fileHexSize} / 2") |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 5 | |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 6 | get_filename_component(inputBase ${input} NAME) |
| 7 | string(REGEX REPLACE "[^a-zA-Z0-9]" "_" funcName "${inputBase}") |
| 8 | set(funcName "GetResource_${funcName}") |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 10 | file( |
| 11 | WRITE |
| 12 | "${output}" |
| 13 | "#include <stddef.h>\n#include <string_view>\nextern \"C\" {\nstatic const unsigned char contents[] = {" |
| 14 | ) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 16 | string(REGEX MATCHALL ".." outputData "${fileHex}") |
| 17 | string(REGEX REPLACE ";" ", 0x" outputData "${outputData}") |
| 18 | file(APPEND "${output}" " 0x${outputData} };\n") |
| 19 | file( |
| 20 | APPEND |
| 21 | "${output}" |
| 22 | "const unsigned char* ${prefix}${funcName}(size_t* len) {\n *len = ${fileSize};\n return contents;\n}\n}\n" |
| 23 | ) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 25 | if(NOT namespace STREQUAL "") |
| 26 | file(APPEND "${output}" "namespace ${namespace} {\n") |
| 27 | endif() |
| 28 | file( |
| 29 | APPEND |
| 30 | "${output}" |
| 31 | "std::string_view ${funcName}() {\n return std::string_view(reinterpret_cast<const char*>(contents), ${fileSize});\n}\n" |
| 32 | ) |
| 33 | if(NOT namespace STREQUAL "") |
| 34 | file(APPEND "${output}" "}\n") |
| 35 | endif() |