data_file_embed_asm.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #
  2. # Convert a file (text or binary) into an assembler source file suitable
  3. # for gcc. Designed to replicate 'objcopy' with more predictable
  4. # naming, and supports appending a null byte for embedding text as
  5. # a string.
  6. #
  7. # Designed to be run as a script with "cmake -P"
  8. #
  9. # Set variables DATA_FILE, SOURCE_FILE, FILE_TYPE when running this.
  10. #
  11. # If FILE_TYPE is set to TEXT, a null byte is appended to DATA_FILE's contents
  12. # before SOURCE_FILE is created.
  13. #
  14. # If FILE_TYPE is unset (or any other value), DATA_FILE is copied
  15. # verbatim into SOURCE_FILE.
  16. #
  17. #
  18. if(NOT DATA_FILE)
  19. message(FATAL_ERROR "DATA_FILE for converting must be specified")
  20. endif()
  21. if(NOT SOURCE_FILE)
  22. message(FATAL_ERROR "SOURCE_FILE destination must be specified")
  23. endif()
  24. file(READ "${DATA_FILE}" data HEX)
  25. string(LENGTH "${data}" data_len)
  26. math(EXPR data_len "${data_len} / 2") # 2 hex bytes per byte
  27. if(FILE_TYPE STREQUAL "TEXT")
  28. set(data "${data}00") # null-byte termination
  29. endif()
  30. ## Convert string of raw hex bytes to lines of hex bytes as gcc .byte expressions
  31. string(REGEX REPLACE "................................" ".byte \\0\n" data "${data}") # 16 bytes per line
  32. string(REGEX REPLACE "[^\n]+$" ".byte \\0\n" data "${data}") # last line
  33. string(REGEX REPLACE "[0-9a-f][0-9a-f]" "0x\\0, " data "${data}") # hex formatted C bytes
  34. string(REGEX REPLACE ", \n" "\n" data "${data}") # trim the last comma
  35. ## Come up with C-friendly variable name based on source file
  36. # unless VARIABLE_BASENAME is set
  37. if(NOT VARIABLE_BASENAME)
  38. get_filename_component(source_filename "${DATA_FILE}" NAME)
  39. string(MAKE_C_IDENTIFIER "${source_filename}" varname)
  40. else()
  41. string(MAKE_C_IDENTIFIER "${VARIABLE_BASENAME}" varname)
  42. endif()
  43. function(append str)
  44. file(APPEND "${SOURCE_FILE}" "${str}")
  45. endfunction()
  46. function(append_line str)
  47. append("${str}\n")
  48. endfunction()
  49. function(append_identifier symbol)
  50. append_line("\n.global ${symbol}")
  51. append("${symbol}:")
  52. if(${ARGC} GREATER 1) # optional comment
  53. append(" /* ${ARGV1} */")
  54. endif()
  55. append("\n")
  56. endfunction()
  57. file(WRITE "${SOURCE_FILE}" "/*")
  58. append_line(" * Data converted from ${DATA_FILE}")
  59. if(FILE_TYPE STREQUAL "TEXT")
  60. append_line(" * (null byte appended)")
  61. endif()
  62. append_line(" */")
  63. append_line(".data")
  64. append_line(".section .rodata.embedded")
  65. append_identifier("${varname}")
  66. append_identifier("_binary_${varname}_start" "for objcopy compatibility")
  67. append("${data}")
  68. append_identifier("_binary_${varname}_end" "for objcopy compatibility")
  69. append_line("")
  70. if(FILE_TYPE STREQUAL "TEXT")
  71. append_identifier("${varname}_length" "not including null byte")
  72. else()
  73. append_identifier("${varname}_length")
  74. endif()
  75. append_line(".word ${data_len}")