Skip to content

Commit

Permalink
Replace resource embedder with cmake function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsjbailey committed Oct 12, 2021
1 parent fef7ff3 commit 4703bdc
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 61 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ configure_file(
# add targets
############################################################
add_subdirectory(src)
add_subdirectory(tools)
if(ADM_EXAMPLES)
add_subdirectory(examples)
endif()
Expand Down
70 changes: 70 additions & 0 deletions cmake/embed_resource.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
set(current_dir ${CMAKE_CURRENT_LIST_DIR})
function(embed_resource)
# parse function arguments
set(options "")
set(oneValueArguments NAMESPACE BASE_NAME)
set(multiValueArguments RESOURCE_FILES)
cmake_parse_arguments(EMBED "${options}" "${oneValueArguments}"
"${multiValueArguments}" ${ARGN})
foreach(arg ${oneValueArguments} ${multiValueArguments})
if(NOT EMBED_${arg})
message(WARNING "Argument ${arg} not defined in call to embed_resource")
endif()
endforeach()

set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS ${EMBED_RESOURCE_FILES})

set(byte_array_template
"const char file_@FILE_COUNT@[] = { @FILE_BYTE_ARRAY@ }")
set(lookup_template
"if (fileName == \"@FILE_NAME@\") { stream.write(file_@FILE_COUNT@, @ARRAY_SIZE@)\; return true\; }"
)

set(FILE_COUNT 1)
foreach(file_path ${EMBED_RESOURCE_FILES})
# read resource file as hex with no byte separator
file(READ ${file_path} file_contents HEX)
# split into 2 char hex bytes
string(REGEX MATCHALL "(..)" file_bytes ${file_contents})
# need length for call to ostream.write()
list(LENGTH file_bytes ARRAY_SIZE)

# add 0x prefix and , separator TRANSFORM only valid in cmake 3.14+ or we
# could just do list(TRANSFORM file_bytes PREPEND "0x") list(JOIN file_bytes
# "," FILE_BYTE_ARRAY)
list(POP_FRONT file_bytes first_byte)
string(PREPEND first_byte "0x")
list(PREPEND file_bytes ${first_byte})
list(JOIN file_bytes ",0x" FILE_BYTE_ARRAY)

# strip path from data file, leaving name
get_filename_component(FILE_NAME "${file_path}" NAME)
# generate single byte array from template
string(CONFIGURE ${byte_array_template} byte_array @ONLY)
# add the trailing semicolon here as it gets interpreted as a list seperator
# if in template
list(APPEND byte_arrays "${byte_array}\;")
# generate single lookup entry
string(CONFIGURE "${lookup_template}" lookup @ONLY)
list(APPEND lookups "${lookup}")
math(EXPR FILE_COUNT "${FILE_COUNT} + 1")
endforeach()

# join entries with newline for readability
if(WIN32)
set(line_end "\n\r")
else()
set(line_end "\n")
endif()
list(JOIN byte_arrays "${line_end}" EMBED_BYTE_ARRAYS)
list(JOIN lookups "${line_end}" EMBED_LOOKUPS)

# generate files
configure_file("${current_dir}/embedded_resource.hpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.hpp" @ONLY)
configure_file("${current_dir}/embedded_resource.cpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.cpp" @ONLY)
endfunction()
13 changes: 13 additions & 0 deletions cmake/embedded_resource.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// WARNING This file is auto-generated during configuration by the cmake
// function embed_resource(). Do not manually edit as changes will be lost

#include "@[email protected]"

namespace {
@EMBED_BYTE_ARRAYS@
}

bool @EMBED_NAMESPACE@::getEmbeddedFile(std::string const& fileName, std::ostream& stream) {
@EMBED_LOOKUPS@
return false;
}
10 changes: 10 additions & 0 deletions cmake/embedded_resource.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// WARNING This file is auto-generated during configuration by the cmake
// function embed_resource(). Do not manually edit as changes will be lost

#pragma once
#include <string>
#include <ostream>

namespace @EMBED_NAMESPACE@ {
bool getEmbeddedFile(std::string const& fileName, std::ostream& stream);
}
2 changes: 1 addition & 1 deletion format.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
DIRS="examples include src tests tools"
DIRS="examples include src tests"

IGNORE="
src/elements/time.cpp
Expand Down
18 changes: 9 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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

add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/resources.hpp
COMMAND resource_embedder common_definitions.xml > ${PROJECT_BINARY_DIR}/resources.hpp
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/resources
DEPENDS resource_embedder ${PROJECT_SOURCE_DIR}/resources/common_definitions.xml
COMMENT "Running resource embedder."
VERBATIM
)
include(embed_resource)
embed_resource(
NAMESPACE adm
BASE_NAME resources
RESOURCE_FILES ${PROJECT_SOURCE_DIR}/resources/common_definitions.xml)

add_library(adm
document.cpp
Expand Down Expand Up @@ -66,10 +63,13 @@ add_library(adm
detail/id_assigner.cpp
parse.cpp
write.cpp
${PROJECT_BINARY_DIR}/resources.hpp
${CMAKE_CURRENT_BINARY_DIR}/resources.hpp
${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
)

target_include_directories(adm
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # resources.hpp
PUBLIC
# Headers used from source/build location:
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
Expand Down
3 changes: 0 additions & 3 deletions tools/CMakeLists.txt

This file was deleted.

47 changes: 0 additions & 47 deletions tools/resource_embedder.cpp

This file was deleted.

0 comments on commit 4703bdc

Please sign in to comment.