Browse Source

finished resource scripts

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
a7996f4de7
  1. 3
      CMakeLists.txt
  2. 30
      cmake/EthUtils.cmake
  3. 21
      cmake/scripts/resource.cpp.in
  4. 26
      cmake/scripts/resource.h.in
  5. 45
      cmake/scripts/resources.cmake
  6. 6
      cmake/scripts/test.cmake

3
CMakeLists.txt

@ -383,9 +383,6 @@ 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)

30
cmake/EthUtils.cmake

@ -62,31 +62,9 @@ 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}")
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()

21
cmake/scripts/resource.cpp.in

@ -1,26 +1,23 @@
// this file is autogenerated, do not modify!!!
#include <string>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include "${ETH_RESOURCE_NAME}.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
${ETH_RESULT_DATA}
static unordered_map <string, const char*> eth_resources;
static unordered_map <string, unsigned> eth_sizes;
void initResources()
${ETH_RESOURCE_NAME}::${ETH_RESOURCE_NAME}()
{
${ETH_RESULT_INIT}
//eth_resources["LICENSE"] = (char const*)eth_LICENSE;
//eth_sizes["LICENSE"] = sizeof(eth_LICENSE);
${ETH_RESULT_DATA}
${ETH_RESULT_INIT}
}
string loadResource(string _name)
string ${ETH_RESOURCE_NAME}::loadResourceAsString(string _name)
{
ostringstream bistream;
bistream.write(eth_resources[_name], eth_sizes[_name]);
return bistream.str();
}

26
cmake/scripts/resource.h.in

@ -0,0 +1,26 @@
// this file is autogenerated, do not modify!!!
#pragma once
#include <string>
#include <unordered_map>
namespace dev
{
namespace eth
{
class ${ETH_RESOURCE_NAME}
{
public:
${ETH_RESOURCE_NAME}();
std::string loadResourceAsString(std::string _name):
private:
std::unordered_map <std::string, const char*> m_resources;
std::unordered_map <std::string, unsigned> m_sizes;
};
}
}

45
cmake/scripts/resources.cmake

@ -1,12 +1,29 @@
# cmake -DETH_RES_FILE=... -DETH_DST_NAME=... -P scripts/resources.cmake
# cmake -DETH_RES_FILE=test.cmake -DETH_DST_NAME=dst -P resources.cmake
# 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)
set(ETH_RESULT_DATA "")
set(ETH_RESULT_INIT "")
# resource is a name visible for cpp application
foreach(resource ${ETH_RESOURCES})
@ -17,21 +34,27 @@ foreach(resource ${ETH_RESOURCES})
# 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
list(APPEND ${ETH_RESULT_DATA} "static const unsigned char eth_${resource}[] = {\n // ${filename}\n ${filedata}\n};\n")
set(ETH_RESULT_DATA "${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")
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)
configure_file("resource.cpp.in" "${ETH_DST_NAME}.cpp.tmp")
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("../EthUtils.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../EthUtils.cmake")
replace_if_different("${ETH_DST_NAME}.cpp.tmp" "${ETH_DST_NAME}.cpp")
replace_if_different("resource.h" "${ETH_DST_NAME}.h")
replace_if_different("${ETH_DST_NAME}.h.tmp" "${ETH_DST_NAME}.h")

6
cmake/scripts/test.cmake

@ -1,6 +0,0 @@
set(copydlls "copydlls.cmake")
set(conf "configure.cmake")
set(ETH_RESOURCES "copyddls" "conf")
Loading…
Cancel
Save