From fbe2d468732f00c4e1208af543e06beedd69262f Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 12 Aug 2024 18:49:28 +1000 Subject: [PATCH] CMake: Add install target Do **not** use this with /usr or /usr/local. It creates subdirectories for resources/translations in the install prefix. It's intended for creating a relocatable, self-contained bundle, which can be packaged. --- CMakeModules/DuckStationBuildOptions.cmake | 1 + CMakeModules/DuckStationBuildSummary.cmake | 6 ++++++ CMakeModules/DuckStationUtils.cmake | 6 ++++++ src/core/CMakeLists.txt | 4 ++++ src/duckstation-qt/CMakeLists.txt | 8 ++++++++ src/util/CMakeLists.txt | 8 ++++++++ 6 files changed, 33 insertions(+) diff --git a/CMakeModules/DuckStationBuildOptions.cmake b/CMakeModules/DuckStationBuildOptions.cmake index 0d3d6d41d..6cdf94e04 100644 --- a/CMakeModules/DuckStationBuildOptions.cmake +++ b/CMakeModules/DuckStationBuildOptions.cmake @@ -9,6 +9,7 @@ option(BUILD_TESTS "Build unit tests" OFF) if(LINUX OR BSD) option(ENABLE_X11 "Support X11 window system" ON) option(ENABLE_WAYLAND "Support Wayland window system" ON) + option(ALLOW_INSTALL "Allow installation to CMAKE_INSTALL_PREFIX" OFF) endif() if(APPLE) option(SKIP_POSTPROCESS_BUNDLE "Disable bundle post-processing, including Qt additions" OFF) diff --git a/CMakeModules/DuckStationBuildSummary.cmake b/CMakeModules/DuckStationBuildSummary.cmake index f9c1571e9..b4e570083 100644 --- a/CMakeModules/DuckStationBuildSummary.cmake +++ b/CMakeModules/DuckStationBuildSummary.cmake @@ -24,6 +24,12 @@ if(BUILD_TESTS) message(STATUS "Building unit tests.") endif() +if(ALLOW_INSTALL) + message(WARNING "Install target is enabled. This will install all DuckStation files into: + ${CMAKE_INSTALL_PREFIX} +It does **not** use the LSB subdirectories of bin, share, etc, so you should disable this option if it is set to /usr or /usr/local.") +endif() + if(NOT IS_SUPPORTED_COMPILER) message(WARNING " *************** UNSUPPORTED CONFIGURATION *************** diff --git a/CMakeModules/DuckStationUtils.cmake b/CMakeModules/DuckStationUtils.cmake index af9be60f7..8cb4c85f2 100644 --- a/CMakeModules/DuckStationUtils.cmake +++ b/CMakeModules/DuckStationUtils.cmake @@ -221,3 +221,9 @@ function(get_scm_version) set(SCM_VERSION ${LOCAL_SCM_VERSION} PARENT_SCOPE) endif() endfunction() + +function(install_imported_dep_library name) + get_target_property(SONAME "${name}" IMPORTED_SONAME_RELEASE) + get_target_property(LOCATION "${name}" IMPORTED_LOCATION_RELEASE) + install(FILES "${LOCATION}" RENAME "${SONAME}" DESTINATION "${CMAKE_INSTALL_PREFIX}") +endfunction() diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 21b501e2a..2297591e8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -214,4 +214,8 @@ function(add_core_resources target) endif() add_resources(${target} ${path} ${CMAKE_SOURCE_DIR}/data/resources/) endforeach() + if(ALLOW_INSTALL) + install_imported_dep_library(cpuinfo::cpuinfo) + install(DIRECTORY "$/resources" DESTINATION "${CMAKE_INSTALL_PREFIX}") + endif() endfunction() diff --git a/src/duckstation-qt/CMakeLists.txt b/src/duckstation-qt/CMakeLists.txt index f50e31514..c504865b3 100644 --- a/src/duckstation-qt/CMakeLists.txt +++ b/src/duckstation-qt/CMakeLists.txt @@ -249,6 +249,11 @@ elseif(APPLE) set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/DuckStation.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources) endif() +if(ALLOW_INSTALL) + # Install main binary. + install(TARGETS duckstation-qt RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}") +endif() + # Compile qrc to a binary file. if(NOT APPLE) set(RCC_FILE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources/duckstation-qt.rcc") @@ -271,6 +276,9 @@ if(NOT APPLE) foreach (QM_FILE IN LISTS QM_FILES) get_filename_component(QM_FILE_NAME ${QM_FILE} NAME) add_custom_command(TARGET duckstation-qt POST_BUILD COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${QM_FILE}" "${QM_OUTPUT_DIR}/${QM_FILE_NAME}") + if(ALLOW_INSTALL) + install(FILES "${QM_FILE}" DESTINATION "${CMAKE_INSTALL_PREFIX}/translations") + endif() endforeach() else() foreach (QM_FILE IN LISTS QM_FILES) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 3ceffc1d7..65c771d61 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -306,5 +306,13 @@ function(add_util_resources target) target_sources(${target} PRIVATE ${version_lib}) set_source_files_properties(${target} PRIVATE ${version_lib} PROPERTIES MACOSX_PACKAGE_LOCATION Frameworks) endforeach() + elseif(ALLOW_INSTALL) + # Ensure we look for dependency libraries in the installation directory. + set_target_properties(${target} PROPERTIES INSTALL_RPATH "$ORIGIN") + + # Copy dependency libraries to installation directory. + install_imported_dep_library(Shaderc::shaderc_shared) + install_imported_dep_library(spirv-cross-c-shared) + install_imported_dep_library(SoundTouch::SoundTouchDLL) endif() endfunction()