Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split more things into libraries #7554

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions CMake/functions/devilutionx_library.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include(functions/genex)
include(functions/set_relative_file_macro)
include(functions/object_libraries)

# This function is equivalent to `add_library` but applies DevilutionX-specific
# compilation flags to it.
Expand Down Expand Up @@ -68,12 +69,7 @@ function(add_devilutionx_library NAME)
set_relative_file_macro(${NAME})
endfunction()

# Same as add_devilutionx_library(${NAME} OBJECT) with an additional
# workaround for https://gitlab.kitware.com/cmake/cmake/-/issues/18090,
# allowing this object library to be "linked" to other object libraries.
# Same as add_devilutionx_library(${NAME} OBJECT).
function(add_devilutionx_object_library NAME)
add_devilutionx_library(${NAME} OBJECT ${ARGN})

# See https://gitlab.kitware.com/cmake/cmake/-/issues/18090
target_sources(${NAME} INTERFACE $<TARGET_OBJECTS:${NAME}>)
endfunction()
60 changes: 60 additions & 0 deletions CMake/functions/object_libraries.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# CMake has limited support for object libraries.
#
# The main limitation of CMake object libraries is the lack
# of transitive dependency support.
# The functions here provide a workaround for that.
#
# Use `target_link_dependencies` instead of `target_link_libraries`
#
# https://gitlab.kitware.com/cmake/cmake/-/issues/18090#note_861617
#
# At the end of the main `CMakeLists.txt`, call `resolve_target_link_dependencies()`.

# Behaves like target_link_libraries, but propagates OBJECT libraries' objects
# up to the first non-object library.
function(target_link_dependencies TARGET)
# The library we're linking may not have been defined yet,
# so we record it for now and resolve it later.
set_property(TARGET ${TARGET} APPEND PROPERTY LINKED_DEPENDENCIES ${ARGN})
set_property(GLOBAL APPEND PROPERTY TARGETS_WITH_LINKED_DEPENDENCIES "${TARGET}")
endfunction()

# Actually resolves the linked dependencies.
function(resolve_target_link_dependencies)
set(MODES PUBLIC PRIVATE INTERFACE)
get_property(TARGETS GLOBAL PROPERTY TARGETS_WITH_LINKED_DEPENDENCIES)
foreach(TARGET ${TARGETS})
get_target_property(TARGET_TYPE ${TARGET} TYPE)
get_target_property(LINKED_DEPENDENCIES ${TARGET} LINKED_DEPENDENCIES)
set(MODE PUBLIC)
foreach(ARG ${LINKED_DEPENDENCIES})
if(ARG IN_LIST MODES)
set(MODE ${ARG})
continue()
endif()
set(LIBRARY "${ARG}")
if(TARGET ${LIBRARY})
# When linking two OBJECT libraries together, record the input library objects in
# a custom target property "LINKED_OBJECTS" together with any other existing ones
# from the input library's LINKED_OBJECTS property.
# Accumulate LINKED_OBJECTS until reaching a non-object target, and add them as
# extra sources - this will de-duplicate the list and link it into the target.
get_target_property(LIBRARY_TYPE ${LIBRARY} TYPE)

if(LIBRARY_TYPE STREQUAL "OBJECT_LIBRARY")
if(TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
message(FATAL_ERROR "OBJECT to INTERFACE library linking is not supported.")
endif()
get_target_property(LIBRARY_LINKED_OBJECTS ${LIBRARY} LINKED_OBJECTS)
if(TARGET_TYPE STREQUAL "OBJECT_LIBRARY")
set_property(TARGET ${TARGET} APPEND PROPERTY LINKED_OBJECTS $<TARGET_OBJECTS:${LIBRARY}>)
elseif(NOT LIBRARY_LINKED_OBJECTS STREQUAL "LIBRARY_LINKED_OBJECTS-NOTFOUND")
target_sources(${TARGET} PRIVATE ${LIBRARY_LINKED_OBJECTS})
endif()
endif()
endif()
target_link_libraries(${TARGET} ${MODE} "${LIBRARY}")
endforeach()
endforeach()
set_property(GLOBAL PROPERTY TARGETS_WITH_LINKED_DEPENDENCIES)
endfunction()
5 changes: 5 additions & 0 deletions CMake/platforms/uwp_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ set(DEVILUTIONX_STATIC_ZLIB ON)

# not really necessary but a good measure for SDL related stuff
set(WINDOWS_STORE ON)

add_library(uwp_defs INTERFACE)
target_compile_definitions(uwp_defs INTERFACE __UWP__=1)
set(DEVILUTIONX_PLATFORM_ASSETS_LINK_LIBRARIES uwp_defs)
set(DEVILUTIONX_PLATFORM_LINK_LIBRARIES uwp_defs)
3 changes: 3 additions & 0 deletions CMake/platforms/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ set(ASAN OFF)
set(UBSAN OFF)
set(DIST ON)

set(DEVILUTIONX_PLATFORM_FILE_UTIL_LINK_LIBRARIES shlwapi)
set(DEVILUTIONX_PLATFORM_ASSETS_LINK_LIBRARIES find_steam_game)

list(APPEND DEVILUTIONX_PLATFORM_LINK_LIBRARIES
find_steam_game
shlwapi
Expand Down
1 change: 1 addition & 0 deletions CMake/platforms/windows9x.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(DEVILUTIONX_WINDOWS_NO_WCHAR ON)
# MinGW force-defines `_WIN32_WINNT=0xa00` if it isn't defined, so define it as 0.
add_definitions(-DWINVER=0x0500 -D_WIN32_WINDOWS=0x0500 -D_WIN32_WINNT=0)

set(DEVILUTIONX_PLATFORM_FILE_UTIL_LINK_LIBRARIES shlwapi)
list(APPEND DEVILUTIONX_PLATFORM_LINK_LIBRARIES
shlwapi
wsock32
Expand Down
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ endif()
if(ANDROID)
add_library(${BIN_TARGET} SHARED Source/main.cpp)
elseif(UWP_LIB)
add_custom_target(${BIN_TARGET}) # we only need libdevilutionx
set(BIN_TARGET libdevilutionx)
else()
add_executable(${BIN_TARGET}
WIN32
Expand All @@ -355,7 +355,7 @@ else()
endif()

if(NOT UWP_LIB)
target_link_libraries(${BIN_TARGET} PRIVATE libdevilutionx)
target_link_dependencies(${BIN_TARGET} PRIVATE libdevilutionx)
endif()

if(GPERF)
Expand Down Expand Up @@ -393,9 +393,9 @@ if(APPLE)
set(MACOSX_BUNDLE_REQUIRED_PLATFORM IPhoneOS)
set_target_properties(${BIN_TARGET} PROPERTIES XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
set(CMAKE_OSX_DEPLOYMENT_TARGET "9.0")
elseif(DARWIN_MAJOR_VERSION GREATER_EQUAL 16)
elseif(DARWIN_MAJOR_VERSION GREATER_EQUAL 17)
set(MACOSX_BUNDLE_REQUIRED_PLATFORM Carbon)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.12.0")
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13.0")
endif()
if(DARWIN_MAJOR_VERSION VERSION_LESS 9)
# Finder on OSX Tiger can only handle icns files with up to 128x128 icons.
Expand Down Expand Up @@ -506,10 +506,6 @@ if(NINTENDO_3DS)
add_cia_target(${BIN_TARGET} ${APP_RSF} ${APP_BANNER} ${APP_AUDIO})
endif()

if(UWP_LIB)
target_compile_definitions(libdevilutionx PRIVATE __UWP__=1)
endif()

if(NXDK)
target_link_libraries(${BIN_TARGET} PRIVATE "${NXDK_DIR}/lib/libnxdk_automount_d.lib")
target_link_options(${BIN_TARGET} PRIVATE "-include:_automount_d_drive")
Expand Down Expand Up @@ -669,3 +665,9 @@ if(CPACK AND (APPLE OR BUILD_ASSETS_MPQ OR SRC_DIST))
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
include(CPack)
endif()

resolve_target_link_dependencies()
if(UWP_LIB)
get_target_property(_linked_objects libdevilutionx LINKED_OBJECTS)
target_sources(libdevilutionx PRIVATE ${_linked_objects})
endif()
Loading
Loading