CMake: Check C++20 features that are missing on some systems

This commit is contained in:
Stenzek 2025-02-03 00:30:39 +10:00
parent cb205c4c36
commit 455836af89
No known key found for this signature in database
5 changed files with 68 additions and 9 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.19)
project(duckstation C CXX)
# Policy settings.
@ -16,10 +16,6 @@ if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|Devel|MinSizeRel|RelWithDebInfo|Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "CMake Version: ${CMAKE_VERSION}")
message(STATUS "CMake System Name: ${CMAKE_SYSTEM_NAME}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
# Pull in modules.
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
include(DuckStationUtils)
@ -34,6 +30,7 @@ detect_cache_line_size()
# Build options. Depends on system attributes.
include(DuckStationBuildOptions)
include(DuckStationDependencies)
include(DuckStationCompilerRequirement)
# Enable PIC on Linux, otherwise the builds do not support ASLR.
if(LINUX OR BSD)
@ -120,10 +117,6 @@ endif()
# Prevent fmt from being built with exceptions, or being thrown at call sites.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFMT_EXCEPTIONS=0")
# Use C++20.
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Recursively include the source tree.
add_subdirectory(dep)
add_subdirectory(src)

View File

@ -1,3 +1,5 @@
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
if(ENABLE_OPENGL)
message(STATUS "Building with OpenGL support.")
endif()

View File

@ -0,0 +1,16 @@
# Use C++20.
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# C++20 feature checks. Some Linux environments are incomplete.
check_cpp20_feature("__cpp_structured_bindings" 201606)
check_cpp20_feature("__cpp_constinit" 201907)
check_cpp20_feature("__cpp_designated_initializers" 201707)
check_cpp20_feature("__cpp_using_enum" 201907)
check_cpp20_feature("__cpp_lib_bit_cast" 201806)
check_cpp20_feature("__cpp_lib_bitops" 201907)
check_cpp20_feature("__cpp_lib_int_pow2" 202002)
check_cpp20_feature("__cpp_lib_starts_ends_with" 201711)
check_cpp20_attribute("likely" 201803)
check_cpp20_attribute("unlikely" 201803)
check_cpp20_attribute("no_unique_address" 201803)

View File

@ -1,3 +1,5 @@
include(CheckSourceCompiles)
function(disable_compiler_warnings_for_target target)
if(MSVC)
target_compile_options(${target} PRIVATE "/W0")
@ -245,3 +247,48 @@ function(install_imported_dep_library name)
get_target_property(LOCATION "${name}" IMPORTED_LOCATION_RELEASE)
install(FILES "${LOCATION}" RENAME "${SONAME}" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endfunction()
function(check_cpp20_feature MACRO MINIMUM_VALUE)
set(CACHE_VAR "CHECK_CPP20_FEATURE_${MACRO}")
if(NOT DEFINED ${CACHE_VAR})
# Create a small source code snippet that fails to compile if the feature is not available.
set(TEMP_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cpp")
file(WRITE "${TEMP_FILE}" "#include <version>
#if !defined(${MACRO}) || ${MACRO} < ${MINIMUM_VALUE}L
#error Missing feature
#endif
")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
try_compile(HAS_FEATURE
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY} "${TEMP_FILE}"
CXX_STANDARD 20
CXX_STANDARD_REQUIRED TRUE
)
set(${CACHE_VAR} ${HAS_FEATURE} CACHE INTERNAL "Cached feature test result for ${MACRO}")
endif()
if(NOT HAS_FEATURE)
message(FATAL_ERROR "${MACRO} is not supported by your compiler, at least ${MINIMUM_VALUE} is required.")
endif()
endfunction()
function(check_cpp20_attribute ATTRIBUTE MINIMUM_VALUE)
set(CACHE_VAR "CHECK_CPP20_ATTRIBUTE_${MACRO}")
if(NOT DEFINED ${CACHE_VAR})
set(TEMP_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cpp")
file(WRITE "${TEMP_FILE}" "#include <version>
#if !defined(__has_cpp_attribute) || __has_cpp_attribute(${ATTRIBUTE}) < ${MINIMUM_VALUE}L
#error Missing feature
#endif
")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
try_compile(HAS_FEATURE
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY} "${TEMP_FILE}"
CXX_STANDARD 20
CXX_STANDARD_REQUIRED TRUE
)
set(${CACHE_VAR} ${HAS_FEATURE} CACHE INTERNAL "Cached attribute test result for ${MACRO}")
endif()
if(NOT HAS_FEATURE)
message(FATAL_ERROR "${ATTRIBUTE} is not supported by your compiler, at least ${MINIMUM_VALUE} is required.")
endif()
endfunction()

View File

@ -13,6 +13,7 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <version>
namespace detail {
struct transparent_string_hash