浏览代码

cmake: Fix CMake policy scopes and add include guards

Include guards ensure that bootstrap includes happen only once for the
entire project. Moving all policy changes into an included file without
its own policy scope ensures that the policy is applied to the project.
PatTheMav 2 年之前
父节点
当前提交
e2e3c9e102

+ 1 - 1
CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 3.16...3.25)
 
 if(OBS_CMAKE_VERSION VERSION_GREATER_EQUAL 3.0.0)
-  include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/common/bootstrap.cmake")
+  include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/common/bootstrap.cmake" NO_POLICY_SCOPE)
 
   project(obs-studio VERSION ${OBS_VERSION_CANONICAL})
 

+ 28 - 9
cmake/common/bootstrap.cmake

@@ -1,21 +1,40 @@
 # OBS CMake bootstrap module
 
+include_guard(GLOBAL)
+
 # Enable automatic PUSH and POP of policies to parent scope
 if(POLICY CMP0011)
   cmake_policy(SET CMP0011 NEW)
 endif()
 
+# Enable distinction between Clang and AppleClang
+if(POLICY CMP0025)
+  cmake_policy(SET CMP0025 NEW)
+endif()
+
+# Enable strict checking of "break()" usage
+if(POLICY CMP0055)
+  cmake_policy(SET CMP0055 NEW)
+endif()
+
+# Honor visibility presets for all target types (executable, shared, module, static)
+if(POLICY CMP0063)
+  cmake_policy(SET CMP0063 NEW)
+endif()
+
+# Disable export function calls to populate package registry by default
+if(POLICY CMP0090)
+  cmake_policy(SET CMP0090 NEW)
+endif()
 # Prohibit in-source builds
-if("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
-  message(
-    FATAL_ERROR
-      "OBS: In-source builds of OBS are not supported. Specify a build directory via 'cmake -S <SOURCE DIRECTORY> -B <BUILD_DIRECTORY>' instead."
-  )
+if("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+  message(FATAL_ERROR "In-source builds of OBS are not supported. "
+                      "Specify a build directory via 'cmake -S <SOURCE DIRECTORY> -B <BUILD_DIRECTORY>' instead.")
+  file(REMOVE_RECURSE "${CMAKE_CURRENT_SOURCE_DIR}/CMakeCache.txt" "${CMAKE_CURRENT_SOURCE_DIR}/CMakeFiles")
 endif()
-file(REMOVE_RECURSE "${CMAKE_SOURCE_DIR}/CMakeCache.txt" "${CMAKE_SOURCE_DIR}/CMakeFiles")
 
 # Use folders for source file organization with IDE generators (Visual Studio/Xcode)
-set_property(GLOBAL PROPERTY USE_FOLDERS ON)
+set_property(GLOBAL PROPERTY USE_FOLDERS TRUE)
 
 # Set default global project variables
 set(OBS_COMPANY_NAME "OBS Project")
@@ -23,9 +42,9 @@ set(OBS_PRODUCT_NAME "OBS Studio")
 set(OBS_WEBSITE "https://www.obsproject.com")
 set(OBS_COMMENTS "Free and open source software for video recording and live streaming")
 set(OBS_LEGAL_COPYRIGHT "(C) Lain Bailey")
+set(OBS_CMAKE_VERSION 3.0.0)
 
 # Configure default version strings
-message(DEBUG "Setting default project version variables...")
 set(_obs_default_version "0" "0" "1")
 set(_obs_release_candidate "0" "0" "0" "0")
 set(_obs_beta "0" "0" "0" "0")
@@ -38,7 +57,7 @@ include(buildnumber)
 include(osconfig)
 
 # Allow selection of common build types via UI
-if(NOT CMAKE_BUILD_TYPE)
+if(NOT CMAKE_GENERATOR MATCHES "(Xcode|Visual Studio .+)")
   set(CMAKE_BUILD_TYPE
       "RelWithDebInfo"
       CACHE STRING "OBS build type [Release, RelWithDebInfo, Debug, MinSizeRel]" FORCE)

+ 3 - 1
cmake/common/buildnumber.cmake

@@ -1,8 +1,10 @@
 # OBS CMake build number module
 
+include_guard(GLOBAL)
+
 # Define build number cache file
 set(_BUILD_NUMBER_CACHE
-    "${CMAKE_SOURCE_DIR}/cmake/.CMakeBuildNumber"
+    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/.CMakeBuildNumber"
     CACHE INTERNAL "OBS build number cache file")
 
 # Read build number from cache file or manual override

+ 3 - 1
cmake/common/ccache.cmake

@@ -1,5 +1,7 @@
 # OBS CMake ccache module
 
+include_guard(GLOBAL)
+
 if(NOT DEFINED CCACHE_PROGRAM)
   message(DEBUG "Trying to find ccache on build host...")
   find_program(CCACHE_PROGRAM "ccache")
@@ -7,7 +9,7 @@ if(NOT DEFINED CCACHE_PROGRAM)
 endif()
 
 if(CCACHE_PROGRAM)
-  message(DEBUG "CCache found as ${CCACHE_PROGRAM}...")
+  message(DEBUG "Ccache found as ${CCACHE_PROGRAM}...")
   option(ENABLE_CCACHE "Enable compiler acceleration with ccache" ON)
 
   if(ENABLE_CCACHE)

+ 2 - 0
cmake/common/compiler_common.cmake

@@ -1 +1,3 @@
 # OBS CMake common compiler options module
+
+include_guard(GLOBAL)

+ 2 - 0
cmake/common/cpackconfig_common.cmake

@@ -1,5 +1,7 @@
 # OBS CMake common CPack module
 
+include_guard(GLOBAL)
+
 # Set default global CPack variables
 set(CPACK_PACKAGE_NAME obs-studio)
 set(CPACK_PACKAGE_VENDOR "${OBS_WEBSITE}")

+ 6 - 0
cmake/common/helpers_common.cmake

@@ -8,6 +8,8 @@
 # cmake-lint: disable=R0915
 # cmake-format: on
 
+include_guard(GLOBAL)
+
 # message_configuration: Function to print configuration outcome
 function(message_configuration)
   include(FeatureSummary)
@@ -31,6 +33,7 @@ function(message_configuration)
     COMPARE NATURAL
     CASE SENSITIVE
     ORDER ASCENDING)
+
   if(OBS_FEATURES_ENABLED)
     message(NOTICE "------------------------       Enabled Features           ------------------------")
     foreach(feature IN LISTS OBS_FEATURES_ENABLED)
@@ -44,6 +47,7 @@ function(message_configuration)
     COMPARE NATURAL
     CASE SENSITIVE
     ORDER ASCENDING)
+
   if(OBS_FEATURES_DISABLED)
     message(NOTICE "------------------------       Disabled Features          ------------------------")
     foreach(feature IN LISTS OBS_FEATURES_DISABLED)
@@ -58,6 +62,7 @@ function(message_configuration)
       COMPARE NATURAL
       CASE SENSITIVE
       ORDER ASCENDING)
+
     if(OBS_MODULES_ENABLED)
       message(NOTICE "------------------------        Enabled Modules           ------------------------")
       foreach(feature IN LISTS OBS_MODULES_ENABLED)
@@ -71,6 +76,7 @@ function(message_configuration)
       COMPARE NATURAL
       CASE SENSITIVE
       ORDER ASCENDING)
+
     if(OBS_MODULES_DISABLED)
       message(NOTICE "------------------------        Disabled Modules          ------------------------")
       foreach(feature IN LISTS OBS_MODULES_DISABLED)

+ 2 - 0
cmake/common/osconfig.cmake

@@ -1,5 +1,7 @@
 # OBS CMake operating system bootstrap module
 
+include_guard(GLOBAL)
+
 # Set minimum CMake version specific to host operating system, add OS-specific module directory to default search paths,
 # and set helper variables for OS detection in other CMake list files.
 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")

+ 3 - 1
cmake/common/versionconfig.cmake

@@ -4,6 +4,8 @@
 # cmake-lint: disable=C0301
 # cmake-format: on
 
+include_guard(GLOBAL)
+
 # Attempt to automatically discover expected OBS version
 if(NOT DEFINED OBS_VERSION_OVERRIDE)
   if(DEFINED RELEASE_CANDIDATE)
@@ -23,7 +25,7 @@ if(NOT DEFINED OBS_VERSION_OVERRIDE)
     else()
       message(FATAL_ERROR "Invalid beta version supplied - must be <MAJOR>.<MINOR>.<PATCH>-beta<RELEASE>.")
     endif()
-  elseif(EXISTS "${CMAKE_SOURCE_DIR}/.git")
+  elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
     execute_process(
       COMMAND git describe --always --tags --dirty=-modified
       OUTPUT_VARIABLE _obs_version

+ 1 - 9
cmake/macos/compilerconfig.cmake

@@ -1,14 +1,6 @@
 # OBS CMake macOS compiler configuration module
 
-# Enable distinction between Clang and AppleClang
-if(POLICY CMP0025)
-  cmake_policy(SET CMP0025 NEW)
-endif()
-
-# Honor visibility presets for all target types (executable, shared, module, static)
-if(POLICY CMP0063)
-  cmake_policy(SET CMP0063 NEW)
-endif()
+include_guard(GLOBAL)
 
 include(ccache)
 include(compiler_common)

+ 1 - 7
cmake/macos/defaults.cmake

@@ -1,12 +1,6 @@
 # OBS CMake macOS defaults module
 
-# Enable selection between arm64 and x86_64 targets
-if(NOT CMAKE_OSX_ARCHITECTURES)
-  set(CMAKE_OSX_ARCHITECTURES
-      arm64
-      CACHE STRING "Build architectures for macOS" FORCE)
-endif()
-set_property(CACHE CMAKE_OSX_ARCHITECTURES PROPERTY STRINGS arm64 x86_64)
+include_guard(GLOBAL)
 
 # Set empty codesigning team if not specified as cache variable
 if(NOT OBS_CODESIGN_TEAM)

+ 2 - 0
cmake/macos/helpers.cmake

@@ -7,6 +7,8 @@
 # cmake-lint: disable=E1126
 # cmake-format: on
 
+include_guard(GLOBAL)
+
 include(helpers_common)
 
 # set_target_properties_obs: Set target properties for use in obs-studio

+ 2 - 0
cmake/macos/xcode.cmake

@@ -1,5 +1,7 @@
 # OBS CMake macOS Xcode module
 
+include_guard(GLOBAL)
+
 # Use a compiler wrapper to enable ccache in Xcode projects
 if(ENABLE_CCACHE AND CCACHE_PROGRAM)
   configure_file("${CMAKE_SOURCE_DIR}/cmake/macos/resources/ccache-launcher-c.in" ccache-launcher-c)