bin2h.cmake 3.9 KB

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