From b51f01ed100f97076584c6900f73deb5dc5f6bbc Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 29 Apr 2015 19:17:07 +0200 Subject: [PATCH] resources scripts --- CMakeLists.txt | 3 +++ cmake/EthUtils.cmake | 28 ++++++++++++++++++++++++++ cmake/scripts/resource.cpp.in | 26 ++++++++++++++++++++++++ cmake/scripts/resources.cmake | 37 +++++++++++++++++++++++++++++++++++ cmake/scripts/test.cmake | 6 ++++++ 5 files changed, 100 insertions(+) create mode 100644 cmake/scripts/resource.cpp.in create mode 100644 cmake/scripts/resources.cmake create mode 100644 cmake/scripts/test.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ff8732156..a770e9704 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -383,6 +383,9 @@ if (GUI) endif() +set(ETH_RESOURCES "LICENSE" "README.md") +eth_create_resources(ETH_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/here.h") + #unset(TARGET_PLATFORM CACHE) if (WIN32) diff --git a/cmake/EthUtils.cmake b/cmake/EthUtils.cmake index 69690156a..5d271f341 100644 --- a/cmake/EthUtils.cmake +++ b/cmake/EthUtils.cmake @@ -62,3 +62,31 @@ macro(eth_add_test NAME) endmacro() +# Based on +# http://stackoverflow.com/questions/11813271/embed-resources-eg-shader-code-images-into-executable-library-with-cmake +# Creates C resources file from files +function(eth_create_resources bins output) + set(tmp_output "${output}.tmp") + # Create empty output file + file(WRITE ${tmp_output} "") + # Collect input files +# file(GLOB bins ${dir}/*) + # Iterate through input files + foreach(bin ${${bins}}) + # Get short filename + string(REGEX MATCH "([^/]+)$" filename ${bin}) + # Replace filename spaces & extension separator for C compatibility + string(REGEX REPLACE "\\.| " "_" filename ${filename}) + # Add eth prefix (qt does the same thing) + set(filename "eth_${filename}") + # full name + file(GLOB the_name ${bin}) + # Read hex data from file + file(READ ${bin} filedata HEX) + # Convert hex data for C compatibility + string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata}) + # Append data to output file + file(APPEND ${tmp_output} "static const unsigned char ${filename}[] = {\n // ${the_name}\n ${filedata}};\nstatic const unsigned ${filename}_size = sizeof(${filename});\n") + endforeach() + replace_if_different("${tmp_output}" "${output}") +endfunction() diff --git a/cmake/scripts/resource.cpp.in b/cmake/scripts/resource.cpp.in new file mode 100644 index 000000000..369f7b56f --- /dev/null +++ b/cmake/scripts/resource.cpp.in @@ -0,0 +1,26 @@ + +#include +#include +#include +#include + +using namespace std; + +${ETH_RESULT_DATA} + +static unordered_map eth_resources; +static unordered_map eth_sizes; + +void initResources() +{ + ${ETH_RESULT_INIT} + //eth_resources["LICENSE"] = (char const*)eth_LICENSE; + //eth_sizes["LICENSE"] = sizeof(eth_LICENSE); +} + +string loadResource(string _name) +{ + ostringstream bistream; + bistream.write(eth_resources[_name], eth_sizes[_name]); + return bistream.str(); +} diff --git a/cmake/scripts/resources.cmake b/cmake/scripts/resources.cmake new file mode 100644 index 000000000..2c92103ff --- /dev/null +++ b/cmake/scripts/resources.cmake @@ -0,0 +1,37 @@ + +# cmake -DETH_RES_FILE=... -DETH_DST_NAME=... -P scripts/resources.cmake +# cmake -DETH_RES_FILE=test.cmake -DETH_DST_NAME=dst -P resources.cmake + +# 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) + + # 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 + list(APPEND ${ETH_RESULT_DATA} "static const unsigned char eth_${resource}[] = {\n // ${filename}\n ${filedata}\n};\n") + + # append init resources + list(APPEND ${ETH_RESULT_INIT} " eth_resources[\"${resource}\"] = (char const*)eth_${resource};\n") + list(APPEND ${ETH_RESULT_INIT} " eth_sizes[\"${resource}\"] = sizeof(eth_${resource});\n") + +endforeach(resource) + +configure_file("resource.cpp.in" "${ETH_DST_NAME}.cpp.tmp") + +include("../EthUtils.cmake") +replace_if_different("${ETH_DST_NAME}.cpp.tmp" "${ETH_DST_NAME}.cpp") +replace_if_different("resource.h" "${ETH_DST_NAME}.h") + diff --git a/cmake/scripts/test.cmake b/cmake/scripts/test.cmake new file mode 100644 index 000000000..883ed324e --- /dev/null +++ b/cmake/scripts/test.cmake @@ -0,0 +1,6 @@ + +set(copydlls "copydlls.cmake") +set(conf "configure.cmake") + +set(ETH_RESOURCES "copyddls" "conf") +