├── LICENSE ├── README.md └── bin2h.cmake /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 Sivachandran Paramasivam 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bin2h.cmake 2 | Pure CMake function to convert any file into C/C++ header, implemented with only CMake commands. 3 | 4 | ## Example Usage 5 | ```cmake 6 | include(bin2h.cmake) 7 | ... 8 | message("Embedding following files into header file ${HEADER_FILE}") 9 | foreach(file ${SOURCE_FILES}) 10 | message(" ${file}") 11 | 12 | get_filename_component(variableName ${file} NAME) 13 | 14 | bin2h(SOURCE_FILE ${file} HEADER_FILE ${HEADER_FILE} VARIABLE_NAME ${variableName} APPEND NULL_TERMINATE) 15 | file(APPEND ${HEADER_FILE} "\n") 16 | endforeach() 17 | ``` 18 | -------------------------------------------------------------------------------- /bin2h.cmake: -------------------------------------------------------------------------------- 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() --------------------------------------------------------------------------------