From e7795ab0d0546cf49529785499852efb2ef85346 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Wed, 30 Dec 2020 13:49:29 +0000 Subject: [PATCH] Embed resources in executable. First draft using cmake. --- CMakeLists.txt | 1 + resource/CMakeLists.txt | 10 +++++ resource/CMakeResources.cmake | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 resource/CMakeLists.txt create mode 100644 resource/CMakeResources.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 56b28832..4e72b780 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ endif() include_directories(source) add_subdirectory(source) +add_subdirectory(resource) add_subdirectory(source/frontends/common2) add_subdirectory(source/frontends/ncurses) add_subdirectory(source/frontends/qt) diff --git a/resource/CMakeLists.txt b/resource/CMakeLists.txt new file mode 100644 index 00000000..eccfb46a --- /dev/null +++ b/resource/CMakeLists.txt @@ -0,0 +1,10 @@ +include(CMakeResources.cmake) + +add_resources(rom_sources roms + Apple2e_Enhanced.rom + Apple2e.rom + ) + +add_library(apple2roms STATIC + ${rom_sources} + ) diff --git a/resource/CMakeResources.cmake b/resource/CMakeResources.cmake new file mode 100644 index 00000000..98d51775 --- /dev/null +++ b/resource/CMakeResources.cmake @@ -0,0 +1,75 @@ +function(add_resources out_var id) + set(result) + + string(CONCAT content_h + "#include \n" + "#include \n" + "\n" + "namespace ${id}\n" + "{\n" + " extern const std::map> resources\;\n" + "}\n") + + string(CONCAT content_cpp_top + "#include \"${id}_resources.h\"\n") + + string(CONCAT content_cpp_private + "extern \"C\"\n" + "{\n" + "\n") + + string(CONCAT content_cpp_public + "namespace ${id}\n" + "{\n" + "\n" + " const std::map> resources = {\n") + + foreach(in_f ${ARGN}) + set(out_f "${CMAKE_CURRENT_BINARY_DIR}/${in_f}.o") + add_custom_command(OUTPUT ${out_f} + COMMAND ld -r -b binary -o ${out_f} ${in_f} + DEPENDS ${in_f} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Adding resource: ${in_f} -> ${out_f}" + VERBATIM + ) + + string(REGEX REPLACE "[ ./]" "_" safe_in_f ${in_f}) + set(symbol "_binary_${safe_in_f}") + + string(APPEND content_cpp_private + " // ${in_f}\n" + " extern const char ${symbol}_start\;\n" + " extern const char ${symbol}_end\;\n" + "\n") + string(APPEND content_cpp_public + " {\"${in_f}\", {&${symbol}_start, &${symbol}_end}},\n") + + list(APPEND result ${out_f}) + endforeach() + + string(APPEND content_cpp_private + "}\n") + + string(APPEND content_cpp_public + " }\;\n" + "\n" + "}\n") + + set(out_h "${CMAKE_CURRENT_BINARY_DIR}/${id}_resources.h") + file(WRITE ${out_h} ${content_h}) + message("Generating header for '${id}': ${out_h}") + list(APPEND result ${out_h}) + + set(out_cpp "${CMAKE_CURRENT_BINARY_DIR}/${id}_resources.cpp") + file(WRITE ${out_cpp} + ${content_cpp_top} + "\n" + ${content_cpp_private} + "\n" + ${content_cpp_public}) + message("Generating cpp for '${id}': ${out_cpp}") + list(APPEND result ${out_cpp}) + + set(${out_var} "${result}" PARENT_SCOPE) +endfunction()