bin2h.cmake (3648B)
1 include(CMakeParseArguments) 2 3 # Function to wrap a given string into multiple lines at the given column position. 4 # Parameters: 5 # VARIABLE - The name of the CMake variable holding the string. 6 # AT_COLUMN - The column position at which string will be wrapped. 7 function(WRAP_STRING) 8 set(oneValueArgs VARIABLE AT_COLUMN) 9 cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN}) 10 11 string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength) 12 math(EXPR offset "0") 13 14 while(stringLength GREATER 0) 15 16 if(stringLength GREATER ${WRAP_STRING_AT_COLUMN}) 17 math(EXPR length "${WRAP_STRING_AT_COLUMN}") 18 else() 19 math(EXPR length "${stringLength}") 20 endif() 21 22 string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line) 23 set(lines "${lines}\n${line}") 24 25 math(EXPR stringLength "${stringLength} - ${length}") 26 math(EXPR offset "${offset} + ${length}") 27 endwhile() 28 29 set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE) 30 endfunction() 31 32 # Function to embed contents of a file as byte array in C/C++ header file(.h). The header file 33 # will contain a byte array and integer variable holding the size of the array. 34 # Parameters 35 # SOURCE_FILE - The path of source file whose contents will be embedded in the header file. 36 # VARIABLE_NAME - The name of the variable for the byte array. The string "_SIZE" will be append 37 # to this name and will be used a variable name for size variable. 38 # HEADER_FILE - The path of header file. 39 # APPEND - If specified appends to the header file instead of overwriting it 40 # NULL_TERMINATE - If specified a null byte(zero) will be append to the byte array. This will be 41 # useful if the source file is a text file and we want to use the file contents 42 # as string. But the size variable holds size of the byte array without this 43 # null byte. 44 # Usage: 45 # bin2h(SOURCE_FILE "Logo.png" HEADER_FILE "Logo.h" VARIABLE_NAME "LOGO_PNG") 46 function(BIN2H) 47 set(options APPEND NULL_TERMINATE) 48 set(oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE) 49 cmake_parse_arguments(BIN2H "${options}" "${oneValueArgs}" "" ${ARGN}) 50 51 # reads source file contents as hex string 52 file(READ ${BIN2H_SOURCE_FILE} hexString HEX) 53 string(LENGTH ${hexString} hexStringLength) 54 55 # appends null byte if asked 56 if(BIN2H_NULL_TERMINATE) 57 set(hexString "${hexString}00") 58 endif() 59 60 # wraps the hex string into multiple lines at column 32(i.e. 16 bytes per line) 61 wrap_string(VARIABLE hexString AT_COLUMN 32) 62 math(EXPR arraySize "${hexStringLength} / 2") 63 64 # adds '0x' prefix and comma suffix before and after every byte respectively 65 string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${hexString}) 66 # removes trailing comma 67 string(REGEX REPLACE ", $" "" arrayValues ${arrayValues}) 68 69 # converts the variable name into proper C identifier 70 string(MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME) 71 string(TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME) 72 73 # declares byte array and the length variables 74 set(arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };") 75 set(arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};") 76 77 set(declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n") 78 if(BIN2H_APPEND) 79 file(APPEND ${BIN2H_HEADER_FILE} "${declarations}") 80 else() 81 file(WRITE ${BIN2H_HEADER_FILE} "${declarations}") 82 endif() 83 endfunction()