From 19506f19973bd6e4a938fbe3ce0a51445ea50b3a Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Tue, 24 Apr 2018 12:31:43 +0200 Subject: [PATCH] cmake: Many fixes and improvements * Standalone builds no longer require you to have a compiled version of OBS Studio. Instead the project will now download the last compatible libOBS version from CI and use it for linking. * The project version now has a tweak field again, which is either 0 or determined by the number of git commits since the last release tag. * The Install command now properly uses CMAKE_INSTALL_PREFIX instead of INSTALL_DIR. * Packaging is now done using CPack instead of custom commands. --- CMakeLists.txt | 135 ++++++++++------ cmake/DownloadProject.CMakeLists.cmake.in | 17 ++ cmake/DownloadProject.cmake | 182 ++++++++++++++++++++++ 3 files changed, 285 insertions(+), 49 deletions(-) create mode 100644 cmake/DownloadProject.CMakeLists.cmake.in create mode 100644 cmake/DownloadProject.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index d1f453ab..1c6d1aa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,32 +1,77 @@ cmake_minimum_required(VERSION 3.2) -PROJECT(obs-stream-effects) +PROJECT(obs-stream-effects VERSION 0.4.2.0) + +################################################################################ +# CMake Setup +################################################################################ + +math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}") + +# Modules +SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") +include(ExternalProject) +include(DownloadProject) + +################################################################################ +# Dependencies +################################################################################ + +# OBS Studio / libOBS +IF(NOT TARGET libobs) + SET(LIBOBS_EXISTS FALSE) + SET(LIBOBS_CUSTOM FALSE CACHE BOOL "Use custom libOBS") + IF(NOT LIBOBS_CUSTOM) + IF (${BITS} STREQUAL "64") + SET(LIBOBS_URL "https://ci.appveyor.com/api/buildjobs/jp18y3sc5icoq8qs/artifacts/build%2Fobs-studio-x64-vs2017-x64.7z" CACHE STRING "URL to libobs Cpack") + ELSE() + SET(LIBOBS_URL "https://ci.appveyor.com/api/buildjobs/nj276esfv3ho0j49/artifacts/build%2Fobs-studio-x86-vs2017-x86.7z" CACHE STRING "URL to libobs Cpack") + ENDIF() + + # OBS Studio (CPack Release) + download_project( + PROJ libobs + URL "${LIBOBS_URL}" + UPDATE_DISCONNECTED 1 + ) + INCLUDE("${libobs_SOURCE_DIR}/cmake/LibObs/LibObsConfig.cmake") + ELSE() + SET(LIBOBS_PATH "" CACHE STRING "Path to libOBS") + ENDIF() +ELSE() + SET(LIBOBS_EXISTS TRUE) +ENDIF() ################################################################################ # Version ################################################################################ -SET(VERSION_MAJOR 0) -SET(VERSION_MINOR 4) -SET(VERSION_PATCH 2) -SET(VERSION_STR "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") + +# Retrieve Tweak version from git. +if(EXISTS "${PROJECT_SOURCE_DIR}/.git") + SET(GIT_RESULT "") + SET(GIT_OUTPUT "") + EXECUTE_PROCESS( + COMMAND git rev-list --count --topo-order ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}..HEAD + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + RESULT_VARIABLE GIT_RESULT + OUTPUT_VARIABLE GIT_OUTPUT + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET + ) + IF(GIT_RESULT EQUAL 0) + SET(PROJECT_VERSION_TWEAK ${GIT_OUTPUT}) + ENDIF() +endif() + +SET(VERSION_MAJOR PROJECT_VERSION_MAJOR) +SET(VERSION_MINOR PROJECT_VERSION_MINOR) +SET(VERSION_PATCH PROJECT_VERSION_PATCH) +SET(VERSION_TWEAK PROJECT_VERSION_TWEAK) +SET(VERSION_STR "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_TWEAK}") configure_file( "${PROJECT_SOURCE_DIR}/source/version.h.in" "${PROJECT_BINARY_DIR}/source/version.h" ) -################################################################################ -# Configuration -################################################################################ -math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}") - -SET(INSTALL_DIR "${PROJECT_BINARY_DIR}/distribute" CACHE PATH "Installation directory") -IF(NOT DEFINED PACKAGE_PREFIX) - SET(PACKAGE_PREFIX "") -ENDIF() -IF(NOT DEFINED PACKAGE_SUFFIX) - SET(PACKAGE_SUFFIX ".${VERSION_STR}") -ENDIF() - ################################################################################ # Code ################################################################################ @@ -105,7 +150,7 @@ source_group("Data Files\\Shaders\\Filter" FILES ${obs-stream-effects_SHADERS_FI ################################################################################ # Standalone and OBS Studio Build Data ################################################################################ -if(TARGET libobs) +if(LIBOBS_EXISTS) # OBS Studio Specific INCLUDE_DIRECTORIES( "${CMAKE_SOURCE_DIR}" @@ -116,28 +161,13 @@ if(TARGET libobs) ) SET(LIBOBS_LIBRARIES libobs) else() - # Standlone Specific - SET(PATH_OBSStudio "" CACHE PATH "OBS Studio Source Code Directory") - if(PATH_OBSStudio STREQUAL "") - message(FATAL_ERROR "PATH_OBSStudio not set!") - return() - endif() - if(NOT EXISTS "${PATH_OBSStudio}/libobs/obs-module.h") - message(FATAL_ERROR "PATH_OBSStudio invalid!") - return() - endif() - - # Find OBS Libraries - SET(obsPath "${PATH_OBSStudio}") - INCLUDE("${PATH_OBSStudio}/cmake/external/FindLibobs.cmake") - # Compiling INCLUDE_DIRECTORIES( "${PROJECT_BINARY_DIR}" "${PROJECT_BINARY_DIR}/source" "${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/source" - "${PATH_OBSStudio}" + ${LIBOBS_INCLUDE_DIRS} ) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() @@ -171,23 +201,30 @@ TARGET_LINK_LIBRARIES(obs-stream-effects ${obs-stream-effects_LIBRARIES} ) -if(TARGET libobs) +if(LIBOBS_EXISTS) install_obs_plugin_with_data(obs-stream-effects data) else() - INSTALL(TARGETS obs-stream-effects DESTINATION "${INSTALL_DIR}/obs-plugins/${BITS}bit") - INSTALL(FILES $ DESTINATION "${INSTALL_DIR}/obs-plugins/${BITS}bit" OPTIONAL) - INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/data/" DESTINATION "${INSTALL_DIR}/data/obs-plugins/obs-stream-effects" OPTIONAL) + # Install + INSTALL( + TARGETS obs-stream-effects + RUNTIME DESTINATION "obs-plugins/${BITS}bit" COMPONENT Runtime + LIBRARY DESTINATION "obs-plugins/${BITS}bit" COMPONENT Runtime + ) + INSTALL(FILES $ DESTINATION "obs-plugins/${BITS}bit" OPTIONAL) + INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/data/" DESTINATION "data/obs-plugins/obs-stream-effects" OPTIONAL) - # Zip Generator - ADD_CUSTOM_TARGET(PACKAGE_ZIP COMMAND - ${CMAKE_COMMAND} -E tar "cfv" "${INSTALL_DIR}/${PACKAGE_PREFIX}obs-stream-effects${PACKAGE_SUFFIX}.zip" --format=zip -- "${INSTALL_DIR}/data/" "${INSTALL_DIR}/obs-plugins" - DEPENDS INSTALL obs-stream-effects - WORKING_DIRECTORY "${INSTALL_DIR}") + # CPack + SET(CPACK_PACKAGE_NAME ${PROJECT_NAME}) + SET(CPACK_PACKAGE_VENDOR "Xaymar") - # 7-Zip Generator - ADD_CUSTOM_TARGET(PACKAGE_7ZIP COMMAND - ${CMAKE_COMMAND} -E tar "cfv" "${INSTALL_DIR}/${PACKAGE_PREFIX}obs-stream-effects${PACKAGE_SUFFIX}.7z" --format=7zip -- "${INSTALL_DIR}/data/" "${INSTALL_DIR}/obs-plugins" - DEPENDS INSTALL obs-stream-effects - WORKING_DIRECTORY "${INSTALL_DIR}") + SET(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) + SET(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) + SET(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) + SET(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_TWEAK}) + SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.${CPACK_PACKAGE_VERSION_TWEAK}") + SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}") + SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}-source") + SET(CPACK_PACKAGE_CHECKSUM SHA512) + include(CPack) endif() diff --git a/cmake/DownloadProject.CMakeLists.cmake.in b/cmake/DownloadProject.CMakeLists.cmake.in new file mode 100644 index 00000000..89be4fdd --- /dev/null +++ b/cmake/DownloadProject.CMakeLists.cmake.in @@ -0,0 +1,17 @@ +# Distributed under the OSI-approved MIT License. See accompanying +# file LICENSE or https://github.com/Crascit/DownloadProject for details. + +cmake_minimum_required(VERSION 2.8.2) + +project(${DL_ARGS_PROJ}-download NONE) + +include(ExternalProject) +ExternalProject_Add(${DL_ARGS_PROJ}-download + ${DL_ARGS_UNPARSED_ARGUMENTS} + SOURCE_DIR "${DL_ARGS_SOURCE_DIR}" + BINARY_DIR "${DL_ARGS_BINARY_DIR}" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/cmake/DownloadProject.cmake b/cmake/DownloadProject.cmake new file mode 100644 index 00000000..e300f426 --- /dev/null +++ b/cmake/DownloadProject.cmake @@ -0,0 +1,182 @@ +# Distributed under the OSI-approved MIT License. See accompanying +# file LICENSE or https://github.com/Crascit/DownloadProject for details. +# +# MODULE: DownloadProject +# +# PROVIDES: +# download_project( PROJ projectName +# [PREFIX prefixDir] +# [DOWNLOAD_DIR downloadDir] +# [SOURCE_DIR srcDir] +# [BINARY_DIR binDir] +# [QUIET] +# ... +# ) +# +# Provides the ability to download and unpack a tarball, zip file, git repository, +# etc. at configure time (i.e. when the cmake command is run). How the downloaded +# and unpacked contents are used is up to the caller, but the motivating case is +# to download source code which can then be included directly in the build with +# add_subdirectory() after the call to download_project(). Source and build +# directories are set up with this in mind. +# +# The PROJ argument is required. The projectName value will be used to construct +# the following variables upon exit (obviously replace projectName with its actual +# value): +# +# projectName_SOURCE_DIR +# projectName_BINARY_DIR +# +# The SOURCE_DIR and BINARY_DIR arguments are optional and would not typically +# need to be provided. They can be specified if you want the downloaded source +# and build directories to be located in a specific place. The contents of +# projectName_SOURCE_DIR and projectName_BINARY_DIR will be populated with the +# locations used whether you provide SOURCE_DIR/BINARY_DIR or not. +# +# The DOWNLOAD_DIR argument does not normally need to be set. It controls the +# location of the temporary CMake build used to perform the download. +# +# The PREFIX argument can be provided to change the base location of the default +# values of DOWNLOAD_DIR, SOURCE_DIR and BINARY_DIR. If all of those three arguments +# are provided, then PREFIX will have no effect. The default value for PREFIX is +# CMAKE_BINARY_DIR. +# +# The QUIET option can be given if you do not want to show the output associated +# with downloading the specified project. +# +# In addition to the above, any other options are passed through unmodified to +# ExternalProject_Add() to perform the actual download, patch and update steps. +# The following ExternalProject_Add() options are explicitly prohibited (they +# are reserved for use by the download_project() command): +# +# CONFIGURE_COMMAND +# BUILD_COMMAND +# INSTALL_COMMAND +# TEST_COMMAND +# +# Only those ExternalProject_Add() arguments which relate to downloading, patching +# and updating of the project sources are intended to be used. Also note that at +# least one set of download-related arguments are required. +# +# If using CMake 3.2 or later, the UPDATE_DISCONNECTED option can be used to +# prevent a check at the remote end for changes every time CMake is run +# after the first successful download. See the documentation of the ExternalProject +# module for more information. It is likely you will want to use this option if it +# is available to you. Note, however, that the ExternalProject implementation contains +# bugs which result in incorrect handling of the UPDATE_DISCONNECTED option when +# using the URL download method or when specifying a SOURCE_DIR with no download +# method. Fixes for these have been created, the last of which is scheduled for +# inclusion in CMake 3.8.0. Details can be found here: +# +# https://gitlab.kitware.com/cmake/cmake/commit/bdca68388bd57f8302d3c1d83d691034b7ffa70c +# https://gitlab.kitware.com/cmake/cmake/issues/16428 +# +# If you experience build errors related to the update step, consider avoiding +# the use of UPDATE_DISCONNECTED. +# +# EXAMPLE USAGE: +# +# include(DownloadProject) +# download_project(PROJ googletest +# GIT_REPOSITORY https://github.com/google/googletest.git +# GIT_TAG master +# UPDATE_DISCONNECTED 1 +# QUIET +# ) +# +# add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) +# +#======================================================================================== + + +set(_DownloadProjectDir "${CMAKE_CURRENT_LIST_DIR}") + +include(CMakeParseArguments) + +function(download_project) + + set(options QUIET) + set(oneValueArgs + PROJ + PREFIX + DOWNLOAD_DIR + SOURCE_DIR + BINARY_DIR + # Prevent the following from being passed through + CONFIGURE_COMMAND + BUILD_COMMAND + INSTALL_COMMAND + TEST_COMMAND + ) + set(multiValueArgs "") + + cmake_parse_arguments(DL_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Hide output if requested + if (DL_ARGS_QUIET) + set(OUTPUT_QUIET "OUTPUT_QUIET") + else() + unset(OUTPUT_QUIET) + message(STATUS "Downloading/updating ${DL_ARGS_PROJ}") + endif() + + # Set up where we will put our temporary CMakeLists.txt file and also + # the base point below which the default source and binary dirs will be. + # The prefix must always be an absolute path. + if (NOT DL_ARGS_PREFIX) + set(DL_ARGS_PREFIX "${CMAKE_BINARY_DIR}") + else() + get_filename_component(DL_ARGS_PREFIX "${DL_ARGS_PREFIX}" ABSOLUTE + BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") + endif() + if (NOT DL_ARGS_DOWNLOAD_DIR) + set(DL_ARGS_DOWNLOAD_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-download") + endif() + + # Ensure the caller can know where to find the source and build directories + if (NOT DL_ARGS_SOURCE_DIR) + set(DL_ARGS_SOURCE_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-src") + endif() + if (NOT DL_ARGS_BINARY_DIR) + set(DL_ARGS_BINARY_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-build") + endif() + set(${DL_ARGS_PROJ}_SOURCE_DIR "${DL_ARGS_SOURCE_DIR}" PARENT_SCOPE) + set(${DL_ARGS_PROJ}_BINARY_DIR "${DL_ARGS_BINARY_DIR}" PARENT_SCOPE) + + # The way that CLion manages multiple configurations, it causes a copy of + # the CMakeCache.txt to be copied across due to it not expecting there to + # be a project within a project. This causes the hard-coded paths in the + # cache to be copied and builds to fail. To mitigate this, we simply + # remove the cache if it exists before we configure the new project. It + # is safe to do so because it will be re-generated. Since this is only + # executed at the configure step, it should not cause additional builds or + # downloads. + file(REMOVE "${DL_ARGS_DOWNLOAD_DIR}/CMakeCache.txt") + + # Create and build a separate CMake project to carry out the download. + # If we've already previously done these steps, they will not cause + # anything to be updated, so extra rebuilds of the project won't occur. + # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project + # has this set to something not findable on the PATH. + configure_file("${_DownloadProjectDir}/DownloadProject.CMakeLists.cmake.in" + "${DL_ARGS_DOWNLOAD_DIR}/CMakeLists.txt") + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" + -D "CMAKE_MAKE_PROGRAM:FILE=${CMAKE_MAKE_PROGRAM}" + . + RESULT_VARIABLE result + ${OUTPUT_QUIET} + WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}" + ) + if(result) + message(FATAL_ERROR "CMake step for ${DL_ARGS_PROJ} failed: ${result}") + endif() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + ${OUTPUT_QUIET} + WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}" + ) + if(result) + message(FATAL_ERROR "Build step for ${DL_ARGS_PROJ} failed: ${result}") + endif() + +endfunction()