Skip to content

Commit

Permalink
refactor(cmake): split CMakeLists into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Sep 1, 2023
1 parent a3eec98 commit 0e5fdaa
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
117 changes: 14 additions & 103 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions cmake/include_base.cmake
Original file line number Diff line number Diff line change
@@ -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()
58 changes: 58 additions & 0 deletions cmake/include_build_version.cmake
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions cmake/include_options.cmake
Original file line number Diff line number Diff line change
@@ -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 ()
23 changes: 23 additions & 0 deletions cmake/include_special_package_configuration.cmake
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
2 changes: 1 addition & 1 deletion packaging/linux/flatpak/dev.lizardbyte.sunshine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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@"
Expand Down

0 comments on commit 0e5fdaa

Please sign in to comment.