bin2array.cmake 905 B

1234567891011121314151617181920
  1. # Creates C resources file from files in given directory
  2. function(create_resources dir output)
  3. # Create empty output file
  4. file(WRITE ${output} "")
  5. # Collect input files
  6. file(GLOB bins ${dir}/*)
  7. # Iterate through input files
  8. foreach(bin ${bins})
  9. # Get short filenames
  10. string(REGEX MATCH "([^/]+)$" filename ${bin})
  11. # Replace filename spaces & extension separator for C compatibility
  12. string(REGEX REPLACE "\\.| |-" "_" filename ${filename})
  13. # Read hex data from file
  14. file(READ ${bin} filedata HEX)
  15. # Convert hex data for C compatibility
  16. string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
  17. # Append data to output file
  18. file(APPEND ${output} "const unsigned char ${filename}[] = {${filedata}};\nconst unsigned ${filename}_size = sizeof(${filename});\n")
  19. endforeach()
  20. endfunction()