Skip to content

Commit

Permalink
Fix shaders not copying to final build on macOS for non Xcode builds …
Browse files Browse the repository at this point in the history
…(2) (#1908)

* Fix shaders not copying to final build on macOS for non Xcode builds

* Fix macOS build after adding minimum CMake 3.20 requirement

* Refactor "Fix macOS build after adding minimum CMake 3.20 requirement"
  • Loading branch information
smilediver authored May 21, 2024
1 parent cb142c6 commit ddeb14c
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 32 deletions.
53 changes: 48 additions & 5 deletions cmake/Modules/AXBuildHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,56 @@ function(source_group_single_file single_file)
source_group("${ide_file_group}" FILES ${single_file})
endfunction()

# Gets a list of all target libraries that a given target depends on. By target libraries
# it is meant that libraries themselves are CMake targets.
function(get_target_all_library_targets output_list target)
list(APPEND visited_targets ${target})
get_target_property(imported ${target} IMPORTED)
if (imported)
get_target_property(libs ${target} INTERFACE_LINK_LIBRARIES)
else()
get_target_property(libs ${target} LINK_LIBRARIES)
endif()
set(lib_targets "")
foreach(lib ${libs})
if(TARGET ${lib})
list(FIND visited_targets ${lib} visited)
if (${visited} EQUAL -1)
get_target_all_library_targets(link_lib_targets ${lib})
list(APPEND lib_targets ${lib} ${link_lib_targets})
endif()
endif()
endforeach()
set(visited_targets ${visited_targets} PARENT_SCOPE)
set(${output_list} ${lib_targets} PARENT_SCOPE)
endfunction()

# Gets a list of all compiled shaders that a given target depends on.
#
# A list of all compiled shaders that the executable or library target builds is kept in
# `AX_COMPILED_SHADERS` property on the target. This function uses this property to gather
# the list of all shaders from this target and all libraries that this target depends on.
function(get_target_compiled_shaders output_list target)
get_target_all_library_targets(libs ${target})
list(APPEND libs ${target})
set(shaders)
foreach(lib ${libs})
get_target_property(target_shaders ${lib} AX_COMPILED_SHADERS)
if(target_shaders)
list(APPEND shaders ${target_shaders})
endif()
endforeach()
set(${output_list} ${shaders} PARENT_SCOPE)
endfunction()

# setup a ax application
function(ax_setup_app_config app_name)
set(options CONSOLE)
set(oneValueArgs RUNTIME_OUTPUT_DIR)
cmake_parse_arguments(opt "${options}" "${oneValueArgs}" ""
"" ${ARGN} )
if (WINRT)
target_include_directories(${app_name}
target_include_directories(${app_name}
PRIVATE "proj.winrt"
)
endif()
Expand Down Expand Up @@ -451,7 +493,7 @@ function(ax_setup_app_config app_name)
message(STATUS "${app_shaders_count} shader sources found in ${app_shaders_dir}")
# compile app shader to ${CMAKE_BINARY_DIR}/runtime/axslc/custom/
ax_target_compile_shaders(${app_name} FILES ${app_shaders} CUSTOM)
source_group("Source Files/Source/shaders" FILES ${app_shaders})
source_group("Source Files/Source/shaders" FILES ${app_shaders})
endif()

if (IS_DIRECTORY ${GLSLCC_OUT_DIR})
Expand All @@ -466,8 +508,9 @@ function(ax_setup_app_config app_name)
if (CMAKE_GENERATOR MATCHES "Xcode")
set_target_properties(${app_name} PROPERTIES XCODE_EMBED_RESOURCES ${GLSLCC_OUT_DIR})
else()
ax_mark_multi_resources(compiled_shader_files RES_TO "Resources/axslc" FOLDERS ${GLSLCC_OUT_DIR})
target_sources(${app_name} PRIVATE ${compiled_shader_files})
get_target_compiled_shaders(shaders ${app_name})
ax_mark_resources(FILES ${shaders} BASEDIR ${GLSLCC_OUT_DIR} RESOURCEBASE "Resources/axslc")
target_sources(${app_name} PRIVATE ${shaders})
endif()
elseif(WINRT OR WASM)
set(app_all_shaders)
Expand All @@ -476,7 +519,7 @@ function(ax_setup_app_config app_name)
if (WINRT)
ax_target_embed_compiled_shaders(${app_name} ${rt_output} FILES ${app_all_shaders})
else()
# --preload-file
# --preload-file
# refer to: https://emscripten.org/docs/porting/files/packaging_files.html
target_link_options(${app_name} PRIVATE "--preload-file" ${GLSLCC_OUT_DIR}@axslc/)
endif()
Expand Down
6 changes: 6 additions & 0 deletions cmake/Modules/AXConfigDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ endif()
# apply axmol spec compile options
add_compile_options(${_ax_compile_options})

if(APPLE)
enable_language(C CXX OBJC OBJCXX)
else()
enable_language(C CXX)
endif()

# Try enable asm & nasm compiler support
set(can_use_assembler TRUE)
enable_language(ASM)
Expand Down
29 changes: 21 additions & 8 deletions cmake/Modules/AXGLSLCC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ endfunction()
# Use global variable to control shader file extension:
# - GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS: default is .frag;.fsh
# - GLSLCC_VERT_SOURCE_FILE_EXTENSIONS: default is .vert;.vsh
#
#
function (ax_target_compile_shaders target_name)
set(options RUNTIME CVAR CUSTOM)
set(multiValueArgs FILES)
cmake_parse_arguments(opt "${options}" "" "${multiValueArgs}" ${ARGN})

set(compiled_shaders)

foreach(SC_FILE ${opt_FILES})
get_filename_component(FILE_EXT ${SC_FILE} LAST_EXT)
get_filename_component(FILE_NAME ${SC_FILE} NAME_WE)
Expand All @@ -95,7 +97,7 @@ function (ax_target_compile_shaders target_name)

# silent when compile shader success
set(SC_FLAGS "--silent" "--err-format=msvc")

# shader lang
set(SC_PROFILE "")
if(AX_GLES_PROFILE)
Expand All @@ -115,7 +117,7 @@ function (ax_target_compile_shaders target_name)
set(OUT_LANG "GLSL")
set(SC_PROFILE "330")
list(APPEND SC_FLAGS "--lang=glsl" "--profile=${SC_PROFILE}")
elseif (AX_USE_METAL)
elseif (AX_USE_METAL)
set(OUT_LANG "MSL")
list(APPEND SC_FLAGS "--lang=msl")
set(SC_DEFINES "METAL")
Expand Down Expand Up @@ -150,7 +152,7 @@ function (ax_target_compile_shaders target_name)
if (AX_USE_METAL)
list(APPEND SC_FLAGS "--sgs" "--reflect")
endif()

# input
if (${FILE_EXT} IN_LIST GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS)
list(APPEND SC_FLAGS "--frag=${SC_FILE}")
Expand All @@ -170,11 +172,11 @@ function (ax_target_compile_shaders target_name)
if (NOT (IS_DIRECTORY ${OUT_DIR}))
file(MAKE_DIRECTORY ${OUT_DIR})
endif()

set(SC_OUTPUT "${OUT_DIR}/${FILE_NAME}_${SC_TYPE}")

set(SC_COMMENT "Compiling shader ${SC_FILE} for ${OUT_LANG}${SC_PROFILE} ...")

get_source_file_property(SOURCE_SC_OUTPUT1 ${SC_FILE} GLSLCC_OUTPUT1)

string(REPLACE ";" " " FULL_COMMAND_LINE "${GLSLCC_EXE};${SC_FLAGS} ...")
Expand All @@ -185,6 +187,7 @@ function (ax_target_compile_shaders target_name)
MAIN_DEPENDENCY ${SC_FILE} OUTPUT ${SC_OUTPUT} COMMAND ${GLSLCC_EXE} ${SC_FLAGS}
COMMENT "${SC_COMMENT}"
)
list(APPEND compiled_shaders ${SC_OUTPUT})
else() # dual outputs
set(SC_DEFINES1 "${SC_DEFINES},${SOURCE_SC_OUTPUT1}")

Expand All @@ -199,16 +202,26 @@ function (ax_target_compile_shaders target_name)
string(REPLACE ";" " " FULL_COMMAND_LINE1 "${GLSLCC_EXE};${SC_FLAGS1} ...")
set_source_files_properties(${SC_FILE} DIRECTORY ${CMAKE_BINARY_DIR} PROPERTIES GLSLCC_OUTPUT "${SC_OUTPUT};${SC_OUTPUT1}")
add_custom_command(
MAIN_DEPENDENCY ${SC_FILE}
MAIN_DEPENDENCY ${SC_FILE}
OUTPUT ${SC_OUTPUT} ${SC_OUTPUT1}
COMMAND ${GLSLCC_EXE} ${SC_FLAGS}
COMMAND ${GLSLCC_EXE} ${SC_FLAGS1}
COMMENT "${SC_COMMENT}"
)
list(APPEND compiled_shaders ${SC_OUTPUT} ${SC_OUTPUT1})
endif()

endforeach()

target_sources(${target_name} PRIVATE ${opt_FILES})

# Set `AX_COMPILED_SHADERS` property on the target to store the list of compiled
# shaders for this target. This is later used for setting up app's resource copying.
get_target_property(target_compiled_shaders ${target_name} AX_COMPILED_SHADERS)
if(NOT target_compiled_shaders)
set(target_compiled_shaders "")
endif()
list(APPEND target_compiled_shaders ${compiled_shaders})
set_property(TARGET ${target_name} PROPERTY AX_COMPILED_SHADERS ${target_compiled_shaders})
endfunction()

function(ax_target_embed_compiled_shaders target_name rc_output)
Expand Down
9 changes: 5 additions & 4 deletions templates/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
# ****************************************************************************/

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME Dummy)

Expand Down Expand Up @@ -176,6 +176,10 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

if (NOT _AX_USE_PREBUILT)
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})
endif()

# The optional thirdparties(not dependent by engine)
if (AX_WITH_YAML_CPP)
list(APPEND GAME_INC_DIRS "${_AX_ROOT}/3rdparty/yaml-cpp/include")
Expand All @@ -186,9 +190,6 @@ target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

# mark app resources, resource will be copy auto after mark
ax_setup_app_config(${APP_NAME})
if (NOT _AX_USE_PREBUILT)
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})
endif()

if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
Expand Down
2 changes: 1 addition & 1 deletion templates/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
# ****************************************************************************/

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME Dummy)

Expand Down
5 changes: 3 additions & 2 deletions tests/cpp-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME cpp-tests)

Expand Down Expand Up @@ -563,6 +563,8 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

if (AX_ENABLE_EXT_EFFEKSEER)
Expand All @@ -575,7 +577,6 @@ endif()

# mark app resources
ax_setup_app_config(${APP_NAME})
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
Expand Down
7 changes: 4 additions & 3 deletions tests/fairygui-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
# ****************************************************************************/

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME fairygui-tests)

Expand Down Expand Up @@ -165,11 +165,12 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

# mark app resources
ax_setup_app_config(${APP_NAME})
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
Expand Down Expand Up @@ -204,7 +205,7 @@ endif()

if((NOT IOS) AND (NOT WINRT))
message("CMake ${APP_NAME} target_precompile_headers")
target_precompile_headers(${APP_NAME} PRIVATE
target_precompile_headers(${APP_NAME} PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:axmol.h>"
)
endif()
Expand Down
7 changes: 4 additions & 3 deletions tests/live2d-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
# ****************************************************************************/

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME live2d-tests)

Expand Down Expand Up @@ -166,11 +166,12 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

# mark app resources
ax_setup_app_config(${APP_NAME})
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
Expand Down Expand Up @@ -205,7 +206,7 @@ endif()

if((NOT IOS) AND (NOT WINRT))
message("CMake ${APP_NAME} target_precompile_headers")
target_precompile_headers(${APP_NAME} PRIVATE
target_precompile_headers(${APP_NAME} PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:axmol.h>"
)
endif()
Expand Down
9 changes: 5 additions & 4 deletions tests/lua-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
# ****************************************************************************/

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME lua-tests)

Expand Down Expand Up @@ -183,6 +183,10 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

if(NOT _AX_USE_PREBUILT)
target_link_libraries(${APP_NAME} ${_AX_LUA_LIB})
endif()

# The optional thirdparties(not dependent by engine)
if (AX_WITH_YAML_CPP)
list(APPEND GAME_INC_DIRS "${_AX_ROOT}/3rdparty/yaml-cpp/include")
Expand All @@ -191,9 +195,6 @@ endif()
target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

# mark app resources, resource will be copy auto after mark
if(NOT _AX_USE_PREBUILT)
target_link_libraries(${APP_NAME} ${_AX_LUA_LIB})
endif()
ax_setup_app_config(${APP_NAME} CONSOLE)

if(APPLE)
Expand Down
5 changes: 3 additions & 2 deletions tests/unit-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

set(APP_NAME unit-tests)

Expand Down Expand Up @@ -161,6 +161,8 @@ else()
config_android_shared_libs("org.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
endif()

target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})

if (AX_ENABLE_EXT_EFFEKSEER)
Expand All @@ -173,7 +175,6 @@ endif()

# mark app resources
ax_setup_app_config(${APP_NAME} CONSOLE)
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})

if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
Expand Down

0 comments on commit ddeb14c

Please sign in to comment.