bin2array.cmake 967 B

1234567891011121314151617181920
  1. # Creates C resources file from files in given directory recursively
  2. function(create_resources dir output)
  3. # Create empty output file
  4. file(WRITE ${output} "#include <stdint.h>\n\n")
  5. # Collect input files
  6. file(GLOB bin_paths ${dir}/ESP*/*)
  7. # Iterate through input files
  8. foreach(bin ${bin_paths})
  9. # Get short filenames, by discarding relative path
  10. file(GLOB name RELATIVE ${dir} ${bin})
  11. # Replace filename spaces & extension separator for C compatibility
  12. string(REGEX REPLACE "[\\./-]" "_" filename ${name})
  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 uint8_t ${filename}[] = {${filedata}};\nconst uint32_t ${filename}_size = sizeof(${filename});\n")
  19. endforeach()
  20. endfunction()