bin2h.cmake 3.4 KB

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