diff --git a/cmake/EthUtils.cmake b/cmake/EthUtils.cmake index 69690156a..147ce0737 100644 --- a/cmake/EthUtils.cmake +++ b/cmake/EthUtils.cmake @@ -62,3 +62,9 @@ macro(eth_add_test NAME) endmacro() +# Creates C resources file from files +function(eth_add_resources TARGET RESOURCE_FILE) + add_custom_command(TARGET ${TARGET} PRE_BUILD + COMMAND ${CMAKE_COMMAND} -DETH_RES_FILE="${RESOURCE_FILE}" -P "${ETH_SCRIPTS_DIR}/resources.cmake" + ) +endfunction() diff --git a/cmake/scripts/resource.cpp.in b/cmake/scripts/resource.cpp.in new file mode 100644 index 000000000..b73a8df1a --- /dev/null +++ b/cmake/scripts/resource.cpp.in @@ -0,0 +1,23 @@ +// this file is autogenerated, do not modify!!! + +#include +#include +#include "${ETH_RESOURCE_NAME}.h" + +using namespace std; +using namespace dev; +using namespace dev::eth; + +${ETH_RESOURCE_NAME}::${ETH_RESOURCE_NAME}() +{ +${ETH_RESULT_DATA} +${ETH_RESULT_INIT} +} + +string ${ETH_RESOURCE_NAME}::loadResourceAsString(string _name) +{ + ostringstream bistream; + bistream.write(eth_resources[_name], eth_sizes[_name]); + return bistream.str(); +} + diff --git a/cmake/scripts/resource.h.in b/cmake/scripts/resource.h.in new file mode 100644 index 000000000..b27c3c882 --- /dev/null +++ b/cmake/scripts/resource.h.in @@ -0,0 +1,26 @@ +// this file is autogenerated, do not modify!!! +#pragma once + +#include +#include + +namespace dev +{ +namespace eth +{ + +class ${ETH_RESOURCE_NAME} +{ +public: + ${ETH_RESOURCE_NAME}(); + std::string loadResourceAsString(std::string _name): + +private: + std::unordered_map m_resources; + std::unordered_map m_sizes; + +}; + +} +} + diff --git a/cmake/scripts/resources.cmake b/cmake/scripts/resources.cmake new file mode 100644 index 000000000..d69d99e96 --- /dev/null +++ b/cmake/scripts/resources.cmake @@ -0,0 +1,60 @@ +# based on: http://stackoverflow.com/questions/11813271/embed-resources-eg-shader-code-images-into-executable-library-with-cmake +# +# example: +# cmake -DETH_RES_FILE=test.cmake -P resources.cmake +# +# where test.cmake is: +# +# # BEGIN OF cmake.test +# +# set(copydlls "copydlls.cmake") +# set(conf "configure.cmake") +# +# # this three properties must be set! +# +# set(ETH_RESOURCE_NAME "EthResources") +# set(ETH_RESOURCE_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}") +# set(ETH_RESOURCES "copydlls" "conf") +# +# # END of cmake.test +# + +# should define ETH_RESOURCES +include(${ETH_RES_FILE}) + +set(ETH_RESULT_DATA "") +set(ETH_RESULT_INIT "") + +# resource is a name visible for cpp application +foreach(resource ${ETH_RESOURCES}) + + # filename is the name of file which will be used in app + set(filename ${${resource}}) + + # filedata is a file content + file(READ ${filename} filedata HEX) + + # read full name of the file + file(GLOB filename ${filename}) + + # Convert hex data for C compatibility + string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata}) + + # append static variables to result variable + set(ETH_RESULT_DATA "${ETH_RESULT_DATA} static const unsigned char eth_${resource}[] = {\n // ${filename}\n ${filedata}\n};\n") + + # append init resources + set(ETH_RESULT_INIT "${ETH_RESULT_INIT} eth_resources[\"${resource}\"] = (char const*)eth_${resource};\n") + set(ETH_RESULT_INIT "${ETH_RESULT_INIT} eth_sizes[\"${resource}\"] = sizeof(eth_${resource});\n") + +endforeach(resource) + +set(ETH_DST_NAME "${ETH_RESOURCE_LOCATION}/${ETH_RESOURCE_NAME}") + +configure_file("${CMAKE_CURRENT_LIST_DIR}/resource.cpp.in" "${ETH_DST_NAME}.cpp.tmp") +configure_file("${CMAKE_CURRENT_LIST_DIR}/resource.h.in" "${ETH_DST_NAME}.h.tmp") + +include("${CMAKE_CURRENT_LIST_DIR}/../EthUtils.cmake") +replace_if_different("${ETH_DST_NAME}.cpp.tmp" "${ETH_DST_NAME}.cpp") +replace_if_different("${ETH_DST_NAME}.h.tmp" "${ETH_DST_NAME}.h") +