Commit 8a104813 authored by Florian Oetke's avatar Florian Oetke
Browse files

added code path to embed_assets.cmake that uses resource scripts instead of...

added code path to embed_assets.cmake that uses resource scripts instead of assembler + inc_bin on MSVC
parent 57d23603
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
if(MSVC)
find_program(MIRRAGE_AUX_ASSEMBLER as)
if(MIRRAGE_AUX_ASSEMBLER)
message(STATUS "Using MIRRAGE_AUX_ASSEMBLER for asset embedding: ${MIRRAGE_AUX_ASSEMBLER}")
else()
message(FATAL_ERROR "Can't find a binary for MIRRAGE_AUX_ASSEMBLER. The asset embedding mechanism of mirrage currently doesn't work natively with MSVC. To support MSVC despite this, MinGWs assembler is required to create embedded asset OBJs. However, this binary wasn't found using find_program().")
endif()
endif()
#optional: generated files to depend on
macro(mirrage_embed_asset target src_files)
string (REPLACE ";" "$<SEMICOLON>" src_files_str "${src_files}")
set(ID "mirrage_embedded_asset_${target}")
if(MSVC)
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.rc")
set(EMBED_MODE "MSVC")
elseif(APPLE)
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
set(EMBED_MODE "APPLE")
else()
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
set(EMBED_MODE "ASM")
endif()
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s"
COMMAND ${CMAKE_COMMAND} -DMIRRAGE_ROOT_DIR=${MIRRAGE_ROOT_DIR} -DID=${ID} -DSRC_FILES=${src_files_str} -DDST_DIR=${CMAKE_CURRENT_BINARY_DIR} -P ${MIRRAGE_ROOT_DIR}/embed_recursive_into_asm.cmake
add_custom_command(OUTPUT ${EMBED_SRC_FILE}
COMMAND ${CMAKE_COMMAND} -DMIRRAGE_ROOT_DIR=${MIRRAGE_ROOT_DIR} -DEMBED_MODE=${EMBED_MODE} -DID=${ID} -DSRC_FILES=${src_files_str} -DDST_DIR=${CMAKE_CURRENT_BINARY_DIR} -P ${MIRRAGE_ROOT_DIR}/embed_recursive_into_asm.cmake
DEPENDS ${ARGN}
VERBATIM
)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp" CONTENT
"#include <mirrage/asset/embedded_asset.hpp>
set(PLATFORM_EMBED_SRC "")
if(MSVC)
string(TOUPPER "${ID}" RES_ID)
set(PLATFORM_EMBED_SRC
"#include <windows.h>
static auto create_asset() -> mirrage::asset::Embedded_asset {
auto handle = GetModuleHandle(NULL);
auto res = FindResource(handle, \"${RES_ID}\", RT_RCDATA);
return mirrage::asset::Embedded_asset(\"${target}\",
gsl::span<const gsl::byte>{reinterpret_cast<const gsl::byte*>(LockResource(LoadResource(handle, res))),
static_cast<int>(SizeofResource(handle, res))});
}
static auto mirrage_asset_reg = create_asset();
")
extern \"C\" const char ${ID}[];
extern \"C\" int ${ID}_size;
else()
set(PLATFORM_EMBED_SRC
"extern \"C\" const char ${ID}[];
extern \"C\" const int ${ID}_size;
static auto mirrage_asset_reg = mirrage::asset::Embedded_asset(\"${target}\",
gsl::span<const gsl::byte>{reinterpret_cast<const gsl::byte*>(&*${ID}), ${ID}_size});
")
endif()
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp" CONTENT
"#include <mirrage/asset/embedded_asset.hpp>
${PLATFORM_EMBED_SRC}
void ref_embedded_assets_${target}() {
volatile mirrage::asset::Embedded_asset* x = &mirrage_asset_reg;
......@@ -38,17 +64,9 @@ void ref_embedded_assets_${target}() {
}
")
if(MSVC)
add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj"
COMMAND ${MIRRAGE_AUX_ASSEMBLER} "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" -o "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj"
MAIN_DEPENDENCY "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s"
COMMENT "Running MIRRAGE_AUX_ASSEMBLER (${MIRRAGE_AUX_ASSEMBLER}) for embedded assets of target ${target}."
VERBATIM)
target_link_libraries(${target} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj")
endif()
target_sources(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
target_sources(${target} PRIVATE ${EMBED_SRC_FILE})
target_sources(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp")
add_custom_target(mirrage_embedded_assets_${target} DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
add_custom_target(mirrage_embedded_assets_${target} DEPENDS ${EMBED_SRC_FILE})
add_dependencies(${target} mirrage_embedded_assets_${target})
endmacro()
......@@ -31,6 +31,16 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E tar "cfv" "${DST_DIR}/embedded_asset
OUTPUT_QUIET
)
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.s.in "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" OUTPUT_QUIET)
if("${EMBED_MODE} " STREQUAL "MSVC ")
string(TOUPPER "${ID}" RES_ID)
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.rc.in "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.rc")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.rc" OUTPUT_QUIET)
else()
if("${EMBED_MODE} " STREQUAL "APPLE ")
set(SECTION ".const_data")
else()
set(SECTION ".section .rodata")
endif()
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.s.in "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" OUTPUT_QUIET)
endif()
${RES_ID} RCDATA "${DST_DIR}/embedded_assets.zip"
.global ${ID}
.global ${ID}_size
.section .rodata
${SECTION}
${ID}:
.incbin "${DST_DIR}/embedded_assets.zip"
1:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment