data_file_embed_asm.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. get_filename_component(varname "${DATA_FILE}" NAME)
  36. function(append str)
  37. file(APPEND "${SOURCE_FILE}" "${str}")
  38. endfunction()
  39. function(append_line str)
  40. append("${str}\n")
  41. endfunction()
  42. function(make_and_append_identifier str)
  43. string(MAKE_C_IDENTIFIER "${str}" symbol)
  44. append_line("\n.global ${symbol}")
  45. append("${symbol}:")
  46. if(${ARGC} GREATER 1) # optional comment
  47. append(" /* ${ARGV1} */")
  48. endif()
  49. append("\n")
  50. endfunction()
  51. file(WRITE "${SOURCE_FILE}" "/*")
  52. append_line(" * Data converted from ${DATA_FILE}")
  53. if(FILE_TYPE STREQUAL "TEXT")
  54. append_line(" * (null byte appended)")
  55. endif()
  56. append_line(" */")
  57. append_line(".data")
  58. append_line(".section .rodata.embedded")
  59. make_and_append_identifier("${varname}")
  60. make_and_append_identifier("_binary_${varname}_start" "for objcopy compatibility")
  61. append("${data}")
  62. make_and_append_identifier("_binary_${varname}_end" "for objcopy compatibility")
  63. append_line("")
  64. if(FILE_TYPE STREQUAL "TEXT")
  65. make_and_append_identifier("${varname}_length" "not including null byte")
  66. else()
  67. make_and_append_identifier("${varname}_length")
  68. endif()
  69. append_line(".word ${data_len}")