|
| 1 | +set(current_dir ${CMAKE_CURRENT_LIST_DIR}) |
| 2 | +function(embed_resource) |
| 3 | + # parse function arguments |
| 4 | + set(options "") |
| 5 | + set(oneValueArguments NAMESPACE BASE_NAME) |
| 6 | + set(multiValueArguments RESOURCE_FILES) |
| 7 | + cmake_parse_arguments(EMBED "${options}" "${oneValueArguments}" |
| 8 | + "${multiValueArguments}" ${ARGN}) |
| 9 | + foreach(arg ${oneValueArguments} ${multiValueArguments}) |
| 10 | + if(NOT EMBED_${arg}) |
| 11 | + message(WARNING "Argument ${arg} not defined in call to embed_resource") |
| 12 | + endif() |
| 13 | + endforeach() |
| 14 | + |
| 15 | + set_property( |
| 16 | + DIRECTORY |
| 17 | + APPEND |
| 18 | + PROPERTY CMAKE_CONFIGURE_DEPENDS ${EMBED_RESOURCE_FILES}) |
| 19 | + |
| 20 | + set(byte_array_template |
| 21 | + "const char file_@FILE_COUNT@[] = { @FILE_BYTE_ARRAY@ }") |
| 22 | + set(lookup_template |
| 23 | + "if (fileName == \"@FILE_NAME@\") { stream.write(file_@FILE_COUNT@, @ARRAY_SIZE@)\; return true\; }" |
| 24 | + ) |
| 25 | + |
| 26 | + set(FILE_COUNT 1) |
| 27 | + foreach(file_path ${EMBED_RESOURCE_FILES}) |
| 28 | + # read resource file as hex with no byte separator |
| 29 | + file(READ ${file_path} file_contents HEX) |
| 30 | + # split into 2 char hex bytes |
| 31 | + string(REGEX MATCHALL "(..)" file_bytes ${file_contents}) |
| 32 | + # need length for call to ostream.write() |
| 33 | + list(LENGTH file_bytes ARRAY_SIZE) |
| 34 | + |
| 35 | + # add 0x prefix and , separator TRANSFORM only valid in cmake 3.14+ or we |
| 36 | + # could just do list(TRANSFORM file_bytes PREPEND "0x") list(JOIN file_bytes |
| 37 | + # "," FILE_BYTE_ARRAY) |
| 38 | + list(POP_FRONT file_bytes first_byte) |
| 39 | + string(PREPEND first_byte "0x") |
| 40 | + list(PREPEND file_bytes ${first_byte}) |
| 41 | + list(JOIN file_bytes ",0x" FILE_BYTE_ARRAY) |
| 42 | + |
| 43 | + # strip path from data file, leaving name |
| 44 | + get_filename_component(FILE_NAME "${file_path}" NAME) |
| 45 | + # generate single byte array from template |
| 46 | + string(CONFIGURE ${byte_array_template} byte_array @ONLY) |
| 47 | + # add the trailing semicolon here as it gets interpreted as a list seperator |
| 48 | + # if in template |
| 49 | + list(APPEND byte_arrays "${byte_array}\;") |
| 50 | + # generate single lookup entry |
| 51 | + string(CONFIGURE "${lookup_template}" lookup @ONLY) |
| 52 | + list(APPEND lookups "${lookup}") |
| 53 | + math(EXPR FILE_COUNT "${FILE_COUNT} + 1") |
| 54 | + endforeach() |
| 55 | + |
| 56 | + # join entries with newline for readability |
| 57 | + if(WIN32) |
| 58 | + set(line_end "\n\r") |
| 59 | + else() |
| 60 | + set(line_end "\n") |
| 61 | + endif() |
| 62 | + list(JOIN byte_arrays "${line_end}" EMBED_BYTE_ARRAYS) |
| 63 | + list(JOIN lookups "${line_end}" EMBED_LOOKUPS) |
| 64 | + |
| 65 | + # generate files |
| 66 | + configure_file("${current_dir}/embedded_resource.hpp.in" |
| 67 | + "${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.hpp" @ONLY) |
| 68 | + configure_file("${current_dir}/embedded_resource.cpp.in" |
| 69 | + "${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.cpp" @ONLY) |
| 70 | +endfunction() |
0 commit comments