resources.cmake 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # based on: http://stackoverflow.com/questions/11813271/embed-resources-eg-shader-code-images-into-executable-library-with-cmake
  2. #
  3. # example:
  4. # cmake -DETH_RES_FILE=test.cmake -P resources.cmake
  5. #
  6. # where test.cmake is:
  7. #
  8. # # BEGIN OF cmake.test
  9. #
  10. # set(copydlls "copydlls.cmake")
  11. # set(conf "configure.cmake")
  12. #
  13. # # this three properties must be set!
  14. #
  15. # set(ETH_RESOURCE_NAME "EthResources")
  16. # set(ETH_RESOURCE_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}")
  17. # set(ETH_RESOURCES "copydlls" "conf")
  18. #
  19. # # END of cmake.test
  20. #
  21. # should define ETH_RESOURCES
  22. include(${ETH_RES_FILE})
  23. set(ETH_RESULT_DATA "")
  24. set(ETH_RESULT_INIT "")
  25. # resource is a name visible for cpp application
  26. foreach(resource ${ETH_RESOURCES})
  27. # filename is the name of file which will be used in app
  28. set(filename ${${resource}})
  29. # filedata is a file content
  30. file(READ ${filename} filedata HEX)
  31. # read full name of the file
  32. file(GLOB filename ${filename})
  33. # Convert hex data for C compatibility
  34. string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
  35. # append static variables to result variable
  36. set(ETH_RESULT_DATA "${ETH_RESULT_DATA} static const unsigned char eth_${resource}[] = {\n // ${filename}\n ${filedata}\n};\n")
  37. # append init resources
  38. set(ETH_RESULT_INIT "${ETH_RESULT_INIT} m_resources[\"${resource}\"] = (char const*)eth_${resource};\n")
  39. set(ETH_RESULT_INIT "${ETH_RESULT_INIT} m_sizes[\"${resource}\"] = sizeof(eth_${resource});\n")
  40. endforeach(resource)
  41. set(ETH_DST_NAME "${ETH_RESOURCE_LOCATION}/${ETH_RESOURCE_NAME}")
  42. configure_file("${CMAKE_CURRENT_LIST_DIR}/resource.hpp.in" "${ETH_DST_NAME}.hpp.tmp")
  43. include("${CMAKE_CURRENT_LIST_DIR}/../EthUtils.cmake")
  44. replace_if_different("${ETH_DST_NAME}.hpp.tmp" "${ETH_DST_NAME}.hpp")