data_file_embed_asm.cmake 2.6 KB

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