Skip to content

Commit 4703bdc

Browse files
committed
Replace resource embedder with cmake function
Currently cross compilation fails as it is built for the target architecture, but then runs on the host architecture. This solves that problem and also * moves the function to retrieve data inside the adm namespace * moves the data inside an implementation file * allows embedding of non-text data This is the same change as recently applied to libear
1 parent fef7ff3 commit 4703bdc

8 files changed

+103
-61
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ configure_file(
6161
# add targets
6262
############################################################
6363
add_subdirectory(src)
64-
add_subdirectory(tools)
6564
if(ADM_EXAMPLES)
6665
add_subdirectory(examples)
6766
endif()

cmake/embed_resource.cmake

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
set(current_dir ${CMAKE_CURRENT_LIST_DIR})
2+
function(embed_resource)
3+
# parse function arguments
4+
set(options "")
5+
set(oneValueArguments NAMESPACE BASE_NAME)
6+
set(multiValueArguments RESOURCE_FILES)
7+
cmake_parse_arguments(EMBED "${options}" "${oneValueArguments}"
8+
"${multiValueArguments}" ${ARGN})
9+
foreach(arg ${oneValueArguments} ${multiValueArguments})
10+
if(NOT EMBED_${arg})
11+
message(WARNING "Argument ${arg} not defined in call to embed_resource")
12+
endif()
13+
endforeach()
14+
15+
set_property(
16+
DIRECTORY
17+
APPEND
18+
PROPERTY CMAKE_CONFIGURE_DEPENDS ${EMBED_RESOURCE_FILES})
19+
20+
set(byte_array_template
21+
"const char file_@FILE_COUNT@[] = { @FILE_BYTE_ARRAY@ }")
22+
set(lookup_template
23+
"if (fileName == \"@FILE_NAME@\") { stream.write(file_@FILE_COUNT@, @ARRAY_SIZE@)\; return true\; }"
24+
)
25+
26+
set(FILE_COUNT 1)
27+
foreach(file_path ${EMBED_RESOURCE_FILES})
28+
# read resource file as hex with no byte separator
29+
file(READ ${file_path} file_contents HEX)
30+
# split into 2 char hex bytes
31+
string(REGEX MATCHALL "(..)" file_bytes ${file_contents})
32+
# need length for call to ostream.write()
33+
list(LENGTH file_bytes ARRAY_SIZE)
34+
35+
# add 0x prefix and , separator TRANSFORM only valid in cmake 3.14+ or we
36+
# could just do list(TRANSFORM file_bytes PREPEND "0x") list(JOIN file_bytes
37+
# "," FILE_BYTE_ARRAY)
38+
list(POP_FRONT file_bytes first_byte)
39+
string(PREPEND first_byte "0x")
40+
list(PREPEND file_bytes ${first_byte})
41+
list(JOIN file_bytes ",0x" FILE_BYTE_ARRAY)
42+
43+
# strip path from data file, leaving name
44+
get_filename_component(FILE_NAME "${file_path}" NAME)
45+
# generate single byte array from template
46+
string(CONFIGURE ${byte_array_template} byte_array @ONLY)
47+
# add the trailing semicolon here as it gets interpreted as a list seperator
48+
# if in template
49+
list(APPEND byte_arrays "${byte_array}\;")
50+
# generate single lookup entry
51+
string(CONFIGURE "${lookup_template}" lookup @ONLY)
52+
list(APPEND lookups "${lookup}")
53+
math(EXPR FILE_COUNT "${FILE_COUNT} + 1")
54+
endforeach()
55+
56+
# join entries with newline for readability
57+
if(WIN32)
58+
set(line_end "\n\r")
59+
else()
60+
set(line_end "\n")
61+
endif()
62+
list(JOIN byte_arrays "${line_end}" EMBED_BYTE_ARRAYS)
63+
list(JOIN lookups "${line_end}" EMBED_LOOKUPS)
64+
65+
# generate files
66+
configure_file("${current_dir}/embedded_resource.hpp.in"
67+
"${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.hpp" @ONLY)
68+
configure_file("${current_dir}/embedded_resource.cpp.in"
69+
"${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.cpp" @ONLY)
70+
endfunction()

cmake/embedded_resource.cpp.in

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// WARNING This file is auto-generated during configuration by the cmake
2+
// function embed_resource(). Do not manually edit as changes will be lost
3+
4+
#include "@[email protected]"
5+
6+
namespace {
7+
@EMBED_BYTE_ARRAYS@
8+
}
9+
10+
bool @EMBED_NAMESPACE@::getEmbeddedFile(std::string const& fileName, std::ostream& stream) {
11+
@EMBED_LOOKUPS@
12+
return false;
13+
}

cmake/embedded_resource.hpp.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// WARNING This file is auto-generated during configuration by the cmake
2+
// function embed_resource(). Do not manually edit as changes will be lost
3+
4+
#pragma once
5+
#include <string>
6+
#include <ostream>
7+
8+
namespace @EMBED_NAMESPACE@ {
9+
bool getEmbeddedFile(std::string const& fileName, std::ostream& stream);
10+
}

format.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
DIRS="examples include src tests tools"
2+
DIRS="examples include src tests"
33

44
IGNORE="
55
src/elements/time.cpp

src/CMakeLists.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
include(${PROJECT_SOURCE_DIR}/submodules/rapidxml.cmake)
44

5-
add_custom_command(
6-
OUTPUT ${PROJECT_BINARY_DIR}/resources.hpp
7-
COMMAND resource_embedder common_definitions.xml > ${PROJECT_BINARY_DIR}/resources.hpp
8-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/resources
9-
DEPENDS resource_embedder ${PROJECT_SOURCE_DIR}/resources/common_definitions.xml
10-
COMMENT "Running resource embedder."
11-
VERBATIM
12-
)
5+
include(embed_resource)
6+
embed_resource(
7+
NAMESPACE adm
8+
BASE_NAME resources
9+
RESOURCE_FILES ${PROJECT_SOURCE_DIR}/resources/common_definitions.xml)
1310

1411
add_library(adm
1512
document.cpp
@@ -66,10 +63,13 @@ add_library(adm
6663
detail/id_assigner.cpp
6764
parse.cpp
6865
write.cpp
69-
${PROJECT_BINARY_DIR}/resources.hpp
66+
${CMAKE_CURRENT_BINARY_DIR}/resources.hpp
67+
${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
7068
)
7169

7270
target_include_directories(adm
71+
PRIVATE
72+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # resources.hpp
7373
PUBLIC
7474
# Headers used from source/build location:
7575
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>

tools/CMakeLists.txt

-3
This file was deleted.

tools/resource_embedder.cpp

-47
This file was deleted.

0 commit comments

Comments
 (0)