CMake: Simplify resource copy

This commit is contained in:
Stenzek
2026-02-01 01:09:11 +10:00
parent b330b3e7da
commit fa4d08491f
4 changed files with 78 additions and 58 deletions

View File

@@ -1,32 +0,0 @@
if(APPLE)
function(add_metal_sources target sources library_name metal_std)
set(air_files)
set(compile_flags -std=${metal_std} -ffast-math)
foreach(source IN LISTS sources)
get_filename_component(source_name ${source} NAME)
set(air_file ${CMAKE_CURRENT_BINARY_DIR}/${library_name}/${source_name}.air)
list(APPEND air_files ${air_file})
add_custom_command(
OUTPUT ${air_file}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${library_name}
COMMAND xcrun metal ${compile_flags} -o ${air_file} -c ${source}
DEPENDS ${source}
COMMENT "Compiling Metal shader ${source_name}"
)
endforeach()
set(metallib_file ${CMAKE_CURRENT_BINARY_DIR}/${library_name}.metallib)
add_custom_command(
OUTPUT ${metallib_file}
COMMAND xcrun metallib -o ${metallib_file} ${air_files}
DEPENDS ${air_files}
COMMENT "Linking Metal library ${library_name}.metallib"
)
target_sources(${target} PRIVATE ${metallib_file})
set_source_files_properties(${metallib_file} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
endfunction()
endif()

View File

@@ -299,3 +299,79 @@ function(check_cpp20_attribute ATTRIBUTE MINIMUM_VALUE)
message(FATAL_ERROR "${ATTRIBUTE} is not supported by your compiler, at least ${MINIMUM_VALUE} is required.") message(FATAL_ERROR "${ATTRIBUTE} is not supported by your compiler, at least ${MINIMUM_VALUE} is required.")
endif() endif()
endfunction() endfunction()
if(APPLE)
function(add_metal_sources target sources library_name metal_std)
set(air_files)
set(compile_flags -std=${metal_std} -ffast-math)
foreach(source IN LISTS sources)
get_filename_component(source_name ${source} NAME)
set(air_file ${CMAKE_CURRENT_BINARY_DIR}/${library_name}/${source_name}.air)
list(APPEND air_files ${air_file})
add_custom_command(
OUTPUT ${air_file}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${library_name}
COMMAND xcrun metal ${compile_flags} -o ${air_file} -c ${source}
DEPENDS ${source}
COMMENT "Compiling Metal shader ${source_name}"
)
endforeach()
set(metallib_file ${CMAKE_CURRENT_BINARY_DIR}/${library_name}.metallib)
add_custom_command(
OUTPUT ${metallib_file}
COMMAND xcrun metallib -o ${metallib_file} ${air_files}
DEPENDS ${air_files}
COMMENT "Linking Metal library ${library_name}.metallib"
)
target_sources(${target} PRIVATE ${metallib_file})
set_source_files_properties(${metallib_file} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
endfunction()
endif()
function(add_resources TARGET DEST_SUBDIR SOURCE_DIR)
# Recursively find all files in the source directory
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS "${SOURCE_DIR}/*")
foreach(SOURCE_FILE IN LISTS SOURCE_FILES)
# Skip directories
if(IS_DIRECTORY "${SOURCE_FILE}")
continue()
endif()
# Get the path relative to SOURCE_DIR
file(RELATIVE_PATH REL_PATH "${SOURCE_DIR}" "${SOURCE_FILE}")
# Get the subdirectory portion (if any)
get_filename_component(REL_SUBDIR "${REL_PATH}" DIRECTORY)
if(APPLE)
# On macOS, add as source with MACOSX_PACKAGE_LOCATION
target_sources(${TARGET} PRIVATE "${SOURCE_FILE}")
if(REL_SUBDIR)
set_source_files_properties("${SOURCE_FILE}" PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources/${REL_SUBDIR}")
else()
set_source_files_properties("${SOURCE_FILE}" PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources")
endif()
else()
# On other platforms, use custom command to copy files
if(REL_SUBDIR)
set(DEST_PATH "$<TARGET_FILE_DIR:${TARGET}>/${DEST_SUBDIR}/${REL_SUBDIR}")
else()
set(DEST_PATH "$<TARGET_FILE_DIR:${TARGET}>/${DEST_SUBDIR}")
endif()
add_custom_command(TARGET ${TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${DEST_PATH}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SOURCE_FILE}" "${DEST_PATH}/"
COMMENT "Copying ${REL_PATH} to ${DEST_SUBDIR}"
)
endif()
endforeach()
endfunction()

View File

@@ -197,37 +197,15 @@ if(CPU_ARCH_RISCV64)
message(STATUS "Building RISC-V-64 recompiler.") message(STATUS "Building RISC-V-64 recompiler.")
endif() endif()
# Copy the provided data directory to the output directory. Borrowed from PCSX2.
function(add_resources target path basedir)
get_filename_component(dir ${path} DIRECTORY)
file(RELATIVE_PATH subdir ${basedir} ${dir})
if(APPLE)
target_sources(${target} PRIVATE ${path})
set_source_files_properties(${path} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/${subdir})
else()
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E make_directory "$<TARGET_FILE_DIR:${target}>/resources/${subdir}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${path}" "$<TARGET_FILE_DIR:${target}>/resources/${subdir}")
endif()
source_group(Resources/${subdir} FILES ${path})
endfunction()
function(add_core_resources target) function(add_core_resources target)
add_util_resources(${target}) add_util_resources(${target})
add_resources(${target} "resources" ${CMAKE_SOURCE_DIR}/data/resources)
if(APPLE) if(APPLE)
# Copy discord-rpc into the bundle # Copy discord-rpc into the bundle
get_target_property(DISCORD_RPC_LIBRARY DiscordRPC::discord-rpc IMPORTED_LOCATION_RELEASE) get_target_property(DISCORD_RPC_LIBRARY DiscordRPC::discord-rpc IMPORTED_LOCATION_RELEASE)
target_sources(${target} PRIVATE "${DISCORD_RPC_LIBRARY}") target_sources(${target} PRIVATE "${DISCORD_RPC_LIBRARY}")
set_source_files_properties("${DISCORD_RPC_LIBRARY}" PROPERTIES MACOSX_PACKAGE_LOCATION Frameworks) set_source_files_properties("${DISCORD_RPC_LIBRARY}" PROPERTIES MACOSX_PACKAGE_LOCATION Frameworks)
endif() endif()
file(GLOB_RECURSE RESOURCE_FILES ${CMAKE_SOURCE_DIR}/data/resources/*)
foreach(path IN LISTS RESOURCE_FILES)
get_filename_component(file ${path} NAME)
if("${file}" MATCHES "^\\.") # Don't copy macOS garbage (mainly Finder's .DS_Store files) into application
continue()
endif()
add_resources(${target} ${path} ${CMAKE_SOURCE_DIR}/data/resources/)
endforeach()
endfunction() endfunction()

View File

@@ -257,8 +257,6 @@ if(WIN32)
target_link_libraries(util PRIVATE WinPixEventRuntime::WinPixEventRuntime) target_link_libraries(util PRIVATE WinPixEventRuntime::WinPixEventRuntime)
endif() endif()
elseif(APPLE) elseif(APPLE)
include(AddMetalSources)
set(MAC_SOURCES set(MAC_SOURCES
metal_device.h metal_device.h
metal_device.mm metal_device.mm