Skip to content

Commit

Permalink
build: rework how dependencies are linked
Browse files Browse the repository at this point in the history
This is a big rework of how dependencies are handled internally. All the
calls to CMake functions changing directory-wide properties that were
previously used to link to external and internal dependencies have been
replaced with their "target_" counterparts. This makes it possible to
express more clearly the relationships between different targets and
should also make the build script more robust in general.

In doing this, I've also made it possible to link to system libraries if
so desired; the versioned calls to find_package() will make sure that
the found dependencies are compatible with the ones required by Cemu,
and will abort the build otherwise.

Cemu's internal targets are deeply interconnected, making it hard to
fully benefit from a target-based approach. Nonetheless, I did my best
to mark PUBLIC dependencies as such, for example when an internal target
like CemuGui exposes a dependency as part of its API, i.e. it includes
a third party header in one of its public headers.

This will significantly help with improving the build experience on
Linux, thus helping a bit with the resolution of #1.
  • Loading branch information
Tachi107 committed Aug 28, 2022
1 parent a5a9077 commit 9fabba1
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 88 deletions.
60 changes: 37 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ endif()


project(Cemu VERSION 0.1)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down Expand Up @@ -64,6 +67,8 @@ option(ENABLE_CUBEB "Enabled cubeb backend" ON)

option(ENABLE_WXWIDGETS "Build with wxWidgets UI (Currently required)" ON)

set(THREADS_PREFER_PTHREAD_FLAG true)
find_package(Threads REQUIRED)
find_package(SDL2 REQUIRED)
find_package(CURL REQUIRED)
find_package(pugixml REQUIRED)
Expand All @@ -73,7 +78,20 @@ find_package(Boost COMPONENTS program_options filesystem nowide REQUIRED)
find_package(libzip REQUIRED)
find_package(glslang REQUIRED)
find_package(ZLIB REQUIRED)
find_package(zstd REQUIRED)
find_package(zstd MODULE REQUIRED) # MODULE so that zstd::zstd is available
find_package(OpenSSL COMPONENTS Crypto SSL REQUIRED)
find_package(glm REQUIRED)
find_package(fmt 7.0.0 REQUIRED)
find_package(PNG REQUIRED)

# glslang versions older than 11.11.0 define targets without a namespace
if (NOT TARGET glslang::SPIRV AND TARGET SPIRV)
add_library(glslang::SPIRV ALIAS SPIRV)
endif()

if (UNIX)
find_package(X11 REQUIRED)
endif()

if (ENABLE_VULKAN)
include_directories("dependencies/Vulkan-Headers/include")
Expand All @@ -93,29 +111,25 @@ if (ENABLE_WXWIDGETS)
find_package(wxWidgets 3.2 REQUIRED COMPONENTS base core gl propgrid xrc)
endif()

find_package(OpenSSL REQUIRED)
find_package(X11)

# find a better way to handle this
link_libraries(${Boost_LIBRARIES})
link_libraries(${X11_LIBRARIES})
link_libraries(SDL2::SDL2 SDL2::SDL2main SDL2::SDL2-static)
if (ENABLE_WXWIDGETS)
link_libraries(wx::core wx::base)
endif()

if (ENABLE_CUBEB)
option(BUILD_TESTS "" OFF)
option(BUILD_TOOLS "" OFF)
option(BUNDLE_SPEEX "" OFF)
set(USE_WINMM OFF CACHE BOOL "")
add_subdirectory(dependencies/cubeb)
set_property(TARGET cubeb PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
link_libraries(cubeb)
add_compile_definitions(HAS_CUBEB=1)
find_package(cubeb)
if (NOT cubeb_FOUND)
option(BUILD_TESTS "" OFF)
option(BUILD_TOOLS "" OFF)
option(BUNDLE_SPEEX "" OFF)
set(USE_WINMM OFF CACHE BOOL "")
add_subdirectory("dependencies/cubeb")
set_property(TARGET cubeb PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_library(cubeb::cubeb ALIAS cubeb)
endif()
add_compile_definitions("HAS_CUBEB=1")
endif()

add_subdirectory(dependencies/ih264d)
add_subdirectory(dependencies/ZArchive)
add_subdirectory("dependencies/ih264d")

find_package(ZArchive)
if (NOT ZArchive_FOUND)
add_subdirectory("dependencies/ZArchive")
endif()

add_subdirectory(src)
add_subdirectory(src)
20 changes: 20 additions & 0 deletions cmake/FindZArchive.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: 2022 Andrea Pappacoda <andrea@pappacoda.it>
# SPDX-License-Identifier: ISC

find_package(PkgConfig)

if (PKG_CONFIG_FOUND)
pkg_search_module(zarchive IMPORTED_TARGET GLOBAL zarchive)
if (zarchive_FOUND)
add_library(ZArchive::zarchive ALIAS PkgConfig::zarchive)
endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZArchive
REQUIRED_VARS
zarchive_LINK_LIBRARIES
zarchive_FOUND
VERSION_VAR
zarchive_VERSION
)
27 changes: 27 additions & 0 deletions cmake/Findimgui.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2022 Andrea Pappacoda <andrea@pappacoda.it>
# SPDX-License-Identifier: ISC

include(FindPackageHandleStandardArgs)

find_package(imgui CONFIG)
if (imgui_FOUND)
# Use upstream imguiConfig.cmake if possible
find_package_handle_standard_args(imgui CONFIG_MODE)
else()
# Fallback to pkg-config otherwise
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_search_module(imgui IMPORTED_TARGET GLOBAL imgui)
if (imgui_FOUND)
add_library(imgui::imgui ALIAS PkgConfig::imgui)
endif()
endif()

find_package_handle_standard_args(imgui
REQUIRED_VARS
imgui_LINK_LIBRARIES
imgui_FOUND
VERSION_VAR
imgui_VERSION
)
endif()
49 changes: 49 additions & 0 deletions cmake/FindwxWidgets.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-FileCopyrightText: 2022 Andrea Pappacoda <andrea@pappacoda.it>
# SPDX-License-Identifier: ISC

include(FindPackageHandleStandardArgs)
find_package(wxWidgets CONFIG COMPONENTS ${wxWidgets_FIND_COMPONENTS})

if (wxWidgets_FOUND)
# Use upstream wxWidgetsConfig.cmake if possible
find_package_handle_standard_args(wxWidgets CONFIG_MODE)
else()
# Fall back to CMake's FindwxWidgets
# Temporarily unset CMAKE_MODULE_PATH to avoid calling the current find
# module recursively
set(_tmp_module_path "${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "")

find_package(wxWidgets MODULE QUIET COMPONENTS ${wxWidgets_FIND_COMPONENTS})

set(CMAKE_MODULE_PATH "${_tmp_module_path}")
unset(_tmp_module_path)

if (wxWidgets_FOUND)
add_library(wx::base IMPORTED INTERFACE)
target_include_directories(wx::base INTERFACE ${wxWidgets_INCLUDE_DIRS})
target_link_libraries(wx::base INTERFACE ${wxWidgets_LIBRARIES})
target_link_directories(wx::base INTERFACE ${wxWidgets_LIBRARY_DIRS})
target_compile_definitions(wx::base INTERFACE ${wxWidgets_DEFINITIONS})
target_compile_options(wx::base INTERFACE ${wxWidgets_CXX_FLAGS})

# FindwxWidgets sets everything into a single set of variables, so it is
# impossible to tell what libraries are required for what component.
# To be compatible with wxWidgetsConfig, we create an alias for each
# component so that the user can still use target_link_libraries(wx::gl)
foreach(component ${wxWidgets_FIND_COMPONENTS})
if (NOT component STREQUAL "base")
# don't alias base to itself
add_library(wx::${component} ALIAS wx::base)
endif()
endforeach()
endif()

find_package_handle_standard_args(wxWidgets
REQUIRED_VARS
wxWidgets_LIBRARIES
wxWidgets_FOUND
VERSION_VAR
wxWidgets_VERSION_STRING
)
endif()
33 changes: 33 additions & 0 deletions cmake/Findzstd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2022 Andrea Pappacoda <andrea@pappacoda.it>
# SPDX-License-Identifier: ISC

include(FindPackageHandleStandardArgs)

find_package(zstd CONFIG)
if (zstd_FOUND)
# Use upstream zstdConfig.cmake if possible
if (NOT TARGET zstd::zstd)
if (TARGET zstd::libzstd_static)
add_library(zstd::zstd ALIAS zstd::libzstd_static)
elseif (TARGET zstd::libzstd_shared)
add_library(zstd::zstd ALIAS zstd::libzstd_shared)
endif()
endif()
find_package_handle_standard_args(zstd CONFIG_MODE)
else()
# Fallback to pkg-config otherwise
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_search_module(libzstd IMPORTED_TARGET GLOBAL libzstd)
if (libzstd_FOUND)
add_library(zstd::zstd ALIAS PkgConfig::libzstd)
endif()
endif()

find_package_handle_standard_args(zstd
REQUIRED_VARS
libzstd_LINK_LIBRARIES
libzstd_FOUND
VERSION_VAR libzstd_VERSION
)
endif()
3 changes: 1 addition & 2 deletions dependencies/discord-rpc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ if(UNIX)
endif (APPLE)
endif(UNIX)

target_link_libraries(discord-rpc PRIVATE rapidjson)
#target_include_directories(discord-rpc PRIVATE ${RAPIDJSON}/include)
target_include_directories(discord-rpc PRIVATE ${RAPIDJSON_INCLUDE_DIRS})

if (NOT ${ENABLE_IO_THREAD})
target_compile_definitions(discord-rpc PUBLIC -DDISCORD_DISABLE_IO_THREAD)
Expand Down
29 changes: 16 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,21 @@ set_target_properties(CemuBin PROPERTIES
OUTPUT_NAME "Cemu"
)

target_link_libraries(CemuBin PRIVATE CemuCommon CemuComponents CemuCafe CemuConfig CemuGui CemuAudio CemuInput CemuUtil)
target_link_libraries(CemuBin PRIVATE CemuAsm)
target_link_libraries(CemuBin PRIVATE OpenSSL::SSL)
target_link_libraries(CemuBin PRIVATE ZLIB::ZLIB)
target_link_libraries(CemuBin PRIVATE ${wxWidgets_LIBRARIES})
target_link_libraries(CemuBin PRIVATE CURL::libcurl)
target_link_libraries(CemuBin PRIVATE imgui::imgui)
target_link_libraries(CemuBin PRIVATE pugixml pugixml::static pugixml::pugixml)
target_link_libraries(CemuBin PRIVATE
CemuAudio
CemuCafe
CemuCommon
CemuComponents
CemuConfig
CemuGui
CemuInput
CemuUtil
)

target_link_libraries(CemuBin PUBLIC
CemuCommon CemuAudio CemuInput CemuComponents CemuCafe CemuConfig CemuGui imguiImpl)
target_link_libraries(CemuBin PRIVATE CemuAsm)
target_link_libraries(CemuBin PRIVATE SDL2::SDL2 SDL2::SDL2main) # is SDL2main needed?
target_link_libraries(CemuBin PRIVATE imguiImpl OpenGL::GL)

# needed because of some cyclic dependencies. fix this
target_link_libraries(CemuBin PUBLIC
CemuCommon CemuInput CemuComponents CemuCafe CemuResource CemuGui CemuAsm)
if (ENABLE_WXWIDGETS)
target_link_libraries(CemuBin PRIVATE wx::base wx::core)
endif()
55 changes: 36 additions & 19 deletions src/Cafe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
project(CemuCafe)

include_directories(".")

if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR (CMAKE_C_COMPILER_ID STREQUAL "Clang"))
add_compile_options(-mssse3 -mavx2)
endif()
Expand All @@ -14,20 +12,39 @@ set_property(TARGET CemuCafe PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CON

target_precompile_headers(CemuCafe PRIVATE ../Common/precompiled.h)

target_include_directories(CemuCafe PRIVATE ../)

#target_link_libraries(CemuCafe ZArchivexx)
#target_link_libraries(CemuCafe CemuCommon CemuCore CemuConfig CemuUtil CemuResource)
#target_link_libraries(CemuCafe OpenSSL::SSL)
#target_link_libraries(CemuCafe ZLIB::ZLIB)
#target_link_libraries(CemuCafe imgui::imgui)
#target_link_libraries(CemuCafe imguiImpl)
#target_link_libraries(CemuCafe pugixml pugixml::static pugixml::pugixml)
#target_link_libraries(CemuCafe libzip::zip)
target_link_libraries(CemuCafe glslang SPIRV)
target_link_libraries(CemuCafe ih264d zarchive)
#target_link_libraries(CemuCafe zstd::libzstd_static)

IF(WIN32)
target_link_libraries(CemuCafe iphlpapi)
ENDIF()
target_include_directories(CemuCafe PUBLIC "../")

target_link_libraries(CemuCafe PRIVATE
CemuAsm
CemuAudio
CemuCommon
CemuComponents
CemuConfig
CemuGui
CemuInput
CemuResource
CemuUtil
imguiImpl
Boost::headers
Boost::nowide
CURL::libcurl
fmt::fmt
glslang::SPIRV
ih264d
imgui::imgui
OpenSSL::Crypto
OpenSSL::SSL
PNG::PNG
pugixml::pugixml
ZArchive::zarchive
ZLIB::ZLIB
zstd::zstd
)

if (ENABLE_WXWIDGETS)
target_link_libraries(CemuCafe PRIVATE wx::base wx::core)
endif()

if(WIN32)
target_link_libraries(CemuCafe PRIVATE iphlpapi)
endif()
20 changes: 18 additions & 2 deletions src/Cemu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ set_property(TARGET CemuComponents PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$

target_precompile_headers(CemuComponents PRIVATE ../Common/precompiled.h)

target_include_directories(CemuComponents PRIVATE ../)
target_include_directories(CemuComponents PUBLIC "../")

target_link_libraries(CemuComponents PRIVATE
CemuCafe
CemuCommon
CemuConfig
CemuGui
CemuUtil
Boost::headers
CURL::libcurl
OpenSSL::Crypto
OpenSSL::SSL
pugixml::pugixml
ZLIB::ZLIB
)

# PUBLIC because fmt/format.h is included in ExpressionParser/ExpressionParser.h
target_link_libraries(CemuComponents PUBLIC fmt::fmt)

if(ENABLE_DISCORD_RPC)
target_link_libraries(CemuComponents PRIVATE discord-rpc)
endif()
target_link_libraries(CemuComponents PRIVATE CemuUtil)
22 changes: 19 additions & 3 deletions src/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
project(CemuCommon)

#include_directories(".")

file(GLOB CPP_FILES *.cpp)
file(GLOB H_FILES *.h)
add_library(CemuCommon ${CPP_FILES} ${H_FILES})
Expand Down Expand Up @@ -29,4 +27,22 @@ target_sources(CemuCommon
)

target_precompile_headers(CemuCommon PUBLIC precompiled.h)
target_include_directories(CemuCommon PRIVATE ../)
target_include_directories(CemuCommon PUBLIC "../")

target_link_libraries(CemuCommon PRIVATE
CemuCafe
CemuConfig
CemuComponents
Boost::nowide
Boost::filesystem
glm::glm
)

if (UNIX)
target_link_libraries(CemuCommon PRIVATE X11::X11 X11::Xrender X11::Xutil)
endif()

# PUBLIC because:
# - boost/predef/os.h is included in platform.h
# - fmt/core.h is included in precompiled.h
target_link_libraries(CemuCommon PUBLIC Boost::headers fmt::fmt)

0 comments on commit 9fabba1

Please sign in to comment.