From 0e5fdaa139e6c40b28d176da27acafe9ebe3e7b9 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:03:18 -0400 Subject: [PATCH] refactor(cmake): split CMakeLists into modules --- .github/workflows/CI.yml | 2 +- CMakeLists.txt | 117 +++--------------- cmake/include_base.cmake | 14 +++ cmake/include_build_version.cmake | 58 +++++++++ cmake/include_options.cmake | 20 +++ ...nclude_special_package_configuration.cmake | 23 ++++ packaging/linux/{aur => Arch}/PKGBUILD | 0 .../linux/flatpak/dev.lizardbyte.sunshine.yml | 2 +- 8 files changed, 131 insertions(+), 105 deletions(-) create mode 100644 cmake/include_base.cmake create mode 100644 cmake/include_build_version.cmake create mode 100644 cmake/include_options.cmake create mode 100644 cmake/include_special_package_configuration.cmake rename packaging/linux/{aur => Arch}/PKGBUILD (100%) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 2b76e22d64c..5cffc089bc3 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -272,7 +272,7 @@ jobs: matrix: include: # package these differently - type: appimage - EXTRA_ARGS: '-DSUNSHINE_CONFIGURE_APPIMAGE=ON' + EXTRA_ARGS: '-DSUNSHINE_BUILD_APPIMAGE=ON' dist: 20.04 steps: diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fa910ef5ed..c6129dc9569 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,103 +1,22 @@ cmake_minimum_required(VERSION 3.18) # `CMAKE_CUDA_ARCHITECTURES` requires 3.18 -# todo - set version to 0.0.0 once confident in automated versioning -project(Sunshine VERSION 0.20.0 - DESCRIPTION "Sunshine is a self-hosted game stream host for Moonlight." - HOMEPAGE_URL "https://app.lizardbyte.dev") - -set(PROJECT_LONG_DESCRIPTION "Offering low latency, cloud gaming server capabilities with support for AMD, Intel, \ -and Nvidia GPUs for hardware encoding. Software encoding is also available. You can connect to Sunshine from any \ -Moonlight client on a variety of devices. A web UI is provided to allow configuration, and client pairing, from \ -your favorite web browser. Pair from the local server or any mobile device.") - -# Check if env vars are defined before attempting to access them, variables will be defined even if blank -if((DEFINED ENV{BRANCH}) AND (DEFINED ENV{BUILD_VERSION}) AND (DEFINED ENV{COMMIT})) # cmake-lint: disable=W0106 - if(($ENV{BRANCH} STREQUAL "master") AND (NOT $ENV{BUILD_VERSION} STREQUAL "")) - # If BRANCH is "master" and BUILD_VERSION is not empty, then we are building a master branch - MESSAGE("Got from CI master branch and version $ENV{BUILD_VERSION}") - set(PROJECT_VERSION $ENV{BUILD_VERSION}) - elseif((DEFINED ENV{BRANCH}) AND (DEFINED ENV{COMMIT})) - # If BRANCH is set but not BUILD_VERSION we are building nightly, we gather only the commit hash - MESSAGE("Got from CI $ENV{BRANCH} branch and commit $ENV{COMMIT}") - set(PROJECT_VERSION ${PROJECT_VERSION}.$ENV{COMMIT}) - endif() -# Generate Sunshine Version based of the git tag -# https://github.com/nocnokneo/cmake-git-versioning-example/blob/master/LICENSE -else() - find_package(Git) - if(GIT_EXECUTABLE) - MESSAGE("${CMAKE_CURRENT_SOURCE_DIR}") - get_filename_component(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) - #Get current Branch - execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD - #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_DESCRIBE_BRANCH - RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - # Gather current commit - execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD - #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_DESCRIBE_VERSION - RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - # Check if Dirty - execute_process( - COMMAND ${GIT_EXECUTABLE} diff --quiet --exit-code - #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - RESULT_VARIABLE GIT_IS_DIRTY - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - if(NOT GIT_DESCRIBE_ERROR_CODE) - MESSAGE("Sunshine Branch: ${GIT_DESCRIBE_BRANCH}") - if(NOT GIT_DESCRIBE_BRANCH STREQUAL "master") - set(PROJECT_VERSION ${PROJECT_VERSION}.${GIT_DESCRIBE_VERSION}) - MESSAGE("Sunshine Version: ${GIT_DESCRIBE_VERSION}") - endif() - if(GIT_IS_DIRTY) - set(PROJECT_VERSION ${PROJECT_VERSION}.dirty) - MESSAGE("Git tree is dirty!") - endif() - else() - MESSAGE(ERROR ": Got git error while fetching tags: ${GIT_DESCRIBE_ERROR_CODE}") - endif() - else() - MESSAGE(WARNING ": Git not found, cannot find git version") - endif() -endif() +# set the module path, used for includes +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) -option(SUNSHINE_CONFIGURE_APPIMAGE "Configuration specific for AppImage." OFF) -option(SUNSHINE_CONFIGURE_AUR "Configure files required for AUR." OFF) -option(SUNSHINE_CONFIGURE_FLATPAK_MAN "Configure manifest file required for Flatpak build." OFF) -option(SUNSHINE_CONFIGURE_FLATPAK "Configuration specific for Flatpak." OFF) -option(SUNSHINE_CONFIGURE_PORTFILE "Configure macOS Portfile." OFF) -option(SUNSHINE_CONFIGURE_ONLY "Configure special files only, then exit." OFF) +# basic project configuration +include(${CMAKE_MODULE_PATH}/include_base.cmake) -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message(STATUS "Setting build type to 'Release' as none was specified.") - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) -endif() +# set version info for this build +include(${CMAKE_MODULE_PATH}/include_build_version.cmake) -if(${SUNSHINE_CONFIGURE_APPIMAGE}) - configure_file(packaging/linux/sunshine.desktop sunshine.desktop @ONLY) -elseif(${SUNSHINE_CONFIGURE_AUR}) - configure_file(packaging/linux/aur/PKGBUILD PKGBUILD @ONLY) -elseif(${SUNSHINE_CONFIGURE_FLATPAK_MAN}) - configure_file(packaging/linux/flatpak/dev.lizardbyte.sunshine.yml dev.lizardbyte.sunshine.yml @ONLY) -elseif(${SUNSHINE_CONFIGURE_PORTFILE}) - configure_file(packaging/macos/Portfile Portfile @ONLY) -endif() +# cmake build flags +include(${CMAKE_MODULE_PATH}/include_options.cmake) + +# configure special package files, such as sunshine.desktop, Flatpak manifest, Portfile , etc. +include(${CMAKE_MODULE_PATH}/include_special_package_configuration.cmake) -# return if configure only is set -if(${SUNSHINE_CONFIGURE_ONLY}) - return() -endif() -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(SUNSHINE_SOURCE_ASSETS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src_assets") if(APPLE) @@ -214,8 +133,6 @@ if(WIN32) elseif(APPLE) add_compile_definitions(SUNSHINE_PLATFORM="macos") - option(SUNSHINE_MACOS_PACKAGE "Should only be used when creating a MACOS package/dmg." OFF) - link_directories(/opt/local/lib) link_directories(/usr/local/lib) ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK) @@ -262,12 +179,6 @@ elseif(APPLE) else() add_compile_definitions(SUNSHINE_PLATFORM="linux") - option(SUNSHINE_ENABLE_DRM "Enable KMS grab if available" ON) - option(SUNSHINE_ENABLE_X11 "Enable X11 grab if available" ON) - option(SUNSHINE_ENABLE_WAYLAND "Enable building wayland specific code" ON) - option(SUNSHINE_ENABLE_CUDA "Enable cuda specific code" ON) - option(SUNSHINE_ENABLE_TRAY "Enable tray icon" ON) - if(${SUNSHINE_ENABLE_X11}) find_package(X11) else() @@ -631,7 +542,7 @@ if(UNIX) endif() # use relative assets path for AppImage... maybe for all unix -if(${SUNSHINE_CONFIGURE_APPIMAGE}) +if(${SUNSHINE_BUILD_APPIMAGE}) string(REPLACE "${CMAKE_INSTALL_PREFIX}" ".${CMAKE_INSTALL_PREFIX}" SUNSHINE_ASSETS_DIR_DEF ${SUNSHINE_ASSETS_DIR}) else() set(SUNSHINE_ASSETS_DIR_DEF "${SUNSHINE_ASSETS_DIR}") @@ -867,7 +778,7 @@ if(APPLE) set(CPACK_BUNDLE_ICON "${PROJECT_SOURCE_DIR}/sunshine.icns") # set(CPACK_BUNDLE_STARTUP_COMMAND "${INSTALL_RUNTIME_DIR}/sunshine") endif() -if(APPLE AND SUNSHINE_MACOS_PACKAGE) # TODO +if(APPLE AND SUNSHINE_PACKAGE_MACOS) # TODO set(MAC_PREFIX "${CMAKE_PROJECT_NAME}.app/Contents") set(INSTALL_RUNTIME_DIR "${MAC_PREFIX}/MacOS") @@ -899,7 +810,7 @@ elseif(UNIX) else() install(DIRECTORY "${SUNSHINE_SOURCE_ASSETS_DIR}/linux/assets/" DESTINATION "${SUNSHINE_ASSETS_DIR}") - if(${SUNSHINE_CONFIGURE_APPIMAGE} OR ${SUNSHINE_CONFIGURE_FLATPAK}) + if(${SUNSHINE_BUILD_APPIMAGE} OR ${SUNSHINE_BUILD_FLATPAK}) install(FILES "${SUNSHINE_SOURCE_ASSETS_DIR}/linux/misc/85-sunshine.rules" DESTINATION "${SUNSHINE_ASSETS_DIR}/udev/rules.d") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sunshine.service" diff --git a/cmake/include_base.cmake b/cmake/include_base.cmake new file mode 100644 index 00000000000..4c3bd54c24d --- /dev/null +++ b/cmake/include_base.cmake @@ -0,0 +1,14 @@ +# todo - set version to 0.0.0 once confident in automated versioning +project(Sunshine VERSION 0.20.0 + DESCRIPTION "Sunshine is a self-hosted game stream host for Moonlight." + HOMEPAGE_URL "https://app.lizardbyte.dev") + +set(PROJECT_LONG_DESCRIPTION "Offering low latency, cloud gaming server capabilities with support for AMD, Intel, \ +and Nvidia GPUs for hardware encoding. Software encoding is also available. You can connect to Sunshine from any \ +Moonlight client on a variety of devices. A web UI is provided to allow configuration, and client pairing, from \ +your favorite web browser. Pair from the local server or any mobile device.") + +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'Release' as none was specified.") + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) +endif() diff --git a/cmake/include_build_version.cmake b/cmake/include_build_version.cmake new file mode 100644 index 00000000000..49f85f9b585 --- /dev/null +++ b/cmake/include_build_version.cmake @@ -0,0 +1,58 @@ +# Check if env vars are defined before attempting to access them, variables will be defined even if blank +if((DEFINED ENV{BRANCH}) AND (DEFINED ENV{BUILD_VERSION}) AND (DEFINED ENV{COMMIT})) # cmake-lint: disable=W0106 + if(($ENV{BRANCH} STREQUAL "master") AND (NOT $ENV{BUILD_VERSION} STREQUAL "")) + # If BRANCH is "master" and BUILD_VERSION is not empty, then we are building a master branch + MESSAGE("Got from CI master branch and version $ENV{BUILD_VERSION}") + set(PROJECT_VERSION $ENV{BUILD_VERSION}) + elseif((DEFINED ENV{BRANCH}) AND (DEFINED ENV{COMMIT})) + # If BRANCH is set but not BUILD_VERSION we are building nightly, we gather only the commit hash + MESSAGE("Got from CI $ENV{BRANCH} branch and commit $ENV{COMMIT}") + set(PROJECT_VERSION ${PROJECT_VERSION}.$ENV{COMMIT}) + endif() + # Generate Sunshine Version based of the git tag + # https://github.com/nocnokneo/cmake-git-versioning-example/blob/master/LICENSE +else() + find_package(Git) + if(GIT_EXECUTABLE) + MESSAGE("${CMAKE_CURRENT_SOURCE_DIR}") + get_filename_component(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) + #Get current Branch + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_DESCRIBE_BRANCH + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + # Gather current commit + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_DESCRIBE_VERSION + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + # Check if Dirty + execute_process( + COMMAND ${GIT_EXECUTABLE} diff --quiet --exit-code + #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE GIT_IS_DIRTY + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT GIT_DESCRIBE_ERROR_CODE) + MESSAGE("Sunshine Branch: ${GIT_DESCRIBE_BRANCH}") + if(NOT GIT_DESCRIBE_BRANCH STREQUAL "master") + set(PROJECT_VERSION ${PROJECT_VERSION}.${GIT_DESCRIBE_VERSION}) + MESSAGE("Sunshine Version: ${GIT_DESCRIBE_VERSION}") + endif() + if(GIT_IS_DIRTY) + set(PROJECT_VERSION ${PROJECT_VERSION}.dirty) + MESSAGE("Git tree is dirty!") + endif() + else() + MESSAGE(ERROR ": Got git error while fetching tags: ${GIT_DESCRIBE_ERROR_CODE}") + endif() + else() + MESSAGE(WARNING ": Git not found, cannot find git version") + endif() +endif() diff --git a/cmake/include_options.cmake b/cmake/include_options.cmake new file mode 100644 index 00000000000..44d94aacb1c --- /dev/null +++ b/cmake/include_options.cmake @@ -0,0 +1,20 @@ +# if this option is set, the build will exit after configuring special package configuration files +option(SUNSHINE_CONFIGURE_ONLY "Configure special files only, then exit." OFF) + +option(SUNSHINE_ENABLE_TRAY "Enable tray icon." ON) + +if (APPLE) + option(SUNSHINE_CONFIGURE_PORTFILE "Configure macOS Portfile." OFF) + option(SUNSHINE_PACKAGE_MACOS "Should only be used when creating a MACOS package/dmg." OFF) +elseif (UNIX) # Linux + option(SUNSHINE_BUILD_APPIMAGE "Enable an AppImage build." OFF) + option(SUNSHINE_BUILD_FLATPAK "Enable a Flatpak build." OFF) + option(SUNSHINE_CONFIGURE_ARCH "Configure files required for AUR." OFF) + option(SUNSHINE_CONFIGURE_FLATPAK_MAN "Configure manifest file required for Flatpak build." OFF) + + # Linux capture methods + option(SUNSHINE_ENABLE_CUDA "Enable cuda specific code." ON) + option(SUNSHINE_ENABLE_DRM "Enable KMS grab if available." ON) + option(SUNSHINE_ENABLE_WAYLAND "Enable building wayland specific code." ON) + option(SUNSHINE_ENABLE_X11 "Enable X11 grab if available." ON) +endif () diff --git a/cmake/include_special_package_configuration.cmake b/cmake/include_special_package_configuration.cmake new file mode 100644 index 00000000000..b686176ac1b --- /dev/null +++ b/cmake/include_special_package_configuration.cmake @@ -0,0 +1,23 @@ +if (APPLE) + if(${SUNSHINE_CONFIGURE_PORTFILE}) + configure_file(packaging/macos/Portfile Portfile @ONLY) + endif() +elseif (UNIX) + # configure the .desktop file + configure_file(packaging/linux/sunshine.desktop sunshine.desktop @ONLY) + + # configure the arch linux pkgbuild + if(${SUNSHINE_CONFIGURE_ARCH}) + configure_file(packaging/linux/Arch/PKGBUILD PKGBUILD @ONLY) + endif() + + # configure the flatpak manifest + if(${SUNSHINE_CONFIGURE_FLATPAK_MAN}) + configure_file(packaging/linux/flatpak/dev.lizardbyte.sunshine.yml dev.lizardbyte.sunshine.yml @ONLY) + endif() +endif() + +# return if configure only is set +if(${SUNSHINE_CONFIGURE_ONLY}) + return() +endif() diff --git a/packaging/linux/aur/PKGBUILD b/packaging/linux/Arch/PKGBUILD similarity index 100% rename from packaging/linux/aur/PKGBUILD rename to packaging/linux/Arch/PKGBUILD diff --git a/packaging/linux/flatpak/dev.lizardbyte.sunshine.yml b/packaging/linux/flatpak/dev.lizardbyte.sunshine.yml index 403c6db01d4..35f9a52c245 100644 --- a/packaging/linux/flatpak/dev.lizardbyte.sunshine.yml +++ b/packaging/linux/flatpak/dev.lizardbyte.sunshine.yml @@ -223,7 +223,7 @@ modules: - -DSUNSHINE_ENABLE_X11=ON - -DSUNSHINE_ENABLE_DRM=ON - -DSUNSHINE_ENABLE_CUDA=ON - - -DSUNSHINE_CONFIGURE_FLATPAK=ON + - -DSUNSHINE_BUILD_FLATPAK=ON sources: - type: git url: "@GITHUB_CLONE_URL@"