From 9fabba1eed995d82591ecf068f9def79e28b68ac Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sun, 28 Aug 2022 23:37:50 +0200 Subject: [PATCH] build: rework how dependencies are linked 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. --- CMakeLists.txt | 60 +++++++++++++-------- cmake/FindZArchive.cmake | 20 +++++++ cmake/Findimgui.cmake | 27 ++++++++++ cmake/FindwxWidgets.cmake | 49 +++++++++++++++++ cmake/Findzstd.cmake | 33 ++++++++++++ dependencies/discord-rpc/src/CMakeLists.txt | 3 +- src/CMakeLists.txt | 29 +++++----- src/Cafe/CMakeLists.txt | 51 ++++++++++++------ src/Cemu/CMakeLists.txt | 20 ++++++- src/Common/CMakeLists.txt | 22 ++++++-- src/Common/platform.h | 2 +- src/audio/CMakeLists.txt | 13 +++-- src/config/CMakeLists.txt | 17 +++++- src/gui/CMakeLists.txt | 33 +++++++++--- src/imgui/CMakeLists.txt | 15 ++++-- src/input/CMakeLists.txt | 21 ++++++-- src/resource/CMakeLists.txt | 4 +- src/util/CMakeLists.txt | 17 ++++-- 18 files changed, 351 insertions(+), 85 deletions(-) create mode 100644 cmake/FindZArchive.cmake create mode 100644 cmake/Findimgui.cmake create mode 100644 cmake/FindwxWidgets.cmake create mode 100644 cmake/Findzstd.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dcfb1cc..82599f2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) @@ -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") @@ -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$<$: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$<$: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") -add_subdirectory(src) \ No newline at end of file +find_package(ZArchive) +if (NOT ZArchive_FOUND) + add_subdirectory("dependencies/ZArchive") +endif() + +add_subdirectory(src) diff --git a/cmake/FindZArchive.cmake b/cmake/FindZArchive.cmake new file mode 100644 index 00000000..0c4b7714 --- /dev/null +++ b/cmake/FindZArchive.cmake @@ -0,0 +1,20 @@ +# SPDX-FileCopyrightText: 2022 Andrea Pappacoda +# 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 +) diff --git a/cmake/Findimgui.cmake b/cmake/Findimgui.cmake new file mode 100644 index 00000000..269297a1 --- /dev/null +++ b/cmake/Findimgui.cmake @@ -0,0 +1,27 @@ +# SPDX-FileCopyrightText: 2022 Andrea Pappacoda +# 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() diff --git a/cmake/FindwxWidgets.cmake b/cmake/FindwxWidgets.cmake new file mode 100644 index 00000000..46abca0e --- /dev/null +++ b/cmake/FindwxWidgets.cmake @@ -0,0 +1,49 @@ +# SPDX-FileCopyrightText: 2022 Andrea Pappacoda +# 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() diff --git a/cmake/Findzstd.cmake b/cmake/Findzstd.cmake new file mode 100644 index 00000000..a8a45a67 --- /dev/null +++ b/cmake/Findzstd.cmake @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2022 Andrea Pappacoda +# 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() diff --git a/dependencies/discord-rpc/src/CMakeLists.txt b/dependencies/discord-rpc/src/CMakeLists.txt index 2eeacf48..afd8902d 100644 --- a/dependencies/discord-rpc/src/CMakeLists.txt +++ b/dependencies/discord-rpc/src/CMakeLists.txt @@ -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) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fd2c4add..e903db4c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 + CemuAudio + CemuCafe + CemuCommon + CemuComponents + CemuConfig + CemuGui + 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 SDL2::SDL2 SDL2::SDL2main) # is SDL2main needed? +target_link_libraries(CemuBin PRIVATE imguiImpl OpenGL::GL) -target_link_libraries(CemuBin PUBLIC -CemuCommon CemuAudio CemuInput CemuComponents CemuCafe CemuConfig CemuGui imguiImpl) - -# 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() diff --git a/src/Cafe/CMakeLists.txt b/src/Cafe/CMakeLists.txt index ae0b3e60..a4a60d0d 100644 --- a/src/Cafe/CMakeLists.txt +++ b/src/Cafe/CMakeLists.txt @@ -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() @@ -14,20 +12,39 @@ set_property(TARGET CemuCafe PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$ #include #if BOOST_OS_WINDOWS diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 6a11033d..9217349d 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -34,9 +34,16 @@ endif() target_precompile_headers(CemuAudio PRIVATE ../Common/precompiled.h) -#target_link_libraries(CemuAudio CemuCommon CemuGui CemuPlatform) -target_include_directories(CemuAudio PRIVATE ../) +target_include_directories(CemuAudio PUBLIC "../") + +target_link_libraries(CemuAudio PRIVATE CemuCafe CemuConfig CemuGui CemuUtil) if(ENABLE_CUBEB) - target_link_libraries(CemuAudio cubeb) + # PUBLIC because cubeb.h/cubeb.h is included in CubebAPI.h + target_link_libraries(CemuAudio PUBLIC cubeb::cubeb) +endif() + +if (ENABLE_WXWIDGETS) + # PUBLIC because wx/wx.h is included in audioDebuggerWindow.h + target_link_libraries(CemuAudio PUBLIC wx::base wx::core) endif() diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt index 4710b5c4..4aafdc04 100644 --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -8,4 +8,19 @@ set_property(TARGET CemuConfig PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") @@ -17,5 +15,14 @@ target_sources(imguiImpl PRIVATE target_precompile_headers(imguiImpl PRIVATE ../Common/precompiled.h) -target_link_libraries(CemuCommon) -target_include_directories(imguiImpl PRIVATE ../) \ No newline at end of file +target_include_directories(imguiImpl PUBLIC "../") + +target_link_libraries(imguiImpl PRIVATE + CemuCafe + CemuCommon + CemuGui + CemuInput + CemuResource + CemuUtil + imgui::imgui +) diff --git a/src/input/CMakeLists.txt b/src/input/CMakeLists.txt index cbd86a32..de79cd8b 100644 --- a/src/input/CMakeLists.txt +++ b/src/input/CMakeLists.txt @@ -1,7 +1,5 @@ project(CemuInput) -include_directories(".") - add_library(CemuInput InputManager.cpp InputManager.h @@ -95,4 +93,21 @@ endif() target_precompile_headers(CemuInput PRIVATE ../Common/precompiled.h) -target_include_directories(CemuInput PRIVATE ../) +target_include_directories(CemuInput PUBLIC "../") + +target_link_libraries(CemuInput PRIVATE + CemuCafe + CemuCommon + CemuConfig + CemuGui + CemuUtil + Boost::headers + Boost::program_options + glm::glm + pugixml::pugixml + SDL2::SDL2 +) + +if (ENABLE_WXWIDGETS) + target_link_libraries(CemuInput PRIVATE wx::base wx::core) +endif() diff --git a/src/resource/CMakeLists.txt b/src/resource/CMakeLists.txt index 85084bca..ec445061 100644 --- a/src/resource/CMakeLists.txt +++ b/src/resource/CMakeLists.txt @@ -16,4 +16,6 @@ target_sources(CemuResource PRIVATE CafeDefaultFont.cpp ) -target_include_directories(CemuResource PRIVATE ../) +target_include_directories(CemuResource PUBLIC "../") + +target_link_libraries(CemuResource PRIVATE CemuComponents) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index d31541ff..69887fa7 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,7 +1,5 @@ project(CemuUtil) -include_directories(".") - file(GLOB_RECURSE CPP_FILES *.cpp) file(GLOB_RECURSE H_FILES *.h) @@ -11,5 +9,16 @@ set_property(TARGET CemuUtil PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$